# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import topojson as tp
import geopandas
import json
# %matplotlib inline

data = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
data.head()
# -

topo = tp.Topology(data)
#topo.toposimplify(10, prevent_oversimplify=False, inplace=True)#.to_geojson()
topo.to_widget()

topo.toposimplify(20).to_gdf().plot()

geopandas.GeoDataFrame().from_features(json.loads(topo.to_geojson())['features']).plot()





# %matplotlib inline

# +
import geopandas
from topojson.core.hashmap import Hashmap

data = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
data = data[
    (data.name == "Botswana")
    | (data.name == "South Africa")
    | (data.name == "Zimbabwe")
    | (data.name == "Zambia")
]

# data = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
# data = data[
#     (data.name == "Togo") | (data.name == "Benin") | (data.name == "Burkina Faso")
# ]
#topo = Dedup(data)
topo = topojson.Topology(data, prequantize=False).toposimplify(2)
topo.to_svg()
# -

topo

# import json
# geopandas.GeoDataFrame().from_features(json.loads(topo.to_geojson())['features']).plot()
topo.to_gdf().iloc[0].geometry

topo.to_gdf().iloc[0].geometry.wkt

topo.to_gdf().plot()

# +
import pandas
import geopandas
import topojson as tp
from shapely.wkt import loads

df = pandas.DataFrame({
    "name": ["P1", "P2", "P3"],
    "geometry": [
        "POLYGON ((60.05 88.85, 60.3 86.9, 61.9 73.4, 51.85 72.1, 50.8 80.5, 57.95 81.4, 57.5 85.05, 59.05 85.25, 58.85 87.15, 60.05 88.85))",
        "POLYGON ((66.35 90.55, 65.75 86.8, 64.5 81.1, 63.7 77, 63 73.55, 61.9 73.4, 60.3 86.9, 64.15 87.45, 64.85 90.8, 66.35 90.55))",
        "POLYGON ((65.75 86.8, 70.5 87.45, 71.45 79, 69.85 78.85, 70.3 74.5, 64.15 73.75, 63.7 77, 64.5 81.1, 66.45 81.35, 65.75 86.8))"
    ]
})

df['geometry'] = df['geometry'].apply(loads)
gdf = geopandas.GeoDataFrame(df, geometry='geometry', crs='EPSG:27700')
topo = tp.Topology(gdf, prequantize=False)
# -

topo.to_alt(color='properties.name:N')

# +
import pandas
import geopandas
import topojson as tp
from shapely.wkt import loads

df = pandas.DataFrame({
    "name": ["P1", "P2", "P3"],
    "geometry": [
        "POLYGON ((60.05 88.85, 60.3 86.9, 61.9 73.4, 51.85 72.1, 50.8 80.5, 57.95 81.4, 57.5 85.05, 59.05 85.25, 58.85 87.15, 60.05 88.85))",
        "POLYGON ((66.35 90.55, 65.75 86.8, 64.5 81.1, 63.7 77, 63 73.55, 61.9 73.4, 60.3 86.9, 64.15 87.45, 64.85 90.8, 66.35 90.55))",
        "POLYGON ((65.75 86.8, 70.5 87.45, 71.45 79, 69.85 78.85, 70.3 74.5, 64.15 73.75, 63.7 77, 64.5 81.1, 66.45 81.35, 65.75 86.8))"
    ]
})

df['geometry'] = df['geometry'].apply(loads)
gdf = geopandas.GeoDataFrame(df, geometry='geometry', crs='EPSG:27700')
topo = tp.Topology(gdf, shared_coords=True)
#topo = tp.Topology(gdf, prequantize=False)
# -

topo.to_alt(color='properties.name:N')

topo.to_svg(separate=True)

c = Cut(data, options={'prequantize':False})
c.to_svg(include_junctions=True, separate=False)

d = Dedup(data, options={'prequantize':False})

d.to_svg(include_junctions=True, separate=True)

# +
from IPython.display import SVG, display
from shapely.ops import linemerge

def svg_split_view(geom):
    svg_custom = geom._repr_svg_()
    for c in ['green','blue', 'orange', 'red']:    
        svg_custom = svg_custom.replace('stroke="#66cc99"', f'stroke="{c}"', 1)
    display(SVG(svg_custom))
    
in_geoms = loads('MULTILINESTRING ((5 0, 4 -1, 4 0), (4 0, 4 1, 3 1, 3 0), (3 0, 2 1, 2 0), (1 0, 1 1))')
svg_split_view(in_geoms) # 4 non shared arcs

# +
import topojson as tp
import geopandas as gpd
import json

# Load JSON geometry
json_string = '{"type": "FeatureCollection", "features": [{"id": "0", "type": "Feature", "properties": {"certainty": 4}, "geometry": {"type": "Polygon", "coordinates": [[[556395.0, -2289375.0], [556485.0, -2289375.0], [556485.0, -2289735.0], [556455.0, -2289735.0], [556455.0, -2289705.0], [556395.0, -2289705.0], [556395.0, -2289735.0], [556335.0, -2289735.0], [556335.0, -2289705.0], [556365.0, -2289705.0], [556365.0, -2289675.0], [556395.0, -2289675.0], [556395.0, -2289615.0], [556365.0, -2289615.0], [556365.0, -2289555.0], [556335.0, -2289555.0], [556335.0, -2289465.0], [556365.0, -2289465.0], [556365.0, -2289435.0], [556395.0, -2289435.0], [556395.0, -2289375.0]]]}}, {"id": "1", "type": "Feature", "properties": {"certainty": 4}, "geometry": {"type": "Polygon", "coordinates": [[[556065.0, -2289075.0], [556155.0, -2289075.0], [556155.0, -2289135.0], [556125.0, -2289135.0], [556125.0, -2289195.0], [556095.0, -2289195.0], [556095.0, -2289225.0], [556065.0, -2289225.0], [556065.0, -2289375.0], [556095.0, -2289375.0], [556095.0, -2289465.0], [556125.0, -2289465.0], [556125.0, -2289525.0], [556155.0, -2289525.0], [556155.0, -2289615.0], [556125.0, -2289615.0], [556125.0, -2289645.0], [556155.0, -2289645.0], [556155.0, -2289675.0], [556125.0, -2289675.0], [556125.0, -2289735.0], [556155.0, -2289735.0], [556155.0, -2289765.0], [556185.0, -2289765.0], [556185.0, -2289795.0], [556215.0, -2289795.0], [556215.0, -2289825.0], [556245.0, -2289825.0], [556245.0, -2289855.0], [556305.0, -2289855.0], [556305.0, -2289825.0], [556335.0, -2289825.0], [556335.0, -2289795.0], [556365.0, -2289795.0], [556365.0, -2289825.0], [556395.0, -2289825.0], [556395.0, -2289855.0], [556455.0, -2289855.0], [556455.0, -2289825.0], [556485.0, -2289825.0], [556485.0, -2289885.0], [556455.0, -2289885.0], [556455.0, -2289915.0], [556425.0, -2289915.0], [556395.0, -2289915.0], [556395.0, -2289945.0], [556365.0, -2289945.0], [556305.0, -2289945.0], [556305.0, -2289975.0], [556275.0, -2289975.0], [556245.0, -2289975.0], [556245.0, -2290005.0], [556215.0, -2290005.0], [556155.0, -2290005.0], [556155.0, -2290035.0], [556125.0, -2290035.0], [556095.0, -2290035.0], [556095.0, -2290065.0], [556065.0, -2290065.0], [556035.0, -2290065.0], [556035.0, -2290095.0], [556005.0, -2290095.0], [556000.0, -2290095.0], [556000.0, -2289135.0], [556005.0, -2289135.0], [556035.0, -2289135.0], [556035.0, -2289105.0], [556065.0, -2289105.0], [556065.0, -2289075.0]]]}}, {"id": "2", "type": "Feature", "properties": {"certainty": 0}, "geometry": {"type": "MultiPolygon", "coordinates": [[[[556000.0, -2290095.0], [556005.0, -2290095.0], [556035.0, -2290095.0], [556035.0, -2290065.0], [556065.0, -2290065.0], [556095.0, -2290065.0], [556095.0, -2290035.0], [556125.0, -2290035.0], [556155.0, -2290035.0], [556155.0, -2290005.0], [556215.0, -2290005.0], [556245.0, -2290005.0], [556245.0, -2289975.0], [556275.0, -2289975.0], [556305.0, -2289975.0], [556305.0, -2289945.0], [556365.0, -2289945.0], [556395.0, -2289945.0], [556395.0, -2289915.0], [556425.0, -2289915.0], [556455.0, -2289915.0], [556455.0, -2289885.0], [556485.0, -2289885.0], [556485.0, -2290250.0], [556000.0, -2290250.0], [556000.0, -2290095.0]]], [[[556485.0, -2289000.0], [556485.0, -2289375.0], [556455.0, -2289375.0], [556395.0, -2289375.0], [556395.0, -2289435.0], [556365.0, -2289435.0], [556365.0, -2289465.0], [556335.0, -2289465.0], [556335.0, -2289555.0], [556365.0, -2289555.0], [556365.0, -2289615.0], [556395.0, -2289615.0], [556395.0, -2289675.0], [556365.0, -2289675.0], [556365.0, -2289705.0], [556335.0, -2289705.0], [556335.0, -2289735.0], [556395.0, -2289735.0], [556395.0, -2289705.0], [556455.0, -2289705.0], [556455.0, -2289735.0], [556485.0, -2289735.0], [556485.0, -2289825.0], [556455.0, -2289825.0], [556455.0, -2289855.0], [556395.0, -2289855.0], [556395.0, -2289825.0], [556365.0, -2289825.0], [556365.0, -2289795.0], [556335.0, -2289795.0], [556335.0, -2289825.0], [556305.0, -2289825.0], [556305.0, -2289855.0], [556245.0, -2289855.0], [556245.0, -2289825.0], [556215.0, -2289825.0], [556215.0, -2289795.0], [556185.0, -2289795.0], [556185.0, -2289765.0], [556155.0, -2289765.0], [556155.0, -2289735.0], [556125.0, -2289735.0], [556125.0, -2289675.0], [556155.0, -2289675.0], [556155.0, -2289645.0], [556125.0, -2289645.0], [556125.0, -2289615.0], [556155.0, -2289615.0], [556155.0, -2289525.0], [556125.0, -2289525.0], [556125.0, -2289465.0], [556095.0, -2289465.0], [556095.0, -2289375.0], [556065.0, -2289375.0], [556065.0, -2289225.0], [556095.0, -2289225.0], [556095.0, -2289195.0], [556125.0, -2289195.0], [556125.0, -2289135.0], [556155.0, -2289135.0], [556155.0, -2289075.0], [556065.0, -2289075.0], [556065.0, -2289105.0], [556035.0, -2289105.0], [556035.0, -2289135.0], [556005.0, -2289135.0], [556000.0, -2289135.0], [556000.0, -2289000.0], [556485.0, -2289000.0]]]]}}]}'
json_data = json.loads(json_string)

# Convert to GeoDataFrame
gdf = gpd.GeoDataFrame.from_features(json_data["features"])

# Construct topology and simplify (with shared_coords=False)
topo = tp.Topology(gdf, shared_coords=False, prequantize=False)
simplified_gdf = topo.toposimplify(30).to_gdf() 
# -

simplified_gdf

topo

topo.toposimplify(30).to_alt(color='properties.certainty:Q')

out_geoms = linemerge(in_geoms)
svg_split_view(out_geoms) # 3 out of 4 can be merged

# +
import pandas
import geopandas
import topojson as tp
from shapely.wkt import loads

df = pandas.DataFrame({
    "name": ["P1", "P2", "P3"],
    "geometry": [
        "POLYGON ((60.05 88.85, 60.3 86.9, 61.9 73.4, 51.85 72.1, 50.8 80.5, 57.95 81.4, 57.5 85.05, 59.05 85.25, 58.85 87.15, 60.05 88.85))",
        "POLYGON ((66.35 90.55, 65.75 86.8, 64.5 81.1, 63.7 77, 63 73.55, 61.9 73.4, 60.3 86.9, 64.15 87.45, 64.85 90.8, 66.35 90.55))",
        "POLYGON ((65.75 86.8, 70.5 87.45, 71.45 79, 69.85 78.85, 70.3 74.5, 64.15 73.75, 63.7 77, 64.5 81.1, 66.45 81.35, 65.75 86.8))"
    ]
})

df['geometry'] = df['geometry'].apply(loads)
gdf = geopandas.GeoDataFrame(df, geometry='geometry', crs='EPSG:27700')
topo = tp.Topology(gdf, prequantize=False)
# -

topo.to_gdf()

topo.to_alt(color='properties.name:N')

# +
The issue is in the process when the remaining non-shared arcs are tried to be merged again. 
In this process are currently two options covered 
(1) the first and last non-shared arc can be merged and 
(2) all arcs can be merged. Until know this were the two situations 

It seems the assumption that this logic works for all cases has been proven wrong.
Somehow need to find a better method to detect which segment-indices where merged
# -

The shared segments of a linestring are collected from all geometries. 
The remaining non-shared segments of each linestring are then tried to be merged. These merged contigious 
segment replaces the



# +
import pandas
import geopandas
import topojson as tp
from shapely.wkt import loads

df = pandas.DataFrame({
    "name": ["P1", "P2", "P3"],
    "geometry": [
        "POLYGON ((60.05 88.85, 60.3 86.9, 61.9 73.4, 51.85 72.1, 50.8 80.5, 57.95 81.4, 57.5 85.05, 59.05 85.25, 58.85 87.15, 60.05 88.85))",
        "POLYGON ((66.35 90.55, 65.75 86.8, 64.5 81.1, 63.7 77, 63 73.55, 61.9 73.4, 60.3 86.9, 64.15 87.45, 64.85 90.8, 66.35 90.55))",
        "POLYGON ((65.75 86.8, 70.5 87.45, 71.45 79, 69.85 78.85, 70.3 74.5, 64.15 73.75, 63.7 77, 64.5 81.1, 66.45 81.35, 65.75 86.8))"
    ]
})

df['geometry'] = df['geometry'].apply(loads)
gdf = geopandas.GeoDataFrame(df, geometry='geometry', crs='EPSG:27700')
topo = Dedup(gdf, options={'prequantize':False})
# -

topo



# +
import topojson as tp
import geopandas as gpd

# Read file
gdf = gpd.read_file(r"C:\Users\lilei\Downloads\polygons\polygons.json")

# Construct topology and simplify
topo = tp.Topology(gdf, prequantize=False)
simplified_gdf = topo.toposimplify(30).to_gdf()
# -



StarcoderdataJupyterScriptsDedupFiltered