10 download osm import pg
In [ ]:
Copied!
import os
import urllib.request
import zipfile
import tempfile
from pathlib import Path
from osgeo import ogr, gdal
import os
import urllib.request
import zipfile
import tempfile
from pathlib import Path
from osgeo import ogr, gdal
In [ ]:
Copied!
country='Czech Republic'
user='xxx'
password='xxx'
country='Czech Republic'
user='xxx'
password='xxx'
In [ ]:
Copied!
url='http://download.geofabrik.de/europe/{}-latest-free.shp.zip'.format(
country.replace(' ', '-').lower()
)
data_dir = tempfile.TemporaryDirectory()
os.chdir(data_dir.name)
# download
print(f'Downloading {url}...')
filehandle, _ = urllib.request.urlretrieve(url)
# unzip
print(f'Unzipping into {data_dir.name}')
with zipfile.ZipFile(filehandle) as zfile:
zfile.extractall(data_dir.name)
url='http://download.geofabrik.de/europe/{}-latest-free.shp.zip'.format(
country.replace(' ', '-').lower()
)
data_dir = tempfile.TemporaryDirectory()
os.chdir(data_dir.name)
# download
print(f'Downloading {url}...')
filehandle, _ = urllib.request.urlretrieve(url)
# unzip
print(f'Unzipping into {data_dir.name}')
with zipfile.ZipFile(filehandle) as zfile:
zfile.extractall(data_dir.name)
In [ ]:
Copied!
conn_str=f'PG:dbname=isdp user={user} host=gislab.fsv.cvut.cz password={password}'
for input_shp in Path(data_dir.name).rglob('*.shp'):
lyr_name = input_shp.stem.lstrip('gis_osm_').rstrip('_free_1')
print(f"Processing '{lyr_name}'...")
gdal.VectorTranslate(conn_str, str(input_shp),
format='PostgreSQL',
layerName=lyr_name, accessMode='overwrite', geometryType='PROMOTE_TO_MULTI',
layerCreationOptions=['GEOMETRY_NAME=geom'])
conn_str=f'PG:dbname=isdp user={user} host=gislab.fsv.cvut.cz password={password}'
for input_shp in Path(data_dir.name).rglob('*.shp'):
lyr_name = input_shp.stem.lstrip('gis_osm_').rstrip('_free_1')
print(f"Processing '{lyr_name}'...")
gdal.VectorTranslate(conn_str, str(input_shp),
format='PostgreSQL',
layerName=lyr_name, accessMode='overwrite', geometryType='PROMOTE_TO_MULTI',
layerCreationOptions=['GEOMETRY_NAME=geom'])
In [ ]:
Copied!
data_dir.cleanup()
print("Done")
ds = None
data_dir.cleanup()
print("Done")
ds = None