如何使用python将geopandas数据写入osm.pbf文件?

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

我有示例节点、边缘数据,如下所示。我正在使用

ElementTree
将数据写入
.osm
文件,然后尝试使用渗透转换为
.osm.pbf
,但是当尝试从
.osm
转换为
.osm.pbf
渗透时,渗透会抛出错误,提示
"osm format is not not supported"
。是我的输出格式设置?而且这个过程非常缓慢、耗时且不具有成本效益。有没有任何库可以将 geopandas 节点、边缘数据转换为
.osm.pbf
?将 geopandas(节点,边)数据转换为
.osm.pbf
文件的最佳方法是什么?

节点数据:- https://easyupload.io/4vib5o 边缘:- https://easyupload.io/4k9du7

代码:-

import xml.etree.ElementTree as ET
import geopandas as gpd
import pandas as pd

def gpd_to_osm(n_gdf,e_gdf):
    root = ET.Element("osm")
    root.set("version", "0.6")
    root.set("generator", "MyNetworkGenerator")
    for i,row in n_gdf.iterrows():
        current_node = ET.SubElement(root, 'node', attrib={
            'id': str(n_gdf.loc[i,'id']),
            'lat': str(n_gdf.loc[i,'lat']),
            'lon': str(n_gdf.loc[i,'lon']),
            'changeset': 'false'})
        root.append(current_node)
    for i,row in e_gdf.iterrows():
        print(e_gdf.loc[i,'u'])
        current_ways = ET.SubElement(root, 'ways', attrib={'u':str(e_gdf.loc[i,'u']),
                                                           'v':str(e_gdf.loc[i,'v']),
                                                            'key':str(e_gdf.loc[i,'key']),
                                                            'access':str(e_gdf.loc[i,'access']), 
                                                            'area':str(e_gdf.loc[i,'area']), 
                                                            'bicycle':str(e_gdf.loc[i,'bicycle']),
                                                            'bridge':str(e_gdf.loc[i,'bridge']), 
                                                            'busway':str(e_gdf.loc[i,'busway']),
                                                           'cycleway':str(e_gdf.loc[i,'cycleway']),
                                                            'est_width':str(e_gdf.loc[i,'est_width']), 
                                                            'foot':str(e_gdf.loc[i,'foot']), 
                                                            'footway':str(e_gdf.loc[i,'footway']), 
                                                            'highway':str(e_gdf.loc[i,'highway']), 
                                                            'int_ref':str(e_gdf.loc[i,'int_ref']),
                                                           'junction':str(e_gdf.loc[i,'junction']),
                                                            'lanes':str(e_gdf.loc[i,'lanes']), 
                                                            'lit':str(e_gdf.loc[i,'lit']), 
                                                            'maxspeed':str(e_gdf.loc[i,'maxspeed']), 
                                                            'motorcar':str(e_gdf.loc[i,'motorcar']), 
                                                            'motorroad':str(e_gdf.loc[i,'motorroad']),
                                                            'motor_vehicle':str(e_gdf.loc[i,'motor_vehicle']), 
                                                            'name':str(e_gdf.loc[i,'name']), 
                                                            'oneway':str(e_gdf.loc[i,'oneway']), 
                                                            'overtaking':str(e_gdf.loc[i,'overtaking']), 
                                                            'path':str(e_gdf.loc[i,'path']),
                                                           'passing_places':str(e_gdf.loc[i,'passing_places']), 
                                                           'psv':str(e_gdf.loc[i,'psv']), 
                                                           'ref':str(e_gdf.loc[i,'ref']), 
                                                           'service':str(e_gdf.loc[i,'service']),
                                                           'segregated':str(e_gdf.loc[i,'segregated']), 
                                                           'sidewalk':str(e_gdf.loc[i,'sidewalk']),
                                                           'smoothness':str(e_gdf.loc[i,'smoothness']), 
                                                           'surface':str(e_gdf.loc[i,'surface']), 
                                                           'tracktype':str(e_gdf.loc[i,'tracktype']), 
                                                           'tunnel':str(e_gdf.loc[i,'tunnel']), 
                                                            'width':str(e_gdf.loc[i,'width']), 
                                                           'timestamp':str(e_gdf.loc[i,'timestamp']), 
                                                           'version':str(e_gdf.loc[i,'version']), 
                                                           'tags':str(e_gdf.loc[i,'tags']), 
                                                           'osm_type':str(e_gdf.loc[i,'osm_type']), 
                                                            'geometry':str(e_gdf.loc[i,'geometry']), 
                                                           'length':str(e_gdf.loc[i,'length'])})
        root.append(current_ways)
    tree = ET.ElementTree(root)
    tree.write("mynetwork.osm") 

输出文件:- https://easyupload.io/1z6u3e

渗透命令和错误:-

osmosis --read-xml file="mynetwork.osm" --write-pbf file="output.osm.pbf"

错误:-

osm format is not not supported
python openstreetmap elementtree osmosis osm.pbf
1个回答
0
投票

渗透是一个记录不良的程序。 这对我有用(ubuntu 23.04)(切割经纬度框)

osmosis --rb file=/home/markus/Downloads/austria-latest.osm.pbf --bounding-box top=48.3200 left=14.3000 bottom=48.3000 right=14.3200 --wb file=local.osm.pbf

马库斯·特尔维辛

© www.soinside.com 2019 - 2024. All rights reserved.