from topojson import Topology
import geopandas as gpd
africa = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).query('continent == "Africa"')
l = Topology(
data=africa,
topology=False
).toposimplify(4).to_alt().properties(title='WITHOUT Topology')
r = Topology(
data=africa,
topology=True
).toposimplify(4).to_alt().properties(title='WITH Topology')
l | r
l = Topology(africa).to_alt().properties(title='NO Topoquantization')
r = Topology(africa).topoquantize(100).to_alt().properties(title='WITH Topoquantization')
l | r
topo = Topology(africa)
l = topo.toposimplify(
epsilon=4,
simplify_with='shapely',
simplify_algorithm='dp'
).to_alt().properties(title=['Douglas-Peucker simplification'])
r = topo.toposimplify(
epsilon=4,
simplify_with='simplification',
simplify_algorithm='vw'
).to_alt().properties(title='Visvalingam-Whyatt simplification')
l | r
africa.to_file('africa.geo.json', driver='GeoJSON')
topo = Topology(
data=africa,
simplify_with='simplification',
simplify_algorithm='vw',
toposimplify=4,
topoquantize=200
)
topo.to_json('africa.topo.json')
import os
size_geojson = os.stat('africa.geo.json').st_size
size_topojson = os.stat('africa.topo.json').st_size
print(f'africa.geo.json: {size_geojson/1000} KB')
print(f'africa.topo.json: {size_topojson/1000} KB')
print(f'factor smaller: {size_geojson/size_topojson} X')
africa.geo.json: 102.724 KB africa.topo.json: 11.916 KB factor smaller: 8.620678079892581 X
l = Topology(
data=africa,
topology=False,
prequantize=False
).to_alt().properties(title=['Raw GeoJSON', '102.72 KB'])
r = topo.to_alt().properties(
title=['Optimized TopoJSON', f'{size_topojson/1000} KB ({round(size_geojson/size_topojson, 2)}X smaller)']
)
l | r
continent = africa.dissolve(by='continent', as_index=False)
a = Topology(
data=[africa, continent],
object_name=['africa_countries', 'africa_continent']
)
l = a.toposimplify(1).to_alt(object_name='africa_countries').properties(title='Object `africa_countries`')
r = a.toposimplify(1).to_alt(object_name='africa_continent').properties(title='Object `africa_continent`')
import altair as alt
alt.data_transformers.consolidate_datasets = False
l | r
t = Topology(africa, prevent_oversimplify=False)
t.to_widget()
interactive(children=(FloatSlider(value=0.01, description='Toposimplify Factor', max=10.0, step=0.01, style=Sl…
<function topojson.utils.toposimpquant(epsilon, quant, algo, topo)>