简体   繁体   English

Postgis计算点之间的距离

[英]Postgis calculate the distance between points

I have a table that has more than 15k rows. 我有一个表,其中有超过15,000行。

The table has locationID, isEmpty, longitude and latitude fields. 该表具有locationID,isEmpty,经度和纬度字段。

I want to calculate the distance between each point has isEmpty field = 1 and the others and insert the result into new table using Postgis. 我想计算每个具有isEmpty字段= 1的点与其他点之间的距离,然后使用Postgis将结果插入到新表中。

Please advise what is the best practice for writing sql statement executing this requirement 请告知编写执行此要求的sql语句的最佳实践是什么

Ok, thanks for explanations - now it's clearer. 好的,谢谢您的解释-现在更清楚了。

In Postgis you don't have to count all distances to find how many other points are within some distance. 在Postgis中,您不必计算所有距离即可找到某个距离内还有多少其他点。

  1. Add extension to database 向数据库添加扩展

     CREATE EXTENSION postgis; 
  2. Now add a column of type geometry 现在添加一个几何类型的列

     ALTER TABLE mytable ADD COLUMN geom geometry; 
  3. You'll need some index to 您需要一些索引来

     CREATE INDEX mytable_geom_idx ON mytable USING gist(geom); 
  4. Now populate new column 现在填充新列

     UPDATE mytable SET geom = ST_SetSRID(ST_MakePoint(lon, lat),4326); 
  5. And now the query: 现在查询:

     Select a.locationID, count(*) from mytable a join mytable b on a.locationID!=b.locationID and ST_Dwithin(a.geom::geography,b.geom::geography,152) where a.isEmpty=1 group by 1; 

ST_DWithin with geography type is taking distance in meters and 152m~500ft ST_DWithin地理类型的距离以米为单位,距离为152m〜500ft

I didn't test it, so if something will not work please write me a comment 我没有对其进行测试,因此,如果无法正常工作,请给我评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM