postgresql srid 4326 中 Postgis 中 2 点之间的距离(以米为单位)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8464666/
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
Distance between 2 POINTs in Postgis in srid 4326 in metres
提问by Rory
This is probably a simple question, but I'm not very good at PostGIS and don't fully grok all of this.
这可能是一个简单的问题,但我不太擅长 PostGIS,也没有完全理解所有这些。
Basically I have a table (nodes
) with a POINT column (point
). I have created an index on this column
基本上我有一个nodes
带有 POINT 列 ( point
)的表( )。我在此列上创建了索引
create index nodes__points on nodes using gist (point)
The column was created with
该列是用
select addgeometrycolumn('nodes', 'points', 'POINT', 4326, 2)
I am using srid 4326 because I'm adding data that's in the form (latitude, longitude). (i.e. the co-ordinate system where the position of Dublin, Ireland is lat=53.353 lon=-6.264 (which I've added with GeomFromText('POINT(-6.264 53.535)')
)).
我正在使用 srid 4326,因为我正在添加形式为(纬度、经度)的数据。(即爱尔兰都柏林的坐标系 lat=53.353 lon=-6.264(我已添加GeomFromText('POINT(-6.264 53.535)')
))。
For each point, I want to find all points that are roughly within a 1km box centred on that point (so selcet a.id, count(*) from nodes as a, nodes as b where SOME_DISTANCE_FUNCTION_HERE(a.point, b.point, 1000) group by a.id;
. It doesn't have to be exact, just a rough hueristic figure. a 1km bbox is fine, a 1km circle is fine. It doesn't have to be exactly 1km, just that order of magnitude.
对于每个点,我想找到大约在以该点为中心的 1km 框内的所有点(因此selcet a.id, count(*) from nodes as a, nodes as b where SOME_DISTANCE_FUNCTION_HERE(a.point, b.point, 1000) group by a.id;
。它不必非常精确,只是一个粗略的色调图。1km bbox 很好,1km 圆很好. 不一定要精确到 1 公里,只是这个数量级。
The ST_Distance
/ST_DWithin
/etc. all use the units of the SRID, which for 4326/WGS64 is degrees (so 1 = 1 degree of lattitude/longitude). But I want to use metres.
在ST_Distance
/ST_DWithin
的/ etc。都使用 SRID 的单位,对于 4326/WGS64,它是度数(所以 1 = 1 度纬度/经度)。但我想用米。
I tried ST_distance_sphere
and st_dwithin
which can use metres, but if I do that, the explain
shows that the index isn't being used.
我试着ST_distance_sphere
和st_dwithin
它可以用米,但如果我这样做,explain
是没有被使用显示了指数。
How can I get roughly what I want, anduse the geographical index?
我怎样才能大致得到我想要的东西,并使用地理索引?
UPDATE: This is on PostgreSQL 9.1 and PostGIS 2.0 svn build.
更新:这是在 PostgreSQL 9.1 和 PostGIS 2.0 svn 构建上。
采纳答案by Rory
Since writing this, I have discovered the "geographic" as opposed to the "geometry" type in PostGIS, which might do exactly what I want.
自从写这篇文章以来,我发现了 PostGIS 中的“地理”而不是“几何”类型,这可能正是我想要的。
回答by Francisco Valdez
You could use ST_Transform to use meters, also remeber that not all functions are available with geography types but if you really need speed use ST_DWithin, is the fastest way. Here's an aproximation of conversions between degrees and meters:
您可以使用 ST_Transform 来使用米,还请记住,并非所有功能都适用于地理类型,但如果您确实需要速度,请使用 ST_DWithin,这是最快的方法。这是度和米之间转换的近似值:
| places | degrees | distance |
| ------ | ---------- | -------- |
| 0 | 1.0 | 111 km |
| 1 | 0.1 | 11.1 km |
| 2 | 0.01 | 1.11 km |
| 3 | 0.001 | 111 m |
| 4 | 0.0001 | 11.1 m |
| 5 | 0.00001 | 1.11 m |
| 6 | 0.000001 | 0.111 m |
| 7 | 0.0000001 | 1.11 cm |
| 8 | 0.00000001 | 1.11 mm |
回答by jr from Yaoundé
Old question but you just have to cast like this: ST_Distance(p.geom::geography, u.geom::geography)
where p and u are alias for two tables
老问题,但你只需要像这样转换:ST_Distance(p.geom::geography, u.geom::geography)
其中 p 和 u 是两个表的别名