def test_topology_computing_topology(): data = [ {"type": "LineString", "coordinates": [[4, 0], [2, 2], [0, 0]]}, {"type": "LineString", "coordinates": [[0, 2], [1, 1], [2, 2], [3, 1], [4, 2]]}, ] no_topo = topojson.Topology(data, topology=False, prequantize=False).to_dict() topo = topojson.Topology(data, topology=True, prequantize=False).to_dict() assert len(topo["arcs"]) == 4 assert len(no_topo["arcs"]) == 2
def test_topology_toposimplify_on_topojson_data(): # load topojson file into dict with open("tests/files_topojson/gm.topo.json", 'r') as f: data = json.load(f) # read as topojson and as geojson topo_0 = topojson.Topology(data, object_name="gm_features") gdf_0 = topo_0.toposimplify(10).to_gdf() topo_1 = topojson.Topology(topo_0.to_geojson(), prequantize=False, object_name="out") gdf_1 = topo_1.toposimplify(10).to_gdf() assert gdf_0.iloc[0].geometry.is_valid == gdf_1.iloc[0].geometry.is_valid
def test_topology_bbox_no_delta_transform(): data = { "foo": { "type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]] }, "bar": { "type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]] }, } topo_1 = topojson.Topology(data, object_name="topo_1").to_dict() topo_2 = topojson.Topology(topo_1, object_name="topo_1").to_dict() assert topo_1["bbox"] == topo_2["bbox"]
def test_topology_topoquantize(): # load example data representing continental Africa data = topojson.utils.example_data_africa() # compute the topology topo = topojson.Topology(data, topoquantize=9).to_dict() assert len(topo['arcs']) == 149
def test_topology_polygon(): data = [ {"type": "Polygon", "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]} ] topo = topojson.Topology(data, topoquantize=True).to_dict() assert topo["transform"]["translate"] == [0.0, 0.0]
def simplify_geo(geo_series,simplify_with,simplify_algorithm,tolerance,geojson_out): """ Use topojson to simplify to minimize the gap/overlay among the border This can take a lot of RAM, depending on how complicated the input geometries are """ print('Simplifying geometry...') geo_series = geo_series.simplify(tolerance=0.005,preserve_topology=True) # The preliminary simplify is needed so RAM needed for next step is reduced # The unit in tolerance is in degree topo = tp.Topology(geo_series, topology=True, simplify_with=simplify_with, simplify_algorithm=simplify_algorithm, toposimplify=tolerance ) topo_out = os.path.join('output',f'geo.topo.json') topo.to_json(topo_out) # Topojson doesn't have a way to convert back to geojson, using pytopojson with open(topo_out) as f: topo_ = f.read() f.close() topo_ = json.loads(topo_) feature_ = feature.Feature() geojson = feature_(topo_, 'data') os.remove(topo_out) with open(geojson_out, "w") as outfile: json.dump(geojson, outfile) outfile.close()
def test_topology_to_geojson_quantized_points_only(): data = [{"type": "MultiPoint", "coordinates": [[0.5, 0.5], [1.0, 1.0]]}] geo = topojson.Topology(data, prequantize=False).to_geojson() gj = json.loads(geo) assert gj["type"] == "FeatureCollection" assert gj["features"][0]["geometry"]["coordinates"] == [[0.5, 0.5], [1.0, 1.0]]
def test_topology_topojson_from_file(): with open("tests/files_topojson/naturalearth.topojson", "r") as f: data = json.load(f) topo = topojson.Topology(data).to_dict() assert len(topo["objects"]) == 1
def test_topology_fiona_shapefile_to_geojson(): with fiona.open("tests/files_shapefile/southamerica.shp") as f: data = [f[0], f[1]] topo = topojson.Topology(data) gj = geojson.loads(topo.to_geojson()) assert gj["type"] == "FeatureCollection"
def test_topology_geodataframe_valid(): data = geopandas.read_file( geopandas.datasets.get_path("naturalearth_lowres")) topo = topojson.Topology(data) gdf = topo.toposimplify(10, prevent_oversimplify=False).to_gdf() assert gdf.shape[0] == 177
def test_prequantize_topoquantize_as_chaining(self): data = geopandas.read_file( geopandas.datasets.get_path("naturalearth_lowres")) data = data[(data.name == "Antarctica")] topo = topojson.Topology(data, prequantize=1e6, topology=True) topos = topo.topoquantize(1e5).to_dict() self.assertEqual("transform" in topos.keys(), True)
def test_topology_winding_order_TopoOptions(): data = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres")) data = data[(data.name == "South Africa")] topo = topojson.Topology(data, winding_order="CW_CCW").to_dict(options=True) assert len(topo["objects"]) == 1 assert len(topo["options"]) == 10
def test_topology_point(): data = [{"type": "Point", "coordinates": [0.5, 0.5]}] with pytest.raises(SystemExit) as topo: topojson.Topology(data, topoquantize=True).to_dict() assert topo.type == SystemExit assert topo.value.code == "Cannot quantize when xmax-xmin OR ymax-ymin equals 0"
def test_topology_point(): data = [{"type": "Point", "coordinates": [0.5, 0.5]}] topo = topojson.Topology(data, topoquantize=True).to_dict() # with pytest.warns(RuntimeWarning) as topo: # topojson.Topology(data, topoquantize=True).to_dict() assert len(topo["arcs"]) == 0
def test_topology_widget(): data = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres")) data = data[(data.continent == "Africa")] topo = topojson.Topology(data, prequantize=1e6, topology=True) widget = topo.to_widget() assert len(widget.widget.children) == 4 # pylint: disable=no-member
def test_topology_to_geojson_nested_geometrycollection(): data = { "collection": { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [[0.1, 0.2], [0.3, 0.4]], }, }, { "type": "Feature", "geometry": { "type": "GeometryCollection", "geometries": [{ "type": "Polygon", "coordinates": [[[0.5, 0.6], [0.7, 0.8], [0.9, 1.0]]], }], }, }, ], } } topo = topojson.Topology(data).to_geojson(pretty=False) assert "]]]}]}]}}]}" in topo
def test_topology_topoquantize_as_chaining(): data = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres")) data = data[(data.name == "Antarctica")] topo = topojson.Topology(data, prequantize=False, simplify_with="shapely") topos = topo.topoquantize(1e2).to_dict() assert "transform" in topos.keys()
def test_topology_fiona_gpkg_to_geojson(): with fiona.open("tests/files_shapefile/rivers.gpkg", driver="Geopackage") as f: data = [f[24], f[25]] topo = topojson.Topology(data) gj = geojson.loads(topo.to_geojson()) assert gj["type"] == "FeatureCollection"
def test_topology_with_arcs_without_linestrings(): data = [ {"type": "Polygon", "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]}, {"type": "Polygon", "coordinates": [[[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]]}, ] topo = topojson.Topology(data, prequantize=False, topology=True).to_dict() assert "linestrings" not in topo.keys()
def test_topology_topoquantization_dups(): gdf = geopandas.read_file( geopandas.datasets.get_path("naturalearth_lowres")) gdf = gdf[gdf.name.isin(['France', 'Belgium', 'Netherlands'])] topo = topojson.Topology(data=gdf, prequantize=False).toposimplify(4) topo = topo.topoquantize(50).to_dict() assert topo['arcs'][6] == [[44, 47], [0, 0]]
def test_topology_toposimplify_set_in_options(): data = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres")) data = data[(data.name == "Antarctica")] topo = topojson.Topology( data, prequantize=True, simplify_with="shapely", toposimplify=4 ).to_dict() assert "transform" in topo.keys()
def test_topology_topojson_winding_order(): data = geometry.MultiLineString([ [[0, 0], [0.97, 0], [0.97, 1], [0, 1], [0, 0]], [[1.03, 0], [2, 0], [2, 1], [1.03, 1], [1.03, 0]], ]) topo = topojson.Topology(data, prequantize=False, winding_order="CW_CCW") gj = geojson.loads(topo.to_geojson()) assert gj["type"] == "FeatureCollection"
def test_topology_linestrings_parsed_to_gdf(): data = [ {"type": "LineString", "coordinates": [[4, 0], [2, 2], [0, 0]]}, {"type": "LineString", "coordinates": [[0, 2], [1, 1], [2, 2], [3, 1], [4, 2]]}, ] topo = topojson.Topology(data).to_gdf() assert topo["geometry"][0].wkt != "GEOMETRYCOLLECTION EMPTY" assert topo["geometry"][0].type == "LineString"
def test_topology_polygon_point(): data = [ {"type": "Polygon", "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]}, {"type": "Point", "coordinates": [-0.5, 1.5]}, ] topo = topojson.Topology(data, topoquantize=True).to_dict() assert len(topo["arcs"]) == 1 assert topo["objects"]["data"]["geometries"][1]["coordinates"] == [0, 999999]
def test_winding_order_kwarg_vars(self): data = geopandas.read_file( geopandas.datasets.get_path("naturalearth_lowres")) data = data[(data.name == "South Africa")] topo = topojson.Topology(data, winding_order="CW_CCW").to_dict() self.assertEqual(len(topo["objects"]), 1) self.assertEqual(isinstance(topo["options"], dict), True)
def test_topology_widget(self): data = geopandas.read_file( geopandas.datasets.get_path("naturalearth_lowres")) data = data[(data.continent == "Africa")] topo = topojson.Topology(data, prequantize=1e6, topology=True) widget = topo.to_widget() self.assertEqual(len(widget.widget.children), 4)
def test_topology_update_bbox_topoquantize_toposimplify(): # load example data representing continental Africa data = topojson.utils.example_data_africa() # compute the topology topo = topojson.Topology(data) # apply simplification on the topology and render as SVG bbox = topo.topoquantize(10).to_dict()["bbox"] assert round(bbox[0], 1) == -17.6
def test_toposimplify_as_chaining(self): data = geopandas.read_file( geopandas.datasets.get_path("naturalearth_lowres")) data = data[(data.name == "Antarctica")] topo = topojson.Topology(data, prequantize=True, simplify_with="shapely") topos = topo.toposimplify(2).to_dict() self.assertEqual("transform" in topos.keys(), True)
def test_topology_round_coordinates_geojson(): # load example data representing continental Africa data = topojson.utils.example_data_africa() # compute the topology topo = topojson.Topology(data) # apply simplification on the topology and render as SVG gjson = topo.topoquantize(10).to_geojson(decimals=2) coord_0 = json.loads(gjson)['features'][0]['geometry']['coordinates'][0][0] assert coord_0 == [35.85, -2.74]
def test_topology_to_geojson_polygon_point(): data = [ {"type": "Polygon", "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]}, {"type": "Point", "coordinates": [0.5, 0.5]}, ] topo = topojson.Topology(data).to_geojson() assert "]]]}}" in topo # feat 1 assert "]}}]}" in topo # feat 2