wiki:UsersWikiComputeMultiCentroid

Version 1 (modified by pierre, 15 years ago) ( diff )

Compute Multi Centroid

 -- Function: st_multicentroid(geometry)
 -- Returns a MULTIPOINT obtained from collecting all
 -- centroids in the supplied MULTI* geometry.
 --   by: Mike Toews
 --
 -- Usage:
 -- postgis=# select [ST_AsText|http://postgis.refractions.net/documentation/manual-svn/ST_AsText.html](ST_!MultiCentroid(
 -- postgis(#       'MULTIPOLYGON (
 -- postgis'#         (( 0 0, 0 1, 1 1, 1 0, 0 0 )),
 -- postgis'#         (( 2 2, 2 3, 3 3, 3 2, 2 2 ))
 -- postgis'#        )'::geometry));
 --           st_astext
 -- -----------------------------
 --  MULTIPOINT(0.5 0.5,2.5 2.5)
 --( 1 row)
 --
 -- DROP FUNCTION ST_!MultiCentroid(geometry);

 CREATE OR REPLACE FUNCTION ST_!MultiCentroid(geometry)
   RETURNS geometry AS
 $BODY$SELECT [ST_Collect|http://postgis.refractions.net/documentation/manual-svn/ST_Collect.html](the_geom)
 FROM (
  SELECT [ST_Centroid|http://postgis.refractions.net/documentation/manual-svn/ST_Centroid.html](([ST_Dump|http://postgis.refractions.net/documentation/manual-svn/ST_Dump.html]([ST_Multi|http://postgis.refractions.net/documentation/manual-svn/ST_Multi.html]($1))).geom) AS the_geom
 ) AS foo;$BODY$
   LANGUAGE 'sql' IMMUTABLE
   COST 100;

Note: See TracWiki for help on using the wiki.