SPARQL Distance problem WIKIDATA - problem with distance measuring - sparql

I am doing SPARQL exercise right now - I want to find all places that dont have airport in 100km range.
Right now I am stuck, cause I wanted to union them to filter data, but that isnt working.
Please help me to understand how to connect data to be able to filter the distance :)
To simplify code I changed all the cities to only Berlin.
SELECT ?placeCoor WHERE{
{
SELECT ?placeCoor WHERE{
?place wdt:P31/wdt:P279* wd:Q1248784.
?place wdt:P625 ?placeCoor.
}
}
UNION
{
SELECT DISTINCT ?berlinLoc WHERE {
wd:Q64 wdt:P625 ?berlinLoc .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } }
}
FILTER (geof:distance(?placeCoor, ?berlinLoc) >5 )
}

Related

Retrieve latitude and longitude of a sample of coordinates from Wikidata using SPARQL

I am trying to retrieve samples of coordinates in Wikidata via SPARQL but am having a very difficult time trying to achieve it. I would want to get only a single pair of coordinates per place and display the result in a column, and the latitude and longitude of the said coordinates sample in their own columns.
The following code (link to WQS) I use below works, but it does not get the coordinates values labels in Point(5.936111111 51.21) format. When I replace p:P625 with wdt:P625, no items are retrieved. Additionally, Borculo (Q1025685) appears twice in the results with two unique coordinates:
SELECT DISTINCT ?place ?placeLabel (SAMPLE(?temp1) AS ?coords_sample) ?lat ?long {
?place p:P31 ?instanceOf.
?instanceOf ps:P31/wdt:279* wd:Q2039348.
?place p:P625 ?temp1.
?temp1 psv:P625 ?temp2.
?temp2 wikibase:geoLatitude ?lat.
?temp2 wikibase:geoLongitude ?long.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} GROUP BY ?place ?placeLabel ?lat ?long
ORDER BY ?placeLabel
Use ps:P625 for obtaining the coordinates in the desired format (see also the manual on Wikibooks).
Also, it is not sufficient to sample the coordinates statement if you also group by ?lat and ?long. Hence, you'd better to sample it in a subquery.
Final result:
SELECT DISTINCT ?place ?placeLabel ?coords ?lat ?long {
?place p:P31/ps:P31/wdt:279* wd:Q2039348 ;
p:P625 ?coords_sample .
{
SELECT (SAMPLE(?coords_stmt) AS ?coords_sample) {
?place p:P31/ps:P31/wdt:279* wd:Q2039348 ;
p:P625 ?coords_stmt .
} GROUP BY ?place
}
?coords_sample ps:P625 ?coords;
psv:P625 [
wikibase:geoLatitude ?lat;
wikibase:geoLongitude ?long
] .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?placeLabel

Querying distance units in Sparql - Wikidata example

I would like to know the simplest way to handle units of distance in Sparql. In this Wikidata example I would like to select all the mountains over 8000m; however when I run the text below it also includes all of the mountains over 8000ft. Is it possible to specify that the units I am interested is meters, and for the mountains that are specified in feet, is a conversion to meters necessary?
#Mountains Over 8000m
SELECT ?mountainLabel ?height
WHERE
{
?mountain wdt:P31 wd:Q8502.
?mountain wdt:P2044 ?height.
FILTER(8000 <= ?height)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en, de". }
}
The psn namespace prefix is used to normalize units of measure. Your query can look like this:
SELECT ?mountainLabel ?height
WHERE
{
?mountain wdt:P31 wd:Q8502.
?mountain p:P2044/psn:P2044/wikibase:quantityAmount ?height .
FILTER(8000 <= ?height)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en, de". }
}

Filtering URL value

What I am trying to do is to select a certain entity for the certain condition. If the condition is url, then how I can make a proper query to get the only result that matches the v1 = "https://www.getmagicbullet.com/" .
The commented sentences are what I tried different ways, but only to fail. How could I adjust the query to get the right answer?
Thank you for your help.
SELECT DISTINCT ?iLabel ?p ?v1
WHERE {
?i wdt:P31 wd:Q212920.
?i wdt:P856 ?v1.
# FILTER (?v1Label = "https://www.getmagicbullet.com/")
# { ?v1 rdfs:label "https://www.getmagicbullet.com/"#en }
# UNION { ?v1 skos:altLabel "https://www.getmagicbullet.com/"#en }
SERVICE wikibase:label { bd:serviceParam wikibase:language "ko,en,[AUTO_LANGUAGE]". }
}
LIMIT 1000

How to query Wikidata for "also known as"

I would like to know how to query Wikidata by using the alias ("also known as").
Right now I am trying
SELECT ?item
WHERE
{
?item rdfs:aliases ?alias.
FILTER(CONTAINS(?alias, "Angela Kasner"#en))
}
LIMIT 5
This is simply a query that works if I replace rdfs:aliases by rdfs:labels.
I am trying this, because Help:Aliases says that aliases are searchable in the same way as labels, but I can't find any other resource on that nor can I find an example.
This query might be helpful for someone querying also known as for properties:
SELECT ?property ?propertyLabel ?propertyDescription (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE {
?property a wikibase:Property .
OPTIONAL { ?property skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "en") }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
}
GROUP BY ?property ?propertyLabel ?propertyDescription
LIMIT 5000

SPARQL bordering countries example

Can anyone show me any SPARQL query to get all bordering contries of all countries from http://www4.wiwiss.fu-berlin.de/factbook/sparql?
For example Afghanistan has:
factbook:landboundary db:China,
factbook:landboundary db:Iran,
factbook:landboundary db:Pakistan,
factbook:landboundary db:Tajikistan,
factbook:landboundary db:Turkmenistan
My try of getting data:
SELECT ?country ?name ?neighbour
WHERE {
?country rdf:type factbook:Country .
?country rdfs:label ?name.
OPTIONAL{
?country factbook:landboundary ?neighbour.
}
}
ended with following message:
rethrew: de.fuberlin.wiwiss.d2rq.D2RQException: Table 'factbook.neighbors' doesn't exist: SELECT DISTINCT `T0_neighbors`.`name_encoded` FROM `bordercountries` AS `T0_bordercountries`, `neighbors` AS `T0_neighbors`, `countries` AS `T0_countries` WHERE `T0_bordercountries`.`Landboundaries_bordercountries_title` = `T0_neighbors`.`Name` AND `T0_bordercountries`.`Name` = `T0_countries`.`Name` AND `T0_countries`.`name_encoded` = 'Aruba' (E0)
I've asked the same question on http://answers.semanticweb.com but no luck yet so I'm trying my luck here
The failure seems to be caused by an internal system error. Your SPARQL query does not have any syntax errors and the predicates you provided are valid according to the data.
However, I don't understand how your query is supposed to return the neighbors of one specific country. Maybe you want to try something like this:
SELECT DISTINCT ?neighbor
WHERE {
?neighbor rdf:type factbook:Country .
?neighbor factbook:landboundary db:Afghanistan .
}
Very much later and not exactly an answer to this question (the SPARQL Endpoint to the CIA Factbook seem to be down at the moment), but WikiData has a few examples how to get bordering countries according to their data set at https://query.wikidata.org/
E.g. if you open the examples and search for "border", you get a query for "countries sharing a border with Cameroon":
#Population of countries sharing a border with Cameroon
#defaultView:LineChart
SELECT ?country ?year ?population ?countryLabel WHERE {
{
SELECT ?country ?year (AVG(?population) AS ?population) WHERE {
{
SELECT ?country (str(YEAR(?date)) AS ?year) ?population WHERE {
?country wdt:P47 wd:Q1009; # shares border with Cameroon
p:P1082 ?populationStatement.
?populationStatement ps:P1082 ?population;
pq:P585 ?date.
}
}
}
GROUP BY ?country ?year
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
To understand this one has to know (or figure out) that wd:Q1009 is actually Cameroon. Not sure how to do this.
This example also displays a - imo not very useful - display of the population of the surrounding countries by year.
A simpler version without the extra data is:
SELECT ?country ?countryLabel WHERE {
?country wdt:P47 wd:Q1009 # shares border with Cameroon
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
}
}
(The SERVICE wikibase:label is a WikiData extension)
Finally all bordering neighbours for all countries might be:
SELECT ?country ?countryLabel ?neighbourLabel ?neighbour WHERE {
?country wdt:P31 wd:Q6256;
wdt:P47 ?neighbour
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
}
} ORDER BY ?countryLabel ?neighbourLabel