Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgis - st_distance

Tags:

php

postgis

I'm having problems with st_distance function in postgis. It returns wrongs results - for small distances the error isn't big - 10, maybe 20 meters but for bigger distances the difference between my results and for example google maps results are too big - 700 meters up to 2 kilometers and higher. I'm using srid = 4326. And second thing - maybe that's the problem - say I've got place that's 4 kilometers away. Postgis says it's ~ 0.0417 {{some_units}} away. Right now I'm just multiplying the result by 100 and get more or less accurate results. Can I pass some argument to this function that would say "return value in kilometers/meters"?

ps. postgres version is 9.0.1

like image 742
marek Avatar asked Jan 17 '26 09:01

marek


2 Answers

You should use the geography data type.

With the function used by the geometry datatype you will get the same unit back as the data is stored in. In your case degrees.

degrees can not be translated to meters in any easy way. To get the result in meters from an unprojected reference system the calculation need to calculate on the "great circle" around the sheroid of the earth. The calculation you are using is just using Pythagoras theorem to calculate the distance on a plane.

So, PostGIS doesn't give wrong result. You could move to a meter based spatial reference system depending on if your data cover that big area so the rounding of the globe is an issue, If so you should use the geography data type instead and use srid 4326.

HTH

Nicklas

like image 165
Nicklas Avén Avatar answered Jan 19 '26 22:01

Nicklas Avén


you should use your projection, for the moment it is in degrees if you want in meter use ST_transform like this :

    ST_Distance(st_transform(ST_GeometryFromText('POINT(longitude     latitude)',4326),900913),st_transform(point,900913)) 
like image 32
cyril Avatar answered Jan 19 '26 22:01

cyril



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!