0

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!

abs8090
  • 111
  • 2
  • 9
  • Do you mean the list of all points defining the boundary lines (edges) of the multipolygon? There are of course infinite points inside a polygon so we can’t list them all. – Michael Delgado Dec 19 '22 at 15:36
  • Thanks for your comment. In that case, is there a way to convert a Multipolygon to a Polygon? – abs8090 Dec 19 '22 at 16:16
  • no. but you could loop through all of the polygon boundary coordinates in a multipolygon by accessing the `.geoms` attribute, e.g. with `np.hstack([np.array(g.boundary.xy) for g in multipolygon.geoms])` – Michael Delgado Dec 19 '22 at 17:39
  • just to make sure I understand, the code snippet iterates through the boundaries of each polygon of each Multipolygon, which means the points in each iteration will be in the border of a polygon, not inside the polygon. Am I correct? – abs8090 Dec 19 '22 at 17:59
  • yes. it's completely impossible to enumerate the points inside a polygon. so I took your question to mean you want the points which define the boundary. if you want points within the polygon, you'll have to specify how you want to sample the space. – Michael Delgado Dec 19 '22 at 19:09
  • got it, I will use the code snippet you suggested. Thank you so much! – abs8090 Dec 20 '22 at 00:04
  • I tested your suggestion and I think it is the best that can be done! Thank you so much! any way I can vote your comment as a solution? – abs8090 Dec 20 '22 at 00:45
  • I added it as an answer – Michael Delgado Dec 20 '22 at 17:48

1 Answers1

1

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])
Michael Delgado
  • 13,789
  • 3
  • 29
  • 54