-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreetTrees_Queries.sql
467 lines (438 loc) · 24.4 KB
/
StreetTrees_Queries.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
---------------------------------------------
-- The below code leverages data from the street tree censuses for NYC to date.
-- This assumes that data are in a PostGIS database as follows; the content on
-- the right side of the hyphen denotes the databas schema and table (schema.table)
-- - NYC NTA boundaries (no water) - admin.nycdcp_nta2010_nowater
-- - NYC Community District boundaries (no water) - admin.nycdcp_commdists_nowater
-- - NYC Council District boundaries (no water) - admin.nycdcp_coundists2010_nowater
-- - NYC Borough boundaries (no water) - admin.nycdcp_borough_nowater
-- - NYC LION dataset, version 16a - infrastructure.lion16a
-- - NYC Parks Street Tree Capacity - env_assets.nycdpr_blockface_capacity
-- - 1995-1996 Street Tree Census - env_assets.streettrees1995
-- - 2005-2006 Street Tree Census - env_assets.streettrees2005
-- - 2015-2016 Street Tree Census - env_assets.streettrees2015
--
-- Results in each step get written to a schema called 'results_streettrees' that should be
-- created before running the queries. Final product for each geography is in a table called
-- following a convention of: 'results_streettrees.treecensus_summaries_[unit]_final'
---------------------
-------Street Tree Metrics for Neighborhood Tabulation Areas
---------------------
-- Compute 2015 Street Tree Summaries - pct living < 6", >30", most common species, total #, # Not Stump (live + dead),
-- and # Live Based on spatial overlay W/ Respective boundaries
create table results_streettrees.treecensus2015_summaries_ntas2010 as
select foo1.ntacode, foo1.ntaname, foo1.geom_2263,
treecount2015_all,
treecount2015_notstump,
treecount2015_alive,
spc_mostcommon_living,
round(roadlength, 2) as roadmiles,
case when roadlength = 0 then NULL else
(round(treecount2015_alive::numeric/roadlength::numeric, 2))
end as livingtree_x_mile2015,
round(avg_dbh_living, 2) as avg_dbh_living,
med_dbh_living,
cnt_dbh_lt6_notstump,
cnt_dbh_lt6_living,
case when treecount2015_alive = 0 then NULL else
(round(cnt_dbh_lt6_living::numeric/treecount2015_alive::numeric*100, 3))
end as pct_lt6_dbh_living,
cnt_dbh_gt30_notstump,
cnt_dbh_gt30_living,
case when treecount2015_alive = 0 then NULL else
(round(cnt_dbh_gt30_living::numeric/treecount2015_alive::numeric*100, 3))
end as pct_gt30_dbh_living
from
(select
count(*) as treecount2015_all,
avg(tree_dbh::numeric) filter(where status like 'Alive') as avg_dbh_living,
percentile_disc(0.5) within group (order by tree_dbh::numeric) filter(where status like 'Alive') as med_dbh_living,
mode() within group (order by spc_common) filter(where status like 'Alive') as spc_mostcommon_living,
count(*) filter (where status not like 'Stump' and tree_dbh::numeric<6) as cnt_dbh_lt6_notstump,
count(*) filter (where status like 'Alive' and tree_dbh::numeric<6) as cnt_dbh_lt6_living,
count(*) filter (where status not like 'Stump' and tree_dbh::numeric>30) as cnt_dbh_gt30_notstump,
count(*) filter (where status like 'Alive' and tree_dbh::numeric>30) as cnt_dbh_gt30_living,
count(*) filter (where status not like 'Stump') as treecount2015_notstump,
count(*) filter (where status like 'Alive') as treecount2015_alive,-- Number of Trees
ntacode, ntaname,
bnd.geom_2263 from
admin.nycdcp_nta2010_nowater bnd --Neighborhood Tabulation Areas
left join env_assets.streettrees2015 streettree2015 on st_intersects(streettree2015.geom_2263, bnd.geom_2263)
group by ntacode, ntaname, bnd.geom_2263) as foo1 --Join street trees based on intersect
left join
(select sum(st_length(st_intersection(roads.geom_2263, bnd.geom_2263)))::numeric/5280 as roadlength, --Length of Road miles intersecting each Community District
ntacode, ntaname from
admin.nycdcp_nta2010_nowater bnd --Neighborhood Tabulation Areas
left join infrastructure.lion16a roads on st_intersects(roads.geom_2263, bnd.geom_2263)
where --join roads based on intersect
(rw_type like ' 1' and featuretyp not like '6' and --road is street and pvt
("segmenttyp" like 'U' or "segmenttyp" like 'B' or "segmenttyp" like 'R') and --only includes features that represent roadbeds
nonped not like 'V') -- not vehicle only street (generally not places that would have street trees)
group by ntacode, ntaname) as foo4 on foo1.ntacode=foo4.ntacode;
-- Calculate Max Capacity based on Stree Tree Capacity Estimates
create table results_streettrees.max_capacity_nta as
select
ntacode,
sum(st_length(geom_2263)/orig_length*max_trees) as max_trees_new
from (
select
bnd.ntacode,
st_intersection(nbc.geom_2263, bnd.geom_2263) as geom_2263,
st_length(nbc.geom_2263) as orig_length,
max_trees
from
env_assets.nycdpr_blockface_capacity_20210108 nbc
left join admin.nycdcp_nta2010_nowater bnd
on st_intersects(nbc.geom_2263, bnd.geom_2263)) as foo
group by ntacode;
-- Join Max Capacity data onto 2015 street tree summaries and calculate stocking rates
create table results_streettrees.treecensus2015_summaries_newcapacity_nta2010 as
select tsf.*, round(max_trees_new::numeric, 0) as max_capacity_new,
round(treecount2015_notstump/round(max_trees_new::numeric, 0),4) as stockingrate_2015_notstump,
round(treecount2015_alive/round(max_trees_new::numeric, 0),4) as stockingrate_2015_living
from results_streetrees.max_capacity_nta edt
full join results_streettrees.treecensus2015_summaries_ntas2010 tsf
on edt.ntacode=tsf.ntacode;
--Calculate Desired Metrics for 1995 and 2005
create table results_streettrees.streettree_counts_1995_2005_ntas as
select nta_2010 as ntacode, treeecount1995_notplantingspace,treeecount1995_notps_notstump, treecount1995_living,
treecount2005_all, treecount2005_living from
(select
nta_2010 ,
count(*) filter (where condition not like 'Planting Space') as treeecount1995_notplantingspace,
count(*) filter (where condition not like 'Planting Space' and condition not like 'Stump') as treeecount1995_notps_notstump,
count(*) filter (where condition not like 'Dead' and
condition not like 'Planting Space' and
condition not like 'Shaft' and
condition not like 'Stump') as treecount1995_living
from env_assets.streettrees1995 s
group by nta_2010) as foo1
full join
(select
nta,
count(*) as treecount2005_all,
count(*) filter (where status not like 'Dead') as treecount2005_living
from env_assets.streettrees2005
group by nta) as foo2
on nta_2010=nta;
-- Add in data on 1995 and 2005 tree censuses to full tables for final result
create table results_streettrees.treecensus_summaries_nta_final as
select a.*, treeecount1995_notplantingspace,treeecount1995_notps_notstump, treecount1995_living,
treecount2005_all, treecount2005_living
from results_streettrees.treecensus2015_summaries_newcapacity_nta2010 a
full join results_streettrees.streettree_counts_1995_2005_ntas b on a.ntacode=b.ntacode;
-- Drop non-final tables created in the process above
drop table results_streettrees.max_capacity_nta, results_streettrees.streettree_counts_1995_2005_ntas,
results_streettrees.treecensus2015_summaries_newcapacity_nta2010, results_streettrees.treecensus2015_summaries_ntas2010;
---------------------
-------Street Tree Metrics for Community Districts
---------------------
-- Compute 2015 Street Tree Summaries - pct living < 6", >30", most common species, total #, # Not Stump (live + dead),
-- and # Live Based on spatial overlay W/ Respective boundaries
create table results_streettrees.treecensus2015_summaries_commdists as
select foo1.borocd, foo1.geom_2263,
treecount2015_all,
treecount2015_notstump,
treecount2015_alive,
spc_mostcommon_living,
round(roadlength, 2) as roadmiles,
round(treecount2015_alive::numeric/roadlength::numeric, 2) as livingtree_x_mile2015,
round(avg_dbh_living, 2) as avg_dbh_living,
med_dbh_living,
cnt_dbh_lt6_notstump,
cnt_dbh_lt6_living,
round(cnt_dbh_lt6_living::numeric/treecount2015_alive::numeric*100, 3) as pct_lt6_dbh_living,
cnt_dbh_gt30_notstump,
cnt_dbh_gt30_living,
round(cnt_dbh_gt30_living::numeric/treecount2015_alive::numeric*100, 3) as pct_gt30_dbh_living
from
(select
count(*) as treecount2015_all,
avg(tree_dbh::numeric) filter(where status like 'Alive') as avg_dbh_living,
percentile_disc(0.5) within group (order by tree_dbh::numeric) filter(where status like 'Alive') as med_dbh_living,
mode() within group (order by spc_common) filter(where status like 'Alive') as spc_mostcommon_living,
count(*) filter (where status not like 'Stump' and tree_dbh::numeric<6) as cnt_dbh_lt6_notstump,
count(*) filter (where status like 'Alive' and tree_dbh::numeric<6) as cnt_dbh_lt6_living,
count(*) filter (where status not like 'Stump' and tree_dbh::numeric>30) as cnt_dbh_gt30_notstump,
count(*) filter (where status like 'Alive' and tree_dbh::numeric>30) as cnt_dbh_gt30_living,
count(*) filter (where status not like 'Stump') as treecount2015_notstump,
count(*) filter (where status like 'Alive') as treecount2015_alive,-- Number of Trees
borocd,
bnd.geom_2263 from
admin.nycdcp_commdists_nowater bnd --Community District Boundaries
left join env_assets.streettrees2015 streettree2015 on st_intersects(streettree2015.geom_2263, bnd.geom_2263)
group by borocd, bnd.geom_2263) as foo1 --Join street trees based on intersect
left join
(select sum(st_length(st_intersection(roads.geom_2263, bnd.geom_2263)))::numeric/5280 as roadlength, --Length of Road miles intersecting each Community District
borocd from
admin.nycdcp_commdists_nowater bnd --Community District Boundaries
left join infrastructure.lion16a roads on st_intersects(roads.geom_2263, bnd.geom_2263)
where --join roads based on intersect
(rw_type like ' 1' and featuretyp not like '6' and --road is street and pvt
("segmenttyp" like 'U' or "segmenttyp" like 'B' or "segmenttyp" like 'R') and --only includes features that represent roadbeds
nonped not like 'V') -- not vehicle only street (generally not places that would have street trees)
group by borocd) as foo4 on foo1.borocd=foo4.borocd;
-- Calculate Max Capacity based on Stree Tree Capacity Estimates
create table results_streettrees.max_capacity_commdist as
select
borocd,
sum(st_length(geom_2263)/orig_length*max_trees) as max_trees_new
from (
select
bnd.borocd,
st_intersection(nbc.geom_2263, bnd.geom_2263) as geom_2263,
st_length(nbc.geom_2263) as orig_length,
max_trees
from
env_assets.nycdpr_blockface_capacity_20210108 nbc
left join admin.nycdcp_commdists_nowater bnd
on st_intersects(nbc.geom_2263, bnd.geom_2263)) as foo
group by borocd;
-- Join Max Capacity data onto 2015 street tree summaries and calculate stocking rates
create table results_streettrees.treecensus2015_summaries_newcapacity_commdist as
select tsf.*, round(max_trees_new::numeric, 0) as max_capacity_new,
round(treecount2015_notstump/round(max_trees_new::numeric, 0),4) as stockingrate_2015_notstump,
round(treecount2015_alive/round(max_trees_new::numeric, 0),4) as stockingrate_2015_living
from results_streetrees.max_capacity_commdist edt
full join results_streettrees.treecensus2015_summaries_commdist tsf
on edt.borocd=tsf.borocd;
--Calculate Desired Metrics for 1995 and 2005
create table results_streettrees.streettree_counts_1995_2005_commdist as
select cb_num as borocd, treeecount1995_notplantingspace,treeecount1995_notps_notstump, treecount1995_living,
treecount2005_all, treecount2005_living from
(select
cb_new,
count(*) filter (where condition not like 'Planting Space') as treeecount1995_notplantingspace,
count(*) filter (where condition not like 'Planting Space' and condition not like 'Stump') as treeecount1995_notps_notstump,
count(*) filter (where condition not like 'Dead' and
condition not like 'Planting Space' and
condition not like 'Shaft' and
condition not like 'Stump') as treecount1995_living
from env_assets.streettrees1995 s
group by cb_new) as foo1
join
(select
cb_num,
count(*) as treecount2005_all,
count(*) filter (where status not like 'Dead') as treecount2005_living
from env_assets.streettrees2005
group by cb_num) as foo2
on cb_num::integer=cb_new;
-- Add in data on 1995 and 2005 tree censuses to full tables
create table results_streettrees.treecensus_summaries_commdists_final as
select a.*, treeecount1995_notplantingspace,treeecount1995_notps_notstump, treecount1995_living,
treecount2005_all, treecount2005_living
from results_streettrees.treecensus2015_summaries_newcapacity_commdist a
full join results_streettrees.streettree_counts_1995_2005_commdist b on a.borocd=b.borocd::integer;
-- Drop non-final tables created in the process above
drop table results_streettrees.max_capacity_commdist, results_streettrees.streettree_counts_1995_2005_commdist,
results_streettrees.treecensus2015_summaries_newcapacity_commdist, results_streettrees.treecensus2015_summaries_commdists;
---------------------
-------Street Tree Metrics for City Council Districts
---------------------
-- Compute 2015 Street Tree Summaries - pct living < 6", >30", most common species, total #, # Not Stump (live + dead),
-- and # Live Based on spatial overlay W/ Respective boundaries
create table results_streettrees.treecensus2015_summaries_councildists as
select foo1.coundist, foo1.geom_2263,
treecount2015_all,
treecount2015_notstump,
treecount2015_alive,
spc_mostcommon_living,
round(roadlength, 2) as roadmiles,
round(treecount2015_alive::numeric/roadlength::numeric, 2) as livingtree_x_mile2015,
round(avg_dbh_living, 2) as avg_dbh_living,
med_dbh_living,
cnt_dbh_lt6_notstump,
cnt_dbh_lt6_living,
round(cnt_dbh_lt6_living::numeric/treecount2015_alive::numeric*100, 3) as pct_lt6_dbh_living,
cnt_dbh_gt30_notstump,
cnt_dbh_gt30_living,
round(cnt_dbh_gt30_living::numeric/treecount2015_alive::numeric*100, 3) as pct_gt30_dbh_living
from
(select
count(*) as treecount2015_all,
avg(tree_dbh::numeric) filter(where status like 'Alive') as avg_dbh_living,
percentile_disc(0.5) within group (order by tree_dbh::numeric) filter(where status like 'Alive') as med_dbh_living,
mode() within group (order by spc_common) filter(where status like 'Alive') as spc_mostcommon_living,
count(*) filter (where status not like 'Stump' and tree_dbh::numeric<6) as cnt_dbh_lt6_notstump,
count(*) filter (where status like 'Alive' and tree_dbh::numeric<6) as cnt_dbh_lt6_living,
count(*) filter (where status not like 'Stump' and tree_dbh::numeric>30) as cnt_dbh_gt30_notstump,
count(*) filter (where status like 'Alive' and tree_dbh::numeric>30) as cnt_dbh_gt30_living,
count(*) filter (where status not like 'Stump') as treecount2015_notstump,
count(*) filter (where status like 'Alive') as treecount2015_alive,-- Number of Trees
coundist,
bnd.geom_2263 from
admin.nycdcp_coundists2010_nowater bnd --Council District Boundaries
left join env_assets.streettrees2015 streettree2015 on st_intersects(streettree2015.geom_2263, bnd.geom_2263)
group by coundist, bnd.geom_2263) as foo1 --Join street trees based on intersect
left join
(select sum(st_length(st_intersection(roads.geom_2263, bnd.geom_2263)))::numeric/5280 as roadlength, --Length of Road miles intersecting each Community District
coundist from
admin.nycdcp_coundists2010_nowater bnd --Council District Boundaries
left join infrastructure.lion16a roads on st_intersects(roads.geom_2263, bnd.geom_2263)
where --join roads based on intersect
(rw_type like ' 1' and featuretyp not like '6' and --road is street and pvt
("segmenttyp" like 'U' or "segmenttyp" like 'B' or "segmenttyp" like 'R') and --only includes features that represent roadbeds
nonped not like 'V') -- not vehicle only street (generally not places that would have street trees)
group by coundist) as foo4 on foo1.coundist=foo4.coundist;
-- Calculate Max Capacity based on Stree Tree Capacity Estimates
create table results_streettrees.max_capacity_councildist as
select
coundist,
sum(st_length(geom_2263)/orig_length*max_trees) as max_trees_new
from (
select
bnd.coundist,
st_intersection(nbc.geom_2263, bnd.geom_2263) as geom_2263,
st_length(nbc.geom_2263) as orig_length,
max_trees
from
env_assets.nycdpr_blockface_capacity_20210108 nbc
left join admin.nycdcp_coundists2010_nowater bnd
on st_intersects(nbc.geom_2263, bnd.geom_2263)) as foo
group by coundist;
-- Join Max Capacity data onto 2015 street tree summaries and calculate stocking rates
create table results_streettrees.treecensus2015_summaries_newcapacity_councildists as
select tsf.*, round(max_trees_new::numeric, 0) as max_capacity_new,
round(treecount2015_notstump/round(max_trees_new::numeric, 0),4) as stockingrate_2015_notstump,
round(treecount2015_alive/round(max_trees_new::numeric, 0),4) as stockingrate_2015_living
from results_streettrees.max_capacity_councildist edt
full join results_streettrees.treecensus2015_summaries_councildists tsf
on edt.coundist=tsf.coundist;
--Calculate Desired Metrics for 1995 and 2005
-- Note - used spatial data from 1995 tree count for Council Districts, as otherwise about half had no entry
create table results_streettrees.streettree_counts_1995_2005_coundist as
select coundist as coundist, treeecount1995_notplantingspace,treeecount1995_notps_notstump, treecount1995_living,
treecount2005_all, treecount2005_living from
(select
coundist,
count(*) filter (where condition not like 'Planting Space') as treeecount1995_notplantingspace,
count(*) filter (where condition not like 'Planting Space' and condition not like 'Stump') as treeecount1995_notps_notstump,
count(*) filter (where condition not like 'Dead' and
condition not like 'Planting Space' and
condition not like 'Shaft' and
condition not like 'Stump') as treecount1995_living
from env_assets.streettrees1995 a
join admin.nycdcp_coundists2010_nowater b on st_intersects(a.geom_2263, b.geom_2263)
group by coundist) as foo1
full join
(select
cncldist,
count(*) as treecount2005_all,
count(*) filter (where status not like 'Dead') as treecount2005_living
from env_assets.streettrees2005
group by cncldist) as foo2
on coundist=cncldist::integer;
-- Add in data on 1995 and 2005 tree censuses to full tables
create table results_streettrees.treecensus_summaries_coundists_final as
select a.*, treeecount1995_notplantingspace,treeecount1995_notps_notstump, treecount1995_living,
treecount2005_all, treecount2005_living
from results_streettrees.treecensus2015_summaries_newcapacity_councildists a
full join results_streettrees.streettree_counts_1995_2005_coundist b on a.coundist=b.coundist;
-- Drop non-final tables created in the process above
drop table results_streettrees.max_capacity_councildist, results_streettrees.streettree_counts_1995_2005_coundist ,
results_streettrees.treecensus2015_summaries_newcapacity_councildists, results_streettrees.treecensus2015_summaries_councildists;
---------------------
-------Street Tree Metrics for Boroughs
---------------------
-- Compute 2015 Street Tree Summaries - pct living < 6", >30", most common species, total #, # Not Stump (live + dead),
-- and # Live Based on spatial overlay W/ Respective boundaries
create table results_streettrees.treecensus2015_summaries_boro as
select foo1.borocode, foo1.boroname, foo1.geom_2263,
treecount2015_all,
treecount2015_notstump,
treecount2015_alive,
spc_mostcommon_living,
round(roadlength, 2) as roadmiles,
round(treecount2015_alive::numeric/roadlength::numeric, 2) as livingtree_x_mile2015,
round(avg_dbh_living, 2) as avg_dbh_living,
med_dbh_living,
cnt_dbh_lt6_notstump,
cnt_dbh_lt6_living,
round(cnt_dbh_lt6_living::numeric/treecount2015_alive::numeric*100, 3) as pct_lt6_dbh_living,
cnt_dbh_gt30_notstump,
cnt_dbh_gt30_living,
round(cnt_dbh_gt30_living::numeric/treecount2015_alive::numeric*100, 3) as pct_gt30_dbh_living
from
(select
count(*) as treecount2015_all,
avg(tree_dbh::numeric) filter(where status like 'Alive') as avg_dbh_living,
percentile_disc(0.5) within group (order by tree_dbh::numeric) filter(where status like 'Alive') as med_dbh_living,
mode() within group (order by spc_common) filter(where status like 'Alive') as spc_mostcommon_living,
count(*) filter (where status not like 'Stump' and tree_dbh::numeric<6) as cnt_dbh_lt6_notstump,
count(*) filter (where status like 'Alive' and tree_dbh::numeric<6) as cnt_dbh_lt6_living,
count(*) filter (where status not like 'Stump' and tree_dbh::numeric>30) as cnt_dbh_gt30_notstump,
count(*) filter (where status like 'Alive' and tree_dbh::numeric>30) as cnt_dbh_gt30_living,
count(*) filter (where status not like 'Stump') as treecount2015_notstump,
count(*) filter (where status like 'Alive') as treecount2015_alive,-- Number of Trees
bnd.borocode,
bnd.boroname,
bnd.geom_2263 from
admin.nycdcp_borough_nowater bnd --borough boundaries
left join env_assets.streettrees2015 streettree2015 on st_intersects(streettree2015.geom_2263, bnd.geom_2263)
group by bnd.borocode, bnd.boroname, bnd.geom_2263) as foo1 --Join street trees based on intersect
left join
(select sum(st_length(st_intersection(roads.geom_2263, bnd.geom_2263)))::numeric/5280 as roadlength, --Length of Road miles intersecting each Community District
borocode, boroname from
admin.nycdcp_borough_nowater bnd --borough boundaries
left join infrastructure.lion16a roads on st_intersects(roads.geom_2263, bnd.geom_2263)
where --join roads based on intersect
(rw_type like ' 1' and featuretyp not like '6' and --road is street and pvt
("segmenttyp" like 'U' or "segmenttyp" like 'B' or "segmenttyp" like 'R') and --only includes features that represent roadbeds
nonped not like 'V') -- not vehicle only street (generally not places that would have street trees)
group by borocode, boroname) as foo4 on foo1.borocode=foo4.borocode;
-- Calculate Max Capacity based on Stree Tree Capacity Estimates
create table results_streettrees.max_capacity_boros as
select
boroname,
sum(st_length(geom_2263)/orig_length*max_trees) as max_trees_new
from (
select
bnd.boroname,
st_intersection(nbc.geom_2263, bnd.geom_2263) as geom_2263,
st_length(nbc.geom_2263) as orig_length,
max_trees
from
env_assets.nycdpr_blockface_capacity_20210108 nbc
left join admin.nycdcp_borough_nowater bnd
on st_intersects(nbc.geom_2263, bnd.geom_2263)) as foo
group by boroname;
-- Join Max Capacity data onto 2015 street tree summaries and calculate stocking rates
create table results_streettrees.treecensus2015_summaries_newcapacity_boro as
select tsf.*, round(max_trees_new::numeric, 0) as max_capacity_new,
round(treecount2015_notstump/round(max_trees_new::numeric, 0),4) as stockingrate_2015_notstump,
round(treecount2015_alive/round(max_trees_new::numeric, 0),4) as stockingrate_2015_living
from results_streettrees.max_capacity_boros edt
full join results_streettrees.treecensus2015_summaries_boro tsf
on edt.boroname=tsf.boroname;
--Calculate Desired Metrics for 1995 and 2005
create table results_streettrees.streettree_counts_1995_2005_boro as
select borough as boroname, treeecount1995_notplantingspace,treeecount1995_notps_notstump, treecount1995_living,
treecount2005_all, treecount2005_living from
(select
borough,
count(*) filter (where condition not like 'Planting Space') as treeecount1995_notplantingspace,
count(*) filter (where condition not like 'Planting Space' and condition not like 'Stump') as treeecount1995_notps_notstump,
count(*) filter (where condition not like 'Dead' and
condition not like 'Planting Space' and
condition not like 'Shaft' and
condition not like 'Stump') as treecount1995_living
from env_assets.streettrees1995
group by borough) as foo1
full join
(select
boroname,
count(*) as treecount2005_all,
count(*) filter (where status not like 'Dead') as treecount2005_living
from env_assets.streettrees2005
group by boroname) as foo2
on borough=boroname;
-- Add in data on 1995 and 2005 tree censuses to full tables
create table results_streettrees.treecensus_summaries_boro_final as
select a.*, treeecount1995_notplantingspace,treeecount1995_notps_notstump, treecount1995_living,
treecount2005_all, treecount2005_living
from results_streettrees.treecensus2015_summaries_newcapacity_boro a
full join results_streettrees.streettree_counts_1995_2005_boro b on a.boroname=b.boroname;
-- Drop non-final tables created in the process above
drop table results_streettrees.max_capacity_boros, results_streettrees.streettree_counts_1995_2005_boro,
results_streettrees.treecensus2015_summaries_newcapacity_boro, results_streettrees.treecensus2015_summaries_boro;