sql网络长度计算lon/lat

7lrncoxx  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(510)

我目前有一个包含openstreetmap数据的azurepostgresql数据库,我想知道是否有一个sql查询可以通过使用路径使用的节点的纬度/经度来获得路径的总距离。
我希望sql查询返回way\u id和distance。
我目前的方法是使用c#将所有方法和所有节点下载到字典中(它们的id是键)。然后我遍历所有的路径,将属于该路径的所有节点分组,然后使用它们的lat/long(值除以10000000)来计算距离。这一部分可以例外地工作,但是可以在服务器上完成。
我尝试的sql如下,但是我仍然停留在基于lat/long计算每种方式的总距离上。
更新:已安装postgis扩展。

  1. SELECT current_ways.id as wId, node_id, (CAST(latitude as float)) / 10000000 as lat, (CAST(longitude as float)) / 10000000 as lon FROM public.current_ways
  2. JOIN current_way_nodes as cwn ON current_ways.id = cwn.way_id
  3. JOIN current_nodes as cn ON cwn.node_id = cn.id
  4. * output*
  5. wId node_id latitude longitude
  6. 2 1312575 51.4761127 -3.1888786
  7. 2 1312574 51.4759647 -3.1874216
  8. 2 1312573 51.4759207 -3.1870016
  9. 2 1213756 51.4758761 -3.1865223
  10. 3 ....
  11. * desired_output*
  12. way_id length
  13. 2 x.xxx
  14. 3 ...
  15. **Tables**
  16. current_nodes
  17. id
  18. latitude
  19. longitude
  20. current_ways
  21. id
  22. current_way_nodes
  23. way_id
  24. node_id
  25. sequence_id
prdp8dxp

prdp8dxp1#

如果你也有 geometry 在你的表中,即实际的点,而不仅仅是坐标,或者更好的是,实际的线。
这就是说,这里有一个查询来获取您要查找的内容:

  1. SELECT w.way_id,
  2. ST_Length( -- compute the length
  3. ST_MAKELINE( --of a new line
  4. ST_SetSRID( --made of an aggregation of NEW points
  5. ST_MAKEPOINT((CAST(longitude as float)) / 10000000,(CAST(latitude as float)) / 10000000), --created using the long/lat from your text fields
  6. 4326) -- specify the projection
  7. ORDER BY w.sequence_id -- order the points using the given sequence
  8. )::geography --cast to geography so the output length will be in meters and not in degrees
  9. ) as length_m
  10. FROM current_way_nodes w
  11. JOIN current_nodes n ON w.node_id = n.node_id
  12. GROUP BY w.way_id;

相关问题