SPARQL examples: Difference between revisions

From geokb
No edit summary
No edit summary
Line 38: Line 38:
   SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
   SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
}
</sparql>
= Prefixes/Namespaces =
SPARQL queries and work with RDF in general requires uses the concept of a short prefix string representing a URI to resolve an identifier. Prefix declaration in a query lets us use the short strings in place of constantly putting in a fully qualified and resolvable path. The wikibase.cloud folks have taken the stance that prefixes should always be explicitly declared as opposed to being in the default configuration for queries, which is good practice but a bit of a pain. Here is a rundown of the full set of prefixes for this Wikibase instance. You'll see the applicable prefixes from this exhaustive list in the query examples.
<sparql>
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix wikibase: <http://wikiba.se/ontology#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix schema: <http://schema.org/> .
@prefix cc: <http://creativecommons.org/ns#> .
@prefix geo: <http://www.opengis.net/ont/geosparql#> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix wd: <https://geokb.wikibase.cloud/entity/> .
@prefix data: <https://geokb.wikibase.cloud/wiki/Special:EntityData/> .
@prefix s: <https://geokb.wikibase.cloud/entity/statement/> .
@prefix ref: <https://geokb.wikibase.cloud/reference/> .
@prefix v: <https://geokb.wikibase.cloud/value/> .
@prefix wdt: <https://geokb.wikibase.cloud/prop/direct/> .
@prefix wdtn: <https://geokb.wikibase.cloud/prop/direct-normalized/> .
@prefix p: <https://geokb.wikibase.cloud/prop/> .
@prefix ps: <https://geokb.wikibase.cloud/prop/statement/> .
@prefix psv: <https://geokb.wikibase.cloud/prop/statement/value/> .
@prefix psn: <https://geokb.wikibase.cloud/prop/statement/value-normalized/> .
@prefix pq: <https://geokb.wikibase.cloud/prop/qualifier/> .
@prefix pqv: <https://geokb.wikibase.cloud/prop/qualifier/value/> .
@prefix pqn: <https://geokb.wikibase.cloud/prop/qualifier/value-normalized/> .
@prefix pr: <https://geokb.wikibase.cloud/prop/reference/> .
@prefix prv: <https://geokb.wikibase.cloud/prop/reference/value/> .
@prefix prn: <https://geokb.wikibase.cloud/prop/reference/value-normalized/> .
@prefix wdno: <https://geokb.wikibase.cloud/prop/novalue/> .
</sparql>
</sparql>

Revision as of 17:35, 26 April 2023

Minerals Related Queries

The following section provides common queries for items in the GeoKB related to mineral resources, one of the principle uses of the knowledgebase.

Mines

This query searches for the first 100 items representing mines with their names, identifiers, and point coordinates (which are a mappable WKT point that can be pulled into a mapping application).

PREFIX wd: <https://geokb.wikibase.cloud/entity/>
PREFIX wdt: <https://geokb.wikibase.cloud/prop/direct/>

SELECT ?mine ?mineLabel ?coordinate_location
WHERE {
  ?mine wdt:P1 wd:Q3646 .
  ?mine wdt:P6 ?coordinate_location .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
LIMIT 100

Try it!


Rock Classification

We've started the GeoKB understanding of rock classification via the Mindat system. We will likely augment this over time with other interpretations and classification systems, but since we are pulling minerals from Mindat and want to link to rock types included in those records, starting with Mindat made some sense. The following queries show a bit of how to work with the classification itself in addition to what we link to specific classes.

Igneous Rocks

The following query starts with igneous rock and pulls the full classification from that point (* on the end of the predicate). Because we pull identifiers in this, you can use something like the graph view in Wikibase to visualize and explore the items through their connections.

PREFIX wd: <https://geokb.wikibase.cloud/entity/>
PREFIX wdt: <https://geokb.wikibase.cloud/prop/direct/>

SELECT ?rock ?rockLabel ?subclass_of
WHERE {
  ?rock wdt:P2* wd:Q41459 .
  ?rock wdt:P2 ?subclass_of .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}

Try it!


Prefixes/Namespaces

SPARQL queries and work with RDF in general requires uses the concept of a short prefix string representing a URI to resolve an identifier. Prefix declaration in a query lets us use the short strings in place of constantly putting in a fully qualified and resolvable path. The wikibase.cloud folks have taken the stance that prefixes should always be explicitly declared as opposed to being in the default configuration for queries, which is good practice but a bit of a pain. Here is a rundown of the full set of prefixes for this Wikibase instance. You'll see the applicable prefixes from this exhaustive list in the query examples.

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix wikibase: <http://wikiba.se/ontology#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix schema: <http://schema.org/> .
@prefix cc: <http://creativecommons.org/ns#> .
@prefix geo: <http://www.opengis.net/ont/geosparql#> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix wd: <https://geokb.wikibase.cloud/entity/> .
@prefix data: <https://geokb.wikibase.cloud/wiki/Special:EntityData/> .
@prefix s: <https://geokb.wikibase.cloud/entity/statement/> .
@prefix ref: <https://geokb.wikibase.cloud/reference/> .
@prefix v: <https://geokb.wikibase.cloud/value/> .
@prefix wdt: <https://geokb.wikibase.cloud/prop/direct/> .
@prefix wdtn: <https://geokb.wikibase.cloud/prop/direct-normalized/> .
@prefix p: <https://geokb.wikibase.cloud/prop/> .
@prefix ps: <https://geokb.wikibase.cloud/prop/statement/> .
@prefix psv: <https://geokb.wikibase.cloud/prop/statement/value/> .
@prefix psn: <https://geokb.wikibase.cloud/prop/statement/value-normalized/> .
@prefix pq: <https://geokb.wikibase.cloud/prop/qualifier/> .
@prefix pqv: <https://geokb.wikibase.cloud/prop/qualifier/value/> .
@prefix pqn: <https://geokb.wikibase.cloud/prop/qualifier/value-normalized/> .
@prefix pr: <https://geokb.wikibase.cloud/prop/reference/> .
@prefix prv: <https://geokb.wikibase.cloud/prop/reference/value/> .
@prefix prn: <https://geokb.wikibase.cloud/prop/reference/value-normalized/> .
@prefix wdno: <https://geokb.wikibase.cloud/prop/novalue/> .