!pip3 install pyshp !pip3 install osmium import osmium import shapefile import time class Worker(osmium.SimpleHandler): def __init__(self, output_filename): osmium.SimpleHandler.__init__(self) self.filename = output_filename.replace(".shp", "") self.shapes = shapefile.Writer(self.filename + ".shp", shapeType=shapefile.POLYLINE) self.shapes.autoBalance = 1 self.shapes.field("POWERLINE", "C") self.shapes.field("TRACK_CNT", "C") def way(self, w): electrified = w.tags.get("electrified") track_count = w.tags.get("passenger_lines") coords = [] for node in w.nodes: if node.location.valid: coords.append([node.location.lon, node.location.lat]) self.shapes.line([coords]) self.shapes.record(electrified, track_count) def saveall(self): # see https://spatialreference.org/ref/epsg/4326/prettywkt/ for coordinate-system epsg = """GEOGCS["WGS 84", DATUM["WGS_1984", SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich",0, AUTHORITY["EPSG","8901"]], UNIT["degree",0.01745329251994328, AUTHORITY["EPSG","9122"]], AUTHORITY["EPSG","4326"]]""" with open(self.filename + ".prj", "w") as f: f.write(epsg) f.close() self.shapes.close() t_1 = time.time() osmhandler = Worker("shapefile_germany.shp") osmhandler.apply_file("filtered_data.osm", locations=True) osmhandler.saveall() t_2 = time.time() print("time elapsed", t_2 - t_1)