Should be really straight forward, but I can't understand what I'm doing wrong. I'm trying to measure the distance between the centre of a polygon (suburb) and an arbitrary point.
SELECT
ST_AsText(ST_Centroid(geom)) as poly_centroid
, ST_AsText(ST_SetSrid(ST_MakePoint(151.182995, -33.890395), 4283)) as pin
, ST_Distance(
ST_Transform(ST_SetSrid(ST_MakePoint(151.182995, -33.890395), 4283), 3112),
ST_Transform(ST_Centroid(geom), 3112)
) as dist
FROM public.suburbs
I'm using 3112, Australia Lambert projection (which is in metres) - and entering the arbitrary pin point in long/lat (EPSG 4283) - but the resulting distance is clearly wrong:
poly_centroid | pin | dist |
---|---|---|
POINT(150.9300421895883 -33.72537351810487) | POINT(151.182995 -33.890395) | 4229563.228668396 |
POINT(151.03083602374133 -33.75769279240827) | POINT(151.182995 -33.890395) | 4229563.161116713 |
POINT(151.15847466229258 -33.75004174996872) | POINT(151.182995 -33.890395) | 4229563.120652905 |
POINT(150.93371519943338 -33.73946132331832) | POINT(151.182995 -33.890395) | 4229563.214226549 |
POINT(150.97498052582384 -34.12744863603382) | POINT(151.182995 -33.890395) | 4229562.838806998 |
These should be 10s of Km apart, not 1000's.
Note that the polygons are all loaded in EPSG 3112 projection, so the ST_Transform isn't necessary.
SELECT ST_AsText(ST_Centroid(geom)) as poly_centroid
returnsPOINT(150.9300421895883 -33.72537351810487)
then geom obviously is not in EPSG:3112. Your SQL should still work but check what you get for the second term withselect ST_AsText(ST_Transform(ST_Centroid(geom), 3112))
.select st_distance( st_geomfromtext('POINT(150.9300421895883 -33.72537351810487)'), st_geomfromtext('POINT(1555654.93979939 -3903769.92078054)'))
gives 4202231 meters, that is close to your numbers.