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 ((483096.7768934701 6044753.445705255, 483092.1604210931 6044760.958789463, 483097.4000118249 6044763.99210823, 483092.7885635301 6044771.802497061, 483087.39826133876 6044768.623047379, 483085.8108729142 6044771.177800914, 483083.7160418289 6044770.023928969, 483077.4265233104 6044766.26501583, 483077.14270682633 6044767.0132977795, 483074.7466072818 6044774.339906729, 483072.4936695409 6044781.366703478, 483070.00719355507 6044792.114748905, 483072.68977718946 6044792.961243324, 483088.0493464834 6044798.350101392, 483090.13664136606 6044799.058031549, 483092.37464560085 6044799.912087187, 483094.6527179778 6044794.37175113, 483104.5039601318 6044798.367422216, 483105.9959637177 6044798.9367902065, 483109.57759867696 6044791.589972285, 483107.62592436513 6044790.136284896, 483111.96109964827 6044783.520100163, 483113.2621539414 6044781.564974856, 483111.90828753717 6044780.398495349, 483118.1121330976 6044770.330624685, 483115.11555967305 6044768.448658151, 483115.8313801609 6044766.949568982, 483112.5233313864 6044764.1807621755, 483112.66398065264 6044763.73229697, 483111.4608257633 6044762.711944694, 483096.7768934701 6044753.445705255))'), 
    loads('POLYGON ((483070.82804693823 6044757.92445423, 483082.6269410743 6044762.884440221, 483082.43222912657 6044763.465573104, 483086.0086969999 6044765.2398316385, 483086.53413558385 6044764.237459034, 483084.97927264444 6044763.219657144, 483087.94893043453 6044757.638352046, 483073.17633677385 6044750.802663586, 483070.82804693823 6044757.92445423))'), 
    loads('POLYGON ((483041.7231944218 6044781.473893065, 483056.03030924714 6044785.988604097, 483058.98163391225 6044785.194935805, 483066.00687531946 6044771.098456483, 483042.53425639204 6044759.305162706, 483041.01973445795 6044766.17069953, 483046.39745859435 6044768.606956287, 483043.43618090375 6044777.578786402, 483041.7231944218 6044781.473893065))'), 
    loads('POLYGON ((483082.4186263676 6044707.070410818, 483078.9951413884 6044713.187774124, 483072.581358362 6044709.6390469745, 483068.9734174609 6044716.678130096, 483068.3484977614 6044716.456041014, 483057.4689861606 6044738.329249107, 483071.9876860194 6044745.816187757, 483083.4738079463 6044724.017490575, 483093.52662249096 6044729.212068421, 483098.4042480907 6044720.367569911, 483088.8841366768 6044715.855952864, 483090.9099060488 6044710.511207601, 483082.80502187135 6044706.573055614, 483082.4186263676 6044707.070410818))'), 
    loads('POLYGON ((483060.7887495585 6044760.526025491, 483065.0393035599 6044751.736744442, 483065.1327314951 6044751.548738353, 483054.29410083324 6044746.708197582, 483043.5489128429 6044741.679644566, 483039.67206256755 6044749.716878642, 483039.4852058791 6044750.092889912, 483045.25676829333 6044752.865657485, 483060.7887495585 6044760.526025491))'), 
    loads('POLYGON ((483082.6237937154 6044744.199401268, 483091.5233870975 6044748.425199497, 483096.5375086381 6044739.033077588, 483087.5473005951 6044734.370590965, 483082.6237937154 6044744.199401268))'), 
    loads('POLYGON ((483127.7442079502 6044736.364400351, 483114.73178073147 6044726.713422463, 483105.54601464357 6044745.896783458, 483126.6894739062 6044760.172891552, 483131.85256515 6044752.511167733, 483120.79268554156 6044744.870537457, 483124.5139267647 6044739.651446626, 483130.0753720944 6044743.617047335, 483128.97353314667 6044745.52951407, 483138.9880076486 6044753.2448412115, 483142.854087839 6044747.994308646, 483127.7442079502 6044736.364400351))'), 
]

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

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

sub1 = fig.add_subplot(111)
sub1.set_aspect('equal')
sub1.set_title('edge_threshold=5.0', pad=10, family='sans-serif')
sub1.axes.get_xaxis().set_visible(False)
sub1.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='none'))

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

sub1.autoscale_view()
plt.show()