sql网络长度计算lon/lat

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

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

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
JOIN current_way_nodes as cwn ON current_ways.id = cwn.way_id
JOIN current_nodes as cn ON cwn.node_id = cn.id

* output*

wId node_id latitude    longitude
2   1312575 51.4761127  -3.1888786
2   1312574 51.4759647  -3.1874216
2   1312573 51.4759207  -3.1870016
2   1213756 51.4758761  -3.1865223
3   ....

* desired_output*

way_id  length
2   x.xxx
3   ...

**Tables**

current_nodes
    id
    latitude
    longitude

current_ways
    id

current_way_nodes
    way_id
    node_id
    sequence_id
prdp8dxp

prdp8dxp1#

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

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

相关问题