from matplotlib import pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch

import numpy
import geopandas as gpd
from shapely.wkt import loads
from shapely import Point, Polygon
import cartagen as c4

lines = [
    loads('LINESTRING (-172813.28032817878 5375780.693659013, -172779.02293264482 5375829.224969353, -172727.63683934385 5375886.320628576, -172653.4124823536 5375914.868458188, -172536.3663809459 5375926.287590032, -172410.75593065468 5375920.57802411, -172305.12896109163 5375920.57802411, -172173.80894487808 5375940.561504838)'), 
    loads('LINESTRING (-172173.80894487808 5375940.561504838, -172085.310673082 5375951.980636683, -172002.52196720825 5375983.383249256, -171879.76629987822 5376011.931078868, -171788.413245121 5376046.188474402, -171657.09322890744 5376063.317172169, -171531.48277861622 5376089.010218819, -171408.7271112862 5376091.86500178)'), 
    loads('LINESTRING (-171408.7271112862 5376091.86500178, -171268.84274618916 5376077.591086974, -171128.95838109215 5376071.881521052, -171006.2027137621 5376077.591086974, -170889.1566123544 5376077.591086974, -170757.83659614084 5376066.17195513)'), 
    loads('LINESTRING (-171408.7271112862 5376091.86500178, -171397.30797944154 5375991.94759814, -171377.3244987134 5375900.5945433825, -171348.77666910176 5375814.951054547, -171328.79318837362 5375746.436263479, -171320.22883949013 5375680.776255373, -171311.66449060664 5375600.84233246)'), 
    loads('LINESTRING (-171311.66449060664 5375600.84233246, -171228.87578473287 5375540.891890275, -171166.07055958727 5375512.344060664, -171071.86272186888 5375483.796231052, -170943.3974886165 5375463.812750324, -170894.86617827672 5375458.103184402, -170837.77051905345 5375458.103184402)'), 
    loads('LINESTRING (-171311.66449060664 5375600.84233246, -171377.3244987134 5375543.746673237, -171471.5323364318 5375498.070145858, -171554.32104230556 5375478.08666513)'), 
    loads('LINESTRING (-171554.32104230556 5375478.08666513, -171685.64105851908 5375466.667533285, -171799.83237696564 5375460.957967363, -171899.74978060636 5375458.103184402)'), 
    loads('LINESTRING (-171899.74978060636 5375458.103184402, -171905.4593465287 5375572.294502849, -171916.87847837334 5375729.307565712, -171931.15239317916 5375809.241488625, -171953.99065686847 5375903.449326343, -172019.65066497523 5376097.574567703, -172071.0367582762 5376220.330235032, -172108.14893677132 5376325.957204595, -172162.38981303343 5376414.455476392, -172219.4854722567 5376505.808531149)'), 
    loads('LINESTRING (-171899.74978060636 5375458.103184402, -172028.21501385875 5375452.393618479, -172148.1158982276 5375469.522316246, -172236.61417002368 5375486.651014013, -172379.35331808188 5375498.070145858, -172556.34986167404 5375440.974486635)'), 
    loads('LINESTRING (-171899.74978060636 5375458.103184402, -171902.60456356753 5375326.783168188, -171888.3306487617 5375229.720547508, -171839.79933842193 5375101.255314256)'), 
    loads('LINESTRING (-171788.413245121 5376046.188474402, -171682.7862755579 5376160.379792848, -171639.96453114046 5376271.716328333, -171622.83583337348 5376405.891127508, -171628.5453992958 5376545.775492605, -171679.93149259675 5376659.966811052)'), 
    loads('LINESTRING (-172173.80894487808 5375940.561504838, -172327.96722478094 5376034.769342557)'), 
    loads('LINESTRING (-172327.96722478094 5376034.769342557, -172422.17506249933 5376077.591086974, -172602.02638905265 5376134.686746198, -172727.63683934385 5376240.313715761, -172776.16814968365 5376383.052863819)'), 
    loads('LINESTRING (-172776.16814968365 5376383.052863819, -172650.55769939243 5376420.165042314, -172504.9637683731 5376431.584174159, -172219.4854722567 5376505.808531149)'), 
    loads('LINESTRING (-172219.4854722567 5376505.808531149, -172268.0167825965 5376600.016368867, -172276.58113148 5376694.224206585)'), 
]

network = gpd.GeoDataFrame(geometry=lines, crs=3857)
dissolved = c4.dissolve_network(network)

fig = plt.figure(1, (12, 12))

sub1 = fig.add_subplot(211)
sub1.set_aspect('equal')
sub1.set_title('Edges and nodes before dissolve')
sub1.axes.get_xaxis().set_visible(False)
sub1.axes.get_yaxis().set_visible(False)

sub2 = fig.add_subplot(212)
sub2.set_aspect('equal')
sub2.set_title('Edges and nodes after dissolve')
sub2.axes.get_xaxis().set_visible(False)
sub2.axes.get_yaxis().set_visible(False)

points = set()
for line in lines:
    if not line.is_empty:
        coords = line.coords
        points.add(coords[0])
        points.add(coords[-1])
list(points)

for line in lines:
    path = Path(numpy.asarray(line.coords)[:, :2])
    sub1.add_patch(PathPatch(path, facecolor="none", edgecolor='black', linewidth=1))

for p in points:
    sub1.plot(p[0], p[1], linestyle="", marker='o', color="black")

points = set()
for line in dissolved.geometry:
    if not line.is_empty:
        coords = line.coords
        points.add(coords[0])
        points.add(coords[-1])
list(points)

for line in dissolved.geometry:
    path = Path(numpy.asarray(line.coords)[:, :2])
    sub2.add_patch(PathPatch(path, facecolor="none", edgecolor='red', linewidth=1))

for p in points:
    sub2.plot(p[0], p[1], linestyle="", marker='o', color="red")

sub1.autoscale_view()
sub2.autoscale_view()
plt.show()