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
import cartagen as c4

line = loads('LINESTRING (290682.99807003745809197 5645165.06180777028203011, 290756.46893396094674245 5645137.67604132648557425, 290798.51430563360918313 5645122.00226553436368704, 290816.31429221149301156 5645111.3163333423435688, 290837.22009258245816454 5645092.35948929749429226, 290852.8938768861698918 5645080.71075731609016657, 290864.53789562312886119 5645076.22805146686732769, 290878.27472078707069159 5645079.06920280121266842, 290884.54200811864575371 5645085.63542265351861715, 290888.87233631056733429 5645092.80144690442830324, 290889.47346156084677204 5645102.05099325161427259, 290886.33425192045979202 5645113.25779423024505377, 290881.25808314030291513 5645128.33175277337431908, 290868.42294585180934519 5645141.76417062059044838, 290846.92715217964723706 5645158.19562794920057058, 290805.12668338674120605 5645198.35101594962179661, 290792.43626143631990999 5645214.76679221633821726, 290782.02788904710905626 5645238.42766090109944344, 290775.25966400688048452 5645271.35409672558307648, 290768.24653608695371076 5645285.08663433603942394, 290759.28531707811634988 5645295.99375672824680805, 290742.56512956094229594 5645308.96867207996547222, 290734.80616105266381055 5645320.61769961565732956, 290733.75975783920148388 5645334.8080665459856391, 290737.04368281754432246 5645343.91579993721097708, 290741.22929567145183682 5645349.44042607303708792, 290750.78050798148615286 5645355.54908806458115578, 290764.8178957705385983 5645355.40702610928565264, 290782.28392387594794855 5645351.22409180738031864, 290801.38634849607478827 5645341.81644289661198854, 290817.66125805006595328 5645331.82477286271750927, 290831.09752058883896098 5645319.8758234279230237, 290851.40219570946646854 5645297.63535082526504993, 290865.58429883659118786 5645284.64466728083789349, 290940.68042732577305287 5645237.90677386149764061, 290953.51556461420841515 5645225.37392511870712042, 290960.98510244640056044 5645213.5829611960798502, 290965.31543063826393336 5645200.73445853032171726, 290967.85351502837147564 5645171.62809038534760475, 290966.06127122661564499 5645155.79641232918947935, 290967.99823036638554186 5645146.40475207101553679, 290974.72192761034239084 5645135.65565379150211811, 290984.57370254548732191 5645128.48959540575742722, 291002.48500861419597641 5645122.20746081229299307, 291049.66220881236949936 5645115.79905667621642351, 291066.68295895465416834 5645116.24101543426513672, 291084.44954968523234129 5645122.96510495152324438, 291122.36496824939968064 5645144.60534265358000994, 291138.19459984020795673 5645150.12984639406204224, 291153.86838414391968399 5645149.98778768535703421, 291169.8427310727420263 5645144.9052441967651248, 291180.29563125828281045 5645136.39751478563994169, 291195.9582836129120551 5645115.19925553724169731, 291210.89735927729634568 5645090.40224872250109911, 291220.30385624937480316 5645074.28659856971353292, 291228.36338738282211125 5645066.07883485872298479, 291241.19852467125747353 5645052.78858718555420637, 291261.80376241711201146 5645038.91434720065444708, 291279.85978382371831685 5645030.09103620238602161, 291291.81549713498679921 5645027.86547969654202461, 291305.25175967370159924 5645028.1653773495927453, 291315.10353460890473798 5645031.44846810027956963, 291324.65474691893905401 5645037.85681215208023787, 291328.53979714761953801 5645046.82218784559518099, 291327.93867189734010026 5645059.05488916393369436, 291326.44699072075309232 5645069.66183847934007645, 291311.66376234334893525 5645099.0677767526358366, 291305.25175967370159924 5645115.94111484475433826, 291300.76558419468346983 5645136.83947455789893866, 291297.33694387832656503 5645198.49307542387396097, 291298.97334039292763919 5645226.2736378600820899, 291306.28703093808144331 5645268.070918426848948, 291311.96432496851775795 5645279.56204771902412176, 291319.43386280070990324 5645287.6279453169554472, 291347.05222846660763025 5645301.8024756433442235, 291357.04871873982483521 5645310.91017562337219715, 291361.34565108444076031 5645326.48957287147641182, 291359.73151846788823605 5645338.67530056741088629, 291355.11175960005493835 5645348.54070101492106915, 291348.38806235609808937 5645361.23156805615872145, 291336.74404361908091232 5645372.58075412083417177, 291326.00171275757020339 5645380.63095041271299124, 291304.04950917314272374 5645386.91326546482741833, 291289.27741274487925693 5645389.43882048968225718, 291281.36259694944601506 5645388.55487615056335926, 291270.46441880078054965 5645385.55577993113547564, 291265.9782433218206279 5645380.9308598255738616, 291248.22278454032493755 5645356.30675185564905405, 291241.9443652595509775 5645348.54070101492106915, 291233.8848341261036694 5645342.57410553190857172, 291223.85494800563901663 5645337.88606899976730347, 291211.19792190246516839 5645339.27511660568416119, 291202.83782814384903759 5645343.45804535411298275, 291193.43133117182878777 5645352.26587903592735529, 291187.15291189111303538 5645364.81468923110514879, 291167.60520930780330673 5645419.14571469742804766, 291154.31366210710257292 5645448.71064357459545135, 291151.77557771699503064 5645462.44345191493630409, 291155.05950269545428455 5645474.99241579603403807, 291160.13567147561116144 5645481.55892462935298681, 291168.79632785927969962 5645486.92578636854887009, 291180.88562455947976559 5645489.62500333599746227, 291190.14740619342774153 5645487.08363529108464718, 291206.41118379833642393 5645481.40107580274343491, 291216.7193686458049342 5645479.17540764901787043, 291245.15036659443285316 5645480.26456433441489935, 291252.54198078304762021 5645480.21720969397574663, 291267.4810564475483261 5645480.65918635670095682, 291300.89916758367326111 5645482.85328510776162148, 291323.76419099263148382 5645488.86732831597328186)')

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

threhsolds = [5, 10, 20, 30]

sub1 = fig.add_subplot(221)
sub1.set_aspect('equal')
sub1.set_title(f'a) Tolerance = {threhsolds[0]}', pad=10, family='sans-serif')
sub1.axes.get_xaxis().set_visible(False)
sub1.axes.get_yaxis().set_visible(False)

path1 = Path(numpy.asarray(line.coords)[:, :2])
path2 = Path(numpy.asarray(c4.simplify_reumann_witkam(line, threhsolds[0]).coords)[:, :2])
sub1.add_patch(PathPatch(path1, facecolor="none", edgecolor='gray', linewidth=1))
sub1.add_patch(PathPatch(path2, facecolor="none", edgecolor='red', linewidth=1))
sub1.autoscale_view()

sub2 = fig.add_subplot(222)
sub2.set_aspect('equal')
sub2.set_title(f'b) Tolerance = {threhsolds[1]}', pad=10, family='sans-serif')
sub2.axes.get_xaxis().set_visible(False)
sub2.axes.get_yaxis().set_visible(False)

path1 = Path(numpy.asarray(line.coords)[:, :2])
path2 = Path(numpy.asarray(c4.simplify_reumann_witkam(line, threhsolds[1]).coords)[:, :2])
sub2.add_patch(PathPatch(path1, facecolor="none", edgecolor='gray', linewidth=1))
sub2.add_patch(PathPatch(path2, facecolor="none", edgecolor='red', linewidth=1))
sub2.autoscale_view()

sub3 = fig.add_subplot(223)
sub3.set_aspect('equal')
sub3.set_title(f'c) Tolerance = {threhsolds[2]}', pad=10, family='sans-serif')
sub3.axes.get_xaxis().set_visible(False)
sub3.axes.get_yaxis().set_visible(False)

path1 = Path(numpy.asarray(line.coords)[:, :2])
path2 = Path(numpy.asarray(c4.simplify_reumann_witkam(line, threhsolds[2]).coords)[:, :2])
sub3.add_patch(PathPatch(path1, facecolor="none", edgecolor='gray', linewidth=1))
sub3.add_patch(PathPatch(path2, facecolor="none", edgecolor='red', linewidth=1))
sub3.autoscale_view()

sub4 = fig.add_subplot(224)
sub4.set_aspect('equal')
sub4.set_title(f'd) Tolerance = {threhsolds[3]}', pad=10, family='sans-serif')
sub4.axes.get_xaxis().set_visible(False)
sub4.axes.get_yaxis().set_visible(False)

path1 = Path(numpy.asarray(line.coords)[:, :2])
path2 = Path(numpy.asarray(c4.simplify_reumann_witkam(line, threhsolds[3]).coords)[:, :2])
sub4.add_patch(PathPatch(path1, facecolor="none", edgecolor='gray', linewidth=1))
sub4.add_patch(PathPatch(path2, facecolor="none", edgecolor='red', linewidth=1))
sub4.autoscale_view()

plt.tight_layout()
plt.show()