Example SPARQL Queries

SPARQL Syntax

In this example we will query the Linked Open Data Cloud for occurrence records for the Monarch Butterfly Danaus plexippus.

Occurrence records have been created that contain a particular pattern of subject predicate and object triples.

<Occurrence> <txn:occurrenceHasSpeciesConcept> <SpeciesConcept>

Since we are interested in occurrences of the Monarch Butterfly, we will use that URI as the object.

<Occurrence> <txn:occurrenceHasSpeciesConcept> <Monarch Butterfly SpeciesConcept>

This leaves the occurrence URI's as the unknown "subject" that we will be querying for.

We will be asking the server to find those URI's that match a pattern, and then return the information at each of these URI's.

The URI's that the server needs to find are those that are the subject of triples which contain a predicate of txn:occurrenceHasSpeciesConcept and a object of "Monarch Butterfly Species Concept"

The server will find the matching occurrence URI's and then return the information associated with each URI, through the "DESCRIBE" function.

Query for Species Occurrence Records

The long URI's are "prefixed" to make the query itself more readable. The "Monarch_Butterfly" is our short name the Monarch Butterfly. The "?x" variable is what we are querying for - the occurrence URI's. The DESCRIBE function is used to return information about each URI - which in this case are observations.


PREFIX txn:               <http://lod.taxonconcept.org/ontology/txn.owl#>
PREFIX Monarch_Butterfly: <http://lod.taxonconcept.org/ses/mCcSp#Species>

DESCRIBE ?x WHERE {
 ?x txn:occurrenceHasSpeciesConcept Monarch_Butterfly:.
 }

If you click on the query link below,  URIBurner will process the query against the LOD cloud and should return two occurrence records. Since these occurrence records include a longitude and latitude in the geo vocabulary, this Virtuoso-based server displays the returned records on a GoogleMap.

Run Live Query via URIburner

 

Photo

 

Query for Species Images

The following query will return the image name and contributor of those images which have been tagged as being images of the Butterfly Vanessa cardui>


PREFIX rdf:                  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dcterms:              <http://purl.org/dc/terms/>
PREFIX Vanessa_cardui_Image: <http://lod.taxonconcept.org/ses/dwAmr#Image>

select distinct ?s, ?s as ?image, ?contributor where {
  ?s rdf:type Vanessa_cardui_Image:.
  ?s dcterms:contributor ?contributor}
limit 10

Run Live Query via URIburner

 

Query for species in the Order Carnivora

 This query will return the family, genus, scientific name, and thumbnail image for species in the Order Carnivora. Since there are some species that do not have thumbnails the optional syntax is used. This will return the species even if there is not a thumbnail image.



PREFIX txn:       <http://lod.taxonconcept.org/ontology/txn.owl#>
PREFIX rdf:       <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dcterms:   <http://purl.org/dc/terms/>
PREFIX carnivora: <http://lod.taxonconcept.org/ontology/p01/Mammalia/index.owl#Order_Carnivora>

select distinct ?s, ?o as ?image, ?family, ?genus, ?sciname where {
 ?s rdf:type            txn:SpeciesConcept.
 ?s txn:inOrder         carnivora:.
 ?s txn:family          ?family.
 ?s txn:genus           ?genus.
 ?s txn:scientificName  ?sciname.
optional {?s  txn:thumbnail ?o.}.

 }

limit 290

If you click on the query link below, URIBurner will process the query against the LOD cloud and should return a table view of species, images, family, genus and scientific name.

Run Live Query

 

If you click on the query link below, URIBurner will process the query against the LOD cloud exposed the results in a browseable Miscrosoft Pivot View (Requires Microsoft Silverlight)

Run Live Query via URIburner

 

Photo