How to select only unique latitude and longtitude for one city given multiple options? - latitude-longitude

I have the following query:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT DISTINCT ?person1 ?birthplace1 ?person2 ?birthplace2
?lat1 ?long1 ?lat2 ?long2
WHERE
{
?person1 a dbpedia-owl:Person ;
dbpedia-owl:birthPlace ?birthplace1 ;
dbpedia-owl:influenced ?person2 .
?person2 dbpedia-owl:birthPlace ?birthplace2 .
?birthplace1 a dbpedia-owl:City .
?birthplace2 a dbpedia-owl:City .
optional {
?birthplace1 geo:lat ?lat1 .
?birthplace1 geo:long ?long1 .
?birthplace2 geo:lat ?lat2 .
?birthplace2 geo:long ?long2 .
}
}
When I query this in iSparql it pulls out a long list. See Perma-Link. I just want an individual lat/long for one city. It doesn't matter which one, I just want a unique one.
Also, can anyone tell me why this doesn't work in Snorql? Thanks.

You need to GROUP your results BY the persons and birthplaces, and then sample the latitude and longitude values. This requires only a slight change to your SPARQL query. There is now
GROUP BY ?person1 ?birthplace1 ?person2 ?birthplace2
at the bottom, and the projection forms for the latitude and longitude include SAMPLE:
SELECT DISTINCT ?person1 ?birthplace1 ?person2 ?birthplace2
(SAMPLE(?lat1) as ?lat1) (SAMPLE(?long1) as ?long1)
(SAMPLE(?lat2) as ?lat2) (SAMPLE(?long2) as ?long2)
Here's the whole query:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT DISTINCT ?person1 ?birthplace1 ?person2 ?birthplace2
(SAMPLE(?lat1) as ?lat1) (SAMPLE(?long1) as ?long1)
(SAMPLE(?lat2) as ?lat2) (SAMPLE(?long2) as ?long2)
WHERE
{
?person1 a dbpedia-owl:Person ;
dbpedia-owl:birthPlace ?birthplace1 ;
dbpedia-owl:influenced ?person2 .
?person2 dbpedia-owl:birthPlace ?birthplace2 .
?birthplace1 a dbpedia-owl:City .
?birthplace2 a dbpedia-owl:City .
optional {
?birthplace1 geo:lat ?lat1 ;
geo:long ?long1 .
?birthplace2 geo:lat ?lat2 ;
geo:long ?long2 .
}
}
GROUP BY ?person1 ?birthplace1 ?person2 ?birthplace2
SPARQL results

Related

Duplicated results from Wikidata

I created the following SPARQL query to Wikidata. And the result of this query are records related to states in Germany. But as you can see, results are occurring four times in a row (you can test it here: https://query.wikidata.org/). I supposed that there is a problem with geo coordinates and languages but I can't resolve it anyway. What is wrong with this query and how can I fix it to receive a result without repetition?
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX schema: <http://schema.org/>
PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT DISTINCT ?subject ?featureCode ?countryCode ?name ?latitude ?longitude ?description ?iso31662
WHERE
{ ?subject wdt:P31 wd:Q1221156 ;
rdfs:label ?name ;
wdt:P17 ?countryClass .
?countryClass
wdt:P297 ?countryCode .
?subject wdt:P31/(wdt:P279)* ?adminArea .
?adminArea wdt:P2452 "A.ADM1" ;
wdt:P2452 ?featureCode .
?subject wdt:P300 ?iso31662
OPTIONAL
{ ?subject schema:description ?description
FILTER ( lang(?description) = "en" )
?subject p:P625 ?coordinate .
?coordinate psv:P625 ?coordinateNode .
?coordinateNode
wikibase:geoLatitude ?latitude ;
wikibase:geoLongitude ?longitude
}
FILTER ( lang(?name) = "en" )
FILTER EXISTS { ?subject wdt:P300 ?iso31662 }
}
ORDER BY lcase(?name)
OFFSET 0
LIMIT 200
In short, "9.0411111111111"^^xsd:double and "9.0411111111111"^^xsd:decimal are distinct, though they might be equal in some sense.
Check this:
SELECT DISTINCT ?subject ?featureCode ?countryCode ?name ?description ?iso31662
(datatype(?latitude) AS ?lat)
(datatype(?longitude) AS ?long)
and this:
SELECT DISTINCT ?subject ?featureCode ?countryCode ?name ?description ?iso31662
(xsd:decimal(?latitude) AS ?lat)
(xsd:decimal(?longitude) AS ?long)

Sparql of DbPedia based upon name not subject

I am trying to query dbpedia to get some people data and I don't have subjects just names of the people I want to query and their birth/death dates.
I am trying to do a query along these lines. I want the name, birth date, death date and thumbnail of everyone with the surname Presley. What I then intend to do is loop through the results returned and find the best match for Elvis Presley 1935-1977 which is the data I have.
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?thumbnail ?birthDate ?deathDate WHERE {
{
dbo:name ?Name ;
dbo:birthDate ?birthDate ;
dbo:birthDate ?deathDate ;
dbo:thumbnail ?thumbnail ;
FILTER contains(?Name#en, "Presley")
}
What is the best way to construct my sparql query?
UPDATE:
I have put together this query which seems to work to some extent but I don't entirely understand it, and I can't figure out the contains, but it does at least run and return results.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?subject ?thumbnail ?birthdate ?deathdate WHERE {
{
?subject rdfs:label "Elvis Presley"#en ;
dbo:thumbnail ?thumbnail ;
dbo:birthDate ?birthdate ;
dbo:deathDate ?deathdate ;
a owl:Thing .
}
UNION
{
?altName rdfs:label "Elvis Presley"#en ;
dbo:thumbnail ?thumbnail ;
dbo:birthDate ?birthdate ;
dbo:deathDate ?deathdate ;
dbo:wikiPageRedirects ?s .
}
}
Some entities might not have all of that information, so it's better to use optional. You can use foaf:surname to check for surname directly.
select * where {
?s foaf:surname "Presley"#en
optional { ?s dbo:name ?name }
optional { ?s dbo:birthDate ?birth }
optional { ?s dbo:deathDate ?death }
optional { ?s dbo:thumbnail ?thumb }
}

Query for banks in DBpedia

Using http://dbpedia.org/sparql, I want to receive the geographic coordinates of all bank buildings. The list of classes tells me that I should query for Bank.
Yet, the following code yields nothing:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[]
rdf:type dbpedia-owl:Bank ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (LANGMATCHES(LANG(?label), 'en'))
}
If instead I query for any sibling to Bank, (e.g. Brewery or LawFirm), I see at least some results. What's wrong with above code?
If you look into a dbpedia page for a bank, you can see that instead on rdf:type, banks have a property dbpedia-owl:industry that has dbpedia:Bank (Refah bank) or dbpedia:Financial_services (Cyprus bank) as a value. So if you rewrite your query as the following, you will get some results:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[] dbpedia-owl:industry dbpedia:Bank ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (LANGMATCHES(LANG(?label), 'en'))
}
If you add dbpedia:Financial_services, other organisations such as London stock exchange will also appear:
SELECT DISTINCT ?bank
WHERE {
?bank dbpedia-owl:industry ?place ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (?place in (dbpedia:Financial_services, dbpedia:Bank) &&
LANGMATCHES(LANG(?label), 'en'))
}
Again, by examining the London stock exchange, you can see that there is a product property that separates these financial institutions. So this will give you banks, but it might not cover all the banks available:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[] dbpedia-owl:industry ?place ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label;
dbpedia-owl:product ?product.
FILTER ( ?place in (dbpedia:Financial_services, dbpedia:Bank)
&& ?product in (dbpedia:Bank, dbpedia:Private_banking, dbpedia:Professional_Banking, dbpedia:Retail_banking, dbpedia:Investment_banking, dbpedia:Commercial_bank)
&& LANGMATCHES(LANG(?label), "en"))
} ORDER BY DESC(COUNT(DISTINCT ?product))

How to SPARQL optional

I have to use this Spaql query to retrive information about a person, my problem is to break the optional construct up into multiple optional constructs.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?label ?abstract ?placeOfBirth
?birthPlace ?birthDate ?page ?thumbnail
WHERE {
<http://dbpedia.org/resource/Ismail_Kadare> rdfs:label ?label ;
dbo:abstract ?abstract ;
foaf:page ?page .
OPTIONAL {
<http://dbpedia.org/resource/Ismail_Kadare> dbpprop:placeOfBirth ?placeOfBirth ;
dbpprop:birthPlace ?birthPlace ;
dbo:birthDate ?birthDate ;
dbo:thumbnail ?thumbnail .
}
FILTER (LANG(?label) = 'en')
FILTER (LANG(?abstract) = 'en')
}
LIMIT 1
Splitting the OPTIONAL pattern into parts
The pattern
<http://dbpedia.org/resource/Ismail_Kadare> dbpprop:placeOfBirth ?placeOfBirth ;
dbpprop:birthPlace ?birthPlace ;
dbo:birthDate ?birthDate ;
dbo:thumbnail ?thumbnail .
is shorthand for four triple patterns:
<http://dbpedia.org/resource/Ismail_Kadare> dbpprop:placeOfBirth ?placeOfBirth .
<http://dbpedia.org/resource/Ismail_Kadare> dbpprop:birthPlace ?birthPlace .
<http://dbpedia.org/resource/Ismail_Kadare> dbo:birthDate ?birthDate .
<http://dbpedia.org/resource/Ismail_Kadare> dbo:thumbnail ?thumbnail .
Instead of OPTIONAL { …first pattern… }, you just need to use four optional blocks, one for each of the four triple patterns:
optional { <http://dbpedia.org/resource/Ismail_Kadare> dbpprop:placeOfBirth ?placeOfBirth }
optional { <http://dbpedia.org/resource/Ismail_Kadare> dbpprop:birthPlace ?birthPlace }
optional { <http://dbpedia.org/resource/Ismail_Kadare> dbo:birthDate ?birthDate }
optional { <http://dbpedia.org/resource/Ismail_Kadare> dbo:thumbnail ?thumbnail }
Other issues
It's worth nothing that language matching is a bit more complicated than string matching, so rather than
FILTER (LANG(?label) = 'en')
FILTER (LANG(?abstract) = 'en')
you should really be using
filter(langMatches(lang(?label),'en'))
filter(langMatches(lang(?abstract),'en'))
which allows you to retrieve results that use different language tags that are all English.
select distinct and limit 1 aren't both necessary
Notice that select distinct ensures that you don't have any duplicate rows in your results. However, limit 1 means that you'll only have one result at most anyhow, so there won't be any duplicates to remove.
Standard Namespaces
It looks like you're querying against DBpedia, so it might be worthwhile to use the same namespace prefixes that the public endpoint defines, so that you can copy and paste queries and experiment more easily. Doing that (and using a values ?x { dbpedia:Ismail_Kadare } to avoid some typing, we end up with this query:
select ?label ?abstract ?placeOfBirth ?birthPlace ?birthDate ?page ?thumbnail
where {
values ?x { dbpedia:Ismail_Kadare }
?x rdfs:label ?label ;
dbpedia-owl:abstract ?abstract ;
foaf:page ?page .
optional { ?x dbpprop:placeOfBirth ?placeOfBirth }
optional { ?x dbpprop:birthPlace ?birthPlace }
optional { ?x dbpedia-owl:birthDate ?birthDate }
optional { ?x dbpedia-owl:thumbnail ?thumbnail }
filter langMatches(lang(?label),'en')
filter langMatches(lang(?abstract),'en')
}
limit 1
The DBpedia endpoint won't return anything for that query, but that's because http://dbpedia.org/resource/Ismail_Kadare doesn't have a foaf:page property, not because the query is malformed. I don't know whether you're actually running this against DBpedia or not, so that may or not matter.

Query dbpedia asking for birthplace of two categories in SPARQL

I am trying to get a list of:
Person A, BirthPlace of A, Person B, BirthPlace of B
where the birthplace is a city.
The relationship between person A and person B is that person A "influenced" person B. My code so far is as follows:
SELECT ?person ?birthplace ?influenced ?birthplace2
WHERE {
?person a <http://dbpedia.org/ontology/Person> .
?person dbpedia2:placeOfBirth ?birthplace .
?person <http://dbpedia.org/ontology/influenced> ?influenced.
?person dbpedia2:placeOfBirth ?birthplace2 .
?influenced a <http://dbpedia.org/ontology/Person>.
?country rdf:type dbpedia-owl:Country .
FILTER (str(?birthplace2) = str(?country))
FILTER (str(?birthplace) = str(?country))
}
At the moment I am doing country because that is only thing which works.
Currently, this is just giving me two columns with the same birthplace. It isn't showing the influenced person's birthplace.
Ideally I want the latitude and longitude of each birth city as well, such as:
Person A, BirthPlace of A, Lat, Long, Person B, BirthPlace of B, Lat, Long
But this might be too much to ask. Sorry, I'm new to SPARQL.
In your query you have:
?person dbpedia2:placeOfBirth ?birthplace .
?person <http://dbpedia.org/ontology/influenced> ?influenced.
?person dbpedia2:placeOfBirth ?birthplace2 .
which says that ?person was born in both ?birthplace and ?birthplace2. I think you meant:
?person dbpedia2:placeOfBirth ?birthplace .
?person <http://dbpedia.org/ontology/influenced> ?influenced.
?influenced dbpedia2:placeOfBirth ?birthplace2 .
As for getting the lat and long of each city, right now dbpedia is down so I can't look at the city resources to see how they map to geo-coordinates. However, once dbpedia is back up, you can do a SPARQL describe query:
describe <http://dbpedia.org/... rest of resource URI>
and see what you get back. That will tell you which predicates you need to use to pull out the location. Beware that if the lat/long information is missing, you won't see that city in the results unless you put the part of the query pattern that selects out the location in a SPARQL optional clause.
Update
OK, DbPedia is back up now. I believe this is what you want:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT DISTINCT ?person1 ?birthplace1 ?person2 ?birthplace2
?lat1 ?long1 ?lat2 ?long2
WHERE
{
?person1 a dbpedia-owl:Person ;
dbpedia-owl:birthPlace ?birthplace1 ;
dbpedia-owl:influenced ?person2 .
?person2 dbpedia-owl:birthPlace ?birthplace2 .
optional {
?birthplace1 geo:lat ?lat1 .
?birthplace1 geo:long ?long1 .
?birthplace2 geo:lat ?lat2 .
?birthplace2 geo:long ?long2 .
}
}
Update 2
The query works for me in dbpedia/iSparql (here's a perma-link):
Update 3
Restricting to only cities:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT DISTINCT ?person1 ?birthplace1 ?person2 ?birthplace2
?lat1 ?long1 ?lat2 ?long2
WHERE
{
?person1 a dbpedia-owl:Person ;
dbpedia-owl:birthPlace ?birthplace1 ;
dbpedia-owl:influenced ?person2 .
?person2 dbpedia-owl:birthPlace ?birthplace2 .
?birthplace1 a dbpedia-owl:City .
?birthplace2 a dbpedia-owl:City .
optional {
?birthplace1 geo:lat ?lat1 .
?birthplace1 geo:long ?long1 .
?birthplace2 geo:lat ?lat2 .
?birthplace2 geo:long ?long2 .
}
}