retrieving the countries details from geonames using a sparql query - sparql

I am trying to get specific countries informations from geonames using "Linked Data HTW Chur sparql endpoint" (url: http://linkeddata.fh-htwchur.ch/lodestar/sparql ):
PREFIX gn: <http://www.geonames.org/ontology#>
PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dcterms: <http://purl.org/dc/terms/>
select distinct ?country ?population ?lattitude ?longitude ?creationDate ?modificationDate
from <http://sws.geonames.org>
where { ?country gn:name ?country_name;
gn:population ?population;
wgs84_pos:lat ?lattitude;
wgs84_pos:long ?longitude;
dcterms:created ?creationDate;
dcterms:modified ?modificationDate
}
The query works if I remove "dcterms:created" and "dctermes:modified" but I need both to extract the data item creation date and the last modification date
If someone could please help me

You have to distinguish between the concept and the document about this concept.
Example for the United States:
https://sws.geonames.org/6252001/ (concept, i.e., the actual country)
https://sws.geonames.org/6252001/about.rdf (GeoNames document about this concept)
These URIs refer to each other via rdfs:isDefinedBy/foaf:primaryTopic.
The dcterms:created property gives the date when the document was created, not when the country was created. The same applies to dcterms:modified.
If you don’t need to refer to the "document about the concept" in your results, you could use property paths to get the values:
PREFIX gn: <http://www.geonames.org/ontology#>
PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?country ?population ?lattitude ?longitude ?creationDate ?modificationDate
WHERE {
?country gn:name ?country_name ;
gn:population ?population ;
wgs84_pos:lat ?lattitude ;
wgs84_pos:long ?longitude ;
rdfs:isDefinedBy/dcterms:created ?creationDate ;
rdfs:isDefinedBy/dcterms:modified ?modificationDate .
}
Otherwise you can use another variable for it:
PREFIX gn: <http://www.geonames.org/ontology#>
PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?country ?population ?lattitude ?longitude ?creationDate ?modificationDate
WHERE {
?country gn:name ?country_name ;
gn:population ?population ;
wgs84_pos:lat ?lattitude ;
wgs84_pos:long ?longitude ;
rdfs:isDefinedBy ?countryDocument .
?countryDocument dcterms:created ?creationDate ;
dcterms:modified ?modificationDate .
}

Related

How to extract data from DBpedia using SPARQL

I'm trying to extract some data from dbpedia using SERVICE function of SPARQL.
In fact I want to extract the names, the lat and lot of all New York theaters. To check if an instance is a theater I can use http://dbpedia.org/class/yago/Theater104417809​. One example of a theater could be http://dbpedia.org/resource/Grand_Theatre_(New_York_City).
How to use service function for getting what I need in SPARQL?
** EDIT **
The query that I'm trying is the following one, but is not returning any value.
PREFIX dbpedia: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX geos: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX : <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#>
SELECT *
WHERE {
SERVICE <http://dbpedia.org/sparql/> {
SELECT ?teatreName ?lat ?long
WHERE {
?teatre rdf:type dbpedia:Theatre .
?teatre foaf:name ?teatreName .
?teatre geo:lat ?lat .
?teatre geo:long ?long .
?teatre dbp:city ?ciutat .
?ciutat rdfs:label "New York City"#en
}
}
}
It's not an issue with federated querying, but with your DBpedia query. dbp:city is not an object property but simply of type rdf:Property, thus it's untyped. In your case, it maps to literals which means, you have to use the literal directly. The weird thing here is, that for some reasons you have to use the datatype http://www.w3.org/1999/02/22-rdf-syntax-ns#langString explicitly instead of "New York City"#en - that's clearly not intuitive for any user. Not sure whether this happened due to the DBpedia extraction or is the expected behaviour of Virtuoso.
PREFIX dbpedia: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX geos: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX : <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#>
SELECT *
WHERE {
SERVICE <http://dbpedia.org/sparql/> {
SELECT ?teatreName ?lat ?long
WHERE {
?teatre rdf:type dbpedia:Theatre .
?teatre foaf:name ?teatreName .
?teatre geo:lat ?lat .
?teatre geo:long ?long .
?teatre dbp:city "New York City"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>
}
}
}

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)

List countries from DBpedia

Trying to query DBpedia for a list of all countries with the dbo:longName property and the capital of each country listed but get 0 results returned. Can't see whats wrong with the query.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:longName ;
dbo:capital ?capital .
}
What's missing?
You are missing that ?country has a rdf:type of dbo:Country and not dbo:longName. The right query should be:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?x a dbo:Country.
?x dbo:longName ?country.
?x dbp:capital ?capital
}
Update
Based on your comment you want the URI's of the country and their capital. Therefore, you don't need dbo:longName, because you don't want the country label name. You will select the instances:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:Country.
?country dbo:capital ?capital
}
Note that results will bring countries that are extinct. If you want to filter countries that have ended, you should get this result with:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:Country.
?country dbo:capital ?capital.
FILTER NOT EXISTS { ?country dbo:dissolutionYear ?yearEnd }
}

Sparql syntax error on nested select

I am trying to select City with highest population. My Query looks like this:
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?population ?capital
WHERE {
?x dct:subject dbc:Countries_in_Europe .
?x dbo:capital ?capital .
?capital dbo:populationTotal ?population .
FILTER (?population =
(
SELECT MAX(?popul) AS ?pop
WHERE{
?capital dbo:populationTotal ?popul .
}
)
)
}
LIMIT 200
I haved followed This PDF page 5.
I am using apache-jena-fuseki-2.4.0 and getting a syntax error
Encountered " "select" "SELECT "" at line 18, column 1.
Any ideas where the problem is?
I don't know where you have seen that you can bind the result of a SELECT query to a variable in the FILTER, but that's totally wrong. This might work for SQL but not for SPARQL.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
SELECT ?population ?capital
WHERE
{ ?x dct:subject dbc:Countries_in_Europe ;
dbo:capital ?capital .
?capital dbo:populationTotal ?population
FILTER ( ?population = ?pop )
# sub-SELECTs are evaluated first and encapsulated by { }
# in addition (as to TomP's comment), you have to restrict to European captials here
{ SELECT (MAX(?popul) AS ?pop)
WHERE
{ ?x dct:subject dbc:Countries_in_Europe ;
dbo:capital ?capital .
?capital dbo:populationTotal ?popul }
}
}
LIMIT 200
But, if you really want to have the "capital in Europe with the highest population", why not using the easier and more obvious way which reflects your task, i.e. ORDER BY + LIMIT 1:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
SELECT ?population ?capital
WHERE
{ ?x dct:subject dbc:Countries_in_Europe ;
dbo:capital ?capital .
?capital dbo:populationTotal ?population
}
ORDER BY DESC(?population)
LIMIT 1

How do get city population by name with SPARQL

I need to get city population by its name. I'm sure SPARQL could provide this, but I'm not sure how to write the query. I have a query which provides COuntry's capital and coordinates by it's name, so I assume it should be something similar. HELP!
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?country ?population ?capital ?lat ?long WHERE {
?country a o:Country ; foaf:name "Germany"#en; o:capital [ geo:lat ?lat ; geo:long ?long ; p:name ?capital ]
}
All you need is:
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?pop WHERE {
?country a o:Country ;
foaf:name ?name ;
p:populationEstimate ?pop .
}
The only tricky part was finding out the name of the property that links Countries and Populations.