SQL 网络长度计算 Lon/Lat

SQL Network Length Calculation Lon/Lat

我目前有一个包含 openstreetmap 数据的 Azure postgresql 数据库,我想知道是否有一个 SQL 查询可以通过使用节点的 lat/longs 来获取路径的总距离使用。

我想 SQL 查询到 return way_id 和距离。

我目前的方法是使用 C# 将所有路径和所有节点下载到字典中(以它们的 ID 为键)。然后我遍历所有路径,将属于该路径的所有节点分组,然后使用它们的 lat/longs(值除以 10000000)来计算距离。这部分作为例外工作,而是在服务器上完成。

下面是我尝试过的 SQL,但我一直坚持根据 lat/longs 计算每条路的总距离。

更新:已安装 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         

如果您的 table 中也有 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;