query-53392391d5647d7fd1cb3087159d7aff

rq turtle/ttl

TODO

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pqv: <http://www.wikidata.org/prop/qualifier/value/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX bd: <http://www.bigdata.com/rdf#>
#title:line-map of rivers based on distance to the mouth
#defaultView:Map
SELECT ?seq ?item ?itemLabel ?coord2 ?line ?riverLabel (?river AS ?layer)





WHERE {                                               # construct lines from mouth to mouth
  {
    SELECT ?river (?seq0 AS ?seq) ?item ?elev ?coord2 ?lat ?long  WHERE {
        {                                                 # unite the sequence and the coordinate details on a single row
  SELECT ?river ?seq0 ?item ?dist ?coord2 ?lat ?long  WHERE {
        {
SELECT DISTINCT ?river ?item ?lat ?long ?coord2 ?dist WHERE { 
    {
     # VALUES ?river {wd:Q24639723} .

      ?river p:P403 ?stat. 
      ?river wdt:P131/wdt:P131 wd:Q22 .
      ?stat pq:P625 ?coord .                            # get the coords for the main river

      ?stat2 ps:P403 ?river.
      ?item p:P403 ?stat2. 
      ?stat2 pqv:P625/wikibase:geoLatitude ?lat. 
      ?stat2 pqv:P625/wikibase:geoLongitude ?long.
      ?stat2 pq:P625 ?coord2 .                          # get coords for all tributary rivers
      BIND(geof:distance(?coord, ?coord2) as ?dist)       # distance between tributary mouth and main river mouth
    }
    UNION
    {
      VALUES ( ?river ?item ?dist ) { ( wd:Q24639723 wd:Q24639723 0.0 ) }
      ?river p:P403 ?stat.
      ?stat pq:P625 ?coord2.
      ?stat pqv:P625/wikibase:geoLatitude ?lat. 
      ?stat pqv:P625/wikibase:geoLongitude ?long.
    }
  }

}.
        {                                                  # work out the sequence of mouths based on distance
  SELECT ?river ?item (COUNT(DISTINCT(?item2)) AS ?seq0) WHERE {
    INCLUDE %points .

    {
      SELECT ?river (?item AS ?item2) (?dist AS ?dist2) WHERE {
         INCLUDE %points .
      }
    }

    FILTER (?dist2 < ?dist) .
  } GROUP BY ?river ?item 
}.
    }
}.
    }
  }
  {
    SELECT ?river ((?seq0-1) AS ?seq) (?lat AS ?lat2) (?long AS ?long2) WHERE {
        {                                                 # unite the sequence and the coordinate details on a single row
  SELECT ?river ?seq0 ?item ?dist ?coord2 ?lat ?long  WHERE {
        {
SELECT DISTINCT ?river ?item ?lat ?long ?coord2 ?dist WHERE { 
    {
     # VALUES ?river {wd:Q24639723} .

      ?river p:P403 ?stat. 
      ?river wdt:P131/wdt:P131 wd:Q22 .
      ?stat pq:P625 ?coord .                            # get the coords for the main river

      ?stat2 ps:P403 ?river.
      ?item p:P403 ?stat2. 
      ?stat2 pqv:P625/wikibase:geoLatitude ?lat. 
      ?stat2 pqv:P625/wikibase:geoLongitude ?long.
      ?stat2 pq:P625 ?coord2 .                          # get coords for all tributary rivers
      BIND(geof:distance(?coord, ?coord2) as ?dist)       # distance between tributary mouth and main river mouth
    }
    UNION
    {
      VALUES ( ?river ?item ?dist ) { ( wd:Q24639723 wd:Q24639723 0.0 ) }
      ?river p:P403 ?stat.
      ?stat pq:P625 ?coord2.
      ?stat pqv:P625/wikibase:geoLatitude ?lat. 
      ?stat pqv:P625/wikibase:geoLongitude ?long.
    }
  }

}.
        {                                                  # work out the sequence of mouths based on distance
  SELECT ?river ?item (COUNT(DISTINCT(?item2)) AS ?seq0) WHERE {
    INCLUDE %points .

    {
      SELECT ?river (?item AS ?item2) (?dist AS ?dist2) WHERE {
         INCLUDE %points .
      }
    }

    FILTER (?dist2 < ?dist) .
  } GROUP BY ?river ?item 
}.
    }
}.
    }
  }
  BIND(CONCAT('LINESTRING (', STR(?long), ' ', STR(?lat), ',', STR(?long2), ' ', STR(?lat2), ')') AS ?str) .
  BIND(STRDT(?str, geo:wktLiteral) AS ?line) 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ?seq

Query found at