Thursday, 21 July 2016

postgis - Distance in meters between two geographic coordinates



I want to determine the distance in meters between two geographic coordinates. The points are:


     Lat (deg)   Lon (deg)
A: -33.3930906 -70.5452765
B: -33.3993212 -70.5468767

So if i calc the distance in google maps or with a python library the measure is approx. 700 m but when I do the same query (distance) in PostGIS it tells me it is 291 (I don't know the unit, I believe that is meters)


My queries are:


SELECT ST_Distance_sphere(
st_makepoint(-33.3930906, -70.5452765),
st_makepoint(-33.3993212, -70.5468767));


SELECT ST_Distance_Sphere(
ST_Centroid(ST_GeomFromText('POINT(-33.3930906 -70.5452765)',4326)),
ST_GeomFromText('POINT(-33.3993212 -70.5468767)',4326));

SELECT ST_Distance(gg1, gg2) As spheroid_dist,
ST_Distance(gg1, gg2, false) As sphere_dist
FROM (SELECT
ST_GeogFromText('SRID=4326;POINT(-33.3930906 -70.5452765)') As gg1,
ST_GeogFromText('SRID=4326;POINT(-33.3993212 -70.5468767)') As gg2

) As foo ;

And each of the three queries give me a result of approx. 291 or 292.


So, how can I do it?


My original intention is to query all the points in a certain radius. For this I'm using ST_DWithin.


And in my database I store it like:


// Column is
coordinates GEOGRAPHY(Point, 4326) NOT NULL,
// Insert is
ST_GeographyFromText('POINT(-33.3940301 -70.5482872)', 4326)


Answer



Your coordinates are out of order. If you reverse the order of coordinates in the first query, postgis says: 708.55982691.


In postgis it's lon, lat, not lat, lon.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...