ncss-tech / soilDB

soilDB: Simplified Access to National Cooperative Soil Survey Databases
http://ncss-tech.github.io/soilDB/
79 stars 19 forks source link

fetchSDA_spatial: Implement `geom.src` `"mupoint"`, `"muline"`, `"featpoint"` and `"featline"` support #345

Closed brownag closed 3 months ago

brownag commented 3 months ago

Addresses fetchSDA_spatial() portion of #342

library(soilDB)

# find different types of line features in MI606 (only SSA with mupoint)
x <- SDA_query("SELECT DISTINCT areasymbol, mukey FROM mupoint")
#> single result set, returning a data.frame
x
#>   areasymbol   mukey
#> 1      MI606 1717884
#> 2      MI606 1717885
#> 3      MI606 1717886
#> 4      MI606 1717905
#> 5      MI606 1717982
#> 6      MI606 1717983
#> 7      MI606 2378083
#> 8      MI606 2438010
#> 9      MI606 2438011

# get all mupoint by mukey 
y <- fetchSDA_spatial(x$mukey[1], geom.src = "mupoint")
#> Using 1 chunks...
#> Chunk #1 completed (n = 1; 0.7 secs)
#> Done in 0.7 secs; mean/chunk: 0.7 secs; mean/symbol: 0.69 secs.
y
#> Simple feature collection with 19 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -88.99907 ymin: 47.97266 xmax: -88.63077 ymax: 48.10264
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>      mukey areasymbol nationalmusym                       geom
#> 1  1717884      MI606         1vnlm POINT (-88.99907 47.97266)
#> 2  1717884      MI606         1vnlm POINT (-88.99845 47.97289)
#> 3  1717884      MI606         1vnlm   POINT (-88.97573 47.975)
#> 4  1717884      MI606         1vnlm POINT (-88.74078 48.01363)
#> 5  1717884      MI606         1vnlm POINT (-88.74148 48.01377)
#> 6  1717884      MI606         1vnlm POINT (-88.74363 48.01752)
#> 7  1717884      MI606         1vnlm POINT (-88.74393 48.01761)
#> 8  1717884      MI606         1vnlm POINT (-88.74801 48.01861)
#> 9  1717884      MI606         1vnlm POINT (-88.67095 48.03642)
#> 10 1717884      MI606         1vnlm POINT (-88.71016 48.05644)

# get all muline (only used in 3 SSAs)
x <- SDA_query("SELECT DISTINCT areasymbol, mukey FROM muline")
#> single result set, returning a data.frame
x
#>   areasymbol   mukey
#> 1      CO662 3084843
#> 2      CO662 3084846
#> 3      WV621 2507851
#> 4      WV622 2512982

# get all muline by mukey 
y <- fetchSDA_spatial(x$mukey[1], geom.src = "muline")
#> Using 1 chunks...
#> Chunk #1 completed (n = 1; 0.2 secs)
#> Done in 0.2 secs; mean/chunk: 0.2 secs; mean/symbol: 0.24 secs.
y
#> Simple feature collection with 7 features and 3 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -107.3575 ymin: 38.01809 xmax: -107.1269 ymax: 38.42888
#> Geodetic CRS:  WGS 84
#>     mukey areasymbol nationalmusym                           geom
#> 1 3084843      CO662         2yprc LINESTRING (-107.1759 38.42...
#> 2 3084843      CO662         2yprc LINESTRING (-107.1269 38.31...
#> 3 3084843      CO662         2yprc LINESTRING (-107.244 38.385...
#> 4 3084843      CO662         2yprc LINESTRING (-107.2789 38.20...
#> 5 3084843      CO662         2yprc LINESTRING (-107.2905 38.13...
#> 6 3084843      CO662         2yprc LINESTRING (-107.303 38.041...
#> 7 3084843      CO662         2yprc LINESTRING (-107.3203 38.02...

# find different types of line features in mariposa county
x <- SDA_query("SELECT DISTINCT areasymbol, featsym, featkey FROM featline WHERE areasymbol = 'CA649'")
#> single result set, returning a data.frame
x
#>   areasymbol featsym featkey
#> 1      CA649     ESB  292331
#> 2      CA649     ESO  292332
#> 3      CA649     GUL  292333

# get all line feature types, aggregated into GEOMETRYCOLLECTION by type
y <- fetchSDA_spatial(x$featkey, by.col = "featkey", geom.src = "featline", method = "collection")
#> Using 1 chunks...
#> Chunk #1 completed (n = 3; 0.3 secs)
#> Done in 0.3 secs; mean/chunk: 0.3 secs; mean/symbol: 0.09 secs.
y
#> Simple feature collection with 3 features and 3 fields
#> Geometry type: GEOMETRYCOLLECTION
#> Dimension:     XY
#> Bounding box:  xmin: -120.368 ymin: 37.2084 xmax: -119.9022 ymax: 37.72812
#> Geodetic CRS:  WGS 84
#>   areasymbol featsym featkey                           geom
#> 1      CA649     ESB  292331 GEOMETRYCOLLECTION (LINESTR...
#> 2      CA649     ESO  292332 GEOMETRYCOLLECTION (LINESTR...
#> 3      CA649     GUL  292333 GEOMETRYCOLLECTION (LINESTR...

# find different types of point features in mariposa county
x <- SDA_query("SELECT DISTINCT areasymbol, featsym, featkey FROM featpoint WHERE areasymbol = 'CA649'")
#> single result set, returning a data.frame
x
#>   areasymbol featsym featkey
#> 1      CA649     MAR  292334
#> 2      CA649     MPI  292335
#> 3      CA649     ROC  292336
#> 4      CA649     WET  292337

# get rock outcrop points, no aggregation
r <- fetchSDA_spatial(subset(x, featsym == "ROC")$featkey, 
                      by.col = "featkey", 
                      geom.src = "featpoint")
#> Using 1 chunks...
#> Chunk #1 completed (n = 1; 0.4 secs)
#> Done in 0.4 secs; mean/chunk: 0.4 secs; mean/symbol: 0.4 secs.
r
#> Simple feature collection with 1596 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -120.374 ymin: 37.21183 xmax: -119.7541 ymax: 37.78091
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>    areasymbol featsym featkey                       geom
#> 1       CA649     ROC  292336  POINT (-120.183 37.74889)
#> 2       CA649     ROC  292336  POINT (-120.373 37.61165)
#> 3       CA649     ROC  292336 POINT (-120.3736 37.61215)
#> 4       CA649     ROC  292336 POINT (-120.3552 37.62628)
#> 5       CA649     ROC  292336 POINT (-120.1843 37.72841)
#> 6       CA649     ROC  292336 POINT (-120.1637 37.73083)
#> 7       CA649     ROC  292336 POINT (-119.9776 37.26105)
#> 8       CA649     ROC  292336   POINT (-120.086 37.5504)
#> 9       CA649     ROC  292336 POINT (-120.2421 37.56304)
#> 10      CA649     ROC  292336 POINT (-120.3088 37.60842)

# get all features, aggregated into GEOMETRYCOLLECTION by type
y <- fetchSDA_spatial(x$featkey, by.col = "featkey", geom.src = "featpoint", method = "collection")
#> Using 1 chunks...
#> Chunk #1 completed (n = 4; 0.3 secs)
#> Done in 0.3 secs; mean/chunk: 0.3 secs; mean/symbol: 0.08 secs.
y
#> Simple feature collection with 4 features and 3 fields
#> Geometry type: GEOMETRYCOLLECTION
#> Dimension:     XY
#> Bounding box:  xmin: -120.374 ymin: 37.21183 xmax: -119.7541 ymax: 37.78091
#> Geodetic CRS:  WGS 84
#>   areasymbol featsym featkey                           geom
#> 1      CA649     MAR  292334 GEOMETRYCOLLECTION (POINT (...
#> 2      CA649     MPI  292335 GEOMETRYCOLLECTION (POINT (...
#> 3      CA649     ROC  292336 GEOMETRYCOLLECTION (POINT (...
#> 4      CA649     WET  292337 GEOMETRYCOLLECTION (POINT (...