-
Notifications
You must be signed in to change notification settings - Fork 4
/
geometry_loader.py
68 lines (58 loc) · 2.24 KB
/
geometry_loader.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
import json
from model import Place, PlaceAlias, get_one_or_create
from util import GeometryUtility
class GeometryLoader:
"""Load Place objects from a NDJSON document like that generated by
geojson-places-us.
"""
def __init__(self, _db):
self._db = _db
self.places_by_external_id = dict()
def load_ndjson(self, fh):
while True:
metadata = fh.readline().strip()
if not metadata:
# End of file.
break
geometry = fh.readline().strip()
yield self.load(metadata, geometry)
def load(self, metadata, geometry):
metadata = json.loads(metadata)
external_id = metadata["id"]
type = metadata["type"]
parent_external_id = metadata["parent_id"]
name = metadata["name"]
aliases = metadata.get("aliases", [])
abbreviated_name = metadata.get("abbreviated_name", None)
if parent_external_id:
parent = self.places_by_external_id[parent_external_id]
else:
parent = None
# This gives us a Geometry object. Set its SRID so the database
# knows it's using real-world latitude and longitude.
geometry = GeometryUtility.from_geojson(geometry)
place, is_new = get_one_or_create(
self._db,
Place,
external_id=external_id,
type=type,
parent=parent,
create_method_kwargs=dict(geometry=geometry),
)
# Set these values, even the ones that were set in
# create_method_kwargs, so that we can update any that have
# changed.
place.external_name = name
place.abbreviated_name = abbreviated_name
place.geometry = geometry
# We only ever add aliases. If the database contains an alias
# for this place that doesn't show up in the metadata, it
# may have been created manually.
for alias in aliases:
name = alias["name"]
language = alias["language"]
alias, is_new = get_one_or_create(
self._db, PlaceAlias, place=place, name=name, language=language
)
self.places_by_external_id[external_id] = place
return place, is_new