#3960 closed task (fixed)

Unify centroid functions

Reported by: komzpa Owned by: komzpa
Priority: medium Milestone: PostGIS 2.5.0
Component: postgis Version: trunk
Keywords: Cc:


Code currently contains at least three centroid implementations:

ST_Centroid backing function, calls GEOS, has CIRCULAR* support, no weight support:


lwgeom_centroid, KMeans backing, calls GEOS, has no CIRCULAR* support visible:


median init_guess, only points, no GEOS, supports weights:


It looks like all three can be merged and pulled closer together, providing a weighted centroid for multipoints and support for CIRCULAR* in kmeans.

Are there any other places?

Change History (5)

comment:1 Changed 17 months ago by dbaston

#1 and #2 seem like good candidates to merge (add curve support to #2, and have #1 delegate to #2).

I'd keep #3 separate, as I don't think the use of M to signify "weight" is obvious or established throughout PostGIS.

comment:2 Changed 17 months ago by komzpa

Another centroid is in Geography.

Weighted by M, accepts multipoint (hey @dbaston there is such practice...): https://github.com/postgis/postgis/blob/64134491b6fad59a11da05d494b6046b715e00df/postgis/geography_centroid.c#L179 It looks like it doesn't accept Z and uses it only because of geog2cart transform.

The whole geography centroid file fully reimplements logic of geos centroid, if I'm correctly looking at its code here: https://github.com/OSGeo/geos/blob/master/src/algorithm/Centroid.cpp

Another small centroid in kmeans, 2D only, has logic of "check if it's current cluster" attached: https://github.com/postgis/postgis/blob/741b88eda71c80701366dffecd7fa665ee47060c/liblwgeom/lwkmeans.c#L45

GBOX centroid, uses fancy point iteration likely hardwired to memory layout: https://github.com/postgis/postgis/blob/b72778f0ff5564a6ec5b5e45620fc0e3b90c8433/liblwgeom/lwgeodetic.c#L258

comment:3 Changed 13 months ago by pramsey

Owner: changed from pramsey to komzpa

comment:4 Changed 13 months ago by pramsey

I'm reassigning this since it energizes me not at all.

comment:5 Changed 12 months ago by komzpa

Resolution: fixed
Status: newclosed

In 16608:

Unify geometry centroid functions

Make ST_Centroid call lwgeom_centroid.

Closes #3960
Closes https://github.com/postgis/postgis/pull/256

Note: See TracTickets for help on using tickets.