Skip to content

Instantly share code, notes, and snippets.

@ksonda
ksonda / R-geoconnex.qmd
Created September 20, 2024 04:43
R-geoconnex
---
title: "Using the geoconnex Reference Feature Server with R"
format:
html:
page-layout: full
---
Below we demonstrates how to use the geoconnex reference feature server API with R's spatial packages, particularly `sf`. We'll explore various use cases for working with hydrological and related spatial data.
PREFIX schema: <https://schema.org/>
PREFIX gsp: <http://www.opengis.net/ont/geosparql#>
PREFIX wiki: <https://www.wikidata.org/wiki/>
PREFIX hyf: <https://www.opengis.net/def/schema/hy_features/hyf/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?dataset ?variableName ?temporalCoverage WHERE {
# Bind the @id of the mainstem of interest
BIND(<https://geoconnex.us/ref/mainstems/1009561> AS ?mainstem)
{
"type": "CoverageCollection",
"domainType": "PointSeries",
"parameters": {
"discharge": {
"type": "Parameter",
"description": {"en":"Water discharge in cubic feet per second"},
"observedProperty": {
"label": {"en":"Discharge"},
"id": "http://waterdata.usgs.gov/nwisweb/rdf?parmCd=00060"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<gml:FeatureCollection
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wml2="http://www.opengis.net/waterml/2.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:om="http://www.opengis.net/om/2.0"
xmlns:sa="http://www.opengis.net/sampling/2.0"
xmlns:sams="http://www.opengis.net/samplingSpatial/2.0"
xmlns:swe="http://www.opengis.net/swe/2.0" gml:id="USGS.waterservices" xsi:schemaLocation="http://www.opengis.net/waterml/2.0 http://schemas.opengis.net/waterml/2.0/waterml2.xsd">
<gml:featureMember>
library(sf)
library(mapview)
# read identifiers for Colorado River and Colorado River (TX) into simple features
colorado_river <- sf::read_sf("https://geoconnex.us/ref/mainstems/29559")
colorado_river_tx <- sf::read_sf("https://geoconnex.us/ref/mainstems/2639515")
## Function that constructs query to retrive the latest discharge observation from the USGS
@ksonda
ksonda / query.md
Last active December 5, 2022 02:55
usgs-sensorthings-by-mainstem-query

https://labs.waterdata.usgs.gov/sta/v1.1/Locations?$filter=properties/mainstemURL%20eq%20%27https://geoconnex.us/ref/mainstems/29559%27&$expand=Things($select=id)/Datastreams($filter=properties/ParameterCode%20eq%20%2700065%27)/Observations($top=1)&$resultFormat=GeoJSON"

https://labs.waterdata.usgs.gov/sta/v1.1/Locations?$filter=properties/mainstemURL%20eq%20%27https://geoconnex.us/ref/mainstems/29559%27&$expand=Things($select=id)/Datastreams($filter=properties/ParameterCode%20eq%20%2700065%27)/Observations($top=1)&$resultFormat=GeoJSON"
@ksonda
ksonda / r-geoconnex.R
Last active November 28, 2022 15:44
R sparql demo for geoconnex
##packages used
require(sf)
require(SPARQL)
# remotes::install_github("cran/SPARQL") this is archived by cran grrrr
require(mapview)
require(dplyr)
library(stringi)
# start gage
gage_uri <- "https://geoconnex.us/ref/gages/1118104"
@ksonda
ksonda / wade-geoconnex.jsonld
Last active June 6, 2022 14:20
wade geoconnex template
{
"@context": [{
"schema": "http://schema.org/",
"skos": "https://www.w3.org/TR/skos-reference/",
"sosa": "http://www.w3.org/ns/sosa/",
"hyf": "https://www.opengis.net/def/schema/hy_features/hyf/",
"gsp": "http://www.opengeospatial.org/standards/geosparql/",
"name": "schema:name",
"sameAs": "schema:sameAs",
"related": "skos:related",