How to write geopandas data into osm.pbf file using python?

193 Views Asked by At

I have sample nodes, edges data like below. I am using ElementTree to write the data into .osm file and then trying to convert into .osm.pbf using osmosis but when trying to convert from .osm to .osm.pbf osmosis throws error saying "osm format is not not supported" .Is my output format worng? Also this process is very slow,time consuming and not cost effiecnt. Is there any library that converts geopandas node,edges data into .osm.pbf? What is the best way to convert geopandas(nodes,edges) data into .osm.pbf file?

nodes data:- https://easyupload.io/4vib5o edges:- https://easyupload.io/4k9du7

code:-

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") 

Ouput file:- https://easyupload.io/1z6u3e

Osmosis command and error:-

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

error:-

osm format is not not supported
1

There are 1 best solutions below

0
Markus Kaukonen On

Osmosis as rather a badly documented program. This worked for me (ubuntu 23.04) (cutting a lat lon box)

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

Terveisin, Markus