-
Notifications
You must be signed in to change notification settings - Fork 0
/
NRT_Depth.R
137 lines (103 loc) · 3.95 KB
/
NRT_Depth.R
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
####################################################
### Filename: NRT_Depth.R
####################################################
### Description:
## Checks data inputs and package requirements
## for depth estimation functions
##
### Author:
## Perry Oddo [perry.oddo@nasa.gov]
##
### Last Modified:
## 7.19.18
####################################################
# Initialize
source("Scripts/Initialize.R")
### Polygonize Raster
# Set Parameters
flood_poly = run_qgis(alg = "gdalogr:polygonize",
INPUT = flood_tiff,
OUTPUT = "Layers/flood_poly.shp",
load_output = TRUE)
### Select flooded attributes (DN = 1)
run_qgis(alg = "qgis:extractbyattribute",
INPUT = flood_poly,
FIELD = "DN",
OPERATOR = "=",
VALUE = 1,
OUTPUT = "Layers/flood_select.shp",
load_output = FALSE)
### Read in shapefile and reclassify with unique DN
flood_select = readOGR("Layers/flood_select.shp")
npoly = length(flood_select$DN)
flood_select@data$DN = 1:npoly
writeOGR(flood_select, dsn = "C:/Users/poddo/Documents/GitHub/NRT_Depth/flood_select.shp",
layer = "flood_select",
driver = "ESRI Shapefile",
overwrite_layer = T)
### Convert polygon to polyline
flood_line = run_qgis(alg = "qgis:polygonstolines",
INPUT = flood_select,
OUTPUT = file.path(tempdir(), "flood_line.shp"),
load_output = TRUE)
### Create points along line
run_qgis(alg = "qgis:createpointsalonglines",
Lines = flood_line,
Distance = "100",
Startpoint = "0",
Endpoint = "0",
output = "Layers/flood_chain.shp",
load_output = FALSE)
### Extract raster data from overlying polygon
flood_chain = readOGR("Layers/flood_chain.shp")
dem = raster("Data/DEM/srtm_23_09.tif")
result <- extract(dem, flood_chain, fun = mean, na.rm=TRUE, df=TRUE)
### Reproject dem
run_qgis(alg = "gdalogr:warpreproject",
INPUT = "Data/DEM/srtm_23_09.tif",
DEST_SRS = "EPSG:32620",
OUTPUT = "Layers/dem_proj.tif")
### Append result to .dbf file of oringinal points
out.dbf <- read.dbf("Layers/flood_chain.dbf", as.is = TRUE)
out.dbf$Elevation <- result$srtm_23_09
write.dbf(out.dbf, "Layers/flood_chain.dbf")
### Produce Triangular Iterpolated network
# Find extent of shape
run_qgis(alg = "qgis:polygonfromlayerextent",
INPUT_LAYER = flood_chain,
BY_FEATURE = "False",
OUTPUT = "Layers/extent.shp")
extent = readOGR("Layers/extent.shp")
extent_box = bbox(extent)
# Subset by flooded shape
flood_chain = readOGR("Layers/flood_chain.shp")
# Loop over each shape to produce a TIN
# for(i in 1:npoly){
run_qgis(alg = "saga:triangulation",
SHAPES = flood_chain,
FIELD = 'Elevation',
OUTPUT_EXTENT = sprintf("%s,%s,%s,%s", extent_box[1], extent_box[3], extent_box[2], extent_box[4]),
TARGET_USER_SIZE = "90",
TARGET_USER_FITS = "0",
TARGET_OUT_GRID = "Layers/tin_out.tif")
TARGET_OUT_GRID = paste0("Layers/tin_out_", i, ".tif")
#}
### Use raster calculator to subtract from underlying DEM
# Clip dem to extent
run_qgis(alg = "saga:clipgridwithpolygon",
INPUT = "Layers/dem_proj.tif",
POLYGONS = "Layers/extent.shp",
OUTPUT = "Layers/dem_clip.tif")
# Raster calculator
run_qgis(alg = "saga:rastercalculator",
GRIDS = "Layers/tin_out.tif",
XGRIDS = "Layers/dem_clip.tif",
FORMULA = "a-b",
USE_NODATA = "False",
TYPE = "7",
RESULT = "Results/depth_full.tif")
### Clip depth raster to flood polygon
run_qgis(alg = "saga:clipgridwithpolygon",
INPUT = "Results/depth_full.tif",
POLYGONS = "Layers/flood_select.shp",
OUTPUT = "Results/depth_clip.tif")