Using geoknife with a custom webgeom and/or webdata

David Blodgett

2019-01-30

This vignette shows how to use a custom Web Feature Service with geoknife. In this case, we are using a WFS from ScienceBase. The url used for the WFS can be found in the “Spatial Services” section of the sciencebase item. If the WFS url you have has parameters included, remove them when passing them to geoknife.
e.g. this: https://www.sciencebase.gov/catalogMaps/mapping/ows/5b68e7e3e4b006a11f75c06a
not this: https://www.sciencebase.gov/catalogMaps/mapping/ows/5b68e7e3e4b006a11f75c06a?service=wfs&request=getcapabilities&version=1.0.0

For advanced users, it may be interesting to see what geoknife is doing behind the scences. Switch verbose=FALSE to verbose=TRUE to see the web service request being made when you execute this vignette.

gconfig(verbose=FALSE)
stencil <- webgeom(url="https://www.sciencebase.gov/catalogMaps/mapping/ows/5b68e7e3e4b006a11f75c06a")

stencil_geoms <- query(stencil, 'geoms')
print(stencil_geoms)
## [1] "sb:footprint"                "sb:Yahara_River_HRUs_alb_eq"

Now we can select a WFS geometry layer (geom) and query for its attributes.

geom(stencil) <- stencil_geoms[2]

stencil_attributes <- query(stencil, 'attributes')
print(stencil_attributes)
## [1] "ID"       "GRIDCODE" "X_COORD"  "Y_COORD"

Now we can select an attribute and query for the values of that attribute. For this demo, we won’t set the values, but rather just move forward using all polygons in this geom

attribute(stencil) <- stencil_attributes[2]

print(query(stencil, 'values'))
##  [1] "55" "53" "54" "51" "50" "48" "52" "45" "46" "47" "42" "49" "43" "41"
## [15] "40" "44" "39" "38" "36" "37" "34" "35" "31" "33" "27" "25" "32" "30"
## [29] "26" "28" "29" "21" "22" "24" "23" "20" "19" "17" "18" "16" "15" "14"
## [43] "13" "9"  "8"  "11" "12" "10" "7"  "6"  "5"  "4"  "2"  "3"  "1"

Now we’ll set up our webdata with an OPeNDAP service as its url. Note that this can be any OPeNDAP service compatible with geoknife and the Geo Data Portal. What datasets are compatible is beyond the scope of this vignette, but is documented here. The OPeNDAP dataset used here is from the main USGS THREDDS archive and is one included in geoknife but many other OPeNDAP datasets could be used by entering their OPeNDAP service base URL in the same way.

fabric <- webdata(url = 'https://cida.usgs.gov/thredds/dodsC/prism_v2', 
                  variables = c('tmx', 'tmn', 'ppt'),
                  times = as.POSIXct(c('2000-01-01', '2010-01-01')))
print(fabric)
## An object of class "webdata":
## times: 2000-01-01T00:00:00Z, 2010-01-01T00:00:00Z
## url: https://cida.usgs.gov/thredds/dodsC/prism_v2 
## variables: tmx, tmn, ppt

Note that if you don’t know the valid variables and times for an OPeNDAP URL, you can use query to find them like this.

fabric_variables <- query(fabric, "variables")
fabric_times <- query(fabric, "times")

print(paste(fabric@url, "has", 
            paste(fabric_variables, collapse = ", "), 
            "variables for the time range", fabric_times[1], 
            "to", fabric_times[2]))
## [1] "https://cida.usgs.gov/thredds/dodsC/prism_v2 has ppt, tmx, tmn variables for the time range 1895-01-01 to 2017-12-01"

Now we can execute the geoknife job for the specified fabric and stencil. Note that we set wait = TRUE here so we can just wait for the result of the process.

prism_yahara_result <- result(geoknife(stencil, fabric, wait = TRUE))
## [1] "The returned dataframe has 58 columns with names 'DateTime', '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', 'variable', 'statistic' and 363 rows from 2000-01-01 to 2010-01-01"