Sal
Peter Hoffmann Director Data Engineering at Blue Yonder. Python Developer, Conference Speaker, Mountaineer

Exploring Mountain Huts with SPARQL and Wikidata

Utilize SPARQL and Wikidata to efficiently query and retrieve data on mountain huts based on specified latitude and longitude coordinates.

For outdoor enthusiasts and avid hikers, searching for mountain huts or shelters is a common thing in tour planning. In this blog post, we'll embark on a journey to retrieve information about mountain huts around a specific latitude and longitude using the powerful combination of SPARQL and Wikidata.

Understanding SPARQL:

SPARQL (SPARQL Protocol and RDF Query Language) is a query language designed for querying data stored in Resource Description Framework (RDF) format. RDF provides a standardized way of representing information, making it an ideal choice for querying diverse datasets.

Accessing Wikidata:

Wikidata, a collaborative knowledge base, hosts a wealth of information on various topics, including geographical features like mountain huts. By employing SPARQL queries on the Wikidata platform, we can extract specific details about these huts based on their geographic coordinates.

Retrieving Mountain Huts:

Let's delve into a SPARQL query to retrieve information about mountain huts around a given latitude and longitude. The following query can be used as a starting point:

SELECT DISTINCT ?distance ?place ?placeLabel ?lat ?long ?elevation WHERE {
  SERVICE wikibase:around { 
     # Looking for items with coordinate locations(P625)
     ?place wdt:P625 ?location . 
     # That are in a circle with a centre of with a point
     bd:serviceParam wikibase:center "Point(8.114444,46.521944)"^^geo:wktLiteral  . 
     # Where the circle has a radius of km
     bd:serviceParam wikibase:radius "10" . 
     bd:serviceParam wikibase:distance ?distance .

  }

  ?place p:P625 ?coordinataes .

  ?coordinataes psv:P625 [
    wikibase:geoLatitude ?lat;
    wikibase:geoLongitude ?long
  ] .

  ?place wdt:P31 ?subclassOf .
  # bivouac shelter (Q879208) or mountain hut (Q182676) 
  VALUES ?subclassOf { wd:Q879208 wd:Q182676} .

  # Use the label service to get the label with fallback languages
 SERVICE wikibase:label { bd:serviceParam wikibase:language "de,ch,en,fr,it" . }
 OPTIONAL { ?place wdt:P2044 ?elevation. }
} 
 ORDER BY ?distance

Replace "LONGITUDE" and "LATITUDE" in the query with the desired coordinates. This query retrieves mountain huts within a specified radius (in this example, 10 kilometers) of the given location.

You can also search around a wikidata location by setting the wikdidata id (eg Q68103 Interlaken) as a reference:

wd:Q68103 wdt:P625 ?mainLoc . 
 # Use the around service
 SERVICE wikibase:around { 
 # Looking for items with coordinate locations(P625)
 ?place wdt:P625 ?location . 
 # That are in a circle with a centre of ?mainLoc(The coordinate location)
 bd:serviceParam wikibase:center ?mainLoc . 
 # Where the circle has a radius of km
 bd:serviceParam wikibase:radius "40" . 
 }

To try it out just copy/past the example into https://query.wikidata.org

You can use the python library SPARQLWrapper to retrieve the results via an api in Json format:

from SPARQLWrapper import SPARQLWrapper, JSON
import json

query =
user_agent = "python test"
endpoint_url = "https://query.wikidata.org/sparql"
sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
result = sparql.queryAndConvert()['results']['bindings']
print(json.dumps(result, indent=4))