def polygon_hole_cw_fully_on_rectangle_boundary(): """Polygon hole (CW) fully on rectangle boundary""" geom1 = load_wkt( "POLYGON ((0 0, 0 30, 30 30, 30 0, 0 0), (10 10, 10 20, 20 20, 20 10, 10 10))" ) geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
def polygon_overlapping_rectangle(): """Polygon overlapping rectangle""" wkt = "POLYGON ((0 0, 0 30, 30 30, 30 0, 0 0), (10 10, 20 10, 20 20, 10 20, 10 10))" geom1 = load_wkt(wkt) geom2 = clip_by_rect(geom1, 5, 5, 15, 15) assert dump_wkt( geom2, rounding_precision=0 ) == "POLYGON ((5 5, 5 15, 10 15, 10 10, 15 10, 15 5, 5 5))"
def voronoi( points: Union[FeatureCollection, List], bbox: Optional[list] = None ) -> Feature: """Takes a FeatureCollection of points, and a bounding box, and returns a FeatureCollection of Voronoi polygons. :param points: To find the Voronoi polygons around. Points should be either FeatureCollection of points or list of points. :param bbox: A bounding box to clip. :return: A GeoJSON Feature. Example: >>> from turfpy.transformation import voronoi >>> points = [ ... [-66.9703, 40.3183], ... [-63.7763, 40.4500], ... [-65.4196, 42.13985310302137], ... [-69.5813, 43.95405461286195], ... [-65.66337553550034, 55.97088945355232], ... [-60.280418548905, 56.240669185466146], ... [-68.5129561347689, 50.12984589640148], ... [-64.2393519226657, 59.66235385923687], ... ] >>> bbox = [-70, 40, -60, 60] >>> voronoi(points, bbox) """ if isinstance(points, FeatureCollection): coords = [] for feature in points["features"]: coords.append(feature["features"][0]["geometry"]["coordinates"]) points = np.array(coords) elif isinstance(points, list): points = np.array(points) else: raise ValueError( "points should be either FeatureCollection of points of List of Points" ) vor = Voronoi(points) lines = [ ShapelyLineString(vor.vertices[line]) for line in vor.ridge_vertices if -1 not in line ] convex_hull = MultiPoint([Point(i) for i in points]).convex_hull.buffer(2) result = MultiPolygon([poly.intersection(convex_hull) for poly in polygonize(lines)]) result = MultiPolygon( [p for p in result] + [p for p in convex_hull.difference(unary_union(result))] ) if bbox is not None: w, s, e, n = bbox cliped_result = clip_by_rect(result, w, s, e, n) return Feature(geometry=cliped_result) return Feature(geometry=result)
def test_line_splitting_rectangle(): """Line splitting rectangle""" geom1 = load_wkt("LINESTRING (10 5, 25 20)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "LINESTRING (15 10, 20 15)"
def test_line_inside(): """Line inside""" geom1 = load_wkt("LINESTRING (15 15, 16 15)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "LINESTRING (15 15, 16 15)"
def test_line_on_boundary(): """Line on boundary""" geom1 = load_wkt("LINESTRING (10 15, 10 10, 15 10)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
def test_point_on_boundary(): """Point on boundary""" geom1 = load_wkt("POINT (15 10)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
def test_line_outside(): """Line outside""" geom1 = load_wkt("LINESTRING (0 0, -5 5)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
def test_point_on_boundary(): """Point on boundary""" geom1 = load_wkt("POINT (15 10)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
def test_point_inside(): """Point inside""" geom1 = load_wkt("POINT (15 15)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "POINT (15 15)"
def polygon_fully_within_rectangle(): """Polygon fully within rectangle""" wkt = "POLYGON ((1 1, 1 30, 30 30, 30 1, 1 1), (10 10, 20 10, 20 20, 10 20, 10 10))" geom1 = load_wkt(wkt) geom2 = clip_by_rect(geom1, 0, 0, 40, 40) assert dump_wkt(geom2, rounding_precision=0) == wkt
def test_point_outside(): """Point outside""" geom1 = load_wkt("POINT (0 0)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
def test_line_splitting_rectangle(): """Line splitting rectangle""" geom1 = load_wkt("LINESTRING (10 5, 25 20)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "LINESTRING (15 10, 20 15)"
def test_polygon_shell_cc_fully_on_rectangle_boundary(): """Polygon shell (CW) fully on rectangle boundary""" geom1 = load_wkt("POLYGON ((10 10, 10 20, 20 20, 20 10, 10 10))") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0 ) == "POLYGON ((10 10, 20 10, 20 20, 10 20, 10 10))"
def test_line_on_boundary(): """Line on boundary""" geom1 = load_wkt("LINESTRING (10 15, 10 10, 15 10)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
def test_line_inside(): """Line inside""" geom1 = load_wkt("LINESTRING (15 15, 16 15)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "LINESTRING (15 15, 16 15)"
def test_line_outside(): """Line outside""" geom1 = load_wkt("LINESTRING (0 0, -5 5)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
def test_polygon_shell_cc_fully_on_rectangle_boundary(): """Polygon shell (CW) fully on rectangle boundary""" geom1 = load_wkt("POLYGON ((10 10, 10 20, 20 20, 20 10, 10 10))") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "POLYGON ((10 10, 20 10, 20 20, 10 20, 10 10))"
def test_point_outside(): """Point outside""" geom1 = load_wkt("POINT (0 0)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
def polygon_hole_cw_fully_on_rectangle_boundary(): """Polygon hole (CW) fully on rectangle boundary""" geom1 = load_wkt("POLYGON ((0 0, 0 30, 30 30, 30 0, 0 0), (10 10, 10 20, 20 20, 20 10, 10 10))") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
def test_point_inside(): """Point inside""" geom1 = load_wkt("POINT (15 15)") geom2 = clip_by_rect(geom1, 10, 10, 20, 20) assert dump_wkt(geom2, rounding_precision=0) == "POINT (15 15)"
def polygon_overlapping_rectangle(): """Polygon overlapping rectangle""" wkt = "POLYGON ((0 0, 0 30, 30 30, 30 0, 0 0), (10 10, 20 10, 20 20, 10 20, 10 10))" geom1 = load_wkt(wkt) geom2 = clip_by_rect(geom1, 5, 5, 15, 15) assert dump_wkt(geom2, rounding_precision=0) == "POLYGON ((5 5, 5 15, 10 15, 10 10, 15 10, 15 5, 5 5))"
def polygon_fully_within_rectangle(): """Polygon fully within rectangle""" wkt = "POLYGON ((1 1, 1 30, 30 30, 30 1, 1 1), (10 10, 20 10, 20 20, 10 20, 10 10))" geom1 = load_wkt(wkt) geom2 = clip_by_rect(geom1, 0, 0, 40, 40) assert dump_wkt(geom2, rounding_precision=0) == wkt