query-ec4a751b627c6b20a646477b92dc094b
Please to make the river reach the seaThis query almost draws a (main) river on a map based on the sequence of tributary river mouths on the main river. However, for obvious reasons, it does not draw the last leg of the river running from tributary mouth 1 to the main mouth. Which is to say, the river does not meet the sea. The syntax for the solution is less obvious - at least to me. We need a dot for the ?coord of the ?river, and a line composed of its ?lat ?long and the ?lat ?long of tributary mouth 1. Any help gratefully received. ... it turns out WD has issues drawing rivers b/c there's no good means of establishing the sequence of tributaries; distance from main mouth fails when rivers double-back. Elevation data is mostly absent and/or inaccurate.) map connecting tributary river mouths to a main river mouth, as the crow flies(Let me share, for interest, its cousin map, which is completely bonkers, but v.colourful:
Use at
- https://query.wikidata.org/sparql
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
} order by ?dist
}.
{ # 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
} order by ?dist
}.
{ # 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