-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresilience.sql
303 lines (234 loc) · 7.84 KB
/
resilience.sql
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Mapping access to medical services in Dar-es-Salaam */
/* 1 we need to isolate clinics */
create table medical_osm_polygon as
select osm_id, building, amenity, name, way
from planet_osm_polygon
where amenity = 'hospital' OR amenity = 'doctors' or amenity = 'clinic'
create table medical_osm_point as
select osm_id, building, amenity, name, way
from planet_osm_point
where amenity = 'hospital' OR amenity = 'doctors' or amenity = 'clinic'
-- needed to change from "where amenity = 'hospital', 'doctors', 'clinic'" cause of syntax error
------------------------------------------------------------
/* 2 isolate residential areas */
create table res_osm_polygon as
select osm_id, building, way
from planet_osm_polygon
where amenity is null and building is not null
-- filtering out amenities
alter table res_osm_polygon
add column res int
-- create table for residential
update res_osm_polygon
set res = 1
where building = 'yes' or building = 'residential'
-- filter out other weird buildings that shouldnt be there
delete from res_osm_polygon
where res is null
-- get rid of the ones that have stuck behind
create table res_osm_point as
select osm_id, building, way
from planet_osm_point
where amenity is null and building is not null
alter table res_osm_point
add column res int
update res_osm_point
set res = 1
where building = 'yes' or building = 'residential'
delete from res_osm_point
where res is null
-- repeat for points
---------------------------------------------------------------
-- IGNORE THIS!!!!!!!
/* 3 check for duplicates and get rid of em */
SELECT osm_id, COUNT( osm_id )
FROM res_osm_polygon
GROUP BY osm_id
HAVING COUNT( osm_id )> 1
ORDER BY osm_id;
-- a few
SELECT osm_id, COUNT( osm_id )
FROM res_osm_point
GROUP BY osm_id
HAVING COUNT( osm_id )> 1
ORDER BY osm_id;
-- none
SELECT osm_id, COUNT( osm_id )
FROM medical_osm_polygon
GROUP BY osm_id
HAVING COUNT( osm_id )> 1
ORDER BY osm_id;
-- none
SELECT osm_id, COUNT( osm_id )
FROM medical_osm_point
GROUP BY osm_id
HAVING COUNT( osm_id )> 1
ORDER BY osm_id;
-- none!!!!!
/* now its time to get rid of em */
alter table res_osm_polygon
add column cntpolygon int,
add column duplicate int
update res_osm_polygon
set cntpolygon as COUNT(osm_id)
from res_osm_polygon
group by osm_id
/* actually, we'll pretend those dont exist */
--------------------------------------------------------------------------------
/* 5 Consolidate polygons and points
/* Get rid of residential points that were also counted as residential polygons */
/* Create column*/
ALTER TABLE res_osm_point
ADD COLUMN duplicate int;
/* check for duplicates */
UPDATE res_osm_point
SET duplicate = 1
FROM res_osm_polygon
WHERE ST_INTERSECTS(res_osm_point.way, res_osm_polygon.way)
/* Delete the points that overlap the polygons */
DELETE FROM res_osm_point
WHERE duplicate = 1
/* now do the same for clinics */
alter table medical_osm_point
add COLUMN duplicate int;
update medical_osm_point
set duplicate = 1
from medical_osm_polygon
WHERE ST_INTERSECTS(medical_osm_point.way, medical_osm_polygon.way);
delete from medical_osm_point
where duplicate = 1;
-----------------------------------------------------------------------------------------------------------
/* 6 convert polygons to centroids */
/* Convert medical polygons to centroids and union with medical points */
CREATE TABLE med_union AS
SELECT osm_id, building, amenity, name, ST_CENTROID(way) AS geom
FROM medical_osm_polygon
UNION
SELECT osm_id, building, amenity, name, way as geom
FROM medical_osm_point
/* same with residential points */
CREATE TABLE res_union AS
SELECT osm_id, building,ST_CENTROID(way) AS geom
FROM res_osm_polygon
UNION
SELECT osm_id, building, way as geom
FROM res_osm_point
----------------------------------------------------------------------------------------------------
/* 7 count number of residences in each ward */
create table res_wards as
select
res_union.building as building,
res_union.osm_id as osm_id,
res_union.geom as point_geom,
wards.fid as fid
from res_union
join wards
on st_intersects(wards.geom, res_union.geom)
-- creating a table that joins ward id to each residence points
create table resward_cnt as
select fid, count(osm_id)
from res_wards
group by fid
-- creating a table that groups and counts residences in each ward
create table wards_w_res_cnt as
select
resward_cnt.count as count
resward_cnt.fid as fid
wards.ward_name as ward_name
wards.geom as geom
from resward_cnt
inner join wards
on resward_cnt.fid = wards.fid
-- joining resward_cnt w ward geometries to get a map of wards with counts of residences
--------------------------------------------------------------------------------
/* 8 count how many residences are within 1mi of a clinic */
SELECT addgeometrycolumn('evan','res_union','utmgeom',32737,'POINT',2);
UPDATE res_union
SET utmgeom = ST_Transform(geom, 32737);
--prepare residences for analysis
SELECT addgeometrycolumn('evan','med_union','utmgeom',32737,'POINT',2);
UPDATE med_union
SET utmgeom = ST_Transform(geom, 32737);
-- prepare clinics for Analysis
SELECT addgeometrycolumn('evan','wards_w_res_cnt','utmgeom',32737,'POLYGON',2);
UPDATE wards_w_res_cnt
SET utmgeom = ST_Transform(geom, 32737);
-- prepare wards for analysis
ALTER TABLE res_union ADD COLUMN res_access INTEGER;
-- add access to residential table
UPDATE res_union
SET res_access = 1
FROM med_union
WHERE ST_DWITHIN(res_union.utmgeom, med_union.utmgeom, 1609.34);
-- make access equal one when residence is within 1mi/1609.34m of a clinic
select *
from res_union
where res_access is null
limit 1000;
-- lets check
CREATE TABLE res_within_clinic AS
SELECT *
FROM res_union
WHERE res_access = 1
-- table with only residences within buffer
--------------------------------------------------------------------------------
/* 9 join residential points within buffer zone to wards with count, count total
number of residences within buffer, and calc percentage */
create table wards_w_access as
select
a.fid, count(b.res_access) as med_access
from wards_w_res_cnt a
join res_within_clinic b
on st_intersects(a.geom, b.geom)
group by a.fid
-- count residences with access in each ward
create table wards_final as
select
wards_w_res_cnt.fid as fid,
wards_w_res_cnt.ward_name as name,
wards_w_res_cnt.count as total_count,
wards_w_access.med_access as access_count,
wards_w_res_cnt.geom as geom
from wards_w_res_cnt
full outer join wards_w_access
on wards_w_res_cnt.fid = wards_w_access.fid
-- join the wards with number with access to the table with the names,
-- total count, and geom
update wards_final
set access_count = 0
where access_count is null
-- make it 0 instead of null
ALTER TABLE wards_final
ADD COLUMN pct_access float(8);
UPDATE wards_final
SET pct_access = access_count*1.0/total_count*1.0
-- calculate percentage
delete from med_union
where name = 'Hospitali ya Wilaya Muranga' or name = 'KEREGE Dispensary' or name = 'Kisarawe Hospital'
--get rid of hospitals that somehow ended up outside of DES
------------------- scratch work (mostly from figuring out how to calculate residences within wards) ----------------------------------------------------------------
select osm_id, count(res_union.geom)
FROM
(from
select res_union.*, wards.fid as fid
re_union inner join wards
on st_within(res_union.geom, wards.geom)) as a
CREATE TABLE wards_join AS
SELECT
wards.fid as fid, wards.ward_name as ward_name, wards.geom as geom1,
COUNT(osm_id) as total_ct
FROM wards
JOIN res_union
ON ST_Intersects(wards.geom, res_union.geom)
GROUP BY wards.fid, wards.ward_name
create table wards_join as
select fid as fid, ward_name as ward_name, geom as geom 1
from wards
select *
from wards_join
full outer join res_union
on ST_Intersects (wards.geom, res_union.geom)
group by fid
update res_union
add column fid
/* */