开发者

Postgis - st_distance

开发者 https://www.devze.com 2023-03-12 03:49 出处:网络
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

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 t开发者_StackOverflowo this function that would say "return value in kilometers/meters"?

ps. postgres version is 9.0.1


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


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)) 


You should do what cyril said and transform your geometries to use a srid with distance units in meters, such as 900913. Multiplying what you get right now in degrees (using srid=4326) by 100 is not the way to go.

Depending on where you are, there may be a local srid in meters that is more accurate for your location -- for instance, the SRID for Switzerland is 21781. Check this also: http://gothos.info/tag/coordinate-systems/ for some srids in the US area.


Detailing Nicklas'answer, given that all your geoms have srid 4326, you can use:

select st_distance(geom1::geography,geom2::geography)

to get the distance in meters

Or, to be more accurate, tranform the geometries to a region-specific projected crs like EPSG:25832 ETRS89 / UTM zone 32N:

select st_distance(st_transform(geom1,25832),st_transform(geom2,25832))


SRID:4326 ie.WGS1984 is to represent the world co ordinates in latitude and longitude.So when u want to measure distances, u have to project in UTM.You can refer the

spatial_ref_sys

table in your database to see which SRID uses which unit. To measure u project it to UTM.You can find the required utm zone using the utmzone function.then u can use the below query.

select st_distance(st_transform(geom1,utmzone(geom1)),st_transform(geom2,utmzone(geom2)))


If you have data as geom answer of sal is good. If you have data as lat long then try this:

select st_distance(point1,point2) from 
(SELECT ST_GeogFromText('SRID=4326;POINT(0 0)') point1,  
ST_GeogFromText('SRID=4326;POINT(0.25 0.4)') point2) a
0

精彩评论

暂无评论...
验证码 换一张
取 消