Skip to content

Instantly share code, notes, and snippets.

@C-Loftus
Last active September 16, 2024 15:43
Show Gist options
  • Select an option

  • Save C-Loftus/1ce984579cd64ff215dfaa28dd715af4 to your computer and use it in GitHub Desktop.

Select an option

Save C-Loftus/1ce984579cd64ff215dfaa28dd715af4 to your computer and use it in GitHub Desktop.
graph.geoconnex sample sparql query
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#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT
?mainstem
?monitoringLocation
?siteName
?datasetDescription
?datasetProvider
?type
?url
?variableMeasured
?variableUnit
?measurementTechnique
?temporalCoverage
?wkt
### group concat output
?distributionNames
?distributionURLs
?distributionFormats
####
WHERE {
VALUES ?mainstem {
<https://geoconnex.us/ref/mainstems/29559>
}
?monitoringLocation (hyf:referencedPosition/hyf:HY_IndirectPosition/hyf:linearElement) ?mainstem;
schema:subjectOf ?item.
?item (schema:variableMeasured/schema:description) ?datasetDescription;
schema:temporalCoverage ?temporalCoverage;
schema:name ?siteName;
schema:provider/schema:name ?datasetProvider.
?monitoringLocation hyf:HydroLocationType ?type;
(schema:subjectOf/schema:url) ?url;
(schema:subjectOf/schema:variableMeasured/schema:name) ?variableMeasured;
(schema:subjectOf/schema:variableMeasured/schema:unitText) ?variableUnit;
(gsp:hasGeometry/gsp:asWKT) ?wkt;
(schema:subjectOf/schema:variableMeasured/schema:measurementTechnique) ?measurementTechnique.
{
SELECT ?monitoringLocation (GROUP_CONCAT(DISTINCT ?distributionFormat; SEPARATOR = " | ") AS ?distributionFormats) (GROUP_CONCAT(DISTINCT ?distributionName; SEPARATOR = " | ") AS ?distributionNames) (GROUP_CONCAT(DISTINCT ?distributionURL; SEPARATOR = " | ") AS ?distributionURLs) WHERE {
VALUES ?mainstem {
<https://geoconnex.us/ref/mainstems/29559>
}
?monitoringLocation (hyf:referencedPosition/hyf:HY_IndirectPosition/hyf:linearElement) ?mainstem;
(schema:subjectOf/schema:distribution) ?distribution.
?distribution schema:encodingFormat ?distributionFormat;
schema:name ?distributionName;
schema:contentUrl ?distributionURL.
}
GROUP BY ?monitoringLocation
}
}
LIMIT 10
@C-Loftus
Copy link
Author

Concat more of the 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#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT 
?mainstem 
?monitoringLocation 
?siteName 
?datasetProvider 
?type 
?measurementTechnique 
?temporalCoverage 
?wkt ?urls 
?variablesMeasured
?variableUnits
?distributionNames
?distributionURLs
?distributionFormats
WHERE {
  VALUES ?mainstem {
    <https://geoconnex.us/ref/mainstems/29559>
  }
  ?monitoringLocation (hyf:referencedPosition/hyf:HY_IndirectPosition/hyf:linearElement) ?mainstem;
    schema:subjectOf ?item.
  ?item (schema:variableMeasured/schema:description) ?datasetDescription;
    schema:temporalCoverage ?temporalCoverage;
    schema:name ?siteName;
    (schema:provider/schema:name) ?datasetProvider.
  ?monitoringLocation hyf:HydroLocationType ?type;
    (gsp:hasGeometry/gsp:asWKT) ?wkt;
    (schema:subjectOf/schema:variableMeasured/schema:measurementTechnique) ?measurementTechnique.
  {
    SELECT DISTINCT 
        ?monitoringLocation 
        (GROUP_CONCAT(DISTINCT ?distributionFormat; SEPARATOR = " | ") AS ?distributionFormats) 
        (GROUP_CONCAT(DISTINCT ?distributionName; SEPARATOR = " | ") AS ?distributionNames)  
        (GROUP_CONCAT(DISTINCT ?distributionURL; SEPARATOR = " | ") AS ?distributionURLs)
        (GROUP_CONCAT(DISTINCT ?variableUnit; SEPARATOR = " | ") AS ?variableUnits) 
        
        # this just duplicates info and slows down the query, so it is commented out
        # (GROUP_CONCAT(DISTINCT ?datasetDescription; SEPARATOR = " | ") AS ?datasetDescriptions)
        (GROUP_CONCAT(DISTINCT ?variableMeasured; SEPARATOR = " | ") AS ?variablesMeasured)
        (GROUP_CONCAT(DISTINCT ?url; SEPARATOR = " | ") AS ?urls) WHERE {
      VALUES ?mainstem {
        <https://geoconnex.us/ref/mainstems/29559>
      }
      ?monitoringLocation (hyf:referencedPosition/hyf:HY_IndirectPosition/hyf:linearElement) ?mainstem;
        (schema:subjectOf/schema:variableMeasured/schema:unitText) ?variableUnit;
        (schema:subjectOf/schema:distribution) ?distribution.
      ?distribution schema:encodingFormat ?distributionFormat;
        schema:name ?distributionName;
        schema:contentUrl ?distributionURL.
      ?monitoringLocation (hyf:referencedPosition/hyf:HY_IndirectPosition/hyf:linearElement) ?mainstem;
        (schema:subjectOf/schema:url) ?url;
        (schema:subjectOf/schema:variableMeasured/schema:name) ?variableMeasured;
        schema:subjectOf ?item.
#      ?item (schema:variableMeasured/schema:description) ?datasetDescription.
    }
    GROUP BY ?monitoringLocation ?datasetDescription
  }
}
LIMIT 10

@C-Loftus
Copy link
Author

C-Loftus commented Sep 16, 2024

Clean flat query no concat

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#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT 
?mainstem 
?monitoringLocation 
?siteName 
?datasetDescription 
?type
?url
?variableMeasured
?variableUnit
?measurementTechnique
?temporalCoverage
?distributionName
?distributionURL
?distributionFormat
?wkt
WHERE {
    VALUES ?mainstem { <https://geoconnex.us/ref/mainstems/29559> }
    
    # location info
    ?monitoringLocation hyf:HydroLocationType ?type;
    		        hyf:referencedPosition/hyf:HY_IndirectPosition/hyf:linearElement ?mainstem;
                        schema:subjectOf ?dataset;
                        gsp:hasGeometry/gsp:asWKT ?wkt .

    # dataset info
    ?dataset  schema:variableMeasured ?var;
              schema:url ?url;
              schema:distribution ?distribution;
              schema:description ?datasetDescription ;
              schema:temporalCoverage ?temporalCoverage ;
              schema:name ?siteName .
    
    # variable metadata
    ?var schema:name ?variableMeasured;
         schema:unitText ?variableUnit;
         schema:measurementTechnique ?measurementTechnique .
    
    # Dataset distribution / download info
    ?distribution schema:name ?distributionName;
                  schema:contentUrl ?distributionURL;
                  schema:encodingFormat ?distributionFormat.
  
}
LIMIT 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment