Skip to content

Commit 598049d

Browse files
SDA_spatialQuery: return nationalmusym (#402)
* SDA_spatialQuery: return nationalmusym * SDA_spatialQuery: fix nationalmusym for statsgo and sapolygon
1 parent 99bc56e commit 598049d

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

R/SDA-spatial.R

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,31 @@ processSDA_WKT <- function(d, g='geom', crs = 4326, p4s = NULL, as_sf = TRUE) {
6767
geom_sql <- sprintf(switch(method,
6868
intersection = "%s.STIntersection(geometry::STGeomFromText('%%s', 4326)) AS geom",
6969
overlap = "%s AS geom"), db_column)
70+
include_nationalmusym <- (id_column == "mukey" && db_table != "sapolygon")
71+
7072
res <- sprintf(
71-
"WITH geom_data (geom, %s) AS (
72-
SELECT %s, %s
73+
"WITH geom_data (geom, %s%s) AS (
74+
SELECT %s, %s.%s %s
7375
FROM %s
76+
%s
7477
WHERE %s.STIntersects(geometry::STGeomFromText('%%s', 4326)) = 1 %s
75-
) SELECT geom.STAsText() AS geom, %s%s
78+
) SELECT geom.STAsText() AS geom, %s%s%s
7679
FROM geom_data",
77-
id_column, geom_sql, id_column, db_table, db_column, clip_sql, id_column,
78-
ifelse(geomAcres, area_ac_sql, "")
80+
id_column,
81+
ifelse(include_nationalmusym, ", nationalmusym", ""),
82+
geom_sql, db_table, id_column,
83+
ifelse(include_nationalmusym, ", mapunit.nationalmusym", ""),
84+
db_table,
85+
ifelse(include_nationalmusym,
86+
sprintf("INNER JOIN mapunit ON mapunit.mukey = %s.mukey",
87+
db_table), ""),
88+
db_column, clip_sql, id_column,
89+
ifelse(geomAcres, area_ac_sql, ""),
90+
ifelse(include_nationalmusym, ", nationalmusym", "")
91+
7992
)
8093

94+
8195
# handle non-polygon results
8296
if (db == "SSURGO" && what %in% c("mupoint", "muline", "featpoint", "featline")) {
8397
res <- gsub("mupolygon", what, res)
@@ -310,8 +324,8 @@ SDA_spatialQuery <- function(geom,
310324
return_terra <- FALSE
311325

312326
# raster support
313-
if (inherits(geom, 'RasterLayer') |
314-
inherits(geom, 'RasterBrick') |
327+
if (inherits(geom, 'RasterLayer') ||
328+
inherits(geom, 'RasterBrick') ||
315329
inherits(geom, 'RasterStack')) {
316330
if (!requireNamespace('terra'))
317331
stop("packages terra is required", call. = FALSE)

0 commit comments

Comments
 (0)