Search code examples
postgresqlgoogle-mapspostgis

Lat/Long spatial reference


I am new to PostGIS, am not getting the area of polygon right, my sample data is from Google maps, I know the area of the polygon is 11 acres, but the area returned by st_area doesn't match, I already referred to a few links like below, but unable to resolve the issue, Internet says google follows 4326 Spatial references, I tried a lot, can you please help, Image attached is the polygon from google maps.

I am expecting an array of such coordinates from the user, I have to calculate the area from PostGIS and give an error back to the user if the area entered is not approximated to calculated area.

https://gis.stackexchange.com/questions/169422/how-does-st-area-in-postgis-work

How do I convert a latitude/longitude pair into a PostGIS geography type?

https://gis.stackexchange.com/questions/56862/what-spatial-reference-system-do-i-store-google-maps-lat-lng-in/56925

17.475197 78.389024 17.4771 78.39044 17.475657 78.391652 17.474408 78.390847 17.475197 78.389024

l_polygon_text='MULTIPOLYGON((( 17.4771000000000001 78.3904399999999981, 17.4751970000000014 78.3890240000000063, 17.4756570000000018 78.3916519999999934, 17.4751970000000014 78.3890240000000063, 17.4744080000000004 78.3908469999999937, 17.4771000000000001 78.3904399999999981)))';

st_area(ST_GeometryFromText(l_polygon_text,4326))
st_area(ST_GeometryFromText(l_polygon_text,2163));
st_area(ST_GeometryFromText(l_polygon_text,2249));
st_area(ST_GeometryFromText(l_polygon_text,3859));

ST_AREA(ST_Transform(ST_GeomFromText(l_polygon_text,4326),31467));
ST_Area(ST_Transform(ST_SetSRID(ST_GeomFromText(l_polygon_text),4326),900913));

polygon


Solution

  • In PostGIS, coordinates must be expressed as longitude first, then latitude. Google uses the opposite.

    After swapping the coordinates to the proper order, you can't directly call st_area, else you would get an area in "square degrees" which is meaningless. You would have to project to a suitable local coordinate system, or you can use the geography type which will return an area in m2.

    select st_area(st_geogFromText('MULTIPOLYGON(((78.3904399999999981 17.4771000000000001, 78.3890240000000063 17.4751970000000014,78.3916519999999934 17.4756570000000018,78.3890240000000063 17.4751970000000014,78.3908469999999937 17.4744080000000004,78.3904399999999981 17.4771000000000001)))'));
          st_area
    --------------------
     26956.897848576307
    

    That being said, the example you have provided is about 6.5 acres, not 11, because the polygon is not properly defined:

    enter image description here