abs8090
abs8090

Reputation: 131

how to get list of all points in a multipolygon in Python?

I have a csv file that contains a column that contains multipolygon representation of NYC. Meaning, NYC is divided into multi-polygons and those multi-polygons are stored in a column called the_geom in the csv file I have.

My question is how can I get the list of all possible points in each multipolygon? What I have done so far is simply pick a random point in a multipolygon, then check if the selected point is contained in the multipolygon, the code to do so is:

multipolygon = shapely.wkt.loads(row['the_geom'])
minx, miny, maxx, maxy = multipolygon.bounds

pnt1 = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
while not multipolygon.contains(pnt1):
    pnt1 = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))

How do I get the list of all points within a multipolygon? Should I convert each multipolygon to a polygon first so I can get the list of all possible points in each multipolygon?

I have seen solutions is google that suggests using GeoPandas.explode(), but I'm not sure if it is the correct approach to convert multipolygon to polygon or not.

I'd like to know what is the correct way to get a list of all points within a multipolygon, thanks!

Upvotes: 0

Views: 797

Answers (1)

Michael Delgado
Michael Delgado

Reputation: 15452

To get all points defining the boundaries of the polygons which make up the MultiPolygon, you can iterate through the shapes using the MultiPolygon.bounds attribute, e.g.:

np.hstack([np.array(g.boundary.xy) for g in multipolygon.geoms])

Upvotes: 2

Related Questions