-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_coordinates.py
executable file
·214 lines (189 loc) · 6.99 KB
/
extract_coordinates.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Copyright (c) 2015 Code for Karlsruhe (http://codefor.de/karlsruhe)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
Script to extract street coordinates from OSM data.
Takes ``highways.osm`` and outputs data into ``streets.geojson``.
"""
from __future__ import unicode_literals
import codecs
import collections
import json
import os.path
import geojson
from lxml import etree
# Streets are straightforward: They are stored as "way" objects in OSM
# and have a "highway" and a "name" tag. The only non-trivial thing is
# that a street may consist of several "way" objects.
#
# Places, on the other hand, have no canonical representation in OSM.
# Here is a list of example places from Karlsruhe:
#
# Relation: Friedrichsplatz (1542213)
# leisure: park
# type: multipolygon
#
# Way: Gotthold-Mayer-Platz (182377023)
# leisure: park
#
# Way: Schlossplatz (162432233)
# area: yes
# highway: pedestrian
#
# Relation: Lidellplatz (2227646)
# area: yes
# highway: pedestrian
# type: multipolygon
#
# Relation: Mendelssohnplatz (4552741)
# highway: pedestrian
# type: multipolygon
#
# Way: Engländerplatz (26723820)
# leisure: pitch
#
# Way: Fliederplatz (4835450)
# leisure: common
#
# Node: Paulckeplatz (1673718306)
# highway: place
#
# This makes the extraction of place coordinates more involved. Places
# marked by nodes only are currently not exported, because they end up
# as very prominent markers on the map (this only affects Paulckeplatz).
def check(d, k, v):
"""
Check if d[k] == v.
"""
try:
return d[k] == v
except KeyError:
return False
def parse_osm(f):
"""
Extract coordinates from OSM file.
"""
relations = {}
ways = {}
nodes = {}
node_refs = []
tags = {}
members = []
for event, element in etree.iterparse(f):
if element.tag == 'node':
nodes[element.get('id')] = (float(element.get('lon')),
float(element.get('lat')))
tags = {}
elif element.tag == 'tag':
tags[element.get('k')] = element.get('v')
elif element.tag == 'nd':
node_refs.append(element.get('ref'))
elif element.tag == 'way':
d = {'nodes': node_refs}
d.update(tags)
ways[element.get('id')] = d
tags = {}
node_refs = []
elif element.tag == 'relation':
name = tags.get('name')
if name and (check(tags, 'leisure', 'park') or
(check(tags, 'highway', 'pedestrian') and
check(tags, 'type', 'multipolygon'))):
d = {'members': members}
d.update(tags)
if name in relations:
raise ValueError('Duplicate relation "%s".' % name)
relations[name] = d
tags = {}
members = []
elif element.tag == 'member':
members.append(dict(element.attrib))
element.clear()
# Resolve node references in ways
for id, props in ways.iteritems():
try:
props['coordinates'] = [nodes[ref] for ref in props['nodes']]
except KeyError:
pass
# Resolve inner/outer members of multipolygon relations
for id, props in relations.iteritems():
if check(props, 'type', 'multipolygon'):
props['inner'] = []
props['outer'] = []
for member in props['members']:
role = member.get('role', 'outer')
if role in ['inner', 'outer']:
props[role].append(ways[member['ref']])
# Extract streets
streets = collections.defaultdict(lambda: [])
for way in ways.itervalues():
if ('name' in way) and ('coordinates' in way):
if ('highway' in way) or way.get('leisure') in ['park', 'pitch', 'common']:
streets[way['name']].append(way)
return streets, relations
def ways2geometry(ways):
"""
Convert a nested list of coordinates into a GeoJSON object.
"""
if len(ways) == 1:
way = ways[0]
highway = way.get('highway')
if ((way.get('area', '') == 'yes' and highway == 'pedestrian') or
(way.get('leisure') in ['park', 'pitch', 'common'])):
# See http://wiki.openstreetmap.org/wiki/Key:area
return geojson.Polygon([way['coordinates']])
elif highway:
return geojson.LineString(way['coordinates'])
else:
return geojson.MultiLineString([w['coordinates'] for w in ways])
def relation2geometry(relation):
"""
Convert relation data into a GeoJSON object.
"""
if check(relation, 'type', 'multipolygon'):
outer = relation['outer']
inner = relation['inner']
if not inner:
return geojson.MultiPolygon([(o['coordinates'],) for o in outer])
if len(outer) == 1:
polygons = [outer[0]['coordinates']]
for way in inner:
polygons.append(way['coordinates'])
return geojson.MultiPolygon([polygons])
raise ValueError('Unknown inner/outer configuration %r' % relation)
else:
raise ValueError('Unknown relation type %r' % relation)
if __name__ == '__main__':
HERE = os.path.dirname(os.path.abspath(__file__))
OSM = os.path.join(HERE, 'karlsruhe.osm')
GEOJSON = os.path.join(HERE, 'coordinates.geojson')
with open(OSM, 'r') as f:
streets, relations = parse_osm(f)
features = []
for name, ways in streets.iteritems():
features.append(geojson.Feature(geometry=ways2geometry(ways),
id=name))
for name, props in relations.iteritems():
features.append(geojson.Feature(geometry=relation2geometry(props),
id=name))
collection = geojson.FeatureCollection(features)
with codecs.open(GEOJSON, 'w', encoding='utf8') as f:
geojson.dump(collection, f)