def test_get_parts_geometry_collection_multi(): """On the first pass, the individual Multi* geometry objects are returned from the collection. On the second pass, the individual singular geometry objects within those are returned. """ geom = pygeos.geometrycollections([multi_point, multi_line_string, multi_polygon]) expected_num_parts = pygeos.get_num_geometries(geom) expected_parts = pygeos.get_geometry(geom, range(0, expected_num_parts)) parts = pygeos.get_parts(geom) assert len(parts) == expected_num_parts assert np.all(pygeos.equals_exact(parts, expected_parts)) expected_subparts = [] for g in np.asarray(expected_parts): for i in range(0, pygeos.get_num_geometries(g)): expected_subparts.append(pygeos.get_geometry(g, i)) subparts = pygeos.get_parts(parts) assert len(subparts) == len(expected_subparts) assert np.all(pygeos.equals_exact(subparts, expected_subparts))
point_polygon_testdata = ( pygeos.points(np.arange(6), np.arange(6)), 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,
def test_geometrycollections(geometries, indices, expected): actual = pygeos.geometrycollections(geometries, indices=indices) assert_geometries_equal(actual, expected)
def test_to_wkt_geometrycollection_with_point_empty(): collection = pygeos.geometrycollections([empty_point, point]) # do not check the full value as some GEOS versions give # GEOMETRYCOLLECTION Z (...) and others give GEOMETRYCOLLECTION (...) assert pygeos.to_wkt(collection).endswith("(POINT EMPTY, POINT (2 3))")
geometry_collection, geometry_collection_z, line_string, line_string_z, linear_ring, multi_line_string, multi_point, multi_polygon, point, point_z, polygon, polygon_with_hole, polygon_z, ) nested_2 = pygeos.geometrycollections([geometry_collection, point]) nested_3 = pygeos.geometrycollections([nested_2, point]) @pytest.mark.parametrize( "geoms,count", [ ([], 0), ([empty], 0), ([point, empty], 1), ([empty, point, empty], 1), ([point, None], 1), ([None, point, None], 1), ([point, point], 2), ([point, point_z], 2), ([line_string, linear_ring], 8),
byte_order=1) == ewkb point = pygeos.points(1, 1) point_with_srid = pygeos.set_srid(point, np.int32(4326)) result = pygeos.to_wkb(point_with_srid, include_srid=True, byte_order=1) assert np.frombuffer(result[5:9], "<u4").item() == 4326 @pytest.mark.skipif(pygeos.geos_version >= (3, 8, 0), reason="Pre GEOS 3.8.0 has 3D empty points") @pytest.mark.parametrize("geom,dims,expected", [ (empty_point, 2, POINT_NAN_WKB), (empty_point, 3, POINTZ_NAN_WKB), (pygeos.multipoints([empty_point]), 2, MULTIPOINT_NAN_WKB), (pygeos.multipoints([empty_point]), 3, MULTIPOINTZ_NAN_WKB), (pygeos.geometrycollections([empty_point]), 2, GEOMETRYCOLLECTION_NAN_WKB), (pygeos.geometrycollections([empty_point ]), 3, GEOMETRYCOLLECTIONZ_NAN_WKB), (pygeos.geometrycollections([pygeos.multipoints([empty_point]) ]), 2, NESTED_COLLECTION_NAN_WKB), (pygeos.geometrycollections([pygeos.multipoints([empty_point]) ]), 3, NESTED_COLLECTIONZ_NAN_WKB), ]) def test_to_wkb_point_empty_pre_geos38(geom, dims, expected): actual = pygeos.to_wkb(geom, output_dimension=dims, byte_order=1) # Use numpy.isnan; there are many byte representations for NaN assert actual[:-dims * 8] == expected[:-dims * 8] assert np.isnan(struct.unpack("<{}d".format(dims), actual[-dims * 8:])).all()
point_polygon_testdata = ( pygeos.points(np.arange(6), np.arange(6)), 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) line_string_z = pygeos.linestrings([(0, 0, 0), (1, 0, 1), (1, 1, 2)]) polygon_z = pygeos.polygons([(0, 0, 0), (2, 0, 1), (2, 2, 2), (0, 2, 3), (0, 0, 0)]) geometry_collection_z = pygeos.geometrycollections([point_z, line_string_z]) 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_polygon = pygeos.Geometry("POLYGON EMPTY") empty = pygeos.Geometry("GEOMETRYCOLLECTION EMPTY") line_string_nan = pygeos.linestrings([(np.nan, np.nan), (np.nan, np.nan)]) all_types = (
# This matches GEOS 3.8.0 behavior on simple empty geometries. assert pygeos.equals( pygeos.line_interpolate_point(geom, 0.2, normalized=normalized), empty_point) @pytest.mark.parametrize("normalized", [False, True]) @pytest.mark.parametrize( "geom", [ empty_point, point, polygon, multi_point, multi_polygon, pygeos.geometrycollections([point]), pygeos.geometrycollections([polygon]), pygeos.geometrycollections([multi_line_string]), pygeos.geometrycollections([multi_point]), pygeos.geometrycollections([multi_polygon]), ], ) def test_line_interpolate_point_invalid_type(geom, normalized): with pytest.raises(TypeError): assert pygeos.line_interpolate_point(geom, 0.2, normalized=normalized) def test_line_interpolate_point_none(): assert pygeos.line_interpolate_point(None, 0.2) is None
def test_geometrycollections_no_index_raises(): with pytest.raises(ValueError): pygeos.geometrycollections( np.array([point, line_string], dtype=object), indices=[0, 2] )
def test_geometrycollections(geometries, indices, expected): actual = pygeos.geometrycollections( np.array(geometries, dtype=object), indices=indices ) assert_geometries_equal(actual, expected)