Querying Geospatial Insights with SPARQL

Structured Query Language for RDF (SPARQL) plays a vital role in extracting meaningful insights from Turtle (TTL) files, especially when dealing with geospatial datasets within GeoPlatform. In this section, we’ll explore SPARQL queries based on the provided TTL file examples, focusing on housing development and multifamily datasets.

SPARQL for Housing Development Dataset

Let’s begin by examining a SPARQL query for extracting information from the Housing Development dataset:

PREFIX schema: <http://schema.org/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX mads: <http://www.loc.gov/mads/rdf/v1#>
PREFIX geosparql: <http://www.opengis.net/ont/geosparql#>
PREFIX building: <http://ontology.eil.utoronto.ca/icity/Building/>
PREFIX spatialloc: <http://ontology.eil.utoronto.ca/icity/SpatialLoc>
PREFIX gci-foundation-v2-owl: <http://ontology.eil.utoronto.ca/GCI/Foundation/GCI-Foundation-v2.owl>

SELECT DISTINCT *
WHERE {
  ?item a gp:HousingDev ;
    dcterms:identifier ?ogc_fid ;
    rdfs:seeAlso ?participant_code ;
    rdfs:label ?formal_participant_name ;
    schema:telephone ?ha_hn_num ;
    schema:faxNumber ?ha_fax_num ;
    schema:email ?ha_email_addr_text ;
    geo:lat ?lat ;
    geo:long ?lon ;
    mads:county ?cnty_nm2kx ;
    mads:city ?std_city ;
    geosparql:asWKT ?wkt ;
    spatialloc:hasLocation ?project_name ;
    building:hasUnitSize ?total_units ;
    gci-foundation-v2-owl:Population_size ?total_occupied .
  FILTER(?item = hdev:1) .
}
LIMIT 5

This SPARQL query retrieves distinct information about housing developments, including their identifiers, location, contact details, and population size. The FILTER condition ensures that only data related to the specified housing development (hdev:1) is selected.

SPARQL for Housing Multifamily Dataset

Next, let’s explore a SPARQL query for the Housing Multifamily dataset:

PREFIX schema: <http://schema.org/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX mads: <http://www.loc.gov/mads/rdf/v1#>
PREFIX geosparql: <http://www.opengis.net/ont/geosparql#>
PREFIX building: <http://ontology.eil.utoronto.ca/icity/Building/>
PREFIX spatialloc: <http://ontology.eil.utoronto.ca/icity/SpatialLoc>

SELECT DISTINCT *
WHERE {
  ?item a gp:HousingMulti ;
    dcterms:identifier ?ogc_fid ;
    rdfs:seeAlso ?property_id ;
    rdfs:label ?property_name_text ;
    geo:lat ?lat ;
    geo:long ?lon ;
    mads:county ?cnty_nm2kx ;
    mads:city ?std_city ;
    geosparql:asWKT ?wkt ;
    building:locatedOn ?address_line1_text ;
    spatialloc:hasLocation ?placed_based_city_name_text ;
    building:hasUnitSize ?units1 .
  FILTER(?item = hmulti:1) .
}
LIMIT 5

Similar to the previous query, this one focuses on retrieving distinct information from the Housing Multifamily dataset. It includes details such as identifiers, location, city, and unit size, with the FILTER condition specifying the multifamily property of interest (hmulti:1).

Connecting External Data with Geospatial Queries

The power of SPARQL becomes evident when connecting external data sources. The example query below demonstrates the utilization of geospatial connections to find relevant data, showcasing the ability to make connections between different datasets:

PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX kwg-ont: <http://stko-kwg.geog.ucsb.edu/lod/ontology/>
PREFIX hdev: <https://geoapi.geoplatform.gov/collections/883b43a4_65af_4458_899a_347b9ffa34d0/items/>

SELECT DISTINCT ?gpitem ?hdevName ?hdevLoc ?item ?name ?kwgwkt
WHERE {
  # GeoPlatform stand-in
  SERVICE <https://c0c5-71-218-150-139.ngrok-free.app/sparql> {
    SELECT ?gpitem ?hdevName ?lat ?lon
    WHERE {
      ?gpitem a gp:HousingDev ;
        rdfs:label ?hdevName ;
        geo:lat ?lat ;
        geosparql:asWKT ?wkt ;
        geo:long ?lon .
    }
  }
  BIND(STRDT(CONCAT("<http://www.opengis.net/def/crs/OGC/1.3/CRS84> POINT(", str(?lon), " ", str(?lat), ")"), geosparql:wktLiteral) AS ?hdevLoc)
  ?item a kwg-ont:MTBSWildfire ;
     kwg-ont:hasFireName ?name ;
     geosparql:hasGeometry ?geom .
  ?geom geosparql:asWKT ?kwgwkt .
  # Alabama state Polygon retrieved from https://gist.github.com/JoshuaCarroll/49630cbeeb254a49986e939a26672e9c
  BIND("<http://www.opengis.net/def/crs/OGC/1.3/CRS84> POLYGON((-88.1955 35.0041,-85.6068 34.9918,-85.1756 32.8404,-84.8927 32.2593,-85.0342 32.1535,-85.1358 31.7947,-85.0438 31.5200,-85.0836 31.3384,-85.1070 31.2093,-84.9944 31.0023,-87.6009 30.9953,-87.5926 30.9423,-87.6256 30.8539,-87.4072 30.674

Conclusion

In the realm of geospatial data exploration, SPARQL emerges as a powerful tool for unraveling insights from Turtle (TTL) files within the GeoPlatform ecosystem. The SPARQL queries presented for housing development and multifamily datasets showcase its effectiveness in retrieving distinct information, ranging from identifiers and locations to contact details and unit sizes.

The first SPARQL query, tailored for the Housing Development dataset, provides a structured approach to extract valuable details about specific developments. By utilizing FILTER conditions, the query ensures precision, focusing solely on the desired housing development (hdev:1) and limiting results to enhance clarity.

Similarly, the SPARQL query for the Housing Multifamily dataset demonstrates the versatility of SPARQL in extracting diverse information. Employing FILTER conditions allows for the isolation of relevant multifamily properties (hmulti:1), enabling a nuanced understanding of their identifiers, locations, and unit sizes.

The true strength of SPARQL shines through in the third query, where external geospatial connections are made. By linking GeoPlatform data with external sources, the query exemplifies the potential for cross-dataset analysis, providing a comprehensive view of geospatial relationships and connections between different datasets.

In essence, SPARQL acts as a bridge between structured RDF data and insightful queries, facilitating a seamless exploration of geospatial information. As we delve into the interconnected landscape of housing developments, SPARQL empowers us to navigate through diverse datasets, uncovering hidden connections and paving the way for a deeper understanding of the GeoPlatform ecosystem. Through its versatility and adaptability, SPARQL proves to be an indispensable tool for geospatial analysts and researchers alike, fostering a richer exploration of the geospatial insights encapsulated within Turtle files.

You can find the GeoPlatform SPARQL service at https://sparql.geoplatform.gov/