regions.py

Fri, 07 Sep 2018 00:38:18 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 07 Sep 2018 00:38:18 +0300
changeset 110
4d1618a4c5ba
parent 109
88a5110b66ba
permissions
-rw-r--r--

removed replacement

88
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #!/usr/bin/env python3
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 from xml.etree import ElementTree
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 from geometry import *
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 def parse_nodes(root):
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 nodes = {}
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 for child in root:
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 if child.tag == 'node':
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 lat, lon = float(child.attrib['lat']), float(child.attrib['lon'])
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 nodes[child.attrib['id']] = Location(lat, lon)
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 return nodes
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 def parse_way(way, nodes):
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 result = {'shape': [], 'name': None}
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 for child in way:
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 if child.tag == 'nd':
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 result['shape'].append(child.attrib['ref'])
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 elif child.tag == 'tag':
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 if child.attrib['k'] == 'shape':
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 raise ValueError('tag "shape" is not allowed')
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 result[child.attrib['k']] = child.attrib['v']
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 if result['shape'][-1] != result['shape'][0]:
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 raise ValueError('polygon for %r is not closed' % result)
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 result['shape'] = [nodes[ref] for ref in result['shape'][:-1]]
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 result['shape'] = Polygon(*result['shape'])
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 return result
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 def parse_regions(filename):
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 tree = ElementTree.parse(filename)
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 root = tree.getroot()
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 nodes = parse_nodes(root)
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 regions = {}
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 for child in root:
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 if child.tag == 'way':
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 way = parse_way(child, nodes = nodes)
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 if 'region' in way and 'name' in way:
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 way['priority'] = int(way.get('priority', 1))
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 regions[way['name']] = way
109
88a5110b66ba added some features
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
39 if 'external' in way:
88a5110b66ba added some features
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
40 way['priority'] = -1
88
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 return regions

mercurial