-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgenerate_parks.py
More file actions
209 lines (152 loc) · 6.3 KB
/
generate_parks.py
File metadata and controls
209 lines (152 loc) · 6.3 KB
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
import json
from collections import defaultdict
import psycopg2
def less_shitty_geometry(cursor, geometry, crs):
geometry['crs'] = crs
cursor.execute('SELECT ST_AsGeoJSON(ST_Transform(ST_GeomFromGeoJson(%s), 4326));', (json.dumps(geometry), ))
return json.loads(cursor.fetchone()[0])
def generate_park(cursor):
with open('./raw/city_of_austin_parks.json', 'r') as fh:
data = fh.read()
data = json.loads(data)
crs = data['crs']
for feature in data['features']:
geometry = less_shitty_geometry(cursor, feature['geometry'], crs)
park_id = feature['properties']['PARK_ID']
feature['geometry'] = geometry
with open('data/park/park_{}.geojson'.format(park_id), 'w+') as fh:
fh.write(json.dumps(feature, sort_keys=True))
def generate_amenity(cursor):
with open('./raw/pard_amenity_points.json', 'r') as fh:
data = fh.read()
data = json.loads(data)
crs = data['crs']
features_by_park = defaultdict(list)
for feature in data['features']:
geometry = less_shitty_geometry(cursor, feature['geometry'], crs)
feature['geometry'] = geometry
park_id = feature['properties']['PARK_ID']
features_by_park[park_id].append(feature)
for park_id, features in features_by_park.items():
feature_collection = {
'type': 'FeatureCollection',
'features': features
}
with open('data/amenity/park_{}.geojson'.format(park_id), 'w+') as fh:
fh.write(json.dumps(feature_collection, sort_keys=True))
def generate_facility(cursor):
with open('./raw/pard_facility_points.json', 'r') as fh:
data = fh.read()
data = json.loads(data)
crs = data['crs']
features_by_park = defaultdict(list)
for feature in data['features']:
geometry = less_shitty_geometry(cursor, feature['geometry'], crs)
feature['geometry'] = geometry
park_id = feature['properties']['PARK_ID']
features_by_park[park_id].append(feature)
for park_id, features in features_by_park.items():
feature_collection = {
'type': 'FeatureCollection',
'features': features
}
with open('data/facility/park_{}.geojson'.format(park_id), 'w+') as fh:
fh.write(json.dumps(feature_collection, sort_keys=True))
def generate_trails(cursor):
with open('./raw/pard_trails_nrpa.json', 'r') as fh:
data = fh.read()
data = json.loads(data)
crs = data['crs']
features_by_park = defaultdict(list)
for feature in data['features']:
geometry = less_shitty_geometry(cursor, feature['geometry'], crs)
feature['geometry'] = geometry
park_id = feature['properties']['PARK_ID']
features_by_park[park_id].append(feature)
for park_id, features in features_by_park.items():
feature_collection = {
'type': 'FeatureCollection',
'features': features
}
with open('data/trail/park_{}.geojson'.format(park_id), 'w+') as fh:
fh.write(json.dumps(feature_collection, sort_keys=True))
def _center_for_geometry(cursor, geometry, crs):
geometry['crs'] = crs
cursor.execute('SELECT ST_AsGeoJSON(ST_Centroid(ST_Transform(ST_GeomFromGeoJson(%s), 4326)));', (json.dumps(geometry), ))
center = json.loads(cursor.fetchone()[0])['coordinates']
center = [center[1], center[0]]
return center
def unshit_parks_topo(cursor):
with open('./raw/city_of_austin_parks.json', 'r') as fh:
data = fh.read()
data = json.loads(data)
crs = data['crs']
for feature in data['features']:
center = _center_for_geometry(cursor, feature['geometry'], crs)
feature['properties']['center'] = center
geometry = less_shitty_geometry(cursor, feature['geometry'], crs)
feature['geometry'] = geometry
with open('data/city_of_austin_parks.geojson', 'w+') as fh:
fh.write(json.dumps(data, sort_keys=True))
def unshit_trails_topo(cursor):
with open('./raw/pard_trails_nrpa.json', 'r') as fh:
data = fh.read()
data = json.loads(data)
crs = data['crs']
for feature in data['features']:
geometry = less_shitty_geometry(cursor, feature['geometry'], crs)
feature['geometry'] = geometry
with open('data/pard_trails_nrpa.geojson', 'w+') as fh:
fh.write(json.dumps(data, sort_keys=True))
def generate_facility_lookup():
with open('./raw/pard_facility_points.json', 'r') as fh:
data = fh.read()
data = json.loads(data)
lookup = defaultdict(set)
for feature in data['features']:
key = feature['properties']['FACILITY_TYPE']
if key.strip():
park_id = feature['properties']['PARK_ID']
if not park_id:
continue
park_id = int(park_id)
lookup[key].add(park_id)
for k, v in lookup.items():
lookup[k] = list(v)
with open('data/facility_lookup_v2.json', 'w+') as fh:
fh.write(json.dumps(lookup, sort_keys=True))
def generate_amenity_lookup():
with open('./raw/pard_amenity_points.json', 'r') as fh:
data = fh.read()
data = json.loads(data)
lookup = defaultdict(set)
for feature in data['features']:
key = feature['properties']['AMENITY_TYPE']
if key.strip():
park_id = feature['properties']['PARK_ID']
if not park_id:
continue
park_id = int(park_id)
lookup[key].add(park_id)
for k, v in lookup.items():
lookup[k] = list(v)
with open('data/amenity_lookup_v2.json', 'w+') as fh:
fh.write(json.dumps(lookup, sort_keys=True))
if __name__ == '__main__':
# FIXME: really we only use this so we can transform the SRID/CRS from 2277 to 4326
# Should get rid of the postgis dependency, I'm sure there is good standalone stuff
conn = psycopg2.connect("dbname='bostongreenmap' user='django' host='localhost' password='django'")
cursor = conn.cursor()
# FIXME: Convert PARK_ID to number
# FIXME: Convert "<Null>" to None
# generate_park(cursor)
# generate_amenity(cursor)
# generate_facility(cursor)
# generate_trails(cursor)
#
# unshit_parks_topo(cursor)
# unshit_trails_topo(cursor)
generate_facility_lookup()
generate_amenity_lookup()
cursor.close()
conn.close()