-
Notifications
You must be signed in to change notification settings - Fork 0
/
osmtogps.py
101 lines (70 loc) · 2.68 KB
/
osmtogps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import overpy
import os.path
import argparse
import name_to_area as nta
import subprocess
from linq import Flow, extension_std
import json
class OverpassApiItemProvider:
def __init__(self, querypath, area):
self.area = area
self.query = readqueryfile(querypath)
area_id = nta.getAreaOsmId(area)
self.query = self.query.format(geoarea=str(area_id))
api = overpy.Overpass()
print("Query Overpass")
# fetch all ways and nodes
self.result = api.query(self.query)
def generateitem(self,node):
item = dict()
item.update({"lon": node.lon})
item.update({"lat": node.lat})
item.update({"name": node.tags.get("name","")})
return item
def getitems(self):
return Flow(self.result.nodes).Map(lambda n : self.generateitem(n)).Unboxed()
class GeojsonItemProvider:
def __init__(self, path):
self.data = None
with open(path, 'r',encoding="utf8") as f:
self.data = json.load(f)
def generateitem(self,node):
coords = node["geometry"]["coordinates"]
if "name" in node["properties"]:
name = node["properties"]["name"]
else:
name = ""
item = dict()
item.update({"lon": coords[0]})
item.update({"lat": coords[1]})
item.update({"name": name})
return item
def getitems(self):
return Flow(self.data["features"]).Map(lambda n : self.generateitem(n)).Unboxed()
def readqueryfile(path):
with open(path, "r",encoding="utf8") as f:
return f.read()
parser = argparse.ArgumentParser(description='Query osm data and convert to TomTom POI file')
parser.add_argument('--query' ,dest="query",type=str)
parser.add_argument('--geoarea' ,dest="geoarea",type=str)
parser.add_argument('--geojson' ,dest="geojson",type=str)
args = parser.parse_args()
if args.query is not None:
filename = args.query
overpassquery = OverpassApiItemProvider(args.query,args.geoarea)
items = overpassquery.getitems()
if args.geojson is not None:
filename = args.geojson
provider = GeojsonItemProvider(args.geojson)
items = provider.getitems()
plainpath = "output/"+os.path.basename(filename)+".out.txt"
print("Store result in "+plainpath)
plaintxtout = open(plainpath,"w",encoding="utf8")
for item in items:
plaintxtout.write(str.format("{0} , {1} , {2} \n",item["lon"],item["lat"],item["name"]))
plaintxtout.close()
outov2 = "output/"+os.path.basename(filename)+".ov2"
cmd = str.format("ov2tools/makeov2.exe {0} {1}",plainpath,outov2)
print("convert to ov2")
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=0x08000000)
process.wait()