[GIS] Centroid of Points using postgis

postgispostgresql

In my postgres database i have a set of points(lat, long) and i created the_geom index for that. Now i have to find the centroid of these points.

I Google a lot and i find that for closed objects(polygon etc) it possible to find the centroid. I am not sure how to find the centroid of points using postgis.

Also i found this:

SELECT ST_AsText(ST_Centroid('MULTIPOINT ( -1 0, -1 2, -1 3, -1 4, -1 7, 0 1, 0 3, 1 1, 2 0, 6 0, 7 8, 9 8, 10 6 )'));

But i didn't get any thing from this.

EDIT:

My requirement is:

Consider i have 5 places latlong, and i have to find which is center of this 5 places.

Help me to solve this. Thanks in advance.

Best Answer

Your SQL query is missing one function. Add ST_GeomFromText and it will work.

SELECT ST_AsText(ST_Centroid(ST_GeomFromText(('MULTIPOINT ( -1 0, -1 2, -1 3, -1 4, -1 7, 0 1, 0 3, 1 1, 2 0, 6 0, 7 8, 9 8, 10 6 )'))));

Result:

POINT(2.307692 3.307692)

If you have a point table "mypoints" the corresponding query is

select ST_AsText(ST_Centroid(ST_Union(geometry)))
from mypoints;

ST_Union builds a multipoint feature from your points, ST_Centroid finds the centroid and ST_AsText shows the result as WKT. Add WHERE if you want to select just some points from the table to centroid calculation.