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

line = loads('LineString (288691.60369923658436164 5645933.61775506939738989, 288761.13385318609653041 5645895.16393054835498333, 288824.40785175294149667 5645854.27929741330444813, 288955.55334385653259233 5645777.15149540547281504, 288974.22162246255902573 5645767.00148801226168871, 289000.79358491487801075 5645757.29348232503980398, 289023.17993451346410438 5645747.1434973394498229, 289040.20068465574877337 5645735.20978158246725798, 289082.74699403694830835 5645702.06065420992672443, 289099.46718155406415462 5645690.56898523308336735, 289120.51769726304337382 5645680.86106202937662601, 289147.24550700257532299 5645666.08609657362103462, 289163.36456926935352385 5645654.57868836540728807, 289176.35555384494364262 5645642.34517726302146912, 289198.29662548034684733 5645615.46308163367211819, 289213.82569444598630071 5645602.62979008443653584, 289249.65943853236967698 5645573.82203747611492872, 289275.48556039633695036 5645548.5817864928394556, 289283.84565415495308116 5645543.81471764761954546, 289311.76458244590321556 5645530.96574333868920803, 289357.89537943061441183 5645515.89111014362424612, 289369.68411350570386276 5645509.18251186981797218, 289378.79004785255528986 5645502.30028448719531298, 289386.11487034673336893 5645494.69195667095482349, 289393.72912351705599576 5645479.75944817345589399, 289407.91122664406429976 5645434.53588178195059299, 289414.17851397569756955 5645419.74553610105067492, 289419.55524538102326915 5645410.6377259399741888, 289428.96174235304351896 5645400.64597947057336569, 289439.25879525148775429 5645393.17980017513036728, 289453.74146100366488099 5645388.69693862646818161, 289501.20809187792474404 5645385.41371750272810459, 289517.48300143191590905 5645380.78879747167229652, 289542.11800474440678954 5645371.08120854943990707, 289559.2834702247637324 5645369.28175419382750988, 289608.55347684980370104 5645371.08120854943990707, 289629.90455518400995061 5645370.33932820055633783, 289655.28539908491075039 5645368.2399649927392602, 289718.58166154992068186 5645359.29005365539342165, 289732.61904933897312731 5645356.44881382398307323, 289741.86969902389682829 5645350.78212150279432535, 289749.03867423092015088 5645342.85822903737425804, 289757.69933061470510438 5645325.70034237392246723, 289769.73296756943454966 5645285.18134156335145235, 289774.56423346983501688 5645255.97999328467994928, 289781.57736138981999829 5645238.06461841706186533, 289787.69993338349740952 5645232.68212069012224674, 289802.33844642277108505 5645225.21608078759163618, 289831.89377122838050127 5645214.18276889342814684, 289842.93666471511824057 5645214.18276889342814684, 289905.19765591574832797 5645234.78145226370543242, 289931.4690557430149056 5645238.6644277460873127, 289961.48079046083148569 5645239.84826206788420677, 290011.34079038718482479 5645240.14816678781062365, 290036.12050903774797916 5645234.92351231444627047, 290049.71261886361753568 5645229.85670528281480074, 290066.43280638073338196 5645219.54947112873196602, 290077.33098452939884737 5645207.60067081451416016, 290085.53523100091842934 5645199.25072598922997713, 290091.81365028157597408 5645195.80973018519580364, 290100.01789675309555605 5645196.7094399705529213, 290139.88140640617348254 5645207.60067081451416016, 290154.66463478351943195 5645207.31655158288776875, 290169.29201587376883253 5645202.23397570848464966, 290178.84322818380314857 5645194.76796107366681099, 290189.89725361956516281 5645182.07732885144650936, 290201.68598769459640607 5645174.61133094597607851, 290227.06683159543899819 5645166.40347570087760687, 290238.71085033245617524 5645165.66161223314702511, 290255.87631581275491044 5645171.32818794902414083, 290321.26538470474770293 5645195.50982694420963526, 290367.1067510133725591 5645213.12521324213594198, 290379.04020042641786858 5645214.6247324850410223, 290393.07758821547031403 5645210.14195991028100252, 290401.29296663595596328 5645202.97584219370037317, 290409.07419904245762154 5645190.80608267430216074, 290416.52147297648480162 5645154.75464785099029541, 290423.5346008965279907 5645143.70563807897269726, 290432.49581990536535159 5645134.45604890491813421, 290439.36423248733626679 5645131.47282166127115488, 290454.4368915407685563 5645133.71418807003647089, 290524.7574138748459518 5645170.88622648455202579, 290545.95264492183923721 5645177.15261071920394897, 290570.86594696139218286 5645179.93065651506185532, 290653.06425896310247481 5645172.52779775112867355, 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, 291377.95451911078998819 5645523.05747711937874556, 291395.48733891069423407 5645531.34458261262625456, 291419.15386265335837379 5645532.32325081434100866, 291437.97798854648135602 5645528.12444928660988808, 291465.28465963812777773 5645522.31558423768728971, 291479.66713784862076864 5645521.08435789030045271, 291496.34279756940668449 5645525.15687645506113768, 291555.3087318426114507 5645548.5817864928394556, 291574.03267019405029714 5645552.87530761677771807, 291616.56784762616734952 5645552.78059756942093372, 291632.4976667586597614 5645550.98110686801373959, 291650.85425079049309716 5645550.98110686801373959, 291674.58756622759392485 5645554.70636871736496687, 291696.8292004881077446 5645559.3471631808206439, 291727.29734511824790388 5645570.23883305583149195, 291741.62416358332848176 5645578.74697275552898645, 291752.6670570700080134 5645588.89676774572581053, 291774.17398269131081179 5645610.5381262544542551, 291781.78823586157523096 5645615.46308163367211819, 291792.08528875990305096 5645617.2625852320343256, 291800.14481989335035905 5645617.40465131960809231, 291825.52566379425115883 5645610.69597734604030848, 291835.82271669257897884 5645610.69597734604030848, 291847.322020091523882 5645613.67936350684612989, 291889.12248888442991301 5645636.07846898771822453, 291913.71296440064907074 5645648.12254946585744619, 291921.72796773770824075 5645648.09097911603748798, 291944.12544928537681699 5645647.99626806657761335, 291966.60085447650635615 5645649.053874840028584, 291980.79408955265535042 5645653.09488097112625837, 291989.44361398729961365 5645658.01985964830964804, 291993.72941438283305615 5645662.47128500137478113, 291993.97431726258946583 5645668.59594476409256458, 291994.7312897999654524 5645703.59182549826800823, 291998.54954833420924842 5645801.93460978381335735, 292000.49763942311983556 5645811.34273987263441086, 292018.2530982046155259 5645848.13872570730745792, 292055.14437745348550379 5645894.26415090821683407, 292061.66769961401587352 5645903.54609257355332375, 292066.13161119481083006 5645914.69074946921318769, 292073.21153080923249945 5645937.81674586143344641, 292076.58451138029340655 5645953.52351451385766268, 292080.82578397949691862 5645987.46285701356828213, 292082.54010413773357868 5645997.83411419577896595, 292085.06705657875863835 5646007.81073816958814859, 292095.26392193540232256 5646030.44761271681636572, 292103.85778662469238043 5646045.06531790364533663, 292107.7428368532564491 5646050.73244757857173681, 292109.56847650231793523 5646055.97336176317185163, 292108.90055955754360184 5646060.50391353573650122, 292107.8096285478095524 5646064.00838371831923723)')

fig = plt.figure(1, (12, 16))
width = {
    'none': 1,
    'left': 3,
    'right': 3,
    'both': 5
}

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

tolerance = 50

sub1 = fig.add_subplot(311)
sub1.set_aspect('equal')
sub1.set_title('a) Tolerance = {0}'.format(tolerance), pad=10, family='sans-serif')
sub1.axes.get_xaxis().set_visible(False)
sub1.axes.get_yaxis().set_visible(False)

lines, coal = c4.coalescence_splitting(line, tolerance)
left, right = c4.dilate_line(line, tolerance, cap_style='flat')

for l in left:
    path1 = Path(numpy.asarray(l.coords)[:, :2])
    sub1.add_patch(PathPatch(path1, facecolor="none", edgecolor='gray', linewidth=1))

for r in right:
    path1 = Path(numpy.asarray(r.coords)[:, :2])
    sub1.add_patch(PathPatch(path1, facecolor="none", edgecolor='gray', linewidth=1))

for i, l in enumerate(lines):
    path3 = Path(numpy.asarray(l.coords)[:, :2])
    sub1.add_patch(PathPatch(path3, facecolor="none", edgecolor='red', linewidth=width[coal[i]]))

sub1.autoscale_view()

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

tolerance = 100

sub2 = fig.add_subplot(312)
sub2.set_aspect('equal')
sub2.set_title('b) Tolerance = {0}'.format(tolerance), pad=10, family='sans-serif')
sub2.axes.get_xaxis().set_visible(False)
sub2.axes.get_yaxis().set_visible(False)

lines, coal = c4.coalescence_splitting(line, tolerance)
left, right = c4.dilate_line(line, tolerance, cap_style='flat')

for l in left:
    path1 = Path(numpy.asarray(l.coords)[:, :2])
    sub2.add_patch(PathPatch(path1, facecolor="none", edgecolor='gray', linewidth=1))

for r in right:
    path1 = Path(numpy.asarray(r.coords)[:, :2])
    sub2.add_patch(PathPatch(path1, facecolor="none", edgecolor='gray', linewidth=1))

for i, l in enumerate(lines):
    path3 = Path(numpy.asarray(l.coords)[:, :2])
    sub2.add_patch(PathPatch(path3, facecolor="none", edgecolor='red', linewidth=width[coal[i]]))

sub2.autoscale_view()

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

tolerance = 250

sub3 = fig.add_subplot(313)
sub3.set_aspect('equal')
sub3.set_title('c) Tolerance = {0}'.format(tolerance), pad=10, family='sans-serif')
sub3.axes.get_xaxis().set_visible(False)
sub3.axes.get_yaxis().set_visible(False)

lines, coal = c4.coalescence_splitting(line, tolerance)
left, right = c4.dilate_line(line, tolerance, cap_style='flat')

for l in left:
    path1 = Path(numpy.asarray(l.coords)[:, :2])
    sub3.add_patch(PathPatch(path1, facecolor="none", edgecolor='gray', linewidth=1))

for r in right:
    path1 = Path(numpy.asarray(r.coords)[:, :2])
    sub3.add_patch(PathPatch(path1, facecolor="none", edgecolor='gray', linewidth=1))

for i, l in enumerate(lines):
    path3 = Path(numpy.asarray(l.coords)[:, :2])
    sub3.add_patch(PathPatch(path3, facecolor="none", edgecolor='red', linewidth=width[coal[i]]))

sub3.autoscale_view()

plt.show()