6

I would like to know how can I fill the gaps within a multipolygon. The convex hull isn't the proper solution, since they change the geometry completely. I have attached a sample figure of such a multipolygon (A) and the result of convex hull (B).

enter image description here

I am also specifying the input multipolygon that is drawn in the sample image. Please consider the below as the input multipolygon.

from shapely.wkt import loads
bound = loads('MULTIPOLYGON(((7515704.00416809 7120667.47744681,7516615.43389189 7120908.05804233,7517359.10228519 7119917.87996673,7516492.76044512 7119713.74812422,7516153.62704039 7119393.96768349,7515166.003333894 7120350.499439177,7515714.11412775 7119266.57283074,7515509.90373781 7119270.31795147,7515013.44156286 7120432.23751209,7515398.51370746 7120586.840317,7515440.21487358 7120823.97722738,7515674.32840975 7120677.40035231,7515704.00416809 7120667.47744681),(7515834.55479406 7119853.71762937,7515945.36454446 7120192.66561018,7516026.84254783 7119723.35288365,7515834.55479406 7119853.71762937),(7515375.019416 7120205.70217427,7515371.76020038 7120453.39495243,7515645.52595753 7120466.43136732,7515554.27060568 7120202.44295865,7515375.019416 7120205.70217427)))')

2 Answers 2

5

Not shorter, but using geoms method for a multi-geometry is more comprehensible.

from shapely.geometry import Polygon, MultiPolygon    
from shapely.wkt import loads

multi_polygon = loads('MULTIPOLYGON(((7515704.00416809 7120667.47744681,7516615.43389189 7120908.05804233,7517359.10228519 7119917.87996673,7516492.76044512 7119713.74812422,7516153.62704039 7119393.96768349,7515166.003333894 7120350.499439177,7515714.11412775 7119266.57283074,7515509.90373781 7119270.31795147,7515013.44156286 7120432.23751209,7515398.51370746 7120586.840317,7515440.21487358 7120823.97722738,7515674.32840975 7120677.40035231,7515704.00416809 7120667.47744681),(7515834.55479406 7119853.71762937,7515945.36454446 7120192.66561018,7516026.84254783 7119723.35288365,7515834.55479406 7119853.71762937),(7515375.019416 7120205.70217427,7515371.76020038 7120453.39495243,7515645.52595753 7120466.43136732,7515554.27060568 7120202.44295865,7515375.019416 7120205.70217427)))')

no_holes = MultiPolygon(Polygon(geom.exterior) for geom in multi_polygon.geoms)
0
5

I got the answer myself.

no_holes = MultiPolygon(Polygon(p.exterior) for p in bound)

The above code does the job for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.