{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from topojson import Topology\n", "import geopandas as gpd\n", "\n", "africa = gpd.read_file(gpd.datasets.get_path(\"naturalearth_lowres\")).query('continent == \"Africa\"')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparison simplification without Topology and with Topology" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.HConcatChart(...)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = Topology(\n", " data=africa, \n", " topology=False\n", ").toposimplify(4).to_alt().properties(title='WITHOUT Topology')\n", "r = Topology(\n", " data=africa, \n", " topology=True\n", ").toposimplify(4).to_alt().properties(title='WITH Topology')\n", "l | r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparison no Topoquantization and with Topoquantization" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.HConcatChart(...)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = Topology(africa).to_alt().properties(title='NO Topoquantization')\n", "r = Topology(africa).topoquantize(100).to_alt().properties(title='WITH Topoquantization')\n", "l | r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparison between Douglas-Peucker simplification and Visvalingam-Whyatt simplification" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.HConcatChart(...)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "topo = Topology(africa) \n", "\n", "l = topo.toposimplify(\n", " epsilon=4, \n", " simplify_with='shapely', \n", " simplify_algorithm='dp'\n", ").to_alt().properties(title=['Douglas-Peucker simplification'])\n", "\n", "\n", "r = topo.toposimplify(\n", " epsilon=4, \n", " simplify_with='simplification', \n", " simplify_algorithm='vw'\n", ").to_alt().properties(title='Visvalingam-Whyatt simplification')\n", "l | r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparison datasize of raw GeoJSON and optimized TopoJSON" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "africa.to_file('africa.geo.json', driver='GeoJSON')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "topo = Topology(\n", " data=africa, \n", " simplify_with='simplification', \n", " simplify_algorithm='vw', \n", " toposimplify=4, \n", " topoquantize=200\n", ")\n", "topo.to_json('africa.topo.json')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "africa.geo.json: 102.724 KB\n", "africa.topo.json: 11.916 KB\n", "factor smaller: 8.620678079892581 X\n" ] } ], "source": [ "import os\n", "size_geojson = os.stat('africa.geo.json').st_size\n", "size_topojson = os.stat('africa.topo.json').st_size\n", "\n", "print(f'africa.geo.json: {size_geojson/1000} KB')\n", "print(f'africa.topo.json: {size_topojson/1000} KB')\n", "print(f'factor smaller: {size_geojson/size_topojson} X')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.HConcatChart(...)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = Topology(\n", " data=africa, \n", " topology=False, \n", " prequantize=False\n", ").to_alt().properties(title=['Raw GeoJSON', '102.72 KB'])\n", "r = topo.to_alt().properties(\n", " title=['Optimized TopoJSON', f'{size_topojson/1000} KB ({round(size_geojson/size_topojson, 2)}X smaller)']\n", ")\n", "l | r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Showcase multi-layer Topology" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.HConcatChart(...)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "continent = africa.dissolve(by='continent', as_index=False)\n", "\n", "a = Topology(\n", " data=[africa, continent], \n", " object_name=['africa_countries', 'africa_continent']\n", ")\n", "\n", "l = a.toposimplify(1).to_alt(object_name='africa_countries').properties(title='Object `africa_countries`')\n", "r = a.toposimplify(1).to_alt(object_name='africa_continent').properties(title='Object `africa_continent`')\n", "\n", "import altair as alt\n", "alt.data_transformers.consolidate_datasets = False\n", "l | r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Showcase computing Topology using Jupyter Widgets" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6a5ea2b9b99942c1b85c007e87fd0a89", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=0.01, description='Toposimplify Factor', max=10.0, step=0.01, style=Sl…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = Topology(africa, prevent_oversimplify=False)\n", "t.to_widget()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }