-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeomorphBySoilSeries-SSURGO.R
272 lines (226 loc) · 10 KB
/
geomorphBySoilSeries-SSURGO.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
## TODO: remove all of these in the next major release
##
## Note: these queries are not very efficient because of the use of WHERE UPPER(compname) IN ", in.statement, "
## this doesn't properly utilize existing indexes
##
## related issue
# https://github.com/ncss-tech/sharpshootR/issues/12
# s: vector of soil series names
# replaceNA: convert missing categories into 0 probabilities
geomPosMountainProbability <- function(s, replaceNA=TRUE) {
.Deprecated(msg = 'This function is now deprecated, consider using soilDB::fetchSDA() or soilDB::fetchOSD(..., extended = TRUE)')
# format IN statement, convert to upper case for comp name normalization
in.statement <- format_SQL_in_statement(toupper(s))
# format query
q <- paste("
SELECT a.compname, q_param, q_param_n, total, round(q_param_n / total, 2) AS p
FROM
(
SELECT UPPER(compname) AS compname, geomposmntn as q_param, CAST(count(geomposmntn) AS numeric) AS q_param_n
FROM legend
INNER JOIN mapunit mu ON mu.lkey = legend.lkey
INNER JOIN component AS co ON mu.mukey = co.mukey
LEFT JOIN cogeomordesc ON co.cokey = cogeomordesc.cokey
LEFT JOIN cosurfmorphgc on cogeomordesc.cogeomdkey = cosurfmorphgc.cogeomdkey
WHERE
legend.areasymbol != 'US'
AND UPPER(compname) IN ", in.statement, "
AND geomposmntn IS NOT NULL
GROUP BY UPPER(compname), geomposmntn
) AS a
JOIN
(
SELECT UPPER(compname) AS compname, CAST(count(compname) AS numeric) AS total
FROM legend
INNER JOIN mapunit AS mu ON mu.lkey = legend.lkey
INNER JOIN component AS co ON mu.mukey = co.mukey
LEFT JOIN cogeomordesc ON co.cokey = cogeomordesc.cokey
LEFT JOIN cosurfmorphgc on cogeomordesc.cogeomdkey = cosurfmorphgc.cogeomdkey
WHERE
legend.areasymbol != 'US'
AND UPPER(compname) IN ", in.statement, "
AND geomposmntn IS NOT NULL
GROUP BY UPPER(compname)
) AS b
ON a.compname = b.compname
ORDER BY compname, p DESC;", sep='')
# perform query
x <- SDA_query(q)
# re-level
x$q_param <- factor(x$q_param, levels=c('Mountaintop', 'Mountainflank', 'Upper third of mountainflank', 'Center third of mountainflank', 'Lower third of mountainflank', 'Mountainbase'))
# convert from long-wide format
y <- dcast(x, compname ~ q_param, value.var='p', drop=FALSE)
# optionally convert NA to 0
if(replaceNA) {
for(i in 1:nrow(y)) {
idx <- which(is.na(y[i, ]))
y[i, ][idx] <- 0
}
}
return(y)
}
# s: vector of soil series names
# replaceNA: convert missing categories into 0 probabilities
geomPosHillProbability <- function(s, replaceNA=TRUE) {
.Deprecated(msg = 'This function is now deprecated, consider using soilDB::fetchSDA() or soilDB::fetchOSD(..., extended = TRUE)')
# format IN statement, convert to upper case for comp name normalization
in.statement <- format_SQL_in_statement(toupper(s))
# format query
q <- paste("
SELECT a.compname, q_param, q_param_n, total, round(q_param_n / total, 2) AS p
FROM
(
SELECT UPPER(compname) AS compname, geomposhill as q_param, CAST(count(geomposhill) AS numeric) AS q_param_n
FROM legend
INNER JOIN mapunit mu ON mu.lkey = legend.lkey
INNER JOIN component AS co ON mu.mukey = co.mukey
LEFT JOIN cogeomordesc ON co.cokey = cogeomordesc.cokey
LEFT JOIN cosurfmorphgc on cogeomordesc.cogeomdkey = cosurfmorphgc.cogeomdkey
WHERE
legend.areasymbol != 'US'
AND UPPER(compname) IN ", in.statement, "
AND geomposhill IS NOT NULL
GROUP BY UPPER(compname), geomposhill
) AS a
JOIN
(
SELECT UPPER(compname) AS compname, CAST(count(compname) AS numeric) AS total
FROM legend
INNER JOIN mapunit mu ON mu.lkey = legend.lkey
INNER JOIN component AS co ON mu.mukey = co.mukey
LEFT JOIN cogeomordesc ON co.cokey = cogeomordesc.cokey
LEFT JOIN cosurfmorphgc on cogeomordesc.cogeomdkey = cosurfmorphgc.cogeomdkey
WHERE
legend.areasymbol != 'US'
AND UPPER(compname) IN ", in.statement, "
AND geomposhill IS NOT NULL
GROUP BY UPPER(compname)
) AS b
ON a.compname = b.compname
ORDER BY compname, p DESC;", sep='')
# perform query
x <- SDA_query(q)
# re-level
x$q_param <- factor(x$q_param, levels=c('Interfluve', 'Crest', 'Head Slope', 'Nose Slope', 'Side Slope', 'Base Slope'))
# convert from long-wide format
y <- dcast(x, compname ~ q_param, value.var='p', drop=FALSE)
# optionally convert NA to 0
if(replaceNA) {
for(i in 1:nrow(y)) {
idx <- which(is.na(y[i, ]))
y[i, ][idx] <- 0
}
}
return(y)
}
# s: vector of soil series names
# replaceNA: convert missing categories into 0 probabilities
surfaceShapeProbability <- function(s, replaceNA=TRUE) {
.Deprecated(msg = 'This function is now deprecated, consider using soilDB::fetchSDA() or soilDB::fetchOSD(..., extended = TRUE)')
# format IN statement, convert to upper case for comp name normalization
in.statement <- format_SQL_in_statement(toupper(s))
# format query
q <- paste("
SELECT a.compname, q_param, q_param_n, total, round(q_param_n / total, 2) AS p
FROM
(
SELECT UPPER(compname) AS compname, shapeacross + '/' + shapedown as q_param, CAST(count(shapeacross + '/' + shapedown) AS numeric) AS q_param_n
FROM legend
INNER JOIN mapunit AS mu ON mu.lkey = legend.lkey
INNER JOIN component AS co ON mu.mukey = co.mukey
LEFT JOIN cogeomordesc ON co.cokey = cogeomordesc.cokey
LEFT JOIN cosurfmorphss on cogeomordesc.cogeomdkey = cosurfmorphss.cogeomdkey
WHERE
legend.areasymbol != 'US'
AND UPPER(compname) IN ", in.statement, "
AND shapeacross IS NOT NULL
AND shapedown IS NOT NULL
GROUP BY UPPER(compname), shapeacross + '/' + shapedown
) AS a
JOIN
(
SELECT UPPER(compname) AS compname, CAST(count(compname) AS numeric) AS total
FROM legend
INNER JOIN mapunit AS mu ON mu.lkey = legend.lkey
INNER JOIN component AS co ON mu.mukey = co.mukey
LEFT JOIN cogeomordesc ON co.cokey = cogeomordesc.cokey
LEFT JOIN cosurfmorphss on cogeomordesc.cogeomdkey = cosurfmorphss.cogeomdkey
WHERE
legend.areasymbol != 'US'
AND UPPER(compname) IN ", in.statement, "
AND shapeacross IS NOT NULL
AND shapedown IS NOT NULL
GROUP BY UPPER(compname)
) AS b
ON a.compname = b.compname
ORDER BY compname, p DESC;", sep='')
# perform query
x <- SDA_query(q)
# re-level
x$q_param <- factor(x$q_param, levels=c('Convex/Convex', 'Linear/Convex', 'Convex/Linear', 'Concave/Convex', 'Linear/Linear', 'Concave/Linear', 'Convex/Concave', 'Linear/Concave', 'Concave/Concave'))
# convert from long-wide format
y <- dcast(x, compname ~ q_param, value.var='p', drop=FALSE)
# optionally convert NA to 0
if(replaceNA) {
for(i in 1:nrow(y)) {
idx <- which(is.na(y[i, ]))
y[i, ][idx] <- 0
}
}
return(y)
}
# 's' is a vector of soil series names
hillslopeProbability <- function(s, replaceNA=TRUE) {
.Deprecated(msg = 'This function is now deprecated, consider using soilDB::fetchSDA() or soilDB::fetchOSD(..., extended = TRUE)')
# format IN statement, convert to upper case for comp name normalization
in.statement <- format_SQL_in_statement(toupper(s))
# format query
q <- paste("
SELECT a.compname, q_param, q_param_n, total, round(q_param_n / total, 2) AS p
FROM
(
SELECT UPPER(compname) AS compname, hillslopeprof as q_param, CAST(count(hillslopeprof) AS numeric) AS q_param_n
FROM legend
INNER JOIN mapunit AS mu ON mu.lkey = legend.lkey
INNER JOIN component AS co ON mu.mukey = co.mukey
LEFT JOIN cogeomordesc ON co.cokey = cogeomordesc.cokey
LEFT JOIN cosurfmorphhpp on cogeomordesc.cogeomdkey = cosurfmorphhpp.cogeomdkey
WHERE
legend.areasymbol != 'US'
AND UPPER(compname) IN ", in.statement, "
AND hillslopeprof IS NOT NULL
GROUP BY UPPER(compname), hillslopeprof
) AS a
JOIN
(
SELECT UPPER(compname) AS compname, CAST(count(compname) AS numeric) AS total
FROM legend
INNER JOIN mapunit AS mu ON mu.lkey = legend.lkey
INNER JOIN component AS co ON mu.mukey = co.mukey
LEFT JOIN cogeomordesc ON co.cokey = cogeomordesc.cokey
LEFT JOIN cosurfmorphhpp on cogeomordesc.cogeomdkey = cosurfmorphhpp.cogeomdkey
WHERE
legend.areasymbol != 'US'
AND UPPER(compname) IN ", in.statement, "
AND hillslopeprof IS NOT NULL
GROUP BY UPPER(compname)
) AS b
ON a.compname = b.compname
ORDER BY compname, p DESC;", sep='')
# perform query
x <- SDA_query(q)
# re-level hillslope positions
x$q_param <- factor(x$q_param, levels=c('Toeslope', 'Footslope', 'Backslope', 'Shoulder', 'Summit'))
# convert from long-wide format
y <- dcast(x, compname ~ q_param, value.var='p', drop=FALSE)
# optionally convert NA to 0
if(replaceNA) {
for(i in 1:nrow(y)) {
idx <- which(is.na(y[i, ]))
y[i, ][idx] <- 0
}
}
return(y)
}
# for backwards compatibility:
hillslope.probability <- hillslopeProbability