forked from bdon/OSMExpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_server.py
77 lines (63 loc) · 2.84 KB
/
web_server.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
import json
import sys
from http.server import BaseHTTPRequestHandler, HTTPServer
import osmx
if len(sys.argv) <= 1:
print("Usage: web_server.py OSMX_FILE")
env = osmx.Environment(sys.argv[1])
# simple implementation of OSM GeoJSON API using osmx + Python standard library.
# not production ready!
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
parts = self.path.split("/")
if len(parts) < 3:
self.send_response(400)
self.wfile.write("bad request".encode('utf-8'))
return
self.send_response(200)
self.send_header('Content-type','application/json')
self.end_headers()
osm_id = parts[2]
resp = {'type':'Feature','properties':{}}
with osmx.Transaction(env) as txn:
locations = osmx.Locations(txn)
def coord(node_id):
loc = locations.get(node_id)
return (loc[1],loc[0])
nodes = osmx.Nodes(txn)
if parts[1] == "node":
node = nodes.get(osm_id)
if node:
for k,v in osmx.tag_dict(node.tags).items():
resp['properties'][k] = v
resp['geometry'] = {'type':'Point','coordinates':coord(osm_id)}
elif parts[1] == "way":
ways = osmx.Ways(txn)
way = ways.get(osm_id)
for k,v in osmx.tag_dict(way.tags).items():
resp['properties'][k] = v
coords = [coord(node_id) for node_id in way.nodes]
resp['geometry'] = {'type':'LineString','coordinates':coords}
elif parts[1] == "relation":
ways = osmx.Ways(txn)
relations = osmx.Relations(txn)
relation = relations.get(osm_id)
for k,v in osmx.tag_dict(relation.tags).items():
resp['properties'][k] = v
geometries = []
def add_relation_geoms(r):
for member in r.members:
if member.type == 'node':
geometries.append({'type':'Point','coordinates':locations.get(member.ref)})
if member.type == 'way':
way = ways.get(member.ref)
coords = [coord(node_id) for node_id in way.nodes]
geometries.append({'type':'LineString','coordinates':coords})
if member.type == 'relation':
add_relation_geoms(relations.get(member.ref))
add_relation_geoms(relation)
resp['geometry'] = {'type':'GeometryCollection','geometries':geometries}
self.wfile.write(json.dumps(resp).encode('utf-8'))
print('Server listening on port 8000...')
httpd = HTTPServer(('', 8000), Handler)
httpd.serve_forever()