[postgis-users] Multi centroids

Mike Toews mwtoews at sfu.ca
Fri Dec 5 15:07:47 PST 2008


Hi Kevin,

That was exactly what I was after, thanks! Here is my modified function 
which I've tested to some degree with MULTIPOLYGONs only:

CREATE OR REPLACE FUNCTION ST_MultiCentroid(geometry)
  RETURNS geometry AS
$BODY$SELECT ST_Collect(the_geom)
FROM (
  SELECT ST_Centroid((ST_Dump($1)).geom) AS the_geom
) AS foo;$BODY$
  LANGUAGE 'sql' IMMUTABLE
  COST 100;

-Mike

Kevin Neufeld wrote:
> Yes.  Use ST_Dump to explode your MULTI* object, take the ST_Centroid 
> of each, and ST_Collect them together again.
>
> SELECT ST_AsText(ST_Collect(the_geom))
> FROM (
>   SELECT ST_Centroid((ST_Dump(the_geom)).geom) AS the_geom
>   FROM (
>     SELECT
>       'MULTIPOLYGON (
>         (( 0 0, 0 1, 1 1, 1 0, 0 0 )),
>         (( 2 2, 2 3, 3 3, 3 2, 2 2 ))
>        )'::geometry AS the_geom
>   ) AS foo
> ) AS foo2;
>
>           st_astext
> -----------------------------
>  MULTIPOINT(0.5 0.5,2.5 2.5)
> (1 row)
>
>
> Cheers,
> Kevin




More information about the postgis-users mailing list