Commit 97ec5d80 authored by Eric Duminil's avatar Eric Duminil
Browse files

OSM script as python

parent 49ec3120
from pathlib import Path
import pickle
import pandas as pd
from pyproj import Transformer
from shapely import LineString
EPSG_ID = 25832
to_local = Transformer.from_crs("EPSG:4326", f"EPSG:{EPSG_ID}", always_xy=True)
def load_cache(cache_path):
with open(Path('cache') / '49_802698__49_804055__9_942167__9_945563.pickle', 'rb') as f:
return pickle.load(f)
def get_way_dict(way):
url = f'https://www.openstreetmap.org/way/{way.id}'
xy_s = [to_local.transform(node.lon, node.lat) for node in way.nodes]
path = LineString(xy_s)
way_info = {'id': way.id,
'URL': f'=HYPERLINK("{url}"; "{url}")',
'length [m]': round(path.length, 1)}
return way_info | way.tags
NUMERICAL_COLUMNS = ['maxspeed [km/h]', 'width [m]', 'length [m]']
def create_table(ways):
df = pd.DataFrame([get_way_dict(way) for way in ways]).set_index('id')
df = df.rename(columns={'width': 'width [m]',
'maxspeed': 'maxspeed [km/h]'})
for col in NUMERICAL_COLUMNS:
df[col] = pd.to_numeric(df[col])
return df
for cache_path in Path('cache').glob('*.pickle'):
print(f"Parsing {cache_path}")
df = create_table(load_cache(cache_path))
csv_output = Path('output') / f'OSM_infos_{cache_path.stem}.csv'
print(f" Exporting to {csv_output}")
df.to_csv(csv_output,
sep=';',
decimal=',',
encoding='utf-8-sig'
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment