7

I want to get the centroids of my multipolygons using Python and GeoPandas. But when viewing the polygons and the centroids points on QGIS, the centroids are not inside the polygons.

Here is the code I used:

import geopandas as gpd
import pandas as pd
import sys

# Read the data
polygons = gpd.GeoDataFrame.from_file('mypolygons.shp')
temp=polygons.centroid
polygons['x'] = temp.x
polygons['y'] = temp.y
centroid=polygons[['x','y']]

gdf = gpd.GeoDataFrame(centroid, geometry=gpd.points_from_xy(centroid.x, centroid.y))
gdf.to_file("centroid/centroid.shp")

What is wrong?

6
  • 10
    There is no guarantee that a centroid will be inside a polygon let alone a multipolygon
    – Ian Turton
    Commented Sep 8, 2020 at 15:31
  • 1
    @IanTurton you know, I do find it interesting how stating the obvious is getting this much popularity...no offense (!), I mean, your comment has literally been the first thing that came to my mind after reading this, and at least 9 others probably experienced the same. Still, I struggled with me, but eventually resisted the +1, since, well, it's only really a piece of though, stating the obvious for those that know, and neither helps OP with their question, nor asks for clarification. I don't think this should get mistaken as a helpful comment (I myself do comments like these...need to stop).
    – geozelot
    Commented Sep 9, 2020 at 7:02
  • 1
    ...I'm probably going to delete my rant at some point, but I felt like it was needed, seeing 9 upvotes for a comment on a board where sth. like 3 upvotes for a good and thoughtful answer is rather the usual...
    – geozelot
    Commented Sep 9, 2020 at 7:08
  • @geozelot your comments sound fair enough to me. I reckon that your claim aims into the upvotes rather than the gist of Ian Turton's comment which to a certain extend can bring a person to reconsidering/investigating the theory of shapes of geometries and where their geocentroids can be located. But as you said it is "a piece of thought", and apparently I post many of them as well, so perhaps it can be a temporal helpful comment =) However, I will be really missing your participation on GIS SE, so I will merely beg you for your patience. And I am keen joining this discussion on Meta GIS SE.
    – Taras
    Commented Sep 9, 2020 at 7:27
  • The OP's question was "what is wrong?" and my comment (not an answer) suggests that their understanding of centroids is the problem. - Note that "votes" on comments gain the poister nothing.
    – Ian Turton
    Commented Sep 9, 2020 at 7:50

1 Answer 1

16

If you want a point that is for sure inside the polygon use representative point:

Returns a cheaply computed point that is guaranteed to be within the geometric object.

import geopandas as gpd
polygons = gpd.GeoDataFrame.from_file("/home/bera/GIS/Data/testdata/stands.shp")
polygons.geometry = polygons.representative_point()
polygons.to_file("/home/bera/GIS/Data/testdata/stands_repr_point.shp")

enter image description here

1

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