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

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

polygon = loads('Polygon ((282542.59291088755708188 6246710.21255062147974968, 282552.92363554652547464 6246707.07839576061815023, 282562.63985435495851561 6246705.30956003256142139, 282572.04487509588943794 6246704.90780341904610395, 282582.3571737366146408 6246704.96759473718702793, 282594.79171645099995658 6246705.1917741671204567, 282606.9229568742448464 6246705.41417842637747526, 282620.56896062707528472 6246705.94953993521630764, 282636.33896250475663692 6246706.34508184343576431, 282655.44704931316664442 6246706.4557218961417675, 282671.06890258751809597 6246706.24194620363414288, 282684.41860752692446113 6246705.55869865883141756, 282697.77443290391238406 6246703.8107709614560008, 282713.41289356967899948 6246700.70712949428707361, 282727.53920047811698169 6246696.83422377798706293, 282736.20167970150941983 6246693.69021395687013865, 282748.36174227442825213 6246688.89331853110343218, 282761.74024172697681934 6246683.19085411261767149, 282775.42377247114200145 6246677.18593702185899019, 282790.63165083207422867 6246669.82091537863016129, 282805.38018763897707686 6246663.21371530741453171, 282818.45443736371817067 6246657.66152363736182451, 282829.7036296414444223 6246653.01137756742537022, 282844.28999324835604057 6246648.22836926393210888, 282858.72033130133058876 6246644.20493108592927456, 282873.2988201116095297 6246640.79071789048612118, 282888.48041351907886565 6246637.98834992107003927, 282903.80754998093470931 6246636.25148193910717964, 282921.25517188635421917 6246634.98309357929974794, 282939.15164624800672755 6246634.78194134496152401, 282958.10619115771260113 6246635.19523384887725115, 282973.11435056757181883 6246636.1941756559535861, 282988.57224663579836488 6246638.10826728213578463, 282983.52049992169486359 6246619.8274519881233573, 282968.0548072544625029 6246619.28219081182032824, 282952.2832057568593882 6246619.19143529888242483, 282934.99336340290028602 6246619.39610635302960873, 282919.52332044584909454 6246619.6112237237393856, 282901.92407867754809558 6246620.8787327827885747, 282884.77455356251448393 6246623.0613858662545681, 282869.74376313312677667 6246626.01670985668897629, 282852.42861840839032084 6246630.63191468548029661, 282839.65947513357969001 6246635.88170337397605181, 282827.19361740624299273 6246641.13322647660970688, 282810.61747378716245294 6246649.09878399223089218, 282794.9529500303324312 6246656.76538350339978933, 282772.90421970747411251 6246666.98076783027499914, 282759.8264331936952658 6246673.14127231668680906, 282745.07959328731521964 6246679.4442154299467802, 282731.4012827395927161 6246684.53652304224669933, 282715.29216547287069261 6246690.37525238841772079, 282705.5716212140978314 6246692.90472469106316566, 282692.66726883716182783 6246695.26367041282355785, 282680.52729081467259675 6246696.56232628040015697, 282666.42196811351459473 6246696.78489612508565187, 282650.80013148474972695 6246696.99866330903023481, 282633.8160594281507656 6246696.74821561854332685, 282614.70799393235938624 6246696.63752852845937014, 282600.6070510467980057 6246696.09952183347195387, 282588.3241723048267886 6246695.87622998002916574, 282579.53190392948454246 6246695.21686188690364361, 282571.65305177349364385 6246693.95438501704484224, 282564.83663748123217374 6246692.54596373531967402, 282558.17626183293759823 6246690.37794688995927572, 282550.91103917174041271 6246687.90221863333135843, 282543.34251836797920987 6246685.4247253155335784, 282544.37249376258114353 6246690.90626741200685501, 282544.48642162285977975 6246697.44719260092824697, 282543.99198933446314186 6246704.28879169747233391, 282542.59291088755708188 6246710.21255062147974968))')
entries = [
    loads('Point (282544.48642162285977975 6246697.44719260092824697)'),
    loads('Point (282986.0463732787175104 6246628.96785963512957096)'),
]

f, axs = plt.subplots(2, 1, figsize=(12, 12), gridspec_kw={'height_ratios': [1, 2]})

#############################################################

axs[0].set_title('densify=10.0 sigma=5.0', pad=10, family='sans-serif')
axs[0].set_aspect('equal')
axs[0].axes.get_xaxis().set_visible(False)
axs[0].axes.get_yaxis().set_visible(False)

generalized = c4.spinalize_polygon(polygon, 10.0, 5.0, entries)

poly = Path.make_compound_path(Path(numpy.asarray(polygon.exterior.coords)[:, :2]),*[Path(numpy.asarray(ring.coords)[:, :2]) for ring in polygon.interiors])
axs[0].add_patch(PathPatch(poly, facecolor="lightgray", edgecolor='none'))

for g in generalized:
    path = Path(numpy.asarray(g.coords)[:, :2])
    axs[0].add_patch(PathPatch(path, facecolor="none", edgecolor='red', linewidth=1))

for e in entries:
    coords = e.coords[0]
    axs[0].plot(coords[0], coords[1], linestyle="", marker='o', color="red")

axs[0].autoscale_view()

polygon = loads('Polygon ((282724.9445644092047587 6246450.56480667740106583, 282704.39857288071652874 6246463.22027701325714588, 282675.94321050320286304 6246479.93589389137923717, 282661.0308829503483139 6246488.66990009602159262, 282655.09644928277703002 6246492.13325823657214642, 282662.01539257436525077 6246501.90759082045406103, 282683.16633796418318525 6246489.55988507438451052, 282702.34056207479443401 6246478.11313065979629755, 282713.44319861702388152 6246472.55069546867161989, 282721.71548622305272147 6246484.31005388684570789, 282729.23046403838088736 6246495.91290224902331829, 282740.53477766981814057 6246507.84208715613931417, 282756.07100765727227554 6246522.22946271020919085, 282763.9184963388252072 6246528.81524563021957874, 282773.43231260468019173 6246535.71494169533252716, 282781.55684216966619715 6246520.70530943106859922, 282775.06089339515892789 6246516.40881332661956549, 282769.02253053092863411 6246511.65871283784508705, 282765.39986920647788793 6246508.74781729094684124, 282763.13670071098022163 6246506.75741036795079708, 282757.70669044705573469 6246501.70667825825512409, 282752.58262288302648813 6246496.2014596750959754, 282747.76273128011962399 6246490.54592775274068117, 282743.24701529974117875 6246484.74008328560739756, 282739.03635820018826053 6246478.63184136152267456, 282734.82924173708306625 6246471.91525936406105757, 282731.07882701617199928 6246464.8971685990691185, 282727.93498816550709307 6246457.88262739777565002, 282724.9445644092047587 6246450.56480667740106583))')
entries = [
    loads('Point (282658.55592092860024422 6246497.02042452804744244)'),
    loads('Point (282777.49457738717319444 6246528.21012556366622448)'),
]
structural = [
    loads('Point (282718.53675920091336593 6246463.61226381175220013)'),
]

axs[1].set_title('densify=10.0 sigma=5.0 with structural point', pad=10, family='sans-serif')
axs[1].set_aspect('equal')
axs[1].axes.get_xaxis().set_visible(False)
axs[1].axes.get_yaxis().set_visible(False)

generalized = c4.spinalize_polygon(polygon, 10.0, 5.0, entries, structural)

poly = Path.make_compound_path(Path(numpy.asarray(polygon.exterior.coords)[:, :2]),*[Path(numpy.asarray(ring.coords)[:, :2]) for ring in polygon.interiors])
axs[1].add_patch(PathPatch(poly, facecolor="lightgray", edgecolor='none'))

for g in generalized:
    path = Path(numpy.asarray(g.coords)[:, :2])
    axs[1].add_patch(PathPatch(path, facecolor="none", edgecolor='red', linewidth=1))

for e in entries:
    coords = e.coords[0]
    axs[1].plot(coords[0], coords[1], linestyle="", marker='o', color="red")

for s in structural:
    coords = s.coords[0]
    axs[1].plot(coords[0], coords[1], linestyle="", marker='o', color="black")

axs[1].autoscale_view()

plt.show()