postgresql PostGis 距离计算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24624257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostGis Distance Calculation
提问by Thomas Dang
I am doing a indoor map navigation application right now and what I am trying to do is to build a database of map point in the building.
我现在正在做一个室内地图导航应用程序,我想要做的是在建筑物中建立一个地图点数据库。
All of the coordinate I use is taken from Google Map (which means the EPSG is 3857). What I need to do now is to find distance in meters as well as use D_Within in meters
我使用的所有坐标都来自谷歌地图(这意味着 EPSG 是 3857)。我现在需要做的是找到以米为单位的距离以及以米为单位使用 D_Within
When I try to extract out the distance between 2 point:
当我尝试提取两点之间的距离时:
SELECT ST_DISTANCE(
ST_GeomFromText('POINT(' || StartLon || ' ' || StartLat || ')',3857),
ST_GeomFromText('POINT(' || EndLon || ' ' || EndLat || ')',3857))
FROM i3_building.floordata;
For the first 2 row with:
对于前 2 行:
Start: 103.776047 1.292149; End: 103.77607 1.292212 (3 meters away)
Start: 103.776070 1.292212; End: 103.77554 1.292406 (50 meters away)
Result given is:
给出的结果是:
2.59422435413724e-005
4.11096095831604e-005
Even though they are in rad, the second result is only twice as high as the first one. So it makes me confuse. Then I try to output it as meters:
即使它们在 rad,第二个结果也只是第一个结果的两倍。所以这让我很困惑。然后我尝试将其输出为米:
SELECT ST_DISTANCE(
ST_GeographyFromText('POINT(' || StartLon || ' ' || StartLat || ')'),
ST_GeographyFromText('POINT(' || EndLon || ' ' || EndLat || ')'))
FROM i3_building.floordata;
The result given for the same rows is:
为相同行给出的结果是:
2.872546829
4.572207435
Which is not what I expected as well. I am not very familiar with PostGis and SRID so this question might seem simple but please help me out, I am stuck no @@
这也不是我所期望的。我对 PostGis 和 SRID 不是很熟悉,所以这个问题可能看起来很简单,但请帮帮我,我被困住了@@
回答by John Powell
Your coordinate reference system (CRS) is 4326, lat/lon. This is a common source of confusion with Google Maps: 3857 is the CRS used by Google Maps for its tiles, and is projected meters based on a spherical globe. Vector sources that are added to Google Maps (KML data, GPS dumps, etc) tend to be in lat/lon, 4326, which is measures in degrees and converted on the fly.
您的坐标参考系统 (CRS) 是 4326,纬度/经度。这是与谷歌地图混淆的常见原因:3857 是谷歌地图用于其图块的 CRS,并且是基于球形地球的投影米数。添加到 Google 地图的矢量源(KML 数据、GPS 转储等)往往以纬度/经度为单位,4326,以度为单位测量并动态转换。
If you want the distance in meters between two lat/lon points, use ST_Distance_Sphere. For example, for your first set of points,
如果您想要两个纬度/经度点之间的距离(以米为单位),请使用ST_Distance_Sphere。例如,对于您的第一组点,
select st_distance_sphere(st_makepoint(103.776047, 1.292149),st_makepoint(103.77607, 1.292212));
which gives 7.457 meters. Your second set of points are 62.74 meters away from each other, based on the same query.
这给出了 7.457 米。根据相同的查询,您的第二组点彼此相距 62.74 米。
Note there is also ST_Distance_Spheroidwhich takes a third parameter, the measurement spheroid, ie, an approximation of the earth's shape. This will potentially be more accurate, but probably not significant over small distances.
请注意,还有ST_Distance_Spheroid,它采用第三个参数,即测量椭球体,即地球形状的近似值。这可能会更准确,但在小距离上可能并不重要。
ST_Distance gives distance in projected coordinates, which is probably why you got strange results plugging in lat/lon values.
ST_Distance 给出投影坐标中的距离,这可能就是为什么你在插入纬度/经度值时得到奇怪的结果。