3

When creating centroids from polygons, ArcMap has an option to placing the centroids inside (within the bounds of) the polygons. I am attempting to create centroids in Python. I have tried:

df['geometry'].centroids

The problem is that some polygons are shaped crescent-like, or doughnuts as in the shown example so the resulting centroids are outside the polygons. Is there a way of making the centroids fall inside their polygons in Python?

Output from df.geometry.centroids method

Using the function below suggested by @ThomasG77 does produce a good result.

df['geometry'].representative_point()

Output from representatitive_points() method

However, is there a way to influence the placement of this point so that it falls as centrally as possible within the bounds of the shape?

5

You just need to use the method representative_point that borrows the shapely method (shapely library borrows the method from GEOS library PointOnSurface method e.g https://github.com/Toblerity/Shapely/blob/0ecc21c0aaef478dbfdc55bcf3b6e90a039e4b64/shapely/geos.py#L631)

So reusing your example you should use df['geometry'].representative_point()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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