FIESTA-IoT Manual for SPARQL users

In this site we have gathered some of the queries that the FIESTA-IoT consortium have been using for their internal tests and experiments. Please have in mind that, as a living document, it is prone to be modified at any time. Besides, all the queries shown will be appended onto the FIESTA-IoT SPARQL query store (you need to log in to see it).

Best practices for a good experimenter

  • If you run first a resource discovery, you can harness this info to save much time in further queries (i.e. observations based on node ids).
  • If your experiment aims at short-term data (not historical values), another thing that can save time is the usage of IoT Service endpoints instead of raw SPARQLs queries.
  • Learn and understand the meaning of the FILTER, GROUP, LIMIT, etc. options, and try to use them if possible.
  • Try, to the extent possible, to avoid the extraction of large amounts of data at once (e.g. >5MB). In this case, split your queries into various ones; for instance, sweeping the time into small windows, etc.
    • As an extreme example, queries like select * where {?s?p?o.} go against our principles and shall be completely prohibited.
  • Last, but not least, recall that you have a thorough documentation in:
    1- SPARQL Query Language for RDF online documentation.
    2- Material of the first training workshop for experimenters

FIESTA-IoT ontology

As a reference for the queries, below you can find the ontology.

FIESTA-IoT

IoT-Registry assets summary (v1)

Summary

With this SPARQL query, we get the tally of deployments (testbeds), devices, sensing devices (sensors) and observations.

Query

PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT  (COUNT(DISTINCT ?dep) AS ?deployments)
        (COUNT(DISTINCT ?dev) AS ?devices)
        (COUNT(DISTINCT ?sens) AS ?sensors)
        (COUNT(DISTINCT ?obs) AS ?observations)
WHERE {
    {?dep a ssn:Deployment}
UNION
    {?dev a ssn:Device}
UNION
    {?sens rdf:type/rdfs:subClassOf ssn:SensingDevice}
UNION
    {?obs a ssn:Observation}
}

Output example

{
    "vars": [
        "deployments",
        "devices",
        "sensors",
        "observations"
    ],
    "items": [
        {
            "deployments": "10^^http://www.w3.org/2001/XMLSchema#integer",
            "observations": "43220^^http://www.w3.org/2001/XMLSchema#integer",
            "sensors": "60^^http://www.w3.org/2001/XMLSchema#integer",
            "devices": "60^^http://www.w3.org/2001/XMLSchema#integer"
        }
    ]
}

IoT-Registry assets summary (v2)

Summary

Yet another way to reckon the number of deployments(testbed), sensors (active and overall) and observations
Note: The performance of this one is quite better than the previous one.

Query

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
PREFIX iot-lite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#> 
SELECT  (count(distinct ?o) as ?count_observations) (count(distinct ?deployment) as ?count_deployments)  (count(distinct ?sensing_device) as ?count_AllSensors) (count(distinct ?sensingDeviceActive) as ?count_ActiveSensors)
WHERE {
  {
    ?o ssn:observedBy ?sensingDeviceActive .
    ?deployment a ssn:Deployment .
  }
  UNION
  {
    ?sensing_device iot-lite:hasQuantityKind ?qk .
  }
}

Output example

{
    "vars": [
        "count_observations",
        "count_deployments",
        "count_AllSensors",
        "count_ActiveSensors"
    ],
    "items": [
        {
            "count_ActiveSensors": "2199^^http://www.w3.org/2001/XMLSchema#integer",
            "count_AllSensors": "3300^^http://www.w3.org/2001/XMLSchema#integer",
            "count_deployments": "4^^http://www.w3.org/2001/XMLSchema#integer",
            "count_observations": "4369074^^http://www.w3.org/2001/XMLSchema#integer"
        }
    ]
}

Number of sensors per type

Query

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
PREFIX iot-lite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#> 
SELECT ?sd (COUNT(?sd) as ?count)
WHERE { 
    ?sensor iot-lite:hasQuantityKind ?qk .
    ?sensor a ?sd .
}
GROUP BY ?sd

Output

{
    "vars": [
        "sd",
        "count"
    ],
    "items": [
        {
            "sd": "http://purl.org/iot/vocab/m3-lite#WindSpeedSensor",
            "count": "3^^http://www.w3.org/2001/XMLSchema#integer"
        },
        {
            "sd": "http://purl.org/iot/vocab/m3-lite#HumanPresenceDetector",
            "count": "42^^http://www.w3.org/2001/XMLSchema#integer"
        },
        {
            "sd": "http://purl.org/iot/vocab/m3-lite#Thermometer",
            "count": "170^^http://www.w3.org/2001/XMLSchema#integer"
        },
        {
            "sd": "http://purl.org/iot/vocab/m3-lite#WeightSensor",
            "count": "8^^http://www.w3.org/2001/XMLSchema#integer"
        },
        .
        .
        .
    ]
}

Number of sensors per type and testbed

Query

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
PREFIX iot-lite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#> 
SELECT ?deployment ?type (COUNT(?type) as ?count) 
WHERE {
    # All sensors must have a QK
    ?sensor iot-lite:hasQuantityKind ?qk .
    # Some sensors might be directly linked to the deployment
    OPTIONAL { ?sensor ssn:hasDeployment ?deployment } .
    # Others are linked to a System that is linked to the deployment
    OPTIONAL {
        # Those linked in an indirect form
        {
            ?sensor ^ssn:hasSubSystem ?dev .
            ?dev  ssn:hasDeployment ?deployment
        }
        OPTIONAL
        # Those linked in a direct form
        {
            ?dev ssn:hasSubSystem ?sensor .
            ?dev ssn:hasDeployment ?deployment .
        }
    } .
    ?sensor a ?type .
}
GROUP BY ?deployment ?type
ORDER BY ?deployment

Output example

{
    "vars": [
        "deployment",
        "type",
        "count"
    ],
    "items": [
         {
            "deployment": "https://platform.fiesta-iot.eu/iot-registry/api/testbeds/a1yp9GcKEPw37Bx5rslgRI4QLSNCwEwBatCIOe_W0dHZCmzj2WmkExz3qoNuvWg1pueAXn1Li0JrNjvBiQwV3Q==",
            "type": "http://purl.org/iot/vocab/m3-lite#GyroscopeSensor",
            "count": "107^^http://www.w3.org/2001/XMLSchema#integer"
        },
        {
            "deployment": "https://platform.fiesta-iot.eu/iot-registry/api/testbeds/a1yp9GcKEPw37Bx5rslgRI4QLSNCwEwBatCIOe_W0dHZCmzj2WmkExz3qoNuvWg1pueAXn1Li0JrNjvBiQwV3Q==",
            "type": "http://purl.org/iot/vocab/m3-lite#Odometer",
            "count": "107^^http://www.w3.org/2001/XMLSchema#integer"
        },
        {
            "deployment": "https://platform.fiesta-iot.eu/iot-registry/api/testbeds/a1yp9GcKEPw37Bx5rslgRI4QLSNCwEwBatCIOe_W0dHZCmzj2WmkExz3qoNuvWg1pueAXn1Li0JrNjvBiQwV3Q==",
            "type": "http://purl.org/iot/vocab/m3-lite#ElectricFieldSensor",
            "count": "144^^http://www.w3.org/2001/XMLSchema#integer"
        },
        .
        .
        .
    ]
}

Get all different kinds of QKs for which sensors produced at least one observation (“today”)

Summary

Answer to Felix question no 5: Is it possible to have a list of the m3-lite types which are measured (return non-zero values) of the 4 core testbeds?

Query

Prefix ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
Prefix time: <http://www.w3.org/2006/time#>
Prefix m3-lite: <http://purl.org/iot/vocab/m3-lite#>
Prefix xsd: <http://www.w3.org/2001/XMLSchema#>
Prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select Distinct ?type
where {
    ?observ a ssn:Observation.
    ?observ ssn:observedProperty ?qkr.
    ?qkr a ?type.
    ?observ ssn:observationSamplingTime ?ot.
    ?ot time:inXSDDateTime ?ti.

    bind(xsd:dateTime(concat(str(year(NOW())),"-",
       str(month(NOW())),"-",
       str(day(NOW())),"T00:00:00.000Z")
      )
       as ?date)
       filter(?ti>?date)
}

Output example

{
"vars": [
  "qk"
],
"items": [
    {
        "qk": "http://purl.org/iot/vocab/m3-lite#SoundPressureLevelAmbient"
    },
    {
        "qk": "http://purl.org/iot/vocab/m3-lite#BatteryLevel"
    },
    {
        "qk": "http://purl.org/iot/vocab/m3-lite#AirTemperature"
    },
    ],
}

Active QKs from some timestamp ago

Summary

This comes to complement the previous query and bring the possibility of only focusing on “fresh” data

Query

Prefix ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
Prefix time: <http://www.w3.org/2006/time#>
Prefix m3-lite: <http://purl.org/iot/vocab/m3-lite#>
Prefix xsd: <http://www.w3.org/2001/XMLSchema#>
Prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select distinct ?type
where {
    ?observ a ssn:Observation.
    ?observ ssn:observedProperty ?qkr.
    ?qkr a ?type.
    ?observ ssn:observationSamplingTime ?ot.
    ?ot time:inXSDDateTime ?ti.
    FILTER (?ti >= "2017-07-19T06:19:35.039Z"^^xsd:dateTime)
}

Output example

{
    "vars": [
        "type"
    ],
    "items": [
        {
            "type": "http://purl.org/iot/vocab/m3-lite#ChemicalAgentAtmosphericConcentrationAirParticles"
        },
        {
            "type": "http://purl.org/iot/vocab/m3-lite#ChemicalAgentAtmosphericConcentrationNO2"
        },
        {
            "type": "http://purl.org/iot/vocab/m3-lite#SpeedInstantaneous"
        },
        .
        .
        .
    ]
}

Active QK from some timestamp ago within a particular area (north, south, east, west bound)

Summary

Taking the previous one as a reference, this query includes a location-based filter.

Query

Prefix ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
Prefix time: <http://www.w3.org/2006/time#>
Prefix m3-lite: <http://purl.org/iot/vocab/m3-lite#>
Prefix xsd: <http://www.w3.org/2001/XMLSchema#>
Prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
select distinct ?type
where {
    ?observ a ssn:Observation.
    ?observ ssn:observedProperty ?qkr.
    ?qkr a ?type.
    ?observ ssn:observationSamplingTime ?ot.
    ?ot time:inXSDDateTime ?ti.
    ?observ geo:location ?point.
    ?point geo:lat ?lat.
    ?point geo:long ?long.
    FILTER (?ti >= "2017-07-19T06:19:35.039Z"^^xsd:dateTime)
    FILTER (
        (xsd:double(?lat) >= "5.0E1"^^xsd:double)
        && (xsd:double(?lat) <= "5.2E1"^^xsd:double)
        && ( xsd:double(?long) >= "-0.65E0"^^xsd:double)
        && ( xsd:double(?long) <= "-0.55E0"^^xsd:double)
    )
}

Output example

Alike the previous one


Active QK from some timestamp ago within a particular area (north, south, east, west bound) + Phenomena filter

Summary

From the previous chain, append a filter based on physical phenomena.

NOTE For the sake of demonstration, you can see a LIMIT statement here.

Query

Prefix ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
Prefix iotlite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#>
Prefix dul: <http://www.loa.istc.cnr.it/ontologies/DUL.owl#>
Prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
Prefix time: <http://www.w3.org/2006/time#>
Prefix m3-lite: <http://purl.org/iot/vocab/m3-lite#>
Prefix xsd: <http://www.w3.org/2001/XMLSchema#>
Prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?lat ?long ?dataValue ?uom ?time ?sensorID ?type
where {
    ?observ a ssn:Observation.
    ?observ ssn:observedBy ?sensorID.
    ?observ ssn:observedProperty ?qkr.
    VALUES ?type {m3-lite:SoundPressureLevelAmbient m3-lite:AirTemperature m3-lite:RelativeHumidity}
    ?qkr a ?type.
    ?observ geo:location ?point.
    ?point geo:lat ?lat.
    ?point geo:long ?long.
    ?observ ssn:observationResult ?output.
    ?output ssn:hasValue ?value.
    ?value iotlite:hasUnit ?uom .
    ?value dul:hasDataValue ?dataValue.
    ?observ ssn:observationSamplingTime ?t.
    ?t time:inXSDDateTime ?time.            

    FILTER (
        (xsd:double(?lat) >= "3.5E1"^^xsd:double)
        && (xsd:double(?lat) <= "4.5E1"^^xsd:double)
        && ( xsd:double(?long) >= "-4"^^xsd:double)
        && ( xsd:double(?long) <= "-2"^^xsd:double)
        )  
}
order by desc(?time)
limit 1

Output example

Same as above


Resource discovery (global)

Summary

Based on the two types of graphs used by the first-party testbeds:

  • ssn:Device as the central element (SmartSantander/SoundCity). This means that classes like ssn:Deployment and ssn:Platform are bound to the device, which spans a number of sensing devices/sensors
  • snn:SensingDevice/ssn:Sensor as the central element (KETI/Smart ICS). Unlike the former case, everything is connected to a ssn:Sensor individual

We can also see as we (optionally) get the IoT Service Endpoints provided that they are part of the resource description.

Query

PREFIX iot-lite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#>
PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
PREFIX geo:  <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?sensor ?type ?qk ?unit ?endp ?lat ?long
WHERE {
    #Device as the basis
    {
            ?dev rdf:type/rdfs:subClassOf ssn:Device .
            ?dev rdf:type ?type .
            ?dev ssn:onPlatform ?platform .
            ?platform geo:location ?point .
            ?point geo:lat ?lat .
            ?point geo:long ?long .
            ?dev ssn:hasSubSystem ?sensor .
            ?sensor iot-lite:hasQuantityKind ?qkr .
            ?qkr rdf:type ?qk .
            ?sensor iot-lite:hasUnit ?unitr .
            ?unitr rdf:type ?unit .
            OPTIONAL {
                ?sensor iot-lite:exposedBy ?serv .
                ?serv iot-lite:endpoint ?endp .
        }
    }
    UNION
    #SensingDevice as the basis
    {
        ?sensor rdf:type/rdfs:subClassOf ssn:SensingDevice .
        ?sensor ssn:onPlatform ?platform .
        ?platform geo:location ?point .
        ?point geo:lat ?lat .
        ?point geo:long ?long .
        ?sensor iot-lite:hasQuantityKind ?qkr .
        ?qkr rdf:type ?qk .
        ?sensor iot-lite:hasUnit ?unitr .
        ?unitr rdf:type ?unit .
        OPTIONAL {
            ?sensor iot-lite:exposedBy ?serv .
            ?serv iot-lite:endpoint ?endp .
        }
    }
}

Output example

{
    "vars": [
        "sensor",
        "type",
        "qk",
        "unit",
        "endp",
        "lat",
        "long"
    ],
    "items": [
        {
            "endp": "https://platform.fiesta-iot.eu/iot-registry/api/endpoints/ETV6cJzgIY6ADaxMe-V_ccxE2ObNgEqPdayROKWuEaWw4bfl5T2gBR-EgEqQUKa5lWCZwfVk4ShkyZS15-uae9aZ6Fy-KqiLJtBfJi3CZl3VXANSGgaLGdw0ztjw2hE_S6IvqBWTlV_JVOx4t16ISy21LovqdEUfGZYU0EWWL7b94SWfWnSjJqNUWi2qdaKb^^http://www.w3.org/2001/XMLSchema#anyURI",
            "unit": "http://purl.org/iot/vocab/m3-lite#Percent",
            "qk": "http://purl.org/iot/vocab/m3-lite#BatteryLevel",
            "sensor": "https://platform.fiesta-iot.eu/iot-registry/api/resources/W_68NYKZCkpFspcj2U0yxSyfvgv6TK4jRemmB9ajOw3sAWsJJz4rJnm-F-CH4eEkRTjN233Bx8w5fZliZw7rGE1tCxVLLLRaGt_0SbJhijysWfmUFhEAXGnawnyUslbRR4aiYGpOV3q7yW5_kqgYYA==",
            "long": "-3.80649^^http://www.w3.org/2001/XMLSchema#double",
            "lat": "43.46104^^http://www.w3.org/2001/XMLSchema#double",
            "type": "http://purl.oclc.org/NET/ssnx/ssn#Device"
        }, 
        .
        .
        .
    ]

Resource discovery + phenomena filter

Summary

In this case, we aim at selecting a certain subset of sensors, i.e. those that match with the phenomena defined in VALUES ?qk {m3-lite:AirTemperature m3-lite:TemperatureSoil m3-lite:Illuminance m3-lite:AtmosphericPressure m3-lite:RelativeHumidity m3-lite:WindSpeed m3-lite:SoundPressureLevel m3-lite:SoundPressureLevelAmbient m3-lite:Sound m3-lite:SolarRadiation m3-lite:ChemicalAgentAtmosphericConcentrationCO m3-lite:chemicalAgentAtmosphericConcentrationO3}..

Query

PREFIX iot-lite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#>
PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
PREFIX geo:  <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
Prefix m3-lite: <http://purl.org/iot/vocab/m3-lite#>

SELECT ?dev ?sensor ?qk ?unit ?endp ?lat ?long 
WHERE {
    ?dev a ssn:Device .

    ?dev ssn:onPlatform ?platform .
    ?platform geo:location ?point .
    ?point geo:lat ?lat .
    ?point geo:long ?long .
    ?dev ssn:hasSubSystem ?sensor .
    ?sensor iot-lite:hasQuantityKind ?qkr .
    ?qkr rdf:type ?qk .
    ?sensor iot-lite:hasUnit ?unitr .
    ?unitr rdf:type ?unit .\r\n
    OPTIONAL {
        ?sensor iot-lite:exposedBy ?serv .
        ?serv iot-lite:endpoint ?endp .
    }
    VALUES ?qk {m3-lite:AirTemperature
        m3-lite:TemperatureSoil
        m3-lite:Illuminance
        m3-lite:AtmosphericPressure
        m3-lite:RelativeHumidity
        m3-lite:WindSpeed
        m3-lite:SoundPressureLevel
        m3-lite:SoundPressureLevelAmbient
        m3-lite:Sound
        m3-lite:SolarRadiation
        m3-lite:ChemicalAgentAtmosphericConcentrationCO
        m3-lite:chemicalAgentAtmosphericConcentrationO3}.
} order by asc(UCASE(str(?qk)))

Output example

Same as above


Resource discovery + location-based filter (templated)

Summary

As an illustrative example of how to use templates, we only focus in this case on device-based resources. On top of it, we can select among them by their location. Namely, we have to specify the coordinates of the north west and south east corners of a rectangle.

Query

PREFIX iot-lite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#>
PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
PREFIX geo:  <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
Prefix m3-lite: <http://purl.org/iot/vocab/m3-lite#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?dev ?sensor ?qk ?unit ?endp ?lat ?long
WHERE {
    ?dev a ssn:Device .
    ?dev ssn:onPlatform ?platform .
    ?platform geo:location ?point .
    ?point geo:lat ?lat .
    ?point geo:long ?long .
    ?dev ssn:hasSubSystem ?sensor .
    ?sensor iot-lite:hasQuantityKind ?qkr .
    ?qkr rdf:type ?qk .
    ?sensor iot-lite:hasUnit ?unitr .
    ?unitr rdf:type ?unit .
    OPTIONAL {
    ?sensor iot-lite:exposedBy ?serv .
    ?serv iot-lite:endpoint ?endp .
}
FILTER ((xsd:double(?lat) >= "%%%south_east_corner_lat%%%"^^xsd:double)
    && (xsd:double(?lat) <= "%%%north_west_corner_lat%%%"^^xsd:double)
    && ( xsd:double(?long) >= "%%%north_west_corner_long%%%"^^xsd:double)
    && ( xsd:double(?long) <= "%%%south_east_corner_long%%%"^^xsd:double)) .
} order by asc(UCASE(str(?qk)))

Template parameters

  • north_west_corner_lat [xsd:double]
  • north_west_corner_long [xsd:double]
  • south_east_corner_lat [xsd:double]
  • south_east_corner_long [xsd:double]

Application example

To directly run this query (which is stored at the Query Store), you can follow this link.

Output example

Same as the two previous ones


Get the last observations + Phenomena & location filters

Query

Prefix ssn: <http://purl.oclc.org/NET/ssnx/ssn#> 
Prefix iotlite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#> 
Prefix dul: <http://www.loa.istc.cnr.it/ontologies/DUL.owl#> 
Prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
Prefix time: <http://www.w3.org/2006/time#>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Prefix m3-lite: <http://purl.org/iot/vocab/m3-lite#>
Prefix xsd: <http://www.w3.org/2001/XMLSchema#>
select ?sensorID (max(?ti) as ?time) ?value ?latitude ?longitude ?qk
where { 
    ?o a ssn:Observation.
    ?o ssn:observedBy ?sensorID.   
    ?o ssn:observedProperty ?qkr.
    ?qkr rdf:type ?qk.
    Values ?qk {m3-lite:Sound m3-lite:SoundPressureLevelAmbient m3-lite:AirTemperature}
    ?o ssn:observationSamplingTime ?t. 
    ?o geo:location ?point. 
    ?point geo:lat ?latitude. 
    ?point geo:long ?longitude. 
    ?t time:inXSDDateTime ?ti. 
    ?o ssn:observationResult ?or. 
    ?or  ssn:hasValue ?v. 
    ?v dul:hasDataValue ?value. 
    { 
        select  (max(?dt)as ?ti) ?sensorID         
        where { 
            ?o a ssn:Observation.
            ?o ssn:observedBy ?sensorID.   
            ?o ssn:observedProperty ?qkr.
            ?qkr rdf:type ?qk .
            Values ?qk {m3-lite:Sound m3-lite:SoundPressureLevelAmbient m3-lite:AirTemperature}
            ?o ssn:observationSamplingTime ?t. 
            ?t time:inXSDDateTime ?dt. 
        }group by (?sensorID) 
    }
    FILTER ( 
       (xsd:double(?latitude) >= "-90"^^xsd:double) 
    && (xsd:double(?latitude) <= "90"^^xsd:double) 
    && ( xsd:double(?longitude) >= "-180"^^xsd:double)  
    && ( xsd:double(?longitude) <= "180"^^xsd:double)
    )  
} group by ?sensorID ?time ?value ?latitude ?longitude ?qk

Output example

{
    "vars": [
        "sensorID",
        "time",
        "value",
        "latitude",
        "longitude",
        "qk"
    ],
    "items": [
        {
            "longitude": "-3.81127E0^^http://www.w3.org/2001/XMLSchema#double",
            "qk": "http://purl.org/iot/vocab/m3-lite#AirTemperature",
            "value": "2.686E1^^http://www.w3.org/2001/XMLSchema#double",
            "sensorID": "https://platform.fiesta-iot.eu/iot-registry/api/resources/tk8ugz0-7daFhtpE7pJy5TbXzw__55Us3h33BXvUdcGYcZpe0oTCTMiusjwuT_P9-9sXALznT1eAMgaEPKAk99IeWmJ73fkiLg2VC1hmnk4V0qVlbfjRgWPpMNivF3Wljo9RMOOWNDZYVRsG93k5FQ==",
            "latitude": "4.346742E1^^http://www.w3.org/2001/XMLSchema#double",
            "time": "2017-07-19T10:37:57.980594+02:00^^http://www.w3.org/2001/XMLSchema#dateTime"
        },
        {
            "longitude": "-5.932438E-1^^http://www.w3.org/2001/XMLSchema#double",
            "qk": "http://purl.org/iot/vocab/m3-lite#Sound",
            "value": "NaN",
            "sensorID": "https://platform.fiesta-iot.eu/iot-registry/api/resources/GiOsa822kxDOsCEC0WD1c89Yll1P1n-eh2j9hdn5DyO4VVUcyoWbkwRQbCF9O0ZrIlXZGmqKKuFs11RSTgnzV6us8YwTBeWbyiej2ORaRdWuKoSenujhWnYizEEXCbdk",
            "latitude": "5.12433445E1^^http://www.w3.org/2001/XMLSchema#double",
            "time": "2017-07-19T08:38:50Z^^http://www.w3.org/2001/XMLSchema#dateTime"
        },
        .
        .
        .
    ]
}

Get the last observation of a particular sensor

Query

Prefix ssn: <http://purl.oclc.org/NET/ssnx/ssn#> 
Prefix iotlite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#> 
Prefix dul: <http://www.loa.istc.cnr.it/ontologies/DUL.owl#> 
Prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
Prefix time: <http://www.w3.org/2006/time#>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Prefix m3-lite: <http://purl.org/iot/vocab/m3-lite#>
Prefix xsd: <http://www.w3.org/2001/XMLSchema#>
select ?sensorID (max(?ti) as ?time) ?value ?latitude ?longitude ?qk
where { 
    ?o a ssn:Observation.
    ?o ssn:observedBy ?sensorID.   
    ?o ssn:observedProperty ?qkr.
    ?qkr rdf:type ?qk.
    VALUES ?sensorID {<https://platform.fiesta-iot.eu/iot-registry/api/resources/W_68NYKZCkpFspcj2U0yxSyfvgv6TK4jRemmB9ajOw3sAWsJJz4rJnm-F-CH4eEkRTjN233Bx8w5fZliZw7rGE1tCxVLLLRaGt_0SbJhijysWfmUFhEAXGnawnyUslbRR4aiYGpOV3q7yW5_kqgYYA==>}.
    ?o ssn:observationSamplingTime ?t. 
    ?o geo:location ?point. 
    ?point geo:lat ?latitude. 
    ?point geo:long ?longitude. 
    ?t time:inXSDDateTime ?ti. 
    ?o ssn:observationResult ?or. 
    ?or  ssn:hasValue ?v. 
    ?v dul:hasDataValue ?value. 
    {
        select  (max(?dt)as ?ti) ?sensorID
        where {
            ?o a ssn:Observation.
            ?o ssn:observedBy ?sensorID.
            ?o ssn:observedProperty ?qkr.
            ?qkr rdf:type ?qk .
            VALUES ?sensorID {<https://platform.fiesta-iot.eu/iot-registry/api/resources/W_68NYKZCkpFspcj2U0yxSyfvgv6TK4jRemmB9ajOw3sAWsJJz4rJnm-F-CH4eEkRTjN233Bx8w5fZliZw7rGE1tCxVLLLRaGt_0SbJhijysWfmUFhEAXGnawnyUslbRR4aiYGpOV3q7yW5_kqgYYA==>}.
            ?o ssn:observationSamplingTime ?t. 
            ?t time:inXSDDateTime ?dt. 
        }group by (?sensorID) 
    }
    FILTER ( 
       (xsd:double(?latitude) >= "-90"^^xsd:double) 
    && (xsd:double(?latitude) <= "90"^^xsd:double) 
    && ( xsd:double(?longitude) >= "-180"^^xsd:double)  
    && ( xsd:double(?longitude) <= "180"^^xsd:double)
    )  
} group by ?sensorID ?time ?value ?latitude ?longitude ?qk LIMIT 10

Observations + Location/phenomena/value filters

Summary

In this case, we only grasp the data data is greater than a certain threshold. In this very example, temperature sensors that yield more than 15ºC.

Query

Prefix ssn: <http://purl.oclc.org/NET/ssnx/ssn#> 
Prefix iotlite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#> 
Prefix dul: <http://www.loa.istc.cnr.it/ontologies/DUL.owl#> 
Prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
Prefix time: <http://www.w3.org/2006/time#>
Prefix m3-lite: <http://purl.org/iot/vocab/m3-lite#>
Prefix xsd: <http://www.w3.org/2001/XMLSchema#>
Prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?sensorID (max(?ti) as ?time) ?value ?latitude ?longitude 
where { 
    ?o a ssn:Observation.
    ?o ssn:observedBy ?sensorID.       
    ?o ssn:observationSamplingTime ?t. 
    ?o geo:location ?point. 
    ?o ssn:observedProperty ?qkr.
    ?qkr rdf:type ?qk.
    Values ?qk {m3-lite:AirTemperature}
    ?point geo:lat ?latitude. 
    ?point geo:long ?longitude. 
    ?t time:inXSDDateTime ?ti. 
    ?o ssn:observationResult ?or. 
    ?or  ssn:hasValue ?v. 
    ?v dul:hasDataValue ?value. 

    FILTER ( 
       (xsd:double(?latitude) >= "-90"^^xsd:double) 
    && (xsd:double(?latitude) <= "90"^^xsd:double) 
    && ( xsd:double(?longitude) >= "-180"^^xsd:double)  
    && ( xsd:double(?longitude) <= "180"^^xsd:double)
    )  
    FILTER(?value>="15"^^xsd:double)    
} group by ?sensorID ?time ?value ?latitude ?longitude

Observations within a time window

Summary

Instead of the very last observation, we might want to seek historical data, specyfing the time window we want to retrieve the information from.

Query

Prefix ssn: <http://purl.oclc.org/NET/ssnx/ssn#> 
Prefix iotlite: <http://purl.oclc.org/NET/UNIS/fiware/iot-lite#> 
Prefix dul: <http://www.loa.istc.cnr.it/ontologies/DUL.owl#> 
Prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
Prefix time: <http://www.w3.org/2006/time#>
Prefix m3-lite: <http://purl.org/iot/vocab/m3-lite#>
Prefix xsd: <http://www.w3.org/2001/XMLSchema#>
Prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?sensorID (max(?ti) as ?time) ?value ?latitude ?longitude 
where { 
    ?o a ssn:Observation.
    ?o ssn:observedBy ?sensorID.   
    Values ?sensorID {<LISTOFSENSORIDS>}
    ?o ssn:observationSamplingTime ?t. 
    ?o geo:location ?point. 
    ?point geo:lat ?latitude. 
    ?point geo:long ?longitude. 
    ?t time:inXSDDateTime ?ti. 
    ?o ssn:observationResult ?or. 
    ?or  ssn:hasValue ?v. 
    ?v dul:hasDataValue ?value. 

    FILTER (
       (xsd:double(?latitude) >= "-90"^^xsd:double) 
    && (xsd:double(?latitude) <= "90"^^xsd:double) 
    && ( xsd:double(?longitude) >= "-180"^^xsd:double)  
    && ( xsd:double(?longitude) <= "180"^^xsd:double)
    )

    FILTER(?ti > "2017-07-19T00:00:00.000Z"^^xsd:dateTime && ?ti < "2018-07-19T23:59:59.000Z"^^xsd:dateTime)
} group by ?sensorID ?time ?value ?latitude ?longitude

Last modified: Friday, 17 November 2017, 5:10 PM