-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborough_mapper.py
35 lines (23 loc) · 965 Bytes
/
borough_mapper.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
import json
from shapely.geometry import Point, shape
class NotFoundError(Exception):
pass
class BoroughMapper:
"""Maps lat/long positions into London boroughsm based on a
borough geojson file"""
def __init__(self, raise_if_not_found=False):
with open("geometry/boroughs.json") as f:
boundaries = json.load(f)
self.polygons = {}
self.raise_if_not_found = raise_if_not_found
for feature in boundaries["features"]:
polygon = shape(feature["geometry"])
self.polygons[feature["properties"]["name"]] = polygon
def get_borough(self, latitude, longitude):
point = Point(longitude, latitude)
for name, polygon in self.polygons.items():
if polygon.contains(point):
return name
if self.raise_if_not_found:
raise NotFoundError(f"Unable to find borough for point {point}")
return "Outside London"