-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-soils-data.Rmd
416 lines (307 loc) · 10.4 KB
/
get-soils-data.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
---
title: "Coweeta Raster Soil Survey"
author: "D.E. Beaudette"
date: "`r Sys.Date()`"
output:
html_document:
mathjax: null
jquery: null
smart: no
number_sections: yes
toc: yes
toc_depth: 3
toc_float:
collapsed: true
smooth_scroll: false
---
```{r setup, echo=FALSE, results='hide', warning=FALSE}
library(knitr, quietly=TRUE)
opts_chunk$set(
message = FALSE,
warning = FALSE,
background = '#F7F7F7',
fig.align = 'center',
dev = 'png',
fig.retina = 2,
tidy = FALSE,
verbose = FALSE
)
options(width=100, stringsAsFactors=FALSE)
```
```{r}
# latest from GitHub
library(soilDB)
library(sharpshootR)
library(dendextend)
# wrangling polygons and CRS transformations
library(sp)
# library(rgdal)
# geometric calculations
library(rgeos)
# this is the future, will eventually replace much of
# sp, rgdal, rgeos methods used here
library(sf)
# raster data visualization
library(raster)
library(rasterVis)
library(viridis)
# soil classification
library(aqp)
## objectives:
# * get current soils data via SDA / 30m gSSURGO mukey grid
# * select a representative soil "type" (component) per MU
# * re-sample to the DEM grid
# * develop soil hydraulic parameter files for each soil type
## get an expanded AOI defined by basins
x <- read_sf('vect/coweeta_boundary_buff.shp')
# basins
b <- read_sf('vect/Coweeta_Hydrologic_Laboratory.shp')
# points
p <- read_sf('vect/Soil_Moisture_Sites_CW.shp')
## request the 30m mukey grid for this AOI
mu <- mukey.wcs(aoi = b, db = 'gssurgo')
# transform to AEA CRS used by gSSURGO grid
x <- st_transform(x, st_crs(mu))
b <- st_transform(b, st_crs(mu))
p <- st_transform(p, st_crs(mu))
```
```{r fig.width=8, fig.height=10}
# check: ok
levelplot(
mu, att = 'ID',
main = 'Coweeta\ngSSURGO Grid (WCS) + Expanded AOI',
margin = FALSE,
colorkey = FALSE,
col.regions = viridis,
scales = list(draw = FALSE),
panel = function(...) {
panel.levelplot(...)
sp.lines(as(b, 'Spatial'), col = 'white', lwd = 2)
sp.points(as(p, 'Spatial'), col = 'white', pch = 16)
}
)
```
```{r fig.width=8, fig.height=10}
## TODO: apply a reasonable MASK... what defines that?
## develop a "soil type" grid: component selection
# get unique mukeys from grid
keys <- levels(mu)[[1]]$ID
# get component data for these map units
# components must not be 'Miscellaneous area' and have hz data
is <- format_SQL_in_statement(keys)
qq <- sprintf("
SELECT DISTINCT mukey, component.cokey, compname, comppct_r, compkind, drainagecl, taxpartsize
FROM
component
JOIN chorizon on component.cokey = chorizon.cokey
WHERE mukey IN %s
AND comppct_r IS NOT NULL
AND compkind != 'Miscellaneous area'
ORDER BY component.mukey, cokey, comppct_r DESC;
", is)
mu.data <- SDA_query(qq)
# check: are there any map unit keys without soils data?
(missing.keys <- setdiff(keys, unique(mu.data$mukey)))
## TODO: double-check to see if this even matters
SDA_query(sprintf("SELECT mukey, cokey, compname, comppct_r FROM component WHERE mukey IN %s;", format_SQL_in_statement(missing.keys)))
# iterate over components, select the largest non-misc. area
m <- split(mu.data, mu.data$mukey)
s <- lapply(m, function(i) {
# Misc. areas have been removed
i <- i[order(i$comppct_r, decreasing = TRUE)[1], ]
})
s <- do.call('rbind', s)
row.names(s) <- NULL
## dominant component data to MU grid via RAT
ll <- levels(mu)[[1]]
# merge RAT + soil type data
# missing data in the soil type DF will result in NA
rat <- merge(ll, s[, c('mukey', 'compname', 'cokey', 'comppct_r', 'drainagecl', 'taxpartsize')], by.x = 'ID', by.y = 'mukey', sort = FALSE, all.x = TRUE)
# fill missing records with a NODATA + codes for finding these later if it matters
missing.idx <- which(is.na(rat$cokey))
if(length(missing.idx) > 0) {
rat$compname[missing.idx] <- 'NODATA'
rat$cokey[missing.idx] <- '0'
}
# re-pack RAT
levels(mu) <- rat
# compute rough estimates of acreage
# convert to unique codes associated with soil type (soil series for now)
soiltype <- deratify(mu, att = 'compname')
drainagecl <- deratify(mu, att = 'drainagecl')
taxpartsize <- deratify(mu, att = 'taxpartsize')
```
```{r fig.width=10, fig.height=8}
## pixel-based estimates of area (largest, non-misc. area)
a <- data.frame(
levels(soiltype)[[1]],
freq(soiltype)
)
a <- merge(a, mu.data, by = 'compname', all.x = TRUE)
a$coweeta_ac <- a$count * (a$comppct_r / 100) * (mean(res(soiltype))^2) * 0.000247105
a$id <- toupper(a$compname)
a <- aggregate(coweeta_ac ~ id, data = a, FUN = sum)
a$coweeta_ac <- round(a$coweeta_ac)
a <- a[order(a$coweeta_ac, decreasing = TRUE), ]
osds <- fetchOSD(a$id)
osds$hzd <- hzDistinctnessCodeToOffset(osds$distinctness)
site(osds) <- a[, c('id', 'coweeta_ac')]
```
```{r fig.width=16, fig.height=7}
SoilTaxonomyDendrogram(osds, width = 0.35, name.style = 'center-center', y.offset = 0.4, hz.distinctness.offset = 'hzd', shrink = TRUE)
```
```{r fig.width=15, fig.height=6}
par(mar = c(0, 0, 0, 3))
plotSPC(osds, width = 0.35, name.style = 'center-center', hz.distinctness.offset = 'hzd', shrink = TRUE, plot.order = order(osds$coweeta_ac, decreasing = TRUE), cex.depth.axis = 0.8, cex.names = 0.7)
axis(side = 1, at = 1:length(osds), labels = osds$coweeta_ac[order(osds$coweeta_ac, decreasing = TRUE)], line = -5, cex.axis = 0.75)
mtext('Approximate Acres within Coweeta', side = 1, line = -2.5, at = 0.5, adj = 0)
```
```{r fig.width=10, fig.height=8}
# check: ok
levelplot(
soiltype, att = 'compname',
main = 'Coweeta\ngSSURGO Grid (WCS) + Expanded AOI',
margin = FALSE,
colorkey = list(space = 'right'),
col.regions = viridis,
scales = list(draw = FALSE),
panel = function(...) {
panel.levelplot(...)
sp.lines(as(b, 'Spatial'), col = 'white', lwd = 2)
# sp.lines(as(x, 'Spatial'), col = 'white', lwd = 1, lty = 3)
}
)
# check: ok
levelplot(
drainagecl, att = 'drainagecl',
main = 'Coweeta\ngSSURGO Grid (WCS) + Expanded AOI',
margin = FALSE,
scales = list(draw = FALSE),
colorkey = list(space = 'right'),
col.regions = viridis,
panel = function(...) {
panel.levelplot(...)
sp.lines(as(b, 'Spatial'), col = 'white', lwd = 2)
# sp.lines(as(x, 'Spatial'), col = 'white', lwd = 1, lty = 3)
}
)
# check: ok
levelplot(
taxpartsize, att = 'taxpartsize',
main = 'Coweeta\ngSSURGO Grid (WCS) + Expanded AOI',
margin = FALSE,
colorkey = list(space = 'right'),
col.regions = rev(RColorBrewer::brewer.pal(n = 8, name = 'Spectral')),
scales = list(draw = FALSE),
panel = function(...) {
panel.levelplot(...)
sp.lines(as(b, 'Spatial'), col = 'black', lwd = 2)
# sp.lines(as(x, 'Spatial'), col = 'white', lwd = 1, lty = 3)
}
)
```
```{r fig.width=10, fig.height=8}
## save
writeRaster(soiltype, file = 'grids/soiltype-soil-series.tif', options = 'COMPRESS=LZW', overwrite = TRUE, datatype = 'INT1U')
```
```{r fig.width=10, fig.height=8}
## develop representative soil type data
## TODO: using components or KSSL, or both?
## develop a soil texture "soil type" map 0-50cm
# get thematic data from SDA
# dominant component
# depth-weighted average
# sand, silt, clay, AWC (RV)
p <- get_SDA_property(property = c("sandtotal_r","silttotal_r","claytotal_r", "awc_r"),
method = "Dominant Component (Numeric)",
mukeys = ll$ID,
top_depth = 0,
bottom_depth = 50)
# check
head(p)
# re-create raster attribute table with aggregate soil properties
rat <- merge(ll, p, by.x = 'ID', by.y = 'mukey', sort = FALSE, all.x = TRUE)
# re-pack RAT
levels(mu) <- rat
# convert raster + RAT --> stack of values
s <- deratify(mu, att = c("sandtotal_r", "silttotal_r", "claytotal_r", "awc_r"))
# graphical check
levelplot(
s[[1:3]],
main = 'Sand, Silt, Clay (RV) 0-50cm\nDominant Component (Numeric)',
margin = FALSE,
scales = list(draw = FALSE),
col.regions = viridis
)
levelplot(
s[[4]],
main = 'AWC (RV) 0-50cm\nDominant Component (Numeric)',
margin = FALSE,
scales = list(draw = FALSE),
col.regions = viridis
)
# convert to a representative soil texture class
txt.lut <- read.csv('http://soilmap2-1.lawr.ucdavis.edu/800m_grids/RAT/texture_2550.csv')
# make a copy
texture_050 <- s[[1]]
# note: soil textures that aren't present are dropped from factor levels
texture_050[] <- ssc_to_texcl(sand = s$sandtotal_r[], clay = s$claytotal_r[])
# extract RAT
rat <- levels(texture_050)[[1]]
# add colors
rat <- merge(rat, txt.lut, by.x = 'VALUE', by.y = 'class', all.x = TRUE, sort = FALSE)
# fix column order
rat <- rat[, c('ID', 'VALUE', 'hex', 'names')]
# re-pack
levels(texture_050) <- rat
# check: ok
cols <- levels(texture_050)[[1]]$hex
levelplot(
texture_050,
att = 'names', col.regions = cols,
main = 'Soil Texture (fine earth fraction) 0-50cm\nDominant Component (Numeric)',
margin = FALSE,
scales = list(draw = FALSE),
panel = function(...) {
panel.levelplot(...)
sp.lines(as(b, 'Spatial'), col = 'black', lwd = 2)
# sp.lines(as(x, 'Spatial'), col = 'white', lwd = 1, lty = 3)
}
)
```
```{r fig.width=10, fig.height=8}
## save
writeRaster(texture_050, file = 'grids/soiltype-texture_050.tif', options = 'COMPRESS=LZW', overwrite = TRUE, datatype = 'INT1U')
```
```{r fig.width=10, fig.height=8}
## be sure to specify a depth range
# get unique mukeys from grid
keys <- levels(mu)[[1]]$ID
s <- get_SDA_property(property = 'Available Water Capacity - Rep Value', method = 'Weighted Average', mukeys = keys, top_depth = 0, bottom_depth = 50)
# get unique mukeys from grid
ll <- levels(mu)[[1]]
# convert into an SQL "IN" statement
IS <- format_SQL_in_statement(ll$ID)
# query SDA by mukey
sql <- sprintf("SELECT mukey, aws025wta, aws050wta, aws0100wta, aws0150wta FROM muaggatt WHERE mukey IN %s", IS)
tab <- SDA_query(sql)
# merge AWS by mukey into raster attribute table (RAT)
rat <- merge(ll, tab, by.x = 'ID', by.y = 'mukey', sort = FALSE, all.x = TRUE)
# re-pack RAT
levels(mu) <- rat
(aws <- deratify(mu, att = 'aws0150wta'))
levelplot(
aws,
main = 'Available Water Storage 0-150cm (cm)\ngSSURGO WCS + SDA',
margin = FALSE,
scales = list(draw = FALSE),
col.regions = viridis,
panel = function(...) {
panel.levelplot(...)
sp.lines(as(b, 'Spatial'), col = 'white', lwd = 2)
}
)
```
```{r fig.width=8, fig.height=6}
hist(aws, breaks = 20, las = 1, main = 'Available Water Storage 0-150cm (cm)\ngSSURGO WCS + SDA', cex.axis = 0.66)
```