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

polygon = loads('Polygon ((-167638.22063858105684631 5356466.46535694133490324, -167638.22063858105684631 5356459.61416990961879492, -167645.07182561262743548 5356459.41842170897871256, -167644.68032921082340181 5356455.11196128837764263, -167654.46773925589513965 5356454.91621308773756027, -167654.27199105499312282 5356458.83117710612714291, -167660.14443708202452399 5356458.63542890548706055, -167659.94868888115161099 5356447.86927785538136959, -167644.28883280901936814 5356448.45652245823293924, -167644.09308460811735131 5356442.58407643064856529, -167637.82914217925281264 5356443.17132103350013494, -167637.43764577744877897 5356434.55840019416064024, -167651.33576804146287031 5356433.57965919002890587, -167650.94427163965883665 5356426.92422035895287991, -167659.16569607754354365 5356426.33697575610131025, -167659.16569607754354365 5356435.53714119829237461, -167668.36586151990923099 5356434.94989659637212753, -167668.17011331900721416 5356430.25193977449089289, -167678.34901976588298567 5356429.66469517163932323, -167678.74051616768701933 5356440.43084622081369162, -167686.178947801963659 5356440.0393498195335269, -167686.178947801963659 5356433.77540739066898823, -167694.40037223981926218 5356433.97115559130907059, -167694.40037223981926218 5356447.86927785538136959, -167676.00004135505878367 5356448.45652245823293924, -167676.39153775686281733 5356455.89495409280061722, -167682.26398378392332233 5356455.11196128837764263, -167682.45973198482533917 5356461.18015551660209894, -167688.527926212787861 5356461.18015551660209894, -167688.33217801188584417 5356455.30770948994904757, -167699.68557366417371668 5356455.30770948994904757, -167700.07707006597775035 5356471.5548101644963026, -167689.31091901636682451 5356471.5548101644963026, -167690.09391181997489184 5356489.36789644695818424, -167681.28524277941323817 5356489.36789644695818424, -167681.08949457851122133 5356480.9507238082587719, -167674.04255934606771916 5356481.34222020953893661, -167674.23830754696973599 5356472.14205476734787226, -167666.79987591269309632 5356472.33780296798795462, -167667.77861691720318049 5356489.75939284823834896, -167674.82555214964668266 5356489.5636446475982666, -167675.21704855145071633 5356498.76381008978933096, -167658.77419967573950998 5356498.5680618891492486, -167657.99120687213144265 5356485.64868062920868397, -167649.96553063514875248 5356485.8444288307800889, -167650.35702703695278615 5356493.08711226377636194, -167640.37386879097903147 5356492.69561586156487465, -167639.39512778646894731 5356478.60174539685249329, -167631.36945154948625714 5356478.21024899557232857, -167631.1737033485842403 5356483.69119862001389265, -167623.53952351343468763 5356483.69119862001389265, -167622.95227891072863713 5356466.26960873976349831, -167638.22063858105684631 5356466.46535694133490324))')

e1 = c4.enclosing_rectangle(polygon, mode='hull')
e2 = c4.enclosing_rectangle(polygon, mode='input')

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

sub1 = fig.add_subplot(211)
sub1.set_aspect('equal')
sub1.set_title("a) mode='hull'", pad=10, family='sans-serif')
sub1.axes.get_xaxis().set_visible(False)
sub1.axes.get_yaxis().set_visible(False)

poly1 = Path.make_compound_path(Path(numpy.asarray(polygon.exterior.coords)[:, :2]),*[Path(numpy.asarray(ring.coords)[:, :2]) for ring in polygon.interiors])
sub1.add_patch(PathPatch(poly1, facecolor="lightgrey", edgecolor='none'))
poly2 = Path.make_compound_path(Path(numpy.asarray(e1.exterior.coords)[:, :2]),*[Path(numpy.asarray(ring.coords)[:, :2]) for ring in e1.interiors])
sub1.add_patch(PathPatch(poly2, facecolor="none", edgecolor='red'))

sub2 = fig.add_subplot(212)
sub2.set_aspect('equal')
sub2.set_title("b) mode='input'", pad=10, family='sans-serif')
sub2.axes.get_xaxis().set_visible(False)
sub2.axes.get_yaxis().set_visible(False)

poly1 = Path.make_compound_path(Path(numpy.asarray(polygon.exterior.coords)[:, :2]),*[Path(numpy.asarray(ring.coords)[:, :2]) for ring in polygon.interiors])
sub2.add_patch(PathPatch(poly1, facecolor="lightgrey", edgecolor='none'))
poly2 = Path.make_compound_path(Path(numpy.asarray(e2.exterior.coords)[:, :2]),*[Path(numpy.asarray(ring.coords)[:, :2]) for ring in e2.interiors])
sub2.add_patch(PathPatch(poly2, facecolor="none", edgecolor='red'))

sub1.autoscale_view()
sub2.autoscale_view()

plt.show()