Skip to content

SPARQL Queries: overlaps

Jonathan Yu edited this page Feb 18, 2021 · 4 revisions

Find overlaps between a MB and a CC (just the rel)

PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ipo: <http://purl.org/dc/terms/isPartOf> 
PREFIX geox: <http://linked.data.gov.au/def/geox#>
PREFIX qb4st: <http://www.w3.org/ns/qb4st/>
PREFIX epsg: <http://www.opengis.net/def/crs/EPSG/0/>
PREFIX dt: <http://linked.data.gov.au/def/datatype/>
SELECT ?o 
WHERE {
    {
        { 
           ?s1 rdf:subject <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> ;           
           rdf:predicate geox:transitiveSfOverlap;
           rdf:object ?o  .
        } UNION {
           ?s2 rdf:subject <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> ;           
           rdf:predicate geo:sfOverlaps;
           rdf:object ?o  .
        }
    }
    UNION
    { <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> geox:transitiveSfOverlap ?o }
    UNION
    { <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> geo:sfOverlaps ?o }    
}
GROUP BY ?o

Find overlaps between a MB and a CC (just area)

PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ipo: <http://purl.org/dc/terms/isPartOf> 
PREFIX geox: <http://linked.data.gov.au/def/geox#>
PREFIX qb4st: <http://www.w3.org/ns/qb4st/>
PREFIX epsg: <http://www.opengis.net/def/crs/EPSG/0/>
PREFIX dt: <http://linked.data.gov.au/def/datatype/>
SELECT ?o (MAX(?a1) as ?uarea) (MAX(?a2) as ?oarea) 
WHERE {
    {
        { 
           ?s1 rdf:subject <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> ;           
           rdf:predicate geox:transitiveSfOverlap;
           rdf:object ?o  .
        } UNION {
           ?s2 rdf:subject <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> ;           
           rdf:predicate geo:sfOverlaps;
           rdf:object ?o  .
        }
    }
    UNION
    { <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> geox:transitiveSfOverlap ?o }
    UNION
    { <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> geo:sfOverlaps ?o }
        OPTIONAL {
        <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> geox:hasAreaM2 ?ha1 .
        ?ha1 geox:inCRS epsg:3577 .
        ?ha1 dt:value ?a1 .
    }
    OPTIONAL {
        ?o geox:hasAreaM2 ?ha2 .
        ?ha2 geox:inCRS epsg:3577 .
        ?ha2 dt:value ?a2 .
    }    
}
GROUP BY ?o

Find overlaps between a MB and a CC with proportions

PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ipo: <http://purl.org/dc/terms/isPartOf> 
PREFIX geox: <http://linked.data.gov.au/def/geox#>
PREFIX qb4st: <http://www.w3.org/ns/qb4st/>
PREFIX epsg: <http://www.opengis.net/def/crs/EPSG/0/>
PREFIX dt: <http://linked.data.gov.au/def/datatype/>
SELECT ?o (MAX(?a1) as ?uarea) (MAX(?a2) as ?oarea) (MAX(?a3) as ?iarea) 
WHERE {
    {
        { 
           ?s1 rdf:subject <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> ;           
           rdf:predicate geox:transitiveSfOverlap;
           rdf:object ?o  .
        } UNION {
           ?s2 rdf:subject <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> ;           
           rdf:predicate geo:sfOverlaps;
           rdf:object ?o  .
        }
    }
    UNION
    { <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> geox:transitiveSfOverlap ?o }
    UNION
    { <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> geo:sfOverlaps ?o }
        OPTIONAL {
        <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> geox:hasAreaM2 ?ha1 .
        ?ha1 geox:inCRS epsg:3577 .
        ?ha1 dt:value ?a1 .
    }
    OPTIONAL {
        ?o geox:hasAreaM2 ?ha2 .
        ?ha2 geox:inCRS epsg:3577 .
        ?ha2 dt:value ?a2 .
    }
        OPTIONAL {
        { <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> geo:sfContains ?i }
        UNION 
        {
            ?s3 rdf:subject <http://linked.data.gov.au/dataset/asgs2016/meshblock/20686780000> ;                
                rdf:predicate geo:sfContains ;
                rdf:object ?i 
        } .
        
        { ?o geo:sfContains ?i }
        UNION 
        {
            ?s4 rdf:subject ?o ;                
                rdf:predicate geo:sfContains ;
                rdf:object ?i 
        } .
        OPTIONAL {
            ?i geox:hasAreaM2 ?ha3 .
            ?ha3 geox:inCRS epsg:3577 .
            ?ha3 dt:value ?a3 .
        }
    }    
}
GROUP BY ?o

Simplified...

PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dct: <http://purl.org/dc/terms/> 
PREFIX geox: <http://linked.data.gov.au/def/geox#>
PREFIX epsg: <http://www.opengis.net/def/crs/EPSG/0/>
PREFIX dt: <http://linked.data.gov.au/def/datatype/>
PREFIX asgs16-mb: <http://linked.data.gov.au/dataset/asgs2016/meshblock/>
SELECT ?targetFeature ?iarea 
WHERE {
   #find the overlapping features in the specified linkset
   ?s1 rdf:subject asgs16-mb:20686780000 ;  
       rdf:predicate ?spatialPredicate ;
       rdf:object ?targetFeature  ;
       dct:isPartOf ?linksetIRI .
   FILTER(?spatialPredicate = geo:sfOverlap 
            || ?spatialPredicate = geox:transitiveSfOverlap)
   #find the contained intersecting sub-features from the source end
   ?s3 rdf:subject asgs16-mb:20686780000 ;
       rdf:predicate geo:sfContains ;
       rdf:object ?intersectingFeature .
   #find the contained intersecting sub-features from the target end 
   ?s4 rdf:subject ?targetFeature ;                
       rdf:predicate geo:sfContains ;
       rdf:object ?intersectingFeature .
   #get the actual area value for the intersecting feature in the specified CRS
   ?intersectingFeature geox:hasAreaM2 [ geox:inCRS epsg:3577 ; dt:value ?iarea ]
   FILTER(?linksetIRI = <http://linked.data.gov.au/dataset/mb16cc>)
}