-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup_geo.py
executable file
·121 lines (94 loc) · 3.89 KB
/
setup_geo.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
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
#!/usr/bin/python3
import json
import psycopg2cffi
import re
from datetime import datetime
from jinjasql import JinjaSql
from project_conf import conf as proj_conf
from data_conf import data_conf
conn = psycopg2cffi.connect(proj_conf.conn_str)
curs = conn.cursor()
j = JinjaSql()
###TODO: TURN THIS INTO A SQL FILE TODO###
crs_number = data_conf.coordinate_crs.split(':')[1]
curs.execute("ALTER TABLE %s ADD COLUMN grid_id INTEGER" % proj_conf.project_name)
curs.execute("ALTER TABLE %s ADD COLUMN geom_point GEOMETRY ;" % proj_conf.project_name)
curs.execute("ALTER TABLE %s ADD COLUMN is_business_district BOOLEAN ;" % proj_conf.project_name)
curs.execute("ALTER TABLE %s ADD COLUMN ward2015 INTEGER ;" % proj_conf.project_name)
curs.execute("ALTER TABLE %s ADD COLUMN ward2003 TEXT ;" % proj_conf.project_name)
curs.execute("ALTER TABLE %s ADD COLUMN census_tract TEXT ;" % proj_conf.project_name)
sql_templ = """
UPDATE {{project_name|sqlsafe}}
SET geom_point = ST_SetSRID(ST_TRANSFORM(GEOMETRY(POINT({{x_col|sqlsafe}}, {{y_col|sqlsafe}})),
'+proj=longlat +datum=WGS84 +no_defs', {{crs_number|sqlsafe}}), {{crs_number|sqlsafe}})
WHERE geom_point IS NULL
"""
#sql_templ = """
# UPDATE {{project_name|sqlsafe}}
# SET geom_point = ST_SetSRID(GEOMETRY(POINT({{x_col|sqlsafe}}, {{y_col|sqlsafe}})), {{crs_number|sqlsafe}})
# WHERE geom_point IS NULL
# """
sql_vals = dict(
project_name=proj_conf.project_name,
x_col=data_conf.x_col,
y_col=data_conf.y_col,
crs_number=crs_number
)
query, bind_params = j.prepare_query(sql_templ, sql_vals)
curs.execute(query, list(bind_params))
sql_templ = """
UPDATE {{project_name|sqlsafe}}
SET {{x_col|sqlsafe}} = st_x(geom_point), {{y_col|sqlsafe}} = st_y(geom_point)
WHERE {{y_col|sqlsafe}} IS NOT NULL
AND {{y_col|sqlsafe}} IS NOT NULL
"""
sql_vals = dict(
project_name=proj_conf.project_name,
x_col=data_conf.x_col,
y_col=data_conf.y_col,
)
query, bind_params = j.prepare_query(sql_templ, sql_vals)
curs.execute(query, list(bind_params))
curs.execute("DELETE FROM grid_geo WHERE ST_IsValid(wkb_geometry) IS NOT TRUE")
curs.execute("ALTER TABLE grid_geo ADD COLUMN wkb_geometry_mercator GEOMETRY")
curs.execute("ALTER TABLE census_tracts_geo ADD COLUMN wkb_geometry_mercator GEOMETRY")
curs.execute("ALTER TABLE wards2015_geo ADD COLUMN wkb_geometry_mercator GEOMETRY")
sqlstr = """UPDATE %s SET census_tract = name10
FROM census_tracts_geo g
WHERE ST_intersects(geom_point, g.wkb_geometry::geometry) ;"""
curs.execute(sqlstr % proj_conf.project_name)
sqlstr = """UPDATE %s SET ward2015 = g.ward::int
FROM wards2015_geo g
WHERE ST_intersects(geom_point, g.wkb_geometry::geometry) ;"""
curs.execute(sqlstr % proj_conf.project_name)
sqlstr = """UPDATE %s SET ward2003 = g.ward
FROM wards2003_geo g
WHERE ST_intersects(geom_point, g.wkb_geometry::geometry) ;"""
curs.execute(sqlstr % proj_conf.project_name)
sqlstr = """
UPDATE grid_geo
SET wkb_geometry_mercator = ST_TRANSFORM(wkb_geometry::geometry, 3857)
"""
curs.execute(sqlstr)
sqlstr = """
UPDATE census_tracts_geo
SET wkb_geometry_mercator = ST_TRANSFORM(wkb_geometry::geometry, 3857)
"""
curs.execute(sqlstr)
sqlstr = """
UPDATE wards2015_geo
SET wkb_geometry_mercator = ST_TRANSFORM(wkb_geometry::geometry, 3857)
"""
curs.execute(sqlstr)
sqlstr = """UPDATE %s SET grid_id = g.id
FROM grid_geo g
WHERE ST_intersects(geom_point, g.wkb_geometry::geometry) ;"""
curs.execute(sqlstr % proj_conf.project_name)
sqlstr = """UPDATE %s SET is_business_district = True
FROM cbd_geo g
WHERE ST_intersects(geom_point, g.wkb_geometry::geometry) ;"""
curs.execute(sqlstr % proj_conf.project_name)
sqlstr = """UPDATE %s SET is_business_district = False
WHERE is_business_district is Null ;"""
curs.execute(sqlstr % proj_conf.project_name)
conn.commit()