-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-DEM-GRASS.R
296 lines (214 loc) · 7.31 KB
/
process-DEM-GRASS.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
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
## Generate derivatives from a warped/re-sampled DEM. The NAD83 / UTM zone 17N (EPSG:26917) projected coordinate reference system is used.
## 2023-10-05
## D.E. Beaudette
## TODO:
## * get notes from basho, related to curvature and other terrain derivatives
## Updates:
## * 2024-03-19: starting R Studio from within GRASS no longer works
## * 2024-03-21: re-write as native GRASS commands, run there, it is simpler / faster
##
## Instructions for a happy R--GRASS experience
##
# 0. create a GRASS location/mapset in GRASS, do this by starting GRASS manually
# I typically setup dummy location/mapset in GCS WGS84
#
# E:\GRASS\gcs_wgs84\PERMANENT
# 1. open OSGeo4W-shell
# 2. start GRASS GIS, find database / location / mapset
#
# adjust paths as needed
# "c:\Program Files\QGIS 3.32.3\bin\grass83.bat" e:\GRASS\gcs_wgs84\PERMANENT
# 3. start RStudio from GRASS console
# "c:\Program Files\RStudio\bin\rstudio.exe"
# 4. interact with GRASS in GRASS GUI / console or within R using rgrass6 package
## reset WD, as it will be in "program files..."
setwd('e:/working_copies/DSS_coweeta/')
# https://cran.r-project.org/web/packages/rgrass/vignettes/use.html
library(rgrass)
## import elevation into previously created GCS WGS84 location/mapset
# https://grass.osgeo.org/grass79/manuals/r.in.gdal.html
execGRASS('r.in.gdal', flags = c('overwrite'), parameters = list(input = 'grids/elev.tif', output = 'coweeta_elev'))
## import expanded watershed boundary into new PCS location/mapset
## CRS and extent are based on this vector layer
# https://grass.osgeo.org/grass79/manuals/v.in.ogr.html
execGRASS('v.in.ogr', flags = c('overwrite'), parameters = list(input = 'vect', layer = 'coweeta_boundary_buff', output = 'boundary', type = 'boundary', location = 'coweeta'))
## exit, and restart into this new location/mapset
# "c:\Program Files\QGIS 3.32.3\bin\grass83.bat" e:\GRASS\coweeta\PERMANENT
# "c:\Program Files\RStudio\bin\rstudio.exe"
## reset WD, as it will be in "program files..."
setwd('e:/working_copies/DSS_coweeta/')
library(rgrass)
library(sf)
library(terra)
library(rasterVis)
# check: boundary should be in there
execGRASS('g.list', parameters = list(type = 'vect'))
## setup region / resolution
# https://grass.osgeo.org/grass79/manuals/g.region.html
execGRASS('g.region', flags = c('a', 'p'), parameters = list(vector = 'boundary', res = '10'))
## warp DEM to local PCS
# https://grass.osgeo.org/grass79/manuals/r.proj.html
# extent / resolution is set by the local location/mapset
execGRASS('r.proj', flags = c('overwrite'), parameters = list(location = 'gcs_wgs84', input = 'coweeta_elev', output = 'elev', method = 'bicubic'))
# check
execGRASS('g.list', parameters = list(type = 'rast'))
## contours for EDA
## watershed modeling
# https://grass.osgeo.org/grass79/manuals/r.watershed.html
# threshold is in number of cells
# note that accumulation is negative for cells affected by out-of-extent cells
execGRASS(
cmd = 'r.watershed',
flags = c('overwrite', 'b'),
parameters = list(
elevation = 'elev',
threshold = 1000,
basin = 'basins',
accumulation = 'acc',
drainage = 'drain_dir'
)
)
# files are visible now
execGRASS('g.list', parameters = list(type = 'rast'))
## extract streams using accumulation map
# https://grass.osgeo.org/grass78/manuals/r.stream.extract.html
# threshold is in number of cells
# accumulation map from r.watershed used here
execGRASS(
cmd = 'r.stream.extract',
flags = c('overwrite'),
parameters = list(
elevation = 'elev',
accumulation = 'acc',
threshold = 1000,
stream_raster = 'streams',
stream_vector = 'streams'
)
)
## alternative flow algorithm
# https://grass.osgeo.org/grass78/manuals/r.flow.html
execGRASS(
cmd = 'r.flow',
flags = c('overwrite', '3'),
parameters = list(
elevation = 'elev',
flowaccumulation = 'flowacc',
flowline = 'flowline'
)
)
## might be simpler to write / source commands directly
# r.flow -u --overwrite elevation=elev flowaccumulation=flowacc flowline=upslope_flowline
# v.out.ogr -c --overwrite input=upslope_flowline type=line output=vect/upslope_flowlines.shp format=ESRI_Shapefile
# list output
execGRASS('g.list', parameters = list(type = 'vect'))
execGRASS('g.list', parameters = list(type = 'rast'))
## check results in R, more convenient
# load raster / vector data into sp / raster objects
# GRASS raster --> terra::spatRaster
# GRASS vector --> terra::spatVector
b <- read_RAST('basins')
s <- read_VECT('streams')
# note special syntax, consider adding CATS above
fl <- read_VECT('flowline', flags = 'c')
d <- read_RAST('drain_dir')
a <- read_RAST('acc')
fa <- read_RAST('flowacc')
# remove 0-values
fa[fa <= 0] <- NA
# # note that some values are 0
# table(values(d))
#
# # remove those
# d[d < 0] <- NA
# convert to factor
.uvals <- unlist(unique(d))
levels(d) <- data.frame(ID = .uvals, name = .uvals)
# load original watershed boundaries
x <- vect('vect/Coweeta_Hydrologic_Laboratory.shp')
# watershed areas + stream network
plot(b)
lines(s)
plot(b)
lines(fl)
# original watershed boundaries
plot(b)
lines(x)
# flow direction + stream network
plot(d)
lines(s)
# note: negative accumulation values -> unreliable flow from off-grid
a.neg <- a < 0
plot(a.neg, main = 'Negative Flow Accumulation (r.watershed)')
lines(x)
# plot log10-transformed values, negative values are discarded
levelplot(
a,
scales = list(draw = FALSE),
margin = FALSE,
zscaleLog = 10,
main = 'Flow Accumulation (r.watershed)',
panel = function(...) {
panel.levelplot(...)
sp::sp.polygons(as(x, 'Spatial'), col = 'white', lwd = 1)
}
)
# plot flow direction
levelplot(
d,
# att = 'ID',
scales = list(draw = FALSE),
col.regions = brewer.pal(8, 'Spectral'),
margin = FALSE,
main = 'Flow Direction (r.watershed)',
panel = function(...) {
panel.levelplot(...)
sp::sp.polygons(as(x, 'Spatial'), col = 'black', lwd = 1)
}
)
# plot log10-transformed values, 0's have been set to NA
levelplot(
fa,
scales = list(draw = FALSE),
margin = FALSE,
zscaleLog = 10,
main = 'Flow Accumulation (r.watershed)',
panel = function(...) {
panel.levelplot(...)
sp::sp.polygons(as(x, 'Spatial'), col = 'white', lwd = 1)
}
)
## export from GRASS directly to GeoTiff
# https://grass.osgeo.org/grass79/manuals/r.out.gdal.html
# paths are relative to the working directory in R
execGRASS(
cmd = 'r.out.gdal',
flags = c('overwrite', 'c', 'm'),
parameters = list(
input = 'elev',
output = 'grids/elev_pcs.tif',
format = 'GTiff',
createopt = 'COMPRESS=LZW'
)
)
# check: looks right
gdal_utils(util = 'info', source = 'grids/elev_pcs.tif')
## export from R, in this case grids we have modified
## the same kind of modifications can be done in GRASS
## and are typically much more efficient
writeRaster(d, file = 'grids/drain_dir.tif', overwrite = TRUE)
writeRaster(fa, file = 'grids/flowacc.tif', overwrite = TRUE)
write_sf(st_as_sf(fl), dsn = 'vect/flowlines.shp', overwrite = TRUE)
# double-check on data type: CELL --> signed integer, Int16
execGRASS('r.info', parameters = list(map = 'drain_dir'))
execGRASS(
cmd = 'r.out.gdal',
flags = c('overwrite', 'c', 'm'),
parameters = list(
input = 'drain_dir',
output = 'grids/drain_dir.tif',
format = 'GTiff',
createopt = 'COMPRESS=LZW'
)
)
# ok
gdal_utils(util = 'info', source = 'grids/drain_dir.tif')