forked from open-austin/austingreenmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_parks.py
More file actions
108 lines (76 loc) · 3.22 KB
/
generate_parks.py
File metadata and controls
108 lines (76 loc) · 3.22 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
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))
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))
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))
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']:
geometry = less_shitty_geometry(cursor, feature['geometry'], crs)
park_id = feature['properties']['PARK_ID']
feature['geometry'] = geometry
with open('data/city_of_austin_parks.geojson'.format(park_id), 'w+') as fh:
fh.write(json.dumps(data))
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()
generate_park(cursor)
generate_amenity(cursor)
generate_facility(cursor)
unshit_parks_topo(cursor)
cursor.close()
conn.close()