generated from ncss-tech/NASIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
figures.Rmd
332 lines (252 loc) · 11.2 KB
/
figures.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
---
title: "figures.Rmd"
author: "Stephen Roecker"
date: "10/20/2020"
output: html_document
editor_options:
chunk_output_type: console
---
# plot example of methods
```{r}
library(soilDB)
library(aqp)
library(mpspline2)
library(ggplot)
vars <- c("pedon_key", "hzn_top", "hzn_bot", "ph_h2o")
int <- c(0, 30, 100, 200)
mid <- round(rowMeans(cbind(int, c(int[-1], NA))))
int2 <- sort(c(mid, mid + 1))
f <- fetchKSSL("Miami")
idx <- site(f)$pedon_key %in% "12696"
test <- f[idx, ]
test2 <- segment(test, intervals = 0:150)
test_df <- cbind(method = "original data", horizons(test2)[vars[-3]])
# point estimate
f_p <- segment(test, intervals = int2)
idx <- which(horizons(f_p)$hzn_top %in% mid)
f_p <- f_p[, idx]
pd_df <- cbind(method = "point sample", horizons(f_p)[vars[-3]])
# spline estimate
test_s <- mpspline(horizons(test)[vars], var_name = "ph_h2o", d = int, vlow = min(test$ph_h2o), vhigh = max(test$ph_h2o))
mps_df <- data.frame(method = "spline estimate",
pedon_key = test_s[[1]]$pedon_key,
hzn_top = 1:length(test_s[[1]]$est_1cm),
ph_h2o = test_s[[1]]$est_1cm
)
# mps_df <- mps_df[mps_df$hzn_top %in% mid, vars[-3]]
# combine
test_c <- rbind(test_df, pd_df, mps_df)
# plot
dp_gg <- ggplot(test_df, aes(y = ph_h2o, x = hzn_top, col = method)) +
geom_line(lwd = 1.5) +
geom_line(data = mps_df, aes(y = ph_h2o, x = hzn_top, method = "spline"), lwd = 1.5, linetype = "longdash") +
# geom_point(data = pd_df, aes(y = ph_h2o, x = hzn_top), size = 3.5) +
scale_x_continuous(breaks = int) +
scale_color_manual(values = c("blue", "orange")) +
coord_flip(xlim = c(150, 0)) +
#xlim(150, 0) +
xlab("depth interval (cm)") + ylab("pH") +
ggtitle("Depth plot") +
theme_bw()
ggsave(dp_gg, filename = "test.png", device = "png", width = 6, height = 5)
```
# estimate CONUS extent
```{r}
library(soilassessment)
library(rgdal)
library(raster)
setwd("D:/geodata/project_data/gsp-sas/deliverables/maps/CONUS/")
f <- c(PH_030 = "USA840_CONUS_pHMap030.tif",
EC_030 = "USA840_CONUS_SalinityMap030.tif",
ESP_030 = "USA840_CONUS_ESPMap030.tif",
PH_100 = "USA840_CONUS_pHMap30100.tif",
EC_100 = "USA840_CONUS_SalinityMap30100.tif",
ESP_100 = "USA840_CONUS_ESPMap30100.tif",
nlcd = "D:/geodata/project_data/gsp-sas/1km covariates/Other/nlcd_2016_conus.tif"
)
rs <- stack(f)
rs <- as(rs, "SpatialGridDataFrame")
# classify salt severity
rs$scl_030 <- aqp:::.rank_salts(rs$EC_030, rs$PH_030, rs$ESP_030)
rs$sco_030 <- as.integer(rs$scl_030)
rs$scl_100 <- aqp:::.rank_salts(rs$EC_100, rs$PH_100, rs$ESP_100)
rs$sco_100 <- as.integer(rs$scl_100)
writeGDAL(rs["sco_030"], fname = "C:/workspace2/gsp_sas_030.tif", type = "Int32", mvFlag = 0)
writeGDAL(rs["sco_100"], fname = "C:/workspace2/gsp_sas_100.tif", type = "Int32", mvFlag = 0)
# extract cropland
rs$cropland <- ifelse(rs$nlcd %in% c(81, 82), 1, 0)
rs$land <- ifelse(! rs$nlcd %in% c(11, 0), 1, 0)
# reproject
rs2 <- projectRaster(readAll(stack(rs)), crs = CRS("+init=epsg:5070"), method = "ngb")
rs2 <- as(rs2, "SpatialGridDataFrame")
rs2$scl_030 <- aqp:::.codify_salt_severity (as.integer(rs2$sco_030))
rs2$scl_100 <- aqp:::.codify_salt_severity(as.integer(rs2$sco_100))
lv <- c(31, 52, 71, 81, 82, 1000)
lb <- c("Barren Land", "Shrub/Scrub", "Grassland/Herbacesous", "Pasture/Hay", "Cultivated Crops", "Other")
rs2$nlcd2 <- ifelse(rs2$nlcd %in% c(0, 11), NA, rs2$nlcd)
rs2$nlcd2 <- ifelse(! rs2$nlcd2 %in% lv, 1000, rs2$nlcd2)
rs2$nlcd2 <- factor(rs2$nlcd2, levels = lv, labels = lb)
rs2_cellsize <- slot(rs2, "grid")@cellsize
acres <- rs2_cellsize[1] * rs2_cellsize[2] * 0.0002471
hectares <- rs2_cellsize[1] * rs2_cellsize[2] * 0.0001
# calculate acres
tb_030 <- table(rs2$scl_030)
tb_100 <- table(rs2$scl_100)
prettyNum(tb_030 * acres, big.mark = ",", scientific = FALSE)
prettyNum(tb_100 * acres, big.mark = ",", scientific = FALSE)
# nlcd acres
tb_030_nlcd <- table(rs2$nlcd2, rs2$scl_030)
tb_100_nlcd <- table(rs2$nlcd2, rs2$scl_100)
tb_030_nlcd_df <- reshape(as.data.frame(tb_030_nlcd),
direction = "wide",
idvar = "Var1",
timevar = "Var2",
v.names = c("Freq")
)
tb_030_nlcd_df <- tb_030_nlcd_df[order(- tb_030_nlcd_df$`Freq.very strongly saline`), ]
tb_030_nlcd_df[-1] <- sapply(tb_030_nlcd_df[-1], function(x) {
prettyNum(x * acres, big.mark = ",", scientific = FALSE)
})
names(tb_030_nlcd_df)[1] <- "NLCD Class"
names(tb_030_nlcd_df)[-1] <- gsub("Freq.", "", names(tb_030_nlcd_df[-1]))
tb_100_nlcd_df <- reshape(as.data.frame(tb_100_nlcd),
direction = "wide",
idvar = "Var1",
timevar = "Var2",
v.names = c("Freq")
)
tb_100_nlcd_df <- tb_100_nlcd_df[order(- tb_100_nlcd_df$`Freq.very strongly saline`), ]
tb_100_nlcd_df[-1] <- sapply(tb_100_nlcd_df[-1], function(x) {
prettyNum(x * acres, big.mark = ",", scientific = FALSE)
})
names(tb_100_nlcd_df)[1] <- "NLCD Class"
names(tb_100_nlcd_df)[-1] <- gsub("Freq.", "", names(tb_100_nlcd_df[-1]))
# cropland acres
idx_nlcd <- ! rs2$nlcd %in% c(0, 11)
idx_crop <- rs2$nlcd %in% c(81, 82)
idx_fao <- ! rs2$nlcd %in% c(0, 11) & (! rs2$scl_030 %in% c("nonsaline") | ! rs2$scl_100 %in% c("nonsaline"))
idx_usda <- ! rs2$nlcd %in% c(0, 11) & (! rs2$scl_030 %in% c("nonsaline", "slightly saline") | ! rs2$scl_100 %in% c("nonsaline", "slightly saline"))
# total saline hectares & acres (fao)
prettyNum(sum(table(rs2$scl_030[idx_nlcd])) * hectares, big.mark = ",", scientific = FALSE)
prettyNum(sum(table(rs2$scl_030[idx_fao])) * hectares, big.mark = ",", scientific = FALSE, digits = 0)
293588043 / 762849852 * 100
prettyNum(sum(table(rs2$scl_030[idx_nlcd])) * acres, big.mark = ",", scientific = FALSE, digits = 0)
prettyNum(sum(table(rs2$scl_030[idx_fao])) * acres, big.mark = ",", scientific = FALSE, digits = 0)
715820269 / 1885001984 * 100
# total saline hectares acres (usda)
prettyNum(sum(table(rs2$scl_030[idx_usda])) * hectares, big.mark = ",", scientific = FALSE, digits = 0)
67869025 / 762849852 * 100
prettyNum(sum(table(rs2$scl_030[idx_usda])) * acres, big.mark = ",", scientific = FALSE, digits = 0)
167704360 / 1885001984 * 100
# total cropland saline hectares & acres (fao)
prettyNum(sum(table(rs2$scl_030[idx_crop])) * hectares, big.mark = ",", scientific = FALSE)
prettyNum(sum(table(rs2$scl_030[idx_crop & idx_fao])) * hectares, big.mark = ",", scientific = FALSE)
63875528 / 198995822 * 100
prettyNum(sum(table(rs2$scl_030[idx_crop])) * acres, big.mark = ",", scientific = FALSE, digits = 0)
prettyNum(sum(table(rs2$scl_030[idx_crop & idx_fao])) * acres, big.mark = ",", scientific = FALSE, digits = 0)
157836431/ 491718677 * 100
# total cropland saline hectares acres (usda)
prettyNum(sum(table(rs2$scl_030[idx_crop & idx_usda])) * hectares, big.mark = ",", scientific = FALSE, digits = 0)
8012528 / 198995822 * 100 # hectares
prettyNum(sum(table(rs2$scl_030[idx_crop & idx_usda])) * acres, big.mark = ",", scientific = FALSE, digits = 0)
19798956 / 491718677 * 100 # acres
```
# test soilassessment R package
```{r}
test <- expand.grid(
EC = sort(sapply(c(0, 0.75, 2, 4, 8, 15, 30), function(x) x + c(0, -0.05, 0.05))),
pH = c(8.1, 8.2, 8.3, 8.4, 8.5, 8.6),
ESP = sort(sapply(c(0, 15, 30, 50, 70, 100), function(x) x + c(0, 0.1, -0.1)))
)
EC = test$EC; pH = test$pH; ESP = test$ESP
EC = 4.5; pH = 8.5; ESP = 0
test$test1 <- classCode(saltSeverity(EC, pH, ESP, method = "FAO"), "saltseverity")
test$test2 <- .salt_severity(EC = EC, pH, ESP)
test$test1 <- reorder(factor(test$test1), as.integer(test$test2))
table(test$test2, test$test1, useNA = "always")
View(test)
```
# Map
```{r}
library(sf)
library(ggplot2)
library(USAboundaries)
# library(gmap)
load(file = "C:/Users/stephen.roecker/Nextcloud/projects/2020_gsp-sas/LDM-compact_20200709.RData")
st <- us_states()
st <- subset(st, !state_name %in% c("Alaska", "Hawaii", "Puerto Rico"))
st <- st_transform(st, crs = 5070)
# conus <- read_sf(dsn = "D:/geodata/soils/SSURGO_CONUS_FY19.gdb", layer = "SAPOLYGON")
# conus <- st_transform(conus, crs = 4326)
# conus$state <- substr(conus$AREASYMBOL, 1, 2)
# # conus <- conus %>% group_by(state) %>% summarize(state = state[1])
# conus <- aggregate(conus, list(conus$state))
# bb <- st_bbox(conus)
# bb <- make_bbox(lon = bb[c(3, 1)], lat = bb[c(2, 4)])
# gmap <- get_map(bb, maptype = "terrain", source = "osm")
s <- s_mps_sf
s <- st_transform(s_mps_sf, crs = 5070)
idx <- st_intersects(s, st)
idx <- sapply(idx, function(x) length(x) > 0)
s <- s[idx, ]
s <- within(s, {
property = NA
property = ifelse(!is.na(ph_ptf.000_030_cm), "pH", property)
property = ifelse(complete.cases(ph_ptf.000_030_cm, s$esp.000_030_cm), "pH & ESP", property)
property = ifelse(complete.cases(ph_ptf.000_030_cm, esp.000_030_cm, ec_ptf.000_030_cm), "pH & ESP & EC", property)
})
s$property <- factor(s$property, ordered = TRUE)
s <- subset(s, !is.na(property))
s <- s[order(s$property), ]
# s_t <- as.data.frame(cbind(st_coordinates(s), pH = s$ph_ptf.000_030_cm))
#
# brks <- c(0, 3.5, 4.5, 5.1, 5.6, 6.1, 6.6, 7.4, 7.9, 8.5, 9.5, 14)
# s_t$ph_lev <- cut(s_t$pH,
# breaks = brks,
# labels = paste(brks[-length(brks)], brks[-1], sep = "-")
# )
gg_gsp <- ggplot() +
geom_sf(data = st, fill = NA) +
geom_sf(data = s, aes(col = property), size = 0.2) +
# scale_color_manual(values = c("blue", "orange")) +
guides(color = guide_legend(override.aes = list(size = 2))) +
scale_fill_viridis_d() +
# ggtitle("Pedon locations") +
theme_bw()
ggsave(gg_gsp, file = "C:/workspace2/pedon_location.png", dpi = 300)
```
# deliverable stats
```{r}
setwd("D:/geodata/project_data/gsp-sas/deliverables/maps/CONUS/")
f <- c(PH_030 = "USA840_CONUS_pHMap030.tif",
EC_030 = "USA840_CONUS_SalinityMap030.tif",
ESP_030 = "USA840_CONUS_ESPMap030.tif",
PH_100 = "USA840_CONUS_pHMap30100.tif",
EC_100 = "USA840_CONUS_SalinityMap30100.tif",
ESP_100 = "USA840_CONUS_ESPMap30100.tif",
SAS_030 = "USA840_CONUS_SaltMap030.tif",
SAS_100 = "USA840_CONUS_SaltMap30100.tif",
nlcd = "D:/geodata/project_data/gsp-sas/1km covariates/Other/nlcd_2016_conus.tif"
)
rs <- rast(f[7:8])
rs <- project(rs, "EPSG:5070", method = "near", rs = 1000)
val <- rs[[1]] |> values() |> as.integer()
table(val)
```
```{r}
# estimate CONUS extent
````{r}
setwd("D:/geodata/project_data/gsp-sas/deliverables/maps/CONUS/")
f <- c(PH_030 = "USA840_CONUS_pHMap030.tif",
EC_030 = "USA840_CONUS_SalinityMap030.tif",
ESP_030 = "USA840_CONUS_ESPMap030.tif",
PH_100 = "USA840_CONUS_pHMap30100.tif",
EC_100 = "USA840_CONUS_SalinityMap30100.tif",
ESP_100 = "USA840_CONUS_ESPMap30100.tif",
SAS_030 = "USA840_CONUS_SaltMap030.tif",
SAS_100 = "USA840_CONUS_SaltMap30100.tif",
nlcd = "D:/geodata/project_data/gsp-sas/1km covariates/Other/nlcd_2016_conus.tif"
)
rs <- rast(f[7:8])
rs <- project(rs, "EPSG:5070", method = "near", res = 1000, use_gdal = TRUE, threads = TRUE)
````