osmnx获取中心点_OSMNx:使用OSM ID获取节点的坐标

通过Python库OSMNx,可以获取并保存城市旅行路径为shp或json文件。要得到每个节点的经纬度,可以从Graph中直接获取节点属性,如`G.node[OSM_ID]`,或者利用`ox.graph_to_gdfs()`转换为GeoDataFrame来方便处理节点几何信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I used the Python library OSMNx to draw an optimal route between several steps of a city trip. The final variable is a list of OSM ids.

Now, I'm trying to save this route as a shp or json files. The problem is that I need for that the latitude/longitude of each node, but I didn't found an OSMNx function to do that.

I tried get_route_edge_attributes (but coordinates are not a valid attribute for this function). There is any way to get coordinates of an OSM node with this single id ?

Thanks in advance.

解决方案

you have all the attribute of each node and edge in the Graph. you can get node attributes using:

G.node[38862848]

#out: {'highway': nan,

# 'lat': 45.3210533,

# 'lon': -122.9790558,

# 'osmid': '38862848',

# 'ref': nan,

# 'x': 501641.47862882155,

# 'y': 5018616.5723966481}

G.node[38862848]['lat']

# out: 45.3210533

and to get edge attributes you can use G[u][v]:

G[5035130880][4963510289]

# out:

#{0: {'bridge': 'yes',

# 'geometry': ,

# 'highway': 'secondary',

# 'length': 671.332597496,

# 'name': 'Northwest 185th Avenue',

# 'oneway': False,

# 'osmid': [124454683, 24446714, 124454682]}}

All attributes are also in GeoDataFrame's of the graph.

If you have list of nodes, the easiest way to get the geometry of all nodes is:

import osmnx as ox

import networkx as nx

gdf_nodes, gdf_edges = ox.graph_to_gdfs()

path = nx.shortest_path(G, G.nodes()[0], G.nodes()[1])

gdf_nodes.loc[path]

#out:

# highway lat lon osmid ref x y geometry traffic_signals

#5035130880 NaN 45.5637 -122.868 5035130880 NaN 510334 5.04558e+06 POINT (510334.0390091945 5045583.999886028) 0

#4963510289 NaN 45.5698 -122.868 4963510289 NaN 510329 5.04625e+06 POINT (510329.3114555664 5046254.728223645) 0

# ...

the output is a GeoDataFrame.

### 使用 osmnx 库加载或解析 OSM 文件 为了利用 `osmnx` 加载或解析 OpenStreetMap (OSM) 数据,可以采用多种方法来获取所需的数据并将其转换成易于处理的形式。以下是几种常见的途径: #### 方法一:直接从网络下载地理区域内的数据 如果目标是从特定地理位置提取最新版的地图数据而不是读取已有的 OSM 文件,则可以直接调用 `ox.graph_from_place()` 函数指定地点名称作为参数。 ```python import osmnx as ox place_name = "Piedmont, California, USA" G = ox.graph_from_place(place_name, network_type='drive') ``` 这种方法会自动连接至OpenStreetMap服务器抓取最新的地图信息,并返回一个 NetworkX 的 MultiDiGraph 对象表示该地区的道路网结构[^1]。 #### 方法二:加载本地存储的 GraphML 或者 shapefile 格式的图文件 当已有预先下载好的 OSM 导出文件时,比如 `.osm`, `.graphml` 或 ESRI Shapefiles (`*.shp`) ,则可以通过相应函数导入这些文件中的图形数据。 对于 **GraphML** 文件: ```python import osmnx as ox filepath = './data/graph.graphml' G = ox.load_graphml(filepath) ``` 而对于 **Shapefile** 形式的文件集合(包括 .shp,.dbf,.prj 和其他辅助文件),可先构建 GeoDataFrame 后再创建图对象: ```python import geopandas as gpd import osmnx as ox edges_path = "./data/edges.shp" nodes_path = "./data/nodes.shp" gdf_edges = gpd.read_file(edges_path) gdf_nodes = gpd.read_file(nodes_path) G = ox.utils_graph.graph_from_gdfs(gdf_nodes, gdf_edges) ``` 上述代码片段展示了如何将来自不同源的数据集组合起来形成完整的交通网络模型[^2]。 #### 方法三:使用原始 OSM XML 文件 虽然不常见,但如果确实有需求处理未经加工过的 OSM XML 文件(.osm),那么也可以借助第三方工具如 PyOsmium 来完成初步解析工作之后再转交给 OsMnx 进一步分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值