def test_line_interpolate_point_geom_array_normalized(): actual = pygeos.line_interpolate_point( [line_string, linear_ring, multi_line_string], 1, normalized=True ) assert pygeos.equals(actual[0], pygeos.Geometry("POINT (1 1)")) assert pygeos.equals(actual[1], pygeos.Geometry("POINT (0 0)")) assert pygeos.equals(actual[2], pygeos.Geometry("POINT (1 2)"))
def test_polygonize_array(): lines = [ pygeos.Geometry("LINESTRING (0 0, 1 1)"), pygeos.Geometry("LINESTRING (0 0, 0 1)"), pygeos.Geometry("LINESTRING (0 1, 1 1)"), ] expected = pygeos.Geometry("GEOMETRYCOLLECTION (POLYGON ((1 1, 0 0, 0 1, 1 1)))") result = pygeos.polygonize(np.array(lines)) assert isinstance(result, pygeos.Geometry) assert result == expected result = pygeos.polygonize(np.array([lines])) assert isinstance(result, np.ndarray) assert result.shape == (1,) assert result[0] == expected arr = np.array([lines, lines]) assert arr.shape == (2, 3) result = pygeos.polygonize(arr) assert isinstance(result, np.ndarray) assert result.shape == (2,) assert result[0] == expected assert result[1] == expected arr = np.array([[lines, lines], [lines, lines], [lines, lines]]) assert arr.shape == (3, 2, 3) result = pygeos.polygonize(arr) assert isinstance(result, np.ndarray) assert result.shape == (3, 2) for res in result.flatten(): assert res == expected
def test_line_interpolate_point_geom_array(): actual = pygeos.line_interpolate_point( [line_string, linear_ring, multi_line_string], -1) assert pygeos.equals(actual[0], pygeos.Geometry("POINT (1 0)")) assert pygeos.equals(actual[1], pygeos.Geometry("POINT (0 1)")) assert pygeos.equals_exact(actual[2], pygeos.Geometry("POINT (0.5528 1.1056)"), tolerance=0.001)
def test_elevation(self) -> None: # 111319.458metres = 1 degree of longtitude at 0 degrees latitude #expecting 45 degree elevation geometry = [pygeos.Geometry("LineString (0 0 0,0.01 0 1113.19458)"), pygeos.Geometry("LineString (45 0 0,45.01 0 1113.19458)"), pygeos.Geometry("LineString (90 0 0,90.01 0 1113.19458)")] lines = gpd.GeoSeries(geometry, crs=cm.constants.epsg_wgs84) self.assertTrue(np.all(44<observations.elevation(lines)) and np.all(46>observations.elevation(lines)))
def test_reverse_none(): assert pygeos.reverse(None) is None assert pygeos.reverse([None]).tolist() == [None] geometry = pygeos.Geometry("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))") expected = pygeos.Geometry("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))") result = pygeos.reverse([None, geometry]) assert result[0] is None assert pygeos.equals(result[1], expected)
def test_rays(self) -> None: r = [[0, 0, 0], [1, 1, 1]] s = [[10000, 0, 0], [10001, 1, 1]] expected = [ pygeos.Geometry("LineString (0 0 0,1000 0 0)"), pygeos.Geometry("LineString (1 1 1,1001 1 1)") ] out = geo.rays(r, s) self.assertTrue(np.all(pygeos.predicates.equals(out, expected)))
def test_polygonize_array_axis(): lines = [ pygeos.Geometry("LINESTRING (0 0, 1 1)"), pygeos.Geometry("LINESTRING (0 0, 0 1)"), pygeos.Geometry("LINESTRING (0 1, 1 1)"), ] arr = np.array([lines, lines]) # shape (2, 3) result = pygeos.polygonize(arr, axis=1) assert result.shape == (2,) result = pygeos.polygonize(arr, axis=0) assert result.shape == (3,)
def test_set_precision_drop_coords(): # setting precision of 0 will not drop duplicated points in original geometry = pygeos.set_precision( pygeos.Geometry("LINESTRING (0 0, 0 0, 0 1, 1 1)"), 0) assert pygeos.equals(geometry, pygeos.Geometry("LINESTRING (0 0, 0 0, 0 1, 1 1)")) # setting precision will remove duplicated points geometry = pygeos.set_precision(geometry, 1) assert pygeos.equals(geometry, pygeos.Geometry("LINESTRING (0 0, 0 1, 1 1)"))
def test_set_precision(): initial_geometry = pygeos.Geometry("POINT (0.9 0.9)") assert pygeos.get_precision(initial_geometry) == 0 geometry = pygeos.set_precision(initial_geometry, 0) assert pygeos.get_precision(geometry) == 0 assert pygeos.equals(geometry, initial_geometry) geometry = pygeos.set_precision(initial_geometry, 1) assert pygeos.get_precision(geometry) == 1 assert pygeos.equals(geometry, pygeos.Geometry("POINT (1 1)")) # original should remain unchanged assert pygeos.equals(initial_geometry, pygeos.Geometry("POINT (0.9 0.9)"))
def test_polygonize_full_array_axis(): lines = [ pygeos.Geometry("LINESTRING (0 0, 1 1)"), pygeos.Geometry("LINESTRING (0 0, 0 1)"), pygeos.Geometry("LINESTRING (0 1, 1 1)"), ] arr = np.array([lines, lines]) # shape (2, 3) result = pygeos.polygonize_full(arr, axis=1) assert len(result) == 4 assert all(arr.shape == (2,) for arr in result) result = pygeos.polygonize_full(arr, axis=0) assert len(result) == 4 assert all(arr.shape == (3,) for arr in result)
def boundary_distance(polygon, points): """ Find the distance between a polygon's boundary and an array of points. Uses either `shapely` or `pygeos` (5-10x faster) as a backend. Parameters ------------- polygon : shapely.geometry.Polygon Polygon to query points : (n, 2) float 2D points Returns ------------ distance : (n,) float Minimum distance from each point to polygon boundary """ try: import pygeos # the pygeos way is 5-10x faster pg_points = pygeos.points(*points.T) pg_boundary = pygeos.boundary(pygeos.Geometry(polygon.wkt)) distance = pygeos.distance(pg_boundary, pg_points) except BaseException: # in pure shapely we have to loop inverse = polygon.boundary distance = np.array([ inverse.distance(i) for i in MultiPoint(points)]) return distance
def test_set_precision_preserve_topology(): # GEOS test case - geometry is valid initially but becomes # invalid after rounding geometry = pygeos.Geometry( "POLYGON((10 10,20 10,16 15,20 20, 10 20, 14 15, 10 10))") assert pygeos.equals( pygeos.set_precision(geometry, 5, preserve_topology=False), pygeos.Geometry( "POLYGON ((10 10, 20 10, 15 15, 20 20, 10 20, 15 15, 10 10))"), ) assert pygeos.equals( pygeos.set_precision(geometry, 5, preserve_topology=True), pygeos.Geometry( "MULTIPOLYGON (((10 10, 15 15, 20 10, 10 10)), ((15 15, 10 20, 20 20, 15 15)))" ), )
def test_polygonize_full_array(): lines = [ pygeos.Geometry("LINESTRING (0 0, 1 1)"), pygeos.Geometry("LINESTRING (0 0, 0 1)"), pygeos.Geometry("LINESTRING (0 1, 1 1)"), ] expected = pygeos.Geometry("GEOMETRYCOLLECTION (POLYGON ((1 1, 0 0, 0 1, 1 1)))") result = pygeos.polygonize_full(np.array(lines)) assert len(result) == 4 assert all(isinstance(geom, pygeos.Geometry) for geom in result) assert result[0] == expected assert all( geom == pygeos.Geometry("GEOMETRYCOLLECTION EMPTY") for geom in result[1:] ) result = pygeos.polygonize_full(np.array([lines])) assert len(result) == 4 assert all(isinstance(geom, np.ndarray) for geom in result) assert all(geom.shape == (1,) for geom in result) assert result[0][0] == expected assert all( geom[0] == pygeos.Geometry("GEOMETRYCOLLECTION EMPTY") for geom in result[1:] ) arr = np.array([lines, lines]) assert arr.shape == (2, 3) result = pygeos.polygonize_full(arr) assert len(result) == 4 assert all(isinstance(arr, np.ndarray) for arr in result) assert all(arr.shape == (2,) for arr in result) assert result[0][0] == expected assert result[0][1] == expected assert all( g == pygeos.Geometry("GEOMETRYCOLLECTION EMPTY") for geom in result[1:] for g in geom ) arr = np.array([[lines, lines], [lines, lines], [lines, lines]]) assert arr.shape == (3, 2, 3) result = pygeos.polygonize_full(arr) assert len(result) == 4 assert all(isinstance(arr, np.ndarray) for arr in result) assert all(arr.shape == (3, 2) for arr in result) for res in result[0].flatten(): assert res == expected for arr in result[1:]: for res in arr.flatten(): assert res == pygeos.Geometry("GEOMETRYCOLLECTION EMPTY")
def test_set_precision_intersection(): """Operations should use the most precise presision grid size of the inputs""" box1 = pygeos.normalize(pygeos.box(0, 0, 0.9, 0.9)) box2 = pygeos.normalize(pygeos.box(0.75, 0, 1.75, 0.75)) assert pygeos.get_precision(pygeos.intersection(box1, box2)) == 0 # GEOS will use and keep the most precise precision grid size box1 = pygeos.set_precision(box1, 0.5) box2 = pygeos.set_precision(box2, 1) out = pygeos.intersection(box1, box2) assert pygeos.get_precision(out) == 0.5 assert pygeos.equals(out, pygeos.Geometry("LINESTRING (1 1, 1 0)"))
def test_polygonize(): lines = [ pygeos.Geometry("LINESTRING (0 0, 1 1)"), pygeos.Geometry("LINESTRING (0 0, 0 1)"), pygeos.Geometry("LINESTRING (0 1, 1 1)"), pygeos.Geometry("LINESTRING (1 1, 1 0)"), pygeos.Geometry("LINESTRING (1 0, 0 0)"), pygeos.Geometry("LINESTRING (5 5, 6 6)"), pygeos.Geometry("POINT (0 0)"), None, ] result = pygeos.polygonize(lines) assert pygeos.get_type_id(result) == 7 # GeometryCollection expected = pygeos.Geometry( "GEOMETRYCOLLECTION (POLYGON ((0 0, 1 1, 1 0, 0 0)), POLYGON ((1 1, 0 0, 0 1, 1 1)))" ) assert result == expected
def trans2wkb4series(s, index=range(0, 0)): if isinstance(index, range): index = range(0, s.size) import pygeos s_arr = [] if not isinstance(s, pd.Series): return None try: for i in range(0, s.size): if not s[i]: s_arr.append(None) else: s_arr.append(pygeos.to_wkb(pygeos.Geometry(s[i]))) s = pd.Series(s_arr, index=index) except: return None return s
def test_polygonize_full_missing(): # set of geometries that is all missing result = pygeos.polygonize_full([None, None]) assert len(result) == 4 assert all(geom == pygeos.Geometry("GEOMETRYCOLLECTION EMPTY") for geom in result)
def test_set_precision_grid_size_nan(): assert pygeos.set_precision(pygeos.Geometry("POINT (0.9 0.9)"), np.nan) is None
def test_set_precision_z(): geometry = pygeos.set_precision(pygeos.Geometry("POINT Z (0.9 0.9 0.9)"), 1) assert pygeos.get_precision(geometry) == 1 assert pygeos.equals(geometry, pygeos.Geometry("POINT Z (1 1 0.9)"))
def test_adapt_ptr_raises(): point = pygeos.Geometry("POINT (2 2)") with pytest.raises(AttributeError): point._ptr += 1
def test_new_from_wkt(geom): actual = pygeos.Geometry(str(geom)) assert pygeos.equals(actual, geom)
) assert pygeos.equals( pygeos.set_precision(geometry, 5, preserve_topology=True), pygeos.Geometry( "MULTIPOLYGON (((10 10, 15 15, 20 10, 10 10)), ((15 15, 10 20, 20 20, 15 15)))" ), ) @pytest.mark.skipif(pygeos.geos_version < (3, 6, 0), reason="GEOS < 3.6") @pytest.mark.parametrize( "geometry,expected", [ ( pygeos.Geometry("LINESTRING (0 0, 0.1 0.1)"), pygeos.Geometry("LINESTRING EMPTY"), ), ( pygeos.Geometry("LINEARRING (0 0, 0.1 0, 0.1 0.1, 0 0.1, 0 0)"), pygeos.Geometry("LINEARRING EMPTY"), ), ( pygeos.Geometry("POLYGON ((0 0, 0.1 0, 0.1 0.1, 0 0.1, 0 0))"), pygeos.Geometry("POLYGON EMPTY"), ), ], ) def test_set_precision_collapse(geometry, expected): """Lines and polygons collapse to empty geometries if vertices are too close""" assert pygeos.equals(pygeos.set_precision(geometry, 1), expected)
def test_line_interpolate_point_float_array(): actual = pygeos.line_interpolate_point(line_string, [0.2, 1.5, -0.2]) assert pygeos.equals(actual[0], pygeos.Geometry("POINT (0.2 0)")) assert pygeos.equals(actual[1], pygeos.Geometry("POINT (1 0.5)")) assert pygeos.equals(actual[2], pygeos.Geometry("POINT (1 0.8)"))
) point = pygeos.points(2, 3) line_string = pygeos.linestrings([(0, 0), (1, 0), (1, 1)]) linear_ring = pygeos.linearrings([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) polygon = pygeos.polygons([(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)]) multi_point = pygeos.multipoints([(0, 0), (1, 2)]) multi_line_string = pygeos.multilinestrings([[(0, 0), (1, 2)]]) multi_polygon = pygeos.multipolygons([ [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], [(2.1, 2.1), (2.2, 2.1), (2.2, 2.2), (2.1, 2.2), (2.1, 2.1)], ]) geometry_collection = pygeos.geometrycollections( [pygeos.points(51, -1), pygeos.linestrings([(52, -1), (49, 2)])]) point_z = pygeos.points(1.0, 1.0, 1.0) polygon_with_hole = pygeos.Geometry( "POLYGON((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, 2 4, 4 4, 4 2, 2 2))") empty = pygeos.Geometry("GEOMETRYCOLLECTION EMPTY") all_types = ( point, line_string, linear_ring, polygon, multi_point, multi_line_string, multi_polygon, geometry_collection, empty, )
def test_query_tree_with_none(): # valid GEOS binary predicate, but not supported for query tree = pygeos.STRtree( [pygeos.Geometry("POINT (0 0)"), None, pygeos.Geometry("POINT (2 2)")]) assert tree.query(pygeos.points(2, 2), predicate="intersects") == [2]
def test_new_from_wkb(): geom = point actual = pygeos.Geometry(geom.to_wkb()) assert pygeos.equals(actual, geom)
def test_line_interpolate_point_geom_array(): actual = pygeos.line_interpolate_point([line_string, linear_ring], -1) assert pygeos.equals(actual[0], pygeos.Geometry("POINT (1 0)")) assert pygeos.equals(actual[1], pygeos.Geometry("POINT (0 1)"))
) point = pygeos.points(2, 3) line_string = pygeos.linestrings([(0, 0), (1, 0), (1, 1)]) linear_ring = pygeos.linearrings([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) polygon = pygeos.polygons([(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)]) multi_point = pygeos.multipoints([(0, 0), (1, 2)]) multi_line_string = pygeos.multilinestrings([[(0, 0), (1, 2)]]) multi_polygon = pygeos.multipolygons([ [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], [(2.1, 2.1), (2.2, 2.1), (2.2, 2.2), (2.1, 2.2), (2.1, 2.1)], ]) geometry_collection = pygeos.geometrycollections( [pygeos.points(51, -1), pygeos.linestrings([(52, -1), (49, 2)])]) point_z = pygeos.points(1.0, 1.0, 1.0) polygon_with_hole = pygeos.Geometry( "POLYGON((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, 2 4, 4 4, 4 2, 2 2))") empty_point = pygeos.Geometry("POINT EMPTY") empty_line_string = pygeos.Geometry("LINESTRING EMPTY") empty = pygeos.Geometry("GEOMETRYCOLLECTION EMPTY") all_types = ( point, line_string, linear_ring, polygon, multi_point, multi_line_string, multi_polygon, geometry_collection, empty, )
pygeos.box(2, 2, 4, 4), ) point = pygeos.points(2, 3) line_string = pygeos.linestrings([(0, 0), (1, 0), (1, 1)]) linear_ring = pygeos.linearrings([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) polygon = pygeos.polygons([(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)]) multi_point = pygeos.multipoints([(0, 0), (1, 2)]) multi_line_string = pygeos.multilinestrings([[(0, 0), (1, 2)]]) multi_polygon = pygeos.multipolygons([ [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], [(2.1, 2.1), (2.2, 2.1), (2.2, 2.2), (2.1, 2.2), (2.1, 2.1)], ]) geometry_collection = pygeos.geometrycollections( [pygeos.points(51, -1), pygeos.linestrings([(52, -1), (49, 2)])]) point_z = pygeos.points(1.0, 1.0, 1.0) polygon_with_hole = pygeos.Geometry( "POLYGON((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, 2 4, 4 4, 4 2, 2 2))") all_types = ( point, line_string, linear_ring, polygon, multi_point, multi_line_string, multi_polygon, geometry_collection, pygeos.Empty, )
actual = pygeos.minimum_clearance([point, empty]) assert np.isinf(actual).all() @pytest.mark.skipif(pygeos.geos_version < (3, 6, 0), reason="GEOS < 3.6") def test_minimum_clearance_missing(): actual = pygeos.minimum_clearance(None) assert np.isnan(actual) @pytest.mark.skipif(pygeos.geos_version < (3, 8, 0), reason="GEOS < 3.8") @pytest.mark.parametrize( "geometry, expected", [ ( pygeos.Geometry("POLYGON ((0 5, 5 10, 10 5, 5 0, 0 5))"), 5, ), ( pygeos.Geometry("LINESTRING (1 0, 1 10)"), 5, ), ( pygeos.Geometry("MULTIPOINT (2 2, 4 2)"), 1, ), ( pygeos.Geometry("POINT (2 2)"), 0, ), (