-
Notifications
You must be signed in to change notification settings - Fork 1
/
MidtermCalgaryCombined.Rmd
1181 lines (919 loc) · 41.3 KB
/
MidtermCalgaryCombined.Rmd
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Calgary and Minneapolis Inundation Modeling"
author: "Kyle McCarthy, Tristan Grupp"
date: "3/30/2021"
output:
html_document:
keep_md: true
toc: true
theme: cosmo
toc_float: true
code_folding: hide
number_sections: FALSE
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Introduction
Knowledge of how and where waters move across the landscape following is necessary to provide a wealth of information for resource managers and local planning organizations.Modeling is necessary to predict where flooding is expected to occur before it happens. Flooding events often cost municipalities thousands of dollars in damage, reduce community health and protect water quality.Planners, environmental scientists, and community members are able to utilize these models to fortify infrastructure in high risk flooding ares, predict nutrient and pollutant levels in streams, reduce fluctuations in various water bodies and improve stormwater infrastructure.
This model will incorporate a variety of data sets to including land use type, demographic data, tree canopy, soil type, and distance to water body.Inundation modeling helps those trying to fortify infrastructure, provide information on flow and flood frequency, predict nutrient and pollutant levels in surface water to understand the impact on regional water quality, and reduce elevation fluctuations in various water bodies.
For this report, we utilize our model to predict flood inundation in Calgary and Minneapolis to test our model's validity. To accomplish this goal, we divide each city into grid cells and classify each city as "inundated" or "not inundated" based on past events or DEM flood inundation models. Utilizing the variables specified in the paragraph above, our model identifies the common characteristics associated with "inundated" areas or "not inundated" areas. We then split our data into a training set and test set. The training set identifies the common characteristics of the data, and the test set predicts weather a grid cell will be inundated or not inundated based on the training set characteristic. Based on how many grid cells the training set predicted correctly, we can evaluate the effectiveness of our model.
```{r setup, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE}
library(caret)
library(pscl)
library(plotROC)
library(pROC)
library(sf)
library(sp)
library(tidyverse)
library(knitr)
library(kableExtra)
library(FNN)
library(lwgeom)
mapTheme <- function(base_size = 12) {
theme(
text = element_text( color = "black"),
plot.title = element_text(size = 14,colour = "black"),
plot.subtitle=element_text(face="italic"),
plot.caption=element_text(hjust=0),
axis.ticks = element_blank(),
panel.background = element_blank(),axis.title = element_blank(),
axis.text = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=2)
)
}
root.dir = "https://raw.githubusercontent.com/urbanSpatial/Public-Policy-Analytics-Landing/master/DATA/"
source("https://raw.githubusercontent.com/urbanSpatial/Public-Policy-Analytics-Landing/master/functions.r")
```
## Exploratory Analysis
This section includes our exploratory analysis. Variables explored included percent imperviousness, distance from streams, tree canopy cover, soil cover, percent open space, assessed property values, and infrastructure density.
```{r ImportData, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE}
sr <- "+proj=utm +zone=12 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
Calgary <- st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/midTermProject_Data/CALGIS_CITYBOUND_LIMIT/CALGIS_CITYBOUND_LIMIT.shp")%>%
#st_read("C:/Users/Tristan/Desktop/PennSpring2021/CPLN675/midterm/midTermProject_Data/CALGIS_CITYBOUND_LIMIT/CALGIS_CITYBOUND_LIMIT.shp")%>%
st_transform(crs = sr)
imperviousness <- st_read("https://data.calgary.ca/resource/i9mt-qafb.geojson")%>%
st_transform(crs = sr)
LandUse <-
st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/LandUse.geojson")%>%
# st_read("C:/Users/Tristan/Desktop/PennSpring2021/CPLN675/midterm/Land Use Districts.geojson")%>%
st_transform(crs = sr)%>%
filter(major == "Parks, Recreation and Public Education" | major == "Residential - Low Density")
PropVal <- st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/midTermProject_Data/PropVal.geojson")%>%
#st_read("C:/Users/Tristan/Desktop/PennSpring2021/CPLN675/midterm/PropVal.geojson")%>%
st_transform(crs = sr)
treecanopy <- st_read("C:/Users/Tristan/Desktop/PennSpring2021/CPLN675/midterm/calgarytreecover/calgarytreecover_repair.shp") %>%
st_transform(crs = sr) %>%
st_make_valid
inundation <- st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/inundation.shp")%>%
# st_read("C:/Users/Tristan/Desktop/PennSpring2021/CPLN675/midterm/midTermProject_Data/inundationpoly.shp")%>%
st_transform(crs = sr)%>%
mutate(Label = ifelse(gridcode == 1 | gridcode == 2, "No Flooding", "Flooding")) %>%
st_make_valid()%>%
st_intersection(Calgary)
imperviousness <- st_read("https://data.calgary.ca/resource/i9mt-qafb.geojson")%>%
st_transform(crs = sr)
fishnet <-
st_make_grid(Calgary, cellsize = 500) %>%
st_sf() %>%
mutate(uniqueID = rownames(.))%>%
st_transform(crs = sr) %>% st_make_valid()
MinnBoundary <- st_read("https://opendata.arcgis.com/datasets/89f1a70c0cf24d7692e2d02fdf8f4e47_0.geojson")%>%
st_transform('ESRI:102293')
# Minneapolis is a smaller area, so a smaller Grid Cell size was used to make the cross validation comparable
fishnetM <-
st_make_grid(MinnBoundary, cellsize = 250) %>%
st_sf() %>%
mutate(uniqueID = rownames(.))%>%
st_transform('ESRI:102293') %>% st_make_valid()
minSales <- st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/New folder/minsale1.shp")%>%
st_transform('ESRI:102293')
ZonalWaterMin <- st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/New folder/ZonalWaterMin.dbf")
imperviousM <- st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/New folder/impervious.shp")%>%
st_transform('ESRI:102293')%>%
st_make_valid()
openSpace <- st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/New folder/OpenSpace.shp")%>%
st_transform('ESRI:102293')%>%
st_make_valid()
E911 <- st_read("https://opendata.arcgis.com/datasets/da0d8e48df10478bb2160977f988a113_0.geojson")%>% st_transform('ESRI:102293')%>%
st_make_valid()
FloodM <- st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/Flood/fldhaz.shp")%>%
st_transform('ESRI:102293')%>%
st_intersection(MinnBoundary)
palette1 <- c("#f2e9e4","#c9ada7","#9a8c98","#4a4e69","#22223b")
palette2 <- c("#0294A1", "#B5F8FE")
palette3 <- c("#FFA866", "#F17007", "#43C4DB", "#004CFF")
```
### Exploring Inundation
```{r Contours from DEM related to Fishnet, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE, fig.width = 10}
inundation_fishnet <-
inundation%>%
filter(gridcode == 1 | gridcode == 2)%>%
st_intersection(fishnet, inundation)%>%
mutate(Area = as.numeric(st_area(.)))%>%
st_drop_geometry()%>%
group_by(uniqueID)%>%
summarise(InundationArea = sum(Area))%>%
left_join(fishnet, .)%>%
mutate(pctInundation = InundationArea / 250000)%>%
mutate_all(funs(replace_na(.,0)))%>%
mutate(Inundated = ifelse(pctInundation > .15, 1, 0))%>%
mutate(Label = ifelse(Inundated == 1, "Inundated", "Not Inundated"))
inundation_fishnet_minn <-
FloodM%>%
st_intersection(fishnetM)%>%
mutate(Area = as.numeric(st_area(.)))%>%
st_drop_geometry()%>%
group_by(uniqueID)%>%
summarise(AreaIn = sum(Area))%>%
mutate(pctInundated = AreaIn / 62500 * 100)%>%
mutate(Inundated = ifelse(pctInundated > 20, 1, 0))%>%
left_join(fishnetM, .)%>%
mutate_all(funs(replace_na(.,0)))%>%
mutate(Label = ifelse(Inundated == 1, "Inundated", "Not Inundated"))
grid.arrange(ncol = 2,
ggplot()+
geom_sf(data = inundation_fishnet, aes(fill = Label))+
scale_fill_manual(values = c("#0294A1", "#B5F8FE"))+
labs(title = "Calgary Flood Inundation Map",
subtitle = "Inundated Grid Cells are > 50% inundated")+
theme(legend.position = "bottom")+
mapTheme(),
ggplot()+
geom_sf(data = inundation_fishnet_minn, aes(fill = Label))+
scale_fill_manual(values = c("#0294A1", "#B5F8FE"))+
labs(title = "Minneapolis Flood Inundation Map",
subtitle = "Inundated Grid Cells are > 50% inundated")+
theme(legend.position = "bottom")+
mapTheme())
# inundation_fishnetw <-
# inundation_fishnet %>%
# st_centroid()%>%
# st_make_valid()%>%
# st_join(contours)%>%
# st_drop_geometry()%>%
# left_join(fishnet, .)
# ggplot(inundation_fishnetw, aes(ContourMin))+
# geom_bar()+
# labs(title = "Count of Inundated Gridcells by Elevation")+
# xlab("Elevation")+
# ylab("Count of Inundated Gridcells")
#
# ggplot()+
# geom_sf(data = contours, aes(fill = ContourMin))+
# labs(title = "Calgary Flood Inundation Map")+
# theme(legend.position = "bottom")+
# mapTheme()
```
### Distance to Water
```{r ImportData, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE, fig.width = 12, fig.height = 12}
# Writing out fishnet as shapefile to perform zonal statistics as table on a euclidean distance raster from Calgary's water features
# Reading zonal statistics Shapefile
inundation_fishnet <- st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/ZonalWater.dbf")%>%
#st_read("C:/Users/Tristan/Downloads/CPLN-675-Midterm--main/Data/ZonalWater.dbf")%>%
dplyr::select(uniqueID, MEAN)%>%
left_join(inundation_fishnet, .)%>%
na.omit()
inundation_fishnet_minn <-
ZonalWaterMin%>%
dplyr:: select(uniqueID, MEAN)%>%
left_join(inundation_fishnet_minn, .)%>%
na.omit()
grid.arrange(ncol = 2,
ggplot() +
geom_sf(data = inundation_fishnet, aes(fill = q5(MEAN)))+
scale_fill_manual(values = palette1,
labels = qBr(inundation_fishnet, "MEAN"),
name = "Grid Cell Mean")+
labs(title = "Calgary Distance From Stream (Meters)")+
theme(legend.position = "bottom")+
mapTheme(),
ggplot()+
geom_bar(data = inundation_fishnet, aes(x = Label, y = MEAN, fill = Label), stat="identity")+
scale_fill_manual(values = palette2)+
ylab("Average Distance From Strea")+
xlab("Inundated Vs. Not Inundated")+
labs(title = "Calgary Average Distance From Stream by Inundation")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"),
legend.position = "none"),
ggplot() +
geom_sf(data = Inundation_fishnet_minn, aes(fill = q5(MEAN)))+
scale_fill_manual(values = palette1,
labels = qBr(inundation_fishnet, "MEAN"),
name = "Grid Cell Mean")+
labs(title = "Minneapolis Distance From Stream (Meters)")+
theme(legend.position = "bottom")+
mapTheme(),
ggplot()+
geom_bar(data = Inundation_fishnet_minn, aes(x = Label, y = MEAN, fill = Label), stat="identity")+
scale_fill_manual(values = palette2)+
ylab("Average Distance From Strea")+
xlab("Inundated Vs. Not Inundated")+
labs(title = "Minneapolis Average Distance From Stream by Inundation")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"),
legend.position = "none"))
```
### Percent Impervious
```{r Imperviousness, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE, fig.width = 12, fig.height = 12}
# The Intersection Here Takes a While -- est. 20 min
Impervious <- st_read("C:/Users/Kyle McCarthy/Documents/GitHub/CPLN-675-Midterm-/Data/Impervious/Imperv_cal.geojson")%>%
st_transform(crs = sr)
# The Intersection Here Takes a While -- est. 20 min
inundation_fishnet <-
Impervious%>%
filter(gen_surface == "Gravel" | gen_surface == "Buildings" | gen_surface == "Pavement" | gen_surface == "Bridge" | gen_surface == "Roads (Pavement)" )%>%
st_make_valid()%>%
st_intersection(fishnet)%>%
mutate(Area = as.numeric(st_area(.)))%>%
st_drop_geometry()%>%
group_by(uniqueID)%>%
summarise(Area = sum(Area))%>%
mutate(pctImpervious = Area / 250000)%>%
left_join(inundation_fishnet, .)%>%
mutate(Imperv = ifelse(pctImpervious > 0.50, "Pervious", "Impermeable"))%>%
mutate_all(funs(replace_na(.,0)))
inundation_fishnet<-
inundation_fishnet%>%
mutate(Imperv = ifelse(pctImpervious > 0.50, "Impervious", "Permeable"),
pctImpervious = pctImpervious * 100)
Impervioustats <-
inundation_fishnet%>%
dplyr::select(pctImpervious, Label)%>%
mutate_all(funs(replace_na(.,0)))%>%
group_by(Label)%>%
summarise(pctImperv = mean(pctImpervious))
inundation_fishnet_minn <-
imperviousM %>%
st_intersection(inundation_fishnet_minn)%>%
mutate(Area = as.numeric(st_area(.)))%>%
st_drop_geometry()%>%
group_by(uniqueID)%>%
summarise(impervArea = sum(Area))%>%
left_join(inundation_fishnet_minn, .)%>%
mutate(pctImpervious = impervArea / 62500 * 100)%>%
mutate_all(funs(replace_na(.,0)))
ImpervioustatMinns <-
inundation_fishnet_minn %>%
dplyr::select(pctImpervious, Label)%>%
mutate_all(funs(replace_na(.,0)))%>%
group_by(Label)%>%
summarise(pctImperv = mean(pctImpervious))
grid.arrange(ncol = 2,
# Group for Graph
ggplot() +
geom_sf(data = inundation_fishnet, aes(fill = q5(pctImpervious)))+
scale_fill_manual(values = palette1,
labels = qBr(inundation_fishnet, "pctImpervious"),
name = "Percent Impervious")+
labs(title = " Calgrary - Percent Impervious in each Grid Cell")+
theme(legend.position = "bottom")+
theme(plot.title = element_text(hjust = 0.5))+mapTheme(),
ggplot()+
geom_bar(data = Impervioustats, aes(x = Label, y = pctImperv, fill = Label), stat="identity")+
scale_fill_manual(values = palette2)+
ylab("Average Percent Impervious")+
xlab("Inundated Grid Cells Vs. Not Inundated Grid Cells")+
labs(title = "Calgary Percent Impervious")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"),
legend.position = "none"),
ggplot() +
geom_sf(data = inundation_fishnet_minn , aes(fill = q5(pctImpervious)))+
scale_fill_manual(values = palette1,
labels = qBr(inundation_fishnet, "pctImpervious"),
name = "Percent Impervious")+
labs(title = " Minneapolis - Percent Impervious in each Grid Cell")+
theme(legend.position = "bottom")+
theme(plot.title = element_text(hjust = 0.5))+mapTheme(),
ggplot()+
geom_bar(data = ImpervioustatMinns, aes(x = Label, y = pctImperv, fill = Label), stat="identity")+
scale_fill_manual(values = palette2)+
ylab("Average Percent Impervious")+
xlab("Inundated Grid Cells Vs. Not Inundated Grid Cells")+
labs(title = "Minneapolis Percent Impervious")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"),
legend.position = "none"))
```
### Infrastructure Density
```{r Building Denisty, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE, fig.height = 12, fig.width = 12}
inundation_fishnet <-st_read("C:/Users/Kyle McCarthy/Documents/GitHub/CPLN-675-Midterm-/Data/Buildings.geojson")%>%
#st_read("C:/Users/Tristan/Desktop/PennSpring2021/CPLN675/midterm/Buildings.geojson")%>%
st_centroid()%>%
st_transform(crs = sr)%>%
mutate(BuildingCount = 1) %>%
dplyr::select(BuildingCount)%>%
aggregate(., inundation_fishnet, sum)%>%
mutate_all(funs(replace_na(.,0)))%>%
mutate(uniqueID = rownames(.))%>%
st_drop_geometry()%>%
left_join(inundation_fishnet, .)
graph <-
inundation_fishnet%>%
na.omit()%>%
mutate(BuildingCount = as.integer(BuildingCount))
BuidlingStats<-
inundation_fishnet%>%
st_drop_geometry()%>%
na.omit()%>%
group_by(Label)%>%
summarise(BuildingSum = sum(BuildingCount),
BuildingCountAverage = mean(BuildingCount))
inundation_fishnet_minn <-E911%>%
mutate(BuildingCount = 1) %>%
dplyr::select(BuildingCount)%>%
aggregate(., inundation_fishnet_minn, sum)%>%
mutate_all(funs(replace_na(.,0)))%>%
mutate(uniqueID = rownames(.))%>%
st_drop_geometry()%>%
left_join(inundation_fishnet_minn, .)
BuidlingStatsMinn<-
inundation_fishnet_minn%>%
st_drop_geometry()%>%
na.omit()%>%
group_by(Label)%>%
summarise(BuildingSum = sum(BuildingCount),
BuildingCountAverage = mean(BuildingCount))
grid.arrange(ncol = 2,
ggplot() +
geom_sf(data = graph, aes(fill = q5(BuildingCount)))+
scale_fill_manual(values = palette1,
labels = qBr(graph, "BuildingCount"),
name = "Percent Impervious")+
labs(title = " Calgrary - Number of Buildings in each Grid Cell")+
theme(legend.position = "bottom")+
theme(plot.title = element_text(hjust = 0.5))+mapTheme(),
ggplot()+
geom_bar(data = BuidlingStats, aes(x = Label, y = BuildingCountAverage, fill = Label), stat="identity")+
scale_fill_manual(values = palette2)+
ylab("Average Building Count")+
xlab("Inundated Grid Cells Vs. Not Inundated Grid Cells")+
labs(title = "Calgary Building Count")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"),
legend.position = "none"),
ggplot() +
geom_sf(data = inundation_fishnet_minn, aes(fill = q5(BuildingCount)))+
scale_fill_manual(values = palette1,
labels = qBr(inundation_fishnet_minn, "BuildingCount"),
name = "Percent Impervious")+
labs(title = " Minneapolis - Number of Buildings in each Grid Cell")+
theme(legend.position = "bottom")+
theme(plot.title = element_text(hjust = 0.5))+mapTheme(),
ggplot()+
geom_bar(data = BuidlingStatsMinn, aes(x = Label, y = BuildingCountAverage, fill = Label), stat="identity")+
scale_fill_manual(values = palette2)+
ylab("Average Building Count")+
xlab("Inundated Grid Cells Vs. Not Inundated Grid Cells")+
labs(title = "Minneapolis Building Count")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"),
legend.position = "none"))
```
### Property Value
```{r assessedValue, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE, fig.height = 12, fig.width = 12}
st_c <- st_coordinates
inundation_fishnet <-
PropVal %>%
mutate(assessed_value = as.numeric(assessed_value))%>%
dplyr::select(assessed_value)%>%
aggregate(., inundation_fishnet, mean)%>%
mutate_all(funs(replace_na(.,0)))%>%
mutate(uniqueID = rownames(.))%>%
st_drop_geometry()%>%
left_join(inundation_fishnet, .)%>%
na.omit()
PropValStats <-
inundation_fishnet%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Label)%>%
summarise(assessed_value = sum(assessed_value))
inundation_fishnet_minn <-
minSales %>%
mutate(assessed_value = as.numeric(USER_ADJ_S))%>%
dplyr::select(assessed_value)%>%
aggregate(., inundation_fishnet_minn, mean)%>%
mutate_all(funs(replace_na(.,0)))%>%
mutate(uniqueID = rownames(.))%>%
st_drop_geometry()%>%
left_join(inundation_fishnet_minn, .)%>%
na.omit()
PropValStatsMin <-
inundation_fishnet_minn%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Label)%>%
summarise(assessed_value = sum(assessed_value))
grid.arrange(ncol = 2,
ggplot() +
geom_sf(data = inundation_fishnet , aes(fill = q5(assessed_value)))+
scale_fill_manual(values = palette1,
labels = qBr(inundation_fishnet , "assessed_value"),
name = "Average Property Value")+
labs(title = " Calgrary - Average Property Value")+
theme(legend.position = "bottom")+
theme(plot.title = element_text(hjust = 0.5))+mapTheme(),
ggplot()+
geom_bar(data = PropValStats, aes(x = Label, y = assessed_value, fill = Label), stat="identity")+
scale_fill_manual(values = palette2)+
ylab("Average Property Value")+
xlab("Inundated Grid Cells Vs. Not Inundated Grid Cells")+
labs(title = "Calgary Property Values")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"),
legend.position = "none"),
ggplot() +
geom_sf(data = inundation_fishnet_minn , aes(fill = q5(assessed_value)))+
scale_fill_manual(values = palette1,
labels = qBr(inundation_fishnet , "assessed_value"),
name = "Average Property Value")+
labs(title = " Calgrary - Average Property Value")+
theme(legend.position = "bottom")+
theme(plot.title = element_text(hjust = 0.5))+mapTheme(),
ggplot()+
geom_bar(data = PropValStatsMin, aes(x = Label, y = assessed_value, fill = Label), stat="identity")+
scale_fill_manual(values = palette2)+
ylab("Average Property Value")+
xlab("Inundated Grid Cells Vs. Not Inundated Grid Cells")+
labs(title = "Calgary Property Values")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"),
legend.position = "none"))
```
### Percent Open Space
```{r LandUse, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE, fig.height = 12, fig.width = 12}
inundation_fishnet <-
LandUse%>%
st_make_valid()%>%
st_intersection(inundation_fishnet)%>%
mutate(landuArea = as.numeric(st_area(.)))%>%
st_drop_geometry()%>%
group_by(uniqueID)%>%
summarise(landuArea = sum(landuArea))%>%
left_join(inundation_fishnet, .)%>%
mutate(pctLandCover = landuArea / 250000)
inundation_fishnet<-
inundation_fishnet%>%
mutate(pctLandCover = pctLandCover * 100)
inundation_fishnet <-
inundation_fishnet %>%
mutate_all(funs(replace_na(.,0)))
LandStats <-
inundation_fishnet %>%
mutate_all(funs(replace_na(.,0)))%>%
group_by(Label)%>%
summarise(Rec_low = mean(pctLandCover))
inundation_fishnet_minn <-
openSpace %>%
st_intersection(inundation_fishnet_minn)%>%
mutate(Area = as.numeric(st_area(.)))%>%
st_drop_geometry()%>%
group_by(uniqueID)%>%
summarise(OpenArea = sum(Area))%>%
left_join(inundation_fishnet_minn, .)%>%
mutate(pctOpen = OpenArea / 62500 * 100)%>%
mutate_all(funs(replace_na(.,0)))
OpenStatMinns <-
inundation_fishnet_minn %>%
dplyr::select(pctOpen, Label)%>%
mutate_all(funs(replace_na(.,0)))%>%
group_by(Label)%>%
summarise(pctOpen = mean(pctOpen))
grid.arrange(ncol = 2,
ggplot() +
geom_sf(data = inundation_fishnet , aes(fill = pctLandCover))+
scale_fill_viridis()+
labs(title = "Minneapolis - Percent Open Space")+
theme(legend.position = "bottom")+
theme(plot.title = element_text(hjust = 0.5),
legend.title = element_blank())+mapTheme(),
ggplot()+
geom_bar(data = LandStats, aes(x = Label, y = Rec_low, fill = Label), stat="identity")+
scale_fill_manual(values = palette2)+
ylab("Average Building Count")+
xlab("Inundated Grid Cells Vs. Not Inundated Grid Cells")+
labs(title = "Calgary Average Open Space by Inundation")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"),
legend.position = "none"),
ggplot() +
geom_sf(data = inundation_fishnet_minn , aes(fill = pctOpen))+
scale_fill_viridis()+
labs(title = "Minneapolis - Percent Open Space")+
theme(legend.position = "bottom")+
theme(plot.title = element_text(hjust = 0.5),
legend.title = element_blank())+mapTheme(),
ggplot()+
geom_bar(data = OpenStatMinns, aes(x = Label, y = pctOpen, fill = Label), stat="identity")+
scale_fill_manual(values = palette2)+
ylab("Average Building Count")+
xlab("Inundated Grid Cells Vs. Not Inundated Grid Cells")+
labs(title = "Minneapolis Average Open Space by Inundation")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"),
legend.position = "none"))
```
### Tree Canopy
```{r Tree Canopy Fishnet and Significance Check, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE}
inundation_fishnet <-
treecanopy %>%
st_intersection(inundation_fishnet, treecanopy) %>%
mutate(Area = as.numeric(st_area(.))) %>%
st_drop_geometry()%>%
group_by(uniqueID)%>%
summarise(TreeCanopyArea = sum(Area)) %>%
left_join(inundation_fishnet, .)%>%
mutate(pctTreeCanopy = TreeCanopyArea / 250000)%>%
mutate_all(funs(replace_na(.,0)))%>%
mutate(Treed = ifelse(pctTreeCanopy > .10, 1, 0))%>%
mutate(Label = ifelse(Treed == 1, "Treed", "Not Treed"))
TreeStats <-
inundation_fishnet%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Inundated)%>%
summarise(pctCanopy = mean(pctTreeCanopy))
ggplot()+
geom_sf(data = inundation_fishnet, aes(fill=pctTreeCanopy))+
scale_fill_gradient(low = "black", high = "green")+
labs(title="Calgary Tree Canopy Percent of Fishnets")+
theme(legend.position = "bottom")+
mapTheme()
#Significance Quick Bar Chart Check
ggplot()+
geom_bar(data = TreeStats, aes(x = Inundated, y = pctCanopy), stat="identity", fill = "blue")
```
```{r River Order and River Distances Significance Check, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE}
# Preprocessing steps in ArcMap:
inundation_fishnet_rivsoil<- st_read("C:/Users/Tristan/Desktop/PennSpring2021/CPLN675/midterm/soilCalgary/inundation_fishnet_riverorderdistance_soil.shp")%>%
st_transform(crs = sr)
#Significance Quick Bar Chart Check
#variable 1: RiverDist (distance to all rivers)
RiverDistStats <-
inundation_fishnet_rivsoil%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Label)%>%
summarise(rivDist1 = mean(RiverDist))
ggplot()+
geom_bar(data = RiverDistStats, aes(x = Label, y = rivDist1), stat="identity", fill = "blue")
#variable 2: RiverDist1 (distance to River Order of 1; Bow River)
RiverOrder1DistStats <-
inundation_fishnet_rivsoil%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Label)%>%
summarise(rivDist1 = mean(RiverDist1))
ggplot()+
geom_bar(data = RiverOrder1DistStats, aes(x = Label, y = rivDist1), stat="identity", fill = "blue")
#variable 3: RiverDist2 (distance to River Order of 2)
RiverOrder2DistStats <-
inundation_fishnet_rivsoil%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Label)%>%
summarise(rivDist2 = mean(RiverDist2))
ggplot()+
geom_bar(data = RiverOrder2DistStats, aes(x = Label, y = rivDist2), stat="identity", fill = "blue")
#variable 4: RiverDist3 (distance to River Order of 3)
RiverOrder3DistStats <-
inundation_fishnet_rivsoil%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Label)%>%
summarise(rivDist3 = mean(RiverDist3))
ggplot()+
geom_bar(data = RiverOrder3DistStats, aes(x = Label, y = rivDist3), stat="identity", fill = "blue")
#variable 5: TempDist (temporary/ephemeral streams)
RiverTempDistStats <-
inundation_fishnet_rivsoil%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Label)%>%
summarise(rivTemp = mean(TempDist))
ggplot()+
geom_bar(data = RiverTempDistStats, aes(x = Label, y = rivTemp), stat="identity", fill = "blue")
```
### Soil Analysis
```{r Soil Significance Check, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE}
# Preprocessing steps in ArcMap:
#Significance Quick Bar Chart Check
#Variable 1: MEAN_SMnum
SoilComplexStats <-
inundation_fishnet_rivsoil%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Label)%>%
summarise(soilComplex = mean(MEAN_SMnum))
ggplot()+
geom_bar(data = SoilComplexStats, aes(x = Label, y = soilComplex), stat="identity", fill = "blue")
#Variable 2: MEAN_SMn_1 (simplifed of MEAN_SMnum)
SoilSimpleStats <-
inundation_fishnet_rivsoil%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Label)%>%
summarise(soilSimple = mean(MEAN_SMn_1))
ggplot()+
geom_bar(data = SoilSimpleStats, aes(x = Label, y = soilSimple), stat="identity", fill = "blue")
#Variable 3: SoilRiv
SoilRiverStats <-
inundation_fishnet_rivsoil%>%
na.omit()%>%
st_drop_geometry()%>%
group_by(Label)%>%
summarise(soilSimple = mean(SoilRiv))
ggplot()+
geom_bar(data = SoilRiverStats, aes(x = Label, y = soilSimple), stat="identity", fill = "blue")
inundation_fishnet_rivsoil_nogeometry <-
inundation_fishnet_rivsoil %>%
st_drop_geometry()
inundation_fishnet_merge <-
merge(x=inundation_fishnet,y=inundation_fishnet_rivsoil_nogeometry, by.x='uniqueID', by.y='uniquID')
inundation_fishnet_merge <-
inundation_fishnet_merge %>%
st_make_valid()
inundation <-
inundation %>%
st_make_valid()
inundation_fishnet_merge2 <-
inundation%>%
filter(gridcode == 1 | gridcode == 2)%>%
st_intersection(inundation_fishnet_merge, inundation)%>%
mutate(Area = as.numeric(st_area(.)))%>%
st_drop_geometry()%>%
group_by(uniqueID)%>%
summarise(InundationArea = sum(Area))%>%
left_join(fishnet, .)%>%
mutate(pctInundation = InundationArea / 250000)%>%
mutate_all(funs(replace_na(.,0)))%>%
mutate(Inundated = ifelse(pctInundation > .15, 1, 0))%>%
mutate(Label = ifelse(Inundated == 1, "Inundated", "Not Inundated"))
st_write(inundation_fishnet_merge2,
"C:/Users/Tristan/Desktop/PennSpring2021/CPLN675/midterm/midTermProject_Data/inundation_fishnet_merge2.shp")
inundation_fishnet_merge2 <-
inundation_fishnet_merge2 %>%
st_drop_geometry()
undesired <- c('pctInundated', 'Inundated', 'InundationArea', 'Label')
inundation_fishnet_merge <- inundation_fishnet_merge %>%
select(-one_of(undesired))
inundation_fishnet_merge <- subset(inundation_fishnet_merge, select = -c('pctImpervious', 'Inundated', 'InundationArea', 'Label'))
inundation_fishnet_merge <-
merge(x=inundation_fishnet_merge,y=inundation_fishnet_merge2, by.x='uniqueID', by.y='uniqueID')
inundation_fishnet_merge<- st_read("C:/Users/Kyle McCarthy/Documents/CPLN 675/Midterm/fishnet2/inundation_fishnet_finalmerge.shp")
```
## Model
```{r model, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE}
inundation_Fish <-
inundation_fishnet_merge %>%
dplyr::select (MEAN, pctImpr, BldngCn, assssd_, pctLndC, pctTrCn, SoilRiv, Inundatd)
#dplyr::select(pctImpervious, landuArea, pctTreeCanopy, TempDist, MEAN_SMnum, SoilRiv, BuildingCount, assessed_value, MEAN, Inundated)
inundation_Fish$Inundatd <- as.factor(inundation_Fish$Inundatd)
set.seed(3456)
trainIndex <- createDataPartition(inundation_Fish$Inundatd, p = .70,
list = FALSE,
times = 1)
preserveTrain <- inundation_Fish[ -trainIndex,]
preserveTest <- inundation_Fish[-trainIndex,]
typeof(inundation_Fish)
preserveModel <- glm(Inundatd ~ .,
family="binomial"(link="logit"), data = preserveTrain %>%
as.data.frame() %>%
dplyr::select(-geometry))
summary(preserveModel)
```
```{r minn model, message=FALSE, warning=FALSE, include=TRUE, results='hide', cache=TRUE}
inundation_fishnet_minn1<-
inundation_fishnet_minn%>%
dplyr::select("MEAN","pctImpervious", "BuildingCount", "assessed_value", "pctOpen", "Inundated" )
inundation_fishnet_minn1$Inundated <- as.factor(inundation_fishnet_minn1$Inundated)
set.seed(3456)
trainIndex <- createDataPartition(inundation_fishnet_minn1$Inundated, p = .70,
list = FALSE,
times = 1)
InundationTrain <- inundation_fishnet_minn1[ -trainIndex,]
InundationTest <- inundation_fishnet_minn1[-trainIndex,]
InundationModel <- glm(Inundated ~ .,
family="binomial"(link="logit"), data = InundationTrain %>%
as.data.frame() %>%
dplyr::select(-geometry))
summary(InundationModel)
```
### Confusion Matrix and ROC Curve
```{r Model Validation, message=FALSE, warning=FALSE, include=TRUE, cache=TRUE, fig.height = 8, fig.width = 8}
classProbs <- predict(preserveModel, preserveTest, type="response")
testProbs <- data.frame(obs = as.numeric(preserveTest$Inundatd),
pred = classProbs)
```
``````{r Model Validation Minn, Min message=FALSE, warning=FALSE, include=TRUE, cache=TRUE, fig.height = 8, fig.width = 8}
classProbsM <- predict(InundationModel, InundationTest, type="response")
testProbsM <- data.frame(obs = as.numeric(InundationTest$Inundated),
pred = classProbsM)
```
```{r ROC, message=FALSE, warning=FALSE, include=TRUE, cache=TRUE, fig.height = 7, fig.width = 7}
testProbs$predClass = ifelse(testProbs$pred > 0.5 , 1,0)
testProbs <-
testProbs %>%
mutate(obs = ifelse(obs == 2, 1, 0))
x <- caret::confusionMatrix(reference = as.factor(testProbs$obs),
data = as.factor(testProbs$predClass),
positive = "1")
draw_confusion_matrix <- function(cm) {
layout(matrix(c(1,1,2)))
par(mar=c(2,2,2,2))
plot(c(100, 345), c(300, 450), type = "n", xlab="", ylab="", xaxt='n', yaxt='n')
title('CONFUSION MATRIX CALGARY', cex.main=2)
# create the matrix
rect(150, 430, 240, 370, col='#3F97D0')
text(195, 435, 'Not Inundated', cex=1.2)
rect(250, 430, 340, 370, col='#F7AD50')
text(295, 435, 'Inundated', cex=1.2)
text(125, 370, 'Predicted', cex=1.3, srt=90, font=2)
text(245, 450, 'Actual', cex=1.3, font=2)
rect(150, 305, 240, 365, col='#F7AD50')
rect(250, 305, 340, 365, col='#3F97D0')
text(140, 400, 'Not Inundated', cex=1.2, srt=90)
text(140, 335, 'Inundated', cex=1.2, srt=90)
# add in the cm results
res <- as.numeric(cm$table)
text(195, 400, res[1], cex=1.6, font=2, col='white')
text(195, 335, res[2], cex=1.6, font=2, col='white')
text(295, 400, res[3], cex=1.6, font=2, col='white')
text(295, 335, res[4], cex=1.6, font=2, col='white')
# add in the specifics
plot(c(100, 0), c(100, 0), type = "n", xlab="", ylab="", main = "DETAILS", xaxt='n', yaxt='n')
text(10, 85, names(cm$byClass[1]), cex=1.2, font=2)
text(10, 70, round(as.numeric(cm$byClass[1]), 3), cex=1.2)
text(30, 85, names(cm$byClass[2]), cex=1.2, font=2)
text(30, 70, round(as.numeric(cm$byClass[2]), 3), cex=1.2)
text(50, 85, names(cm$byClass[5]), cex=1.2, font=2)
text(50, 70, round(as.numeric(cm$byClass[5]), 3), cex=1.2)
text(70, 85, names(cm$byClass[6]), cex=1.2, font=2)
text(70, 70, round(as.numeric(cm$byClass[6]), 3), cex=1.2)
text(90, 85, names(cm$byClass[7]), cex=1.2, font=2)
text(90, 70, round(as.numeric(cm$byClass[7]), 3), cex=1.2)
# add in the accuracy information
text(30, 35, names(cm$overall[1]), cex=1.5, font=2)
text(30, 20, round(as.numeric(cm$overall[1]), 3), cex=1.4)
text(70, 35, names(cm$overall[2]), cex=1.5, font=2)
text(70, 20, round(as.numeric(cm$overall[2]), 3), cex=1.4)
}
testProbsM$predClass = ifelse(testProbsM$pred > 0.5 , 1,0)
testProbsM <-
testProbsM %>%
mutate(obs = ifelse(obs == 2, 1, 0))
y <- caret::confusionMatrix(reference = as.factor(testProbsM$obs),
data = as.factor(testProbsM$predClass),
positive = "1")
draw_confusion_matrix2 <- function(cm) {
layout(matrix(c(1,1,2)))
par(mar=c(2,2,2,2))
plot(c(100, 345), c(300, 450), type = "n", xlab="", ylab="", xaxt='n', yaxt='n')
title('CONFUSION MATRIX CALGARY', cex.main=2)
# create the matrix
rect(150, 430, 240, 370, col='#3F97D0')
text(195, 435, 'Not Inundated', cex=1.2)
rect(250, 430, 340, 370, col='#F7AD50')
text(295, 435, 'Inundated', cex=1.2)
text(125, 370, 'Predicted', cex=1.3, srt=90, font=2)
text(245, 450, 'Actual', cex=1.3, font=2)
rect(150, 305, 240, 365, col='#F7AD50')
rect(250, 305, 340, 365, col='#3F97D0')
text(140, 400, 'Not Inundated', cex=1.2, srt=90)
text(140, 335, 'Inundated', cex=1.2, srt=90)
# add in the cm results
res <- as.numeric(cm$table)
text(195, 400, res[1], cex=1.6, font=2, col='white')
text(195, 335, res[2], cex=1.6, font=2, col='white')
text(295, 400, res[3], cex=1.6, font=2, col='white')
text(295, 335, res[4], cex=1.6, font=2, col='white')
# add in the specifics
plot(c(100, 0), c(100, 0), type = "n", xlab="", ylab="", main = "DETAILS", xaxt='n', yaxt='n')
text(10, 85, names(cm$byClass[1]), cex=1.2, font=2)
text(10, 70, round(as.numeric(cm$byClass[1]), 3), cex=1.2)
text(30, 85, names(cm$byClass[2]), cex=1.2, font=2)
text(30, 70, round(as.numeric(cm$byClass[2]), 3), cex=1.2)
text(50, 85, names(cm$byClass[5]), cex=1.2, font=2)
text(50, 70, round(as.numeric(cm$byClass[5]), 3), cex=1.2)