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

buildings = [
    loads('POLYGON ((-16445769.90343573503196239 9565194.61930340714752674, -16445792.84602352418005466 9565217.9208535123616457, -16445776.92295048944652081 9565233.54289877600967884, -16445800.75452758930623531 9565257.8121693879365921, -16445810.88958853296935558 9565247.98951656185090542, -16445815.1017750333994627 9565252.34511262737214565, -16445816.81570799462497234 9565255.65360098518431187, -16445803.91874257661402225 9565268.27381659671664238, -16445822.15288663841784 9565286.94194398634135723, -16445841.34366313554346561 9565277.72974181361496449, -16445847.46795775182545185 9565272.00128521397709846, -16445854.27475111372768879 9565278.98447582684457302, -16445860.17558612860739231 9565278.06259039789438248, -16445875.74638394080102444 9565262.71289533749222755, -16445863.91059311293065548 9565250.68171883933246136, -16445858.70656732283532619 9565253.05072155222296715, -16445855.22309414483606815 9565249.4555655550211668, -16445854.39750245772302151 9565243.47427018918097019, -16445853.10021414794027805 9565242.16049247048795223, -16445852.42708230577409267 9565238.37819834798574448, -16445918.42495660670101643 9565173.77619693800806999, -16445913.48404448293149471 9565168.66014488600194454, -16445906.59748032502830029 9565163.39363366365432739, -16445878.55377110280096531 9565134.76883729174733162, -16445866.46533393673598766 9565146.43287067674100399, -16445861.11606256477534771 9565140.9706835076212883, -16445855.78433665446937084 9565146.08628677576780319, -16445811.88466941006481647 9565153.55259451828896999, -16445769.90343573503196239 9565194.61930340714752674))'),
    loads('POLYGON ((-16445673.03684977442026138 9565176.6483513880521059, -16445665.90061257034540176 9565180.34316345863044262, -16445653.5893903411924839 9565192.62301020510494709, -16445640.3486892506480217 9565199.61323594488203526, -16445628.45425108447670937 9565211.50580072216689587, -16445616.37973554059863091 9565199.2317900825291872, -16445624.93457353301346302 9565190.72240895964205265, -16445584.17282082512974739 9565149.86659961007535458, -16445565.70597401633858681 9565168.28639988414943218, -16445555.74040162935853004 9565158.38791372813284397, -16445550.52516151033341885 9565163.51411633379757404, -16445555.37781883217394352 9565172.84971715696156025, -16445542.33458356186747551 9565185.9497199971228838, -16445545.40880368277430534 9565191.62371978349983692, -16445536.96070533618330956 9565199.89408565871417522, -16445571.18202736973762512 9565234.10162506811320782, -16445574.18088417313992977 9565232.5734377671033144, -16445586.96177498623728752 9565245.44969083182513714, -16445585.96509834006428719 9565249.59247013740241528, -16445595.12261356040835381 9565258.84340568818151951, -16445597.70395467802882195 9565263.43709457106888294, -16445604.53970895893871784 9565265.0560989286750555, -16445621.08936811238527298 9565256.38929185084998608, -16445649.86563412100076675 9565262.93190191499888897, -16445655.55329562537372112 9565260.02381126396358013, -16445661.29212603718042374 9565254.27116234041750431, -16445659.8135148286819458 9565251.03017182089388371, -16445669.72556527704000473 9565241.11965942569077015, -16445680.13421580567955971 9565243.47403953783214092, -16445710.07870760932564735 9565213.54897686652839184, -16445673.03684977442026138 9565176.6483513880521059))'),
    loads('POLYGON ((-16445483.29433412849903107 9565178.98152867332100868, -16445477.90432207100093365 9565172.52430536411702633, -16445479.14066466316580772 9565167.62205064855515957, -16445465.98266351781785488 9565151.04736647196114063, -16445459.81279468163847923 9565147.39936141110956669, -16445457.20671865344047546 9565149.38399451225996017, -16445436.7125804927200079 9565144.72876629047095776, -16445427.53108084015548229 9565152.07889371551573277, -16445411.4249127060174942 9565148.25535679422318935, -16445401.92636869288980961 9565156.03714415989816189, -16445399.78957432322204113 9565159.95805237255990505, -16445385.70025909319519997 9565171.41495081596076488, -16445384.95206265710294247 9565172.8014993853867054, -16445379.73355873115360737 9565177.05534337274730206, -16445373.60899900086224079 9565208.20278927311301231, -16445372.0048083458095789 9565211.21463017351925373, -16445369.27848801016807556 9565223.20393527485430241, -16445366.564603291451931 9565225.4273363184183836, -16445387.00854126736521721 9565250.75250991247594357, -16445390.27416198328137398 9565251.3648095391690731, -16445393.71724938787519932 9565248.60845630988478661, -16445400.34361305460333824 9565250.16340492479503155, -16445445.73773754015564919 9565213.46771560423076153, -16445457.35448097996413708 9565216.41374660655856133, -16445467.58224273659288883 9565208.09897370077669621, -16445483.29433412849903107 9565178.98152867332100868))'),
    loads('POLYGON ((-16445298.27468828484416008 9565232.23113539628684521, -16445356.01376059465110302 9565148.00431583635509014, -16445357.0734139047563076 9565139.52773570455610752, -16445337.78744398057460785 9565126.21552732400596142, -16445344.83712434768676758 9565115.98329763673245907, -16445304.18607483617961407 9565088.16255086474120617, -16445295.59602989442646503 9565100.59334020502865314, -16445276.43620028160512447 9565087.53594640456140041, -16445255.21472355723381042 9565118.317164221778512, -16445269.76526298001408577 9565128.30179925449192524, -16445269.03469766117632389 9565133.981042155995965, -16445262.70967242494225502 9565143.36824057810008526, -16445262.21708244271576405 9565146.75859333388507366, -16445264.15107865259051323 9565148.12377315945923328, -16445262.56362100876867771 9565159.22728428803384304, -16445258.59468923695385456 9565165.06241135857999325, -16445255.17615172453224659 9565164.62385800294578075, -16445234.94995610043406487 9565150.7986628282815218, -16445199.97284563630819321 9565202.04580828733742237, -16445223.86822386831045151 9565218.43087673373520374, -16445229.44999330304563046 9565210.31269338168203831, -16445258.55088653974235058 9565230.2818042729049921, -16445267.35833560861647129 9565217.59751206450164318, -16445286.44550728425383568 9565230.73924398235976696, -16445298.27468828484416008 9565232.23113539628684521))'),
    loads('POLYGON ((-16445900.94107409752905369 9565069.69617463834583759, -16445900.52545591071248055 9564993.75357603281736374, -16445867.69554758258163929 9564993.77934299595654011, -16445867.60971730202436447 9564982.23543832451105118, -16445865.33002951554954052 9564981.19866980984807014, -16445851.71750888600945473 9564981.42505143582820892, -16445849.00876138918101788 9564987.53070319816470146, -16445845.85206511057913303 9564987.37340268678963184, -16445838.79554681479930878 9564984.21533236838877201, -16445835.90887295268476009 9564984.38536521233618259, -16445828.14742149785161018 9564980.80443611554801464, -16445806.80445409007370472 9564980.94102218002080917, -16445804.75885204412043095 9564985.58418905548751354, -16445797.46331639215350151 9564985.58991718292236328, -16445789.0494993906468153 9564981.86569732986390591, -16445775.00206642411649227 9564981.99656078778207302, -16445769.25478632934391499 9564994.67151856794953346, -16445717.31261405907571316 9564995.05585910566151142, -16445706.97584365122020245 9565018.24796009995043278, -16445714.84601929411292076 9565021.85278647765517235, -16445714.9088993277400732 9565044.91695499606430531, -16445702.38724144361913204 9565044.84692544303834438, -16445702.48022894933819771 9565055.32044675573706627, -16445716.97200529836118221 9565061.98784920386970043, -16445717.03982073254883289 9565076.20826661214232445, -16445790.48629660904407501 9565075.99060318991541862, -16445806.39153264090418816 9565082.96846438199281693, -16445812.19500652141869068 9565070.03766644187271595, -16445900.94107409752905369 9565069.69617463834583759))'),
    loads('POLYGON ((-16445488.46124071069061756 9565094.3113662376999855, -16445577.87631641142070293 9565095.41829439997673035, -16445585.13654625415802002 9565098.65749361366033554, -16445590.97733942791819572 9565111.32028840109705925, -16445588.28873666748404503 9565117.4034637026488781, -16445592.1689129825681448 9565119.11856903880834579, -16445592.17384812980890274 9565122.96687756851315498, -16445594.83390822634100914 9565124.15769388154149055, -16445636.6060217022895813 9565124.66807053610682487, -16445638.53429365903139114 9564980.46431903913617134, -16445589.08891759440302849 9564979.76224717870354652, -16445586.88411200419068336 9564985.09773928672075272, -16445586.81148847937583923 9564993.40393006056547165, -16445584.18336765840649605 9564999.39364967495203018, -16445577.62026295810937881 9565002.30689920112490654, -16445573.67387440428137779 9565002.1400337778031826, -16445561.472601268440485 9564996.89707169681787491, -16445558.63182720355689526 9564990.68348870053887367, -16445556.63050219602882862 9564995.22251051291823387, -16445556.44834152236580849 9565000.80602226778864861, -16445551.41912153363227844 9565000.86664807423949242, -16445542.20656754449009895 9565004.98257021233439445, -16445537.91938377171754837 9565004.86037549376487732, -16445530.59859562292695045 9565001.71458254754543304, -16445522.52932999655604362 9565005.5095415897667408, -16445515.13666358776390553 9565005.36681438982486725, -16445512.24459870532155037 9565012.24647192656993866, -16445504.57161756977438927 9565012.05495882779359818, -16445496.56281632743775845 9565015.7564508318901062, -16445489.52231262251734734 9565012.65942418947815895, -16445488.46124071069061756 9565094.3113662376999855))'),
    loads('POLYGON ((-16445229.21599313803017139 9565042.37352433428168297, -16445245.13238558359444141 9565041.78410887718200684, -16445245.34804894775152206 9565049.64511938206851482, -16445346.4170769639313221 9565045.98110156133770943, -16445347.12281009741127491 9565065.62201193906366825, -16445401.77523212507367134 9565063.63293547369539738, -16445400.68919301219284534 9565033.77969861961901188, -16445422.50940128788352013 9565033.03942657820880413, -16445422.00232863798737526 9565019.42301953583955765, -16445440.13967283628880978 9565018.67808906547725201, -16445439.76065624505281448 9565006.89049328677356243, -16445424.84369221888482571 9565007.40992887504398823, -16445419.72364374622702599 9564999.62982792221009731, -16445389.44552543759346008 9565000.69980327412486076, -16445389.0639372244477272 9564992.06284805573523045, -16445384.82840951159596443 9564988.94646615348756313, -16445380.22572669014334679 9564982.18030309118330479, -16445376.82241263426840305 9564979.79325822181999683, -16445351.31934093497693539 9564980.52888117171823978, -16445338.68363113701343536 9564997.69083508104085922, -16445316.91416865587234497 9564981.62530761212110519, -16445295.20500842295587063 9564982.35781046003103256, -16445284.98348115757107735 9564989.37437553331255913, -16445282.53785983100533485 9564992.69604298286139965, -16445282.69992635399103165 9564998.19793575443327427, -16445275.12653571926057339 9565003.19133865088224411, -16445275.32647659443318844 9565007.64044425077736378, -16445265.82740355655550957 9565013.81883550994098186, -16445243.89614792726933956 9565014.56688662245869637, -16445243.87914133816957474 9565012.73032645136117935, -16445228.16655077785253525 9565013.04293776303529739, -16445229.21599313803017139 9565042.37352433428168297))'),
]

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

sub1 = fig.add_subplot(211)
sub1.set_aspect('equal')
sub1.axes.get_xaxis().set_visible(False)
sub1.axes.get_yaxis().set_visible(False)

sub2 = fig.add_subplot(212)
sub2.set_aspect('equal')
sub2.axes.get_xaxis().set_visible(False)
sub2.axes.get_yaxis().set_visible(False)

for building in buildings:
    poly = Path.make_compound_path(Path(numpy.asarray(building.exterior.coords)[:, :2]),*[Path(numpy.asarray(ring.coords)[:, :2]) for ring in building.interiors])
    sub1.add_patch(PathPatch(poly, facecolor="lightgray", edgecolor='black'))

    generalized = c4.regularize_building_regression(building, 8)
    poly = Path.make_compound_path(Path(numpy.asarray(generalized.exterior.coords)[:, :2]),*[Path(numpy.asarray(ring.coords)[:, :2]) for ring in generalized.interiors])
    sub2.add_patch(PathPatch(poly, facecolor="lightgray", edgecolor='red', linewidth=1.5))

sub1.autoscale_view()
sub2.autoscale_view()

plt.show()