GeoPandas Representative Point – How Does GeoPandas’ representative_point() Work?

geodataframegeopandaspoint-creationpythonwithin

I need to compute points that, unlike centroids, are assured to be within a geometry. GeoPandas offers the method geopandas.GeoDataFrame.representative_point(), but I cannot find how it works. Inspecting the source code on GitHub looks like this:

def representative_point(self):
    """
    [...]

    """
    return _delegate_geo_method("representative_point", self)

But when I look for _delegate_geo_method I can't find anything else that is useful.

Best Answer

Geopandas uses shapely for geometry manipulations. In shapely representative_point is a call to the GEOSPointOnSurface method in the GEOS C library. The GEOS library is also used by PostGIS, so I'm copying the answer from this similar questions here: https://gis.stackexchange.com/a/76563

If you want to search thru the GEOS library for exactly how things are done, it is common to have to dig 5+ method calls deep to come to the actual algorithm.


Based on a few experiments, I think ST_PointOnSurface() works roughly like this, if the geometry is a polygon:

  1. Trace an east-west ray, lying half-way between the northern and southern extents of the polygon.
  2. Find the longest segment of the ray that intersects the polygon.
  3. Return the point that is half-way along said segment.

That may not make sense, so here's a sketch of a polygon with a ray dividing it into a northern and southern parts:

             _
            / \             <-- northern extent
           /   \
          /     \
         /       \
        /         \      __
       /           \    /  \
      /_ _ _ P _ _ _\  / _ _\  P = point-on-surface
     /               \/      \
    /                         \
   /            C              \   C = centroid
  /                             \
 /                              /
/______________________________/  <-- southern extent

Thus, ST_PointOnSurface() and ST_Centroid() are usually different points, even on convex polygons.

The only reason for the "surface" in the name, I think, is that if the geometry has 3D lines then the result will be simply be one of the vertexes.

I would agree that more explanation (and better naming) would have been useful and hope a GEOS programmer might shed some more light on the matter.