6

I would like to have a function plot(shapely_objects) where shapely_objects is a list of Polygons, Points and MultiPolygons. I have something extremely similar, but it fills the holes of polygons. How can I plot Shapely polygons with holes?

Code

from shapely.geometry import Point, shape


def plot(shapely_objects, figure_path='fig.png'):
    from matplotlib import pyplot as plt
    import geopandas as gpd
    boundary = gpd.GeoSeries(shapely_objects)
    boundary.plot(color=['red', 'green'])
    plt.savefig(figure_path, dpi=300, bbox_inches="tight")


p = Point(12.12, 54.085)
name = ''
multi_poly = {'coordinates': (((12.11, 54.08), (12.11, 54.09),
                               (12.13, 54.09), (12.13, 54.08)),
                              ((12.11, 54.08), (12.11, 54.09),
                               (12.13, 54.09), (12.13, 54.08))),
              'type': 'Polygon'}
multi_poly = shape(multi_poly)
plot([p, multi_poly, p])

print('multi_poly.intersects(p) == {}'.format(multi_poly.intersects(p)))
print('multi_poly.touches(p) == {}'.format(multi_poly.touches(p)))
print('multi_poly.contains(p) == {}'.format(multi_poly.contains(p)))
print('p.within(multi_poly) == {}'.format(p.within(multi_poly)))

p, multi_poly = multi_poly, p
print('multi_poly.intersects(p) == {}'.format(multi_poly.intersects(p)))
print('multi_poly.touches(p) == {}'.format(multi_poly.touches(p)))
print('multi_poly.contains(p) == {}'.format(multi_poly.contains(p)))
print('p.within(multi_poly) == {}'.format(p.within(multi_poly)))

Image

The green point is NOT within the red polygon. The hole is missing / filled.

enter image description here

Georgy
  • 12,464
  • 7
  • 65
  • 73
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • Am I correct in understanding that your multipoly coordinates are the same for both the outer and exterior? – zacdav Sep 16 '18 at 06:50
  • In this case, yes. This was mainly for me seeing that there is an issue in the visualization. But my question is more about the more realistic case that it is a "real" hole – Martin Thoma Sep 16 '18 at 08:00
  • I am unable to install `geopandas` myself to check, but that would not be a valid `shapely` polygon as far as I understand from the docs. https://shapely.readthedocs.io/en/latest/manual.html#polygons – zacdav Sep 16 '18 at 09:07
  • 3
    Yes, indeed, multy_poly is not a valid polygon. I think you should re-formulate the question with valid polygons. – eguaio Sep 16 '18 at 19:54

0 Answers0