Skip to content

Commit

Permalink
createSSURGO: generalize to work with STATSGO and/or subset of spatia…
Browse files Browse the repository at this point in the history
…l layers
  • Loading branch information
brownag committed Dec 1, 2023
1 parent 83f76c4 commit 35270d1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Fix bugs in `get_SDA_interpretation()` when `dsn` refers to a local SQLite source
and in concatenation of reason string when `wide_reason=TRUE`
- `createSSURGO()` now creates indices for foreign keys and other columns important data analysis, dramatically improving the performance of standard soilDB queries on SQLite sources.
- `createSSURGO()` now works properly on STATSGO datasets for individual states or CONUS. Previously tabular data would be transferred but spatial data were not.
- `get_SDA_property()`: weighted average/dominant component numeric methods now returns `mukey` in first column position; for parity with other `get_SDA*` methods recently updated/already doing this, making it easier to use these columns for raster attribute tables via `terra::set.levels()`

# soilDB 2.7.10 (2023-11-16)
Expand Down
13 changes: 6 additions & 7 deletions R/createSSURGO.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ createSSURGO <- function(filename,
# create and add combined vector datasets:
# "soilmu_a", "soilmu_l", "soilmu_p", "soilsa_a", "soilsf_l", "soilsf_p"
f.shp <- f[grepl(".*\\.shp$", f)]
shp.grp <- do.call('rbind', strsplit(gsub(".*soil([musfa]{2})_([apl])_([a-z]{2}\\d{3})\\.shp", "\\1;\\2;\\3", f.shp), ";", fixed = TRUE))
if (length(shp.grp) > 1 && include_spatial) {
shp.grp <- do.call('rbind', strsplit(gsub(".*soil([musfa]{2})_([apl])_([a-z]{2}\\d{3}|[a-z]{2})\\.shp", "\\1;\\2;\\3", f.shp), ";", fixed = TRUE))

if (nrow(shp.grp) >= 1 && ncol(shp.grp) == 3 && include_spatial) {
f.shp.grp <- split(f.shp, list(feature = shp.grp[,1], geom = shp.grp[,2]))

lapply(seq_along(f.shp.grp), function(i) {
Expand All @@ -151,9 +152,6 @@ createSSURGO <- function(filename,
if (overwrite && j == 1) {
sf::write_sf(sf::read_sf(f.shp.grp[[i]][j]), filename, layer = lnm, overwrite = TRUE, ...)
} else sf::write_sf(sf::read_sf(f.shp.grp[[i]][j]), filename, layer = lnm, append = TRUE, ...)

# TODO: check/optimize: GPKG vector data includes R*Tree spatial index

NULL
})
})
Expand Down Expand Up @@ -192,6 +190,7 @@ createSSURGO <- function(filename,

con <- RSQLite::dbConnect(RSQLite::SQLite(), filename, loadable.extensions = TRUE)
on.exit(RSQLite::dbDisconnect(con))

lapply(names(f.txt.grp), function(x) {

if (!is.null(mstabcol)) {
Expand Down Expand Up @@ -254,11 +253,11 @@ createSSURGO <- function(filename,

# for GPKG output, add gpkg_contents (metadata for features and attributes)
if (IS_GPKG) {
# update gpkg_contents table entry
if (!include_spatial) {
if (!.gpkg_has_contents(con)) {
# if no spatial data inserted, there will be no gpkg_contents table initally
try(.gpkg_create_contents(con))
}
# update gpkg_contents table entry
try(.gpkg_delete_contents(con, mstab_lut[x]))
try(.gpkg_add_contents(con, mstab_lut[x]))
}
Expand Down

0 comments on commit 35270d1

Please sign in to comment.