-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkpoly
executable file
·181 lines (137 loc) · 4.75 KB
/
mkpoly
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
#!/usr/bin/env python3
import json
import copy
import pickle
import sys
from argparse import ArgumentParser
from collections import namedtuple
import requests
from requests.compat import urljoin
formats = 'nodelist', 'ffmap'
Position = namedtuple('Position', ['lat', 'lng'])
geojson_area = {
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'Polygon',
'coordinates': []
}
}
geojson_frame = {
'type': 'FeatureCollection',
'features': []
}
def fetch(url, maptype):
"""
fetch node information
:param url: data location
:param maptype: data format
:return:
"""
response = requests.get(url)
document = response.json()
if maptype == 'ffmap':
return {node['id']: Position(lat=node['geo'][0], lng=node['geo'][1])
for node in document['nodes']
if node['geo']}
elif maptype == 'nodelist':
return {node['id']: Position(lat=node['position']['lat'], lng=node['position']['long'])
for node in document['nodes']
if 'position' in node}
def query_administrative_areas(position):
"""
fetch administrative area information for a position
:param position: namedtuple containg latitude and longitude
:return: areas
"""
if position in cache['point']:
return cache['point'][position]
baseurl = 'http://global.mapit.mysociety.org'
endpoint = '/point/{SRID}/{lng},{lat}'.format(SRID=4326, lat=position.lat, lng=position.lng)
response = requests.get(urljoin(baseurl, endpoint))
document = response.json()
cache['point'][position] = document
return document
def query_area_geojson(area_id):
"""
fetch geojson information for area
:param area_id: key
:return: geojson
"""
if area_id in cache['area']:
return cache['area'][area_id]
baseurl = 'http://global.mapit.mysociety.org'
endpoint = '/area/{id}.geojson'.format(id=area_id)
response = requests.get(urljoin(baseurl, endpoint))
document = response.json()
cache['area'][area_id] = document
return document
def get_municipal_area(areas):
"""
return the most local municipal area
:param areas: candidate administrative areas
:return most local area key
"""
def is_municipal(area):
k, v = area
return v['type'] in ('O06', 'O07', 'O08')
def get_type(area):
k, v = area
return int(v['type'][1:])
filtered_list = [area for area in areas.items() if is_municipal(area)]
if filtered_list:
return max(filtered_list, key=get_type)[1]
else:
print('Could not find a municipal area for the location!', file=sys.stderr)
return None
def main(url, fmt):
# get node information
nodes = fetch(url, fmt)
distribution = {}
for node, position in nodes.items():
# get administrative area information
areas = query_administrative_areas(position)
# find the most local administrative area
try:
local = get_municipal_area(areas)
if not local:
raise KeyError("No valid areas found.")
if local['id'] in distribution:
distribution[local['id']]['count'] += 1
else:
distribution[local['id']] = {'name': local['name'], 'count': 1}
except TypeError:
print('No appropriate administrative layer found for {}: {}'.format(node, position), file=sys.stderr)
areas = []
for area, data in distribution.items():
# get geojson for area to copy polygon coordinates
polygon = query_area_geojson(area)
# deepcopy template, reference polygon information
instance = copy.deepcopy(geojson_area)
instance['properties']['name'] = data['name']
instance['properties']['count'] = data['count']
instance['geometry']['type'] = polygon['type']
instance['geometry']['coordinates'] = polygon['coordinates']
areas.append(instance)
with open('nodes.geojson', 'w') as handle:
frame = geojson_frame
frame['features'] = areas
handle.write(json.dumps(frame))
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('-f', '--format', choices=formats, default=formats[0])
parser.add_argument('url', metavar='URL')
args = parser.parse_args()
# ensure FileNotFoundError is defined for python2.7 compatibility
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
try:
with open('app.cache', 'rb') as handle:
cache = pickle.load(handle)
except FileNotFoundError:
cache = {'point': {}, 'area': {}}
main(args.url, args.format)
with open('app.cache', 'wb+') as handle:
pickle.dump(cache, handle)