SQL网络长度计算LonLat

问题描述 投票:0回答:1

我目前有一个Azure postgresql数据库,其中包含openstreetmap数据,我想知道是否有一个SQL查询,可以通过使用方式使用的节点的latlongs来获得方式的总距离。

我希望SQL查询能够返回way_id和距离。

我目前的方法是使用C#将所有的方式和所有的节点下载到字典中(以它们的id为键)。然后我在所有的方式中循环,将所有属于该方式的节点分组,然后使用它们的latlongs(值除以10000000)来计算距离。这一部分的工作是例外的,但它应该在服务器上完成。

我尝试过的SQL如下,但我卡在了根据latlongs计算每条路的总距离上。

更新了。 安装了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         

sql postgresql geospatial openstreetmap
1个回答
1
投票

这将是简单得多,你应该也有的。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;
© www.soinside.com 2019 - 2024. All rights reserved.