In [1]:
%matplotlib inline
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np
import ogr, gdal
import os
import sqlite3
In [2]:
#config
layertype = "lines" #Type of features
sqlite_path = 'targetsqlite.db' #location of SQLite file
csv_path = 'csv' #location of csv's on folder
shp_path = 'shp' #location of csv's on folder
geojson_path = 'geojson' #location of csv's on folder
pbf_path = 'romania-latest.osm.pbf' #location of PBF file
In [3]:
#Define Input datasource
driver = ogr.GetDriverByName('PBF') # Format
data_source = ogr.Open(pbf_path, 0) #open Data
LayerCount = data_source.GetLayerCount()

layers = []
# get Required Layer
for i in range(LayerCount):
    layers.append(data_source.GetLayer(i).GetName())
print ("Layers in PBF File : " , layers)
Layers in PBF File :  ['points', 'lines', 'multilinestrings', 'multipolygons', 'other_relations']
In [24]:
#SQLITE Example
if (os.path.isfile(sqlite_path)):
    print ( 'Using Already Converted File '+ sqlite_path )
else :
    print( 'Converting File ' + pbf_path + " to " + sqlite_path )
    dir(os.system('ogr2ogr -f "SQLite" -dsco SPATIALITE=YES ' + sqlite_path + ' ' + pbf_path ))
    print ('Done Converting File ' + pbf_path + " to " + sqlite_path)
    
#Define Input datasource
driver = ogr.GetDriverByName('SQLite') # Format
data_source = driver.Open(sqlite_path) #open Data
LayerCount = data_source.GetLayerCount()
Using Already Converted File targetsqlite.db
In [7]:
#CSV Example
if os.path.exists(csv_path):
    print ( 'Using Already Converted Files in '+ csv_path + ' Folder')
else :
    print( 'Converting File ' + pbf_path + " to " + csv_path )
    dir(os.system('ogr2ogr -f "CSV" ' + csv_path + ' ' + pbf_path + ' -lco GEOMETRY=AS_WKT'))
    print ('Done Converting File ' + pbf_path + " to " + csv_path)
Converting File romania-latest.osm.pbf to csv
Done Converting File romania-latest.osm.pbf to csv
In [5]:
#SHP Example
if os.path.exists(shp_path):
    print ( 'Using Already Converted Files in '+ shp_path + ' Folder' )
else :
    print( 'Converting File ' + pbf_path + " to " + shp_path )
    os.makedirs(shp_path)
    for layer in layers:
        dir(os.system('ogr2ogr -f "ESRI Shapefile" ' + shp_path + '/' + layer + '.shp' + ' ' + pbf_path + ' ' + layer))
        print ('Done Converting File ' + pbf_path + " to " + shp_path + '/' + layer + '.shp ')
Converting File romania-latest.osm.pbf to shp
Done Converting File romania-latest.osm.pbf to shp/points.shp 
Done Converting File romania-latest.osm.pbf to shp/lines.shp 
Done Converting File romania-latest.osm.pbf to shp/multilinestrings.shp 
Done Converting File romania-latest.osm.pbf to shp/multipolygons.shp 
Done Converting File romania-latest.osm.pbf to shp/other_relations.shp 
In [21]:
#GeoJSON Example
if os.path.exists(geojson_path):
    print ( 'Using Already Converted Files in '+ geojson_path + ' Folder' )
else :
    print( 'Converting File ' + pbf_path + " to " + geojson_path )
    os.makedirs(geojson_path)
    for layer in layers:
        dir(os.system('ogr2ogr -f "GeoJSON" ' + geojson_path + '/' + layer + '.geojson ' + pbf_path + ' ' + layer))
        print ('Done Converting File ' + pbf_path + " to " +  geojson_path + '/' + layer + '.geojson ')
Converting File romania-latest.osm.pbf to geojson
Done Converting File romania-latest.osm.pbf to geojson/points.geojson 
Done Converting File romania-latest.osm.pbf to geojson/lines.geojson 
Done Converting File romania-latest.osm.pbf to geojson/multilinestrings.geojson 
Done Converting File romania-latest.osm.pbf to geojson/multipolygons.geojson 
Done Converting File romania-latest.osm.pbf to geojson/other_relations.geojson 
In [22]:
#spatialReference
WKT_SR = data_source.GetLayer(0).GetSpatialRef().ExportToWkt()
In [ ]:
disk_layer = data_source.GetLayer(3)
In [13]:
#get GeoDataFrame from geojson
layer = 'points'
file = geojson_path + '/' + layer
gdf = gpd.read_file(file)
gdf.crs = WKT_SR
In [ ]: