def test_extract_geopandas_geodataframe(): data = geopandas.read_file( "tests/files_geojson/naturalearth_alb_grc.geojson", driver="GeoJSON" ) topo = Extract(data).to_dict() assert len(topo["bookkeeping_geoms"]) == 3
def test_extract_shapely_org_data_untouched(): data = geometry.LineString([[0, 0], [1, 0], [1, 1], [0, 1]]) topo = Extract(data).to_dict() topo_0 = topo["objects"][0] assert "arcs" in topo_0.keys() assert data.type == "LineString"
def test_geojson_feature_geom_col(self): with open("tests/files_geojson/feature.geojson") as f: data = geojson.load(f) topo = Extract(data).to_dict() self.assertEqual(len(topo["objects"]), 1) self.assertEqual(len(topo["bookkeeping_geoms"]), 3) self.assertEqual(len(topo["linestrings"]), 3)
def test_extract_geomcol_multipolygon_polygon(self): data = { "foo": { "type": "GeometryCollection", "geometries": [ { "type": "MultiPolygon", "coordinates": [ [ [[10, 20], [20, 0], [0, 0], [3, 13], [10, 20]], [[3, 2], [10, 16], [17, 2], [3, 2]], ], [[[10, 4], [14, 4], [10, 12], [10, 4]]], ], }, { "type": "Polygon", "coordinates": [[[20, 0], [35, 5], [10, 20], [20, 0]]], }, ], } } topo = Extract(data).to_dict() self.assertEqual(len(topo["linestrings"]), 4)
def test_extract_geo_interface_shapefile(): import shapefile data = shapefile.Reader("tests/files_shapefile/southamerica.shp") topo = Extract(data).to_dict() assert len(topo["linestrings"]) == 15
def test_nested_geometrycollection(self): data = { "foo": { "type": "GeometryCollection", "geometries": [ { "type": "GeometryCollection", "geometries": [{ "type": "LineString", "coordinates": [[0.1, 0.2], [0.3, 0.4]], }], }, { "type": "Polygon", "coordinates": [[[0.5, 0.6], [0.7, 0.8], [0.9, 1.0]]], }, ], } } topo = Extract(data).to_dict() self.assertEqual( len(topo["objects"]["foo"]["geometries"][0]["geometries"][0] ["arcs"]), 1)
def test_extract_invalid_dict_item(): data = { "type": "MultiPolygon", "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]], } topo = Extract(data).to_dict() assert len(topo["bookkeeping_geoms"]) == 0
def test_extract_invalid_polygon(): data = { "wrong": {"type": "Polygon", "coordinates": [[[0, 0], [1, 0], [2, 0], [0, 0]]]}, "valid": {"type": "Polygon", "coordinates": [[[0, 0], [2, 0], [1, 1], [0, 0]]]}, } topo = Extract(data).to_dict() assert len(topo["linestrings"]) == 1
def test_extract_linestring(): data = { "foo": {"type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]]}, "bar": {"type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]]}, } topo = Extract(data).to_dict() assert len(topo["linestrings"]) == 2
def test_extract_geo_interface_from_list(): data = [ {"type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]]}, {"type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]]}, ] topo = Extract(data).to_dict() assert len(topo["linestrings"]) == 2
def test_shapely_geo_interface_from_list(self): data = [ geometry.Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), geometry.Polygon([[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]), ] topo = Extract(data).to_dict() self.assertEqual(len(topo["linestrings"]), 2) self.assertEqual(isinstance(topo["objects"][0], dict), True)
def test_extract_geojson_feature_geom_col(): with open("tests/files_geojson/feature.geojson") as f: data = geojson.load(f) topo = Extract(data).to_dict() assert len(topo["objects"]) == 1 assert len(topo["bookkeeping_geoms"]) == 3 assert len(topo["linestrings"]) == 3
def test_shapely_geometrycollection(self): data = geometry.GeometryCollection([ geometry.Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), geometry.Polygon([[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]), ]) topo = Extract(data).to_dict() self.assertEqual(len(topo["objects"]), 1) self.assertEqual(len(topo["bookkeeping_geoms"]), 2) self.assertEqual(len(topo["linestrings"]), 2)
def test_extract_shapely_geo_interface_from_list(): data = [ geometry.Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), geometry.Polygon([[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]), ] topo = Extract(data).to_dict() assert len(topo["linestrings"]) == 2 assert isinstance(topo["objects"][0], dict)
def test_extract_gdf_org_data_untouched(): data = geopandas.read_file( "tests/files_geojson/naturalearth_alb_grc.geojson", driver="GeoJSON") topo = Extract(data).to_dict() topo_0 = topo["objects"][0] data_0 = data.iloc[0] assert "arcs" in topo_0.keys() assert data_0.geometry.type == "Polygon"
def test_geopandas_geoseries(self): data = geopandas.GeoSeries([ geometry.Polygon([(0, 0), (1, 0), (1, 1)]), geometry.Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), geometry.Polygon([(2, 0), (3, 0), (3, 1), (2, 1)]), ]) topo = Extract(data).to_dict() self.assertEqual(len(topo["objects"]), 3) self.assertEqual(len(topo["bookkeeping_geoms"]), 3) self.assertEqual(len(topo["linestrings"]), 3)
def test_extract_shapefile_org_data_untouched(): import shapefile data = shapefile.Reader("tests/files_shapefile/southamerica.shp") topo = Extract(data).to_dict() topo_0 = topo["objects"]["feature_00"]["geometries"][0] data_0 = data.__geo_interface__["features"][0]["geometry"] assert "arcs" in topo_0.keys() assert "arcs" not in data_0.keys()
def test_extract_shapely_geometrycollection(): data = geometry.GeometryCollection([ geometry.Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), geometry.Polygon([[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]), ]) topo = Extract(data).to_dict() assert len(topo["objects"]) == 1 assert len(topo["bookkeeping_geoms"]) == 2 assert len(topo["linestrings"]) == 2
def test_extract_points(): data = [ {"type": "Polygon", "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]}, {"type": "Point", "coordinates": [0.5, 0.5]}, ] topo = Extract(data).to_dict() assert len(topo["bookkeeping_coords"]) == 1 assert len(topo["bookkeeping_geoms"]) == 1 assert topo["coordinates"][0].wkt == "POINT (0.5 0.5)" assert "coordinates" in topo["objects"][1].keys()
def test_extract_list_org_data_untouched(): data = [ geometry.Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), geometry.Polygon([[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]), ] topo = Extract(data).to_dict() topo_0 = topo["objects"][0] data_0 = data[0] assert "arcs" in topo_0.keys() assert data_0.type == "Polygon"
def test_linestring(self): data = { "foo": { "type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]] }, "bar": { "type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]] }, } topo = Extract(data).to_dict() self.assertEqual(len(topo["linestrings"]), 2)
def test_geo_interface_from_list(self): data = [ { "type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]] }, { "type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]] }, ] topo = Extract(data).to_dict() self.assertEqual(len(topo["linestrings"]), 2)
def test_extract_geometrycollection_polygon(): data = { "bar": {"type": "Polygon", "coordinates": [[[0, 0], [1, 1], [2, 0]]]}, "foo": { "type": "GeometryCollection", "geometries": [ {"type": "LineString", "coordinates": [[0.1, 0.2], [0.3, 0.4]]} ], }, } topo = Extract(data).to_dict() assert len(topo["linestrings"]) == 2
def test_invalid_polygon(self): data = { "wrong": { "type": "Polygon", "coordinates": [[[0, 0], [1, 0], [2, 0], [0, 0]]], }, "valid": { "type": "Polygon", "coordinates": [[[0, 0], [2, 0], [1, 1], [0, 0]]], }, } topo = Extract(data).to_dict() self.assertEqual(len(topo["linestrings"]), 1)
def test_multilinestring(self): data = { "foo": { "type": "MultiLineString", "coordinates": [ [[0.0, 0.0], [1, 1], [3, 3]], [[1, 1], [0, 1]], [[3, 3], [4, 4], [0, 1]], ], } } topo = Extract(data).to_dict() self.assertEqual(len(topo["linestrings"]), 3)
def test_extract_dict_org_data_untouched(): data = { "foo": { "type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]] }, "bar": { "type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]] }, } topo = Extract(data).to_dict() topo_foo = topo["objects"]["foo"] data_foo = data["foo"] assert "arcs" in topo_foo.keys() assert "arcs" not in data_foo.keys()
def test_extract_features(): data = { "foo": { "type": "Feature", "geometry": {"type": "LineString", "coordinates": [[0.1, 0.2], [0.3, 0.4]]}, }, "bar": { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [[[0.5, 0.6], [0.7, 0.8], [0.9, 1.0]]], }, }, } topo = Extract(data).to_dict() assert len(topo["linestrings"]) == 2
def test_multipolygon(self): # multipolygon with hole data = { "foo": { "type": "MultiPolygon", "coordinates": [ [ [[0, 0], [20, 0], [10, 20], [0, 0]], # CCW [[3, 2], [10, 16], [17, 2], [3, 2]], # CW ], [[[6, 4], [14, 4], [10, 12], [6, 4]]], # CCW [[[25, 5], [30, 10], [35, 5], [25, 5]]], ], } } topo = Extract(data).to_dict() self.assertEqual(len(topo["bookkeeping_geoms"]), 3) self.assertEqual(len(topo["linestrings"]), 4)
def test_features(self): data = { "foo": { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [[.1, .2], [.3, .4]] }, }, "bar": { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [[[0.5, 0.6], [0.7, 0.8], [0.9, 1.0]]], }, }, } topo = Extract(data).to_dict() self.assertEqual(len(topo["linestrings"]), 2)
def test_extract_source_data_modify(): # prepare data feat_1 = Feature( geometry=Polygon([[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]), properties={"name": "abc"}, ) feat_2 = Feature( geometry=Polygon([[[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]]), properties={"name": "def"}, ) data = FeatureCollection([feat_1, feat_2]) # before Topology() assert "geometry" in data["features"][0].keys() # apply Topology() topo = Extract(data) # after Topology() assert "geometry" in data["features"][0].keys()