Files
Sternsinger/data/transform_osm-csv.py
T
2026-10-19 23:05:18 +02:00

58 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python3
import csv
STREETS_CSV="OSM/Godesberg.csv"
#
# wrap the CSV reader in a function that returns a generator (via the yield statement)
#
def read_osm_data(path):
with open(path, 'r') as data:
reader = csv.DictReader(data)
for row in reader:
yield row
def str2latlong(s):
points = s.split(",")
latlon = []
for p in points:
(lon, lat) = p.split(" ")
latlon.append([lat, lon])
return latlon
def wkt2polylines(street, wkt_lines):
print(" %s : [" % (street, ))
for line in wkt_lines:
latlon = str2latlong(line)
print(" [")
for p in latlon:
print(" [%s, %s]," % (p[0], p[1]))
print(" ],")
print(" ],")
if __name__ == "__main__":
streets = {}
for idx, row in enumerate(read_osm_data(STREETS_CSV)):
if row['name'] != "":
print("ID %(full_id)s : %(name)s = %(WKT)s" % row)
# !!! TOO SIMPLE, more than 1 row !!!!
#streets[row['full_id']] = {row['name']: row['WKT'][12:-1]}
streets.setdefault(row['name'], [])
#e.g. WKT: "LINESTRING (7.1752343 50.6625148,7.1752247 50.6624694,7.1749479 50.6623222)"
# ^ ^ ^
# 0 12 -1
wkt_line = row['WKT'][12:-1]
streets[row['name']].append(wkt_line)
print("")
print("--------------------------------")
print("KENNEDYALLEE")
#print(streets['Kennedyallee'])
wkt2polylines("Kennedyallee", streets['Kennedyallee'])
print("")
print("--------------------------------")
print("AUSTRASSE")
#print(streets['Austraße'])
wkt2polylines("Austraße", streets['Austraße'])