Get data about attractions and point of interest from DBpedia using SPARQL - sparql

I don't know how to use DBpedia or SPARQL.
Could you please help me in finding data list of attractions and point of interest from DBpedia.
Actually I want Hotels and Tourist Places from all Cities/Country with Latitude, Longitude, attraction Name, Location Name, City, State, continents, etc.
Please Help me to get data using Query of SPARQL.
If you have any other suggestion then please guide me..

This is the endpoint: http://dbpedia.org/sparql/
To get all the hotels in Syria:
select * where {
?hotel a <http://dbpedia.org/ontology/Hotel> .
?hotel dbo:location dbr:Syria
}
This answer is just to give you an idea how to query DBPedia
Update
After you comment Suppose I want to get all Tourist Places or point-of-interest for Mumbai City only
You should define what those "Tourist Places* are? In the below query, I gave you an example, assuming the tourist places are museums, towers, parks, churches, Skyscrapers,
select ?thing ?type ?typeName where {
VALUES ?city {<http://dbpedia.org/resource/London>}
?thing dbo:location ?city.
optional
{
?thing a ?type .
VALUES ?type {<http://dbpedia.org/ontology/Hotel>}
BIND( "Hotel" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Museum}
BIND( "Museum" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Pyramid}
BIND( "Pyramid" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {yago:Skyscraper104233124}
BIND( "Skyscraper" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Park}
BIND( "Park" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {yago:Church103028079}
BIND( "Church" as ?typeName )
}
{
?thing a dbo:Place
}
filter (BOUND (?type))
}
If you get the idea you can extend it
Update 2
You wanted to show long, lat and country. Here you go:
select ?thing ?type ?typeName ?long ?lat ?country where {
VALUES ?city {<http://dbpedia.org/resource/London>}
optional {
?city dbo:country ?country
}
?thing dbo:location ?city.
optional
{
?thing a ?type .
VALUES ?type {<http://dbpedia.org/ontology/Hotel>}
BIND( "Hotel" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Museum}
BIND( "Museum" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Pyramid}
BIND( "Pyramid" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {yago:Skyscraper104233124}
BIND( "Skyscraper" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Park}
BIND( "Park" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {yago:Church103028079}
BIND( "Church" as ?typeName )
}
optional
{
?thing geo:long ?long.
?thing geo:lat ?lat
}
{
?thing a dbo:Place
}
filter (BOUND (?type))
}

Related

Wikidata SPARQL query to return only most recent population

I have this query that returns each value of a POPULATION (property) for each POINT IN TIME (property), but I only want the most recent population for each city. Sorry for the extraneous query items.
SELECT ?cityLabel ?gps ?population ?date (SAMPLE(?areacode) as ?areacode_sample) (SAMPLE(?image) as ?image_sample)
WITH {
SELECT DISTINCT *
WHERE {
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P17 wd:Q30 .
?city wdt:P625 ?gps .
?city p:P1082 ?statement .
?statement ps:P1082 ?population .
?statement pq:P585 ?date .
OPTIONAL {?city wdt:P473 ?areacode .
}
OPTIONAL {?city wdt:P18 ?image .
}
}
ORDER BY DESC(?population)
} AS %i
WHERE {
INCLUDE %i
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }
}
GROUP BY ?cityLabel ?gps ?population ?date
ORDER BY DESC(?population)
LIMIT 750

SPARQL - Extracting Director and Starring from DBPedia

Could anyone please help me with this code. It doesn't come out with any output but with no error.
q = """SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE {
<http://dbpedia.org/resource/Seven_Beauties> rdfs:label ?label.
?label dbo:abstract ?abstract.
?label dbo:director ?director.
?label dbo:starring ?starring.
FILTER (lang(?label) = "en")
FILTER (lang(?abstract) = "en")
}"""
from textwrap import wrap
for result in query(q):
print(result['label'],
"\n----Director----\n",result['director'],
"\n----Starring----\n",result['starring'],
"\n----Abstract----\n",
"\n".join(wrap(result['abstract'])))
<http://dbpedia.org/resource/Seven_Beauties> is the entity which has a Label, Abstract, Director, Star, etc.
The Label of that entity is a literal, and it has no Director, Star, Abstract, etc.
Try changing your query to --
SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE
{
<http://dbpedia.org/resource/Seven_Beauties>
rdfs:label ?label ;
dbo:abstract ?abstract ;
dbo:director ?director ;
dbo:starring ?starring
FILTER ( lang(?label) = "en" )
FILTER ( lang(?abstract) = "en" )
}
-- and see results from DBpedia.
DBpedia Live delivers nothing for the query above -- because the descriptions of films vary. Making some things optional will get you more results on DBpedia Live --
SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE
{
<http://dbpedia.org/resource/Seven_Beauties> rdfs:label ?label
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:abstract ?abstract }
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:director ?director }
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:starring ?starring }
FILTER ( lang(?label) = "en" )
FILTER ( lang(?abstract) = "en" )
}

SPARQL query for dbpedia to find ski lifts

I'm currently trying to teach myself how to formulate SPARQL queries to extract tourism-related information from DBpedia (via http://dbpedia.org/sparql/).
So far, I've managed to get all museums for a country.
select ?thing ?type ?category ?long ?lat ?country
where
{
VALUES ?country { <http://dbpedia.org/resource/Canada> }
optional
{
?city dbo:country ?country
}
?thing dbo:location ?city.
optional
{
?thing a ?type .
VALUES ?type { dbo:Museum }
BIND( 'Museum' as ?category )
}
optional
{
?thing a ?type.
VALUES ?type { dbo:skiLift }
BIND( 'Skilift' as ?category )
}
optional
{
?thing geo:long ?long.
?thing geo:lat ?lat
}
{
?thing a dbo:Place
}
filter (BOUND (?type))
}
However, I don't understand what I need to do to also get the same information for things like dbo:skiLift, dbo:touristicSite and the like (found here: http://dbpedia.org/ontology/Place).
What am I doing wrong?
This is because both dbo:skiLift and dbo:touristicSite are properties. These resources show up in the page for Place not as subclasses of Place, but as properties which have the class Place as their domain or range. If you want to find subclasses of Place you can perform the exploratory query (which also uses property path to retrieve the transitive closure of the subClassOf property):
select ?thing
where
{
?thing rdfs:subClassOf+ <http://dbpedia.org/ontology/Place> .
}
Apart from that, I cannot understand why you use two optional clauses for different types in the same query. For example, the following query retrieves museums located at cities of Canada, possibly with their lat and lon, without the use of other optional clauses:
select ?thing ?city ?long ?lat
where
{
?city dbo:country <http://dbpedia.org/resource/Canada> .
?thing dbo:location ?city .
?thing a dbo:Museum .
optional
{
?thing geo:long ?long .
?thing geo:lat ?lat
}
}

Group_concat in SPARQL

I am a beginner with SPARQL, and I am trying to deal with the endpoint of the Spanish National Library.
I have a code that works, here it is:
prefix bne: <http://datos.bne.es/def/> # base URI for ontology documented at http://datos.bne.es/def/
prefix resource: <http://datos.bne.es/resource/>
select distinct
?book
?author
?title
?subtitle
?ISBN
?publisher
?date
?pags
?size
?series
?edition
?subjectLabel
where {
?book a bne:C1003 .
?book bne:P3001 "Errata Naturae" .
?book bne:P1011 ?author .
?book bne:P3002 ?title .
OPTIONAL { ?book bne:P3013 ?ISBN }
OPTIONAL { ?book bne:P3001 ?publisher }
OPTIONAL { ?book bne:P3006 ?date }
OPTIONAL { ?book bne:P3004 ?pags }
OPTIONAL { ?book bne:P3007 ?size }
OPTIONAL { ?book bne:P3016 ?series }
OPTIONAL { ?book bne:P1004 ?date }
OPTIONAL { ?book bne:P3017 ?edition }
OPTIONAL { ?book bne:P3014 ?subtitle }
OPTIONAL { ?book bne:OP3008 ?subject }
?subject rdfs:label ?subjectLabel
}
limit 50
But as some books has two or more subjects, the SPARQL repeats them in the results. I used group_concat, but for some reason it doesn't work:
prefix bne: <http://datos.bne.es/def/> # base URI for ontology documented at http://datos.bne.es/def/
prefix resource: <http://datos.bne.es/resource/>
select distinct
?book
?author
?title
?subtitle
?ISBN
?publisher
?date
?pags
?size
?series
?edition
(GROUP_CONCAT(DISTINCT(?subjectLabel); separator="//") as ?subjects)
where {
?book a bne:C1003 .
?book bne:P3001 "Errata Naturae" .
?book bne:P1011 ?author .
?book bne:P3002 ?title .
OPTIONAL { ?book bne:P3013 ?ISBN }
OPTIONAL { ?book bne:P3001 ?publisher }
OPTIONAL { ?book bne:P3006 ?date }
OPTIONAL { ?book bne:P3004 ?pags }
OPTIONAL { ?book bne:P3007 ?size }
OPTIONAL { ?book bne:P3016 ?series }
OPTIONAL { ?book bne:P1004 ?date }
OPTIONAL { ?book bne:P3017 ?edition }
OPTIONAL { ?book bne:P3014 ?subtitle }
OPTIONAL { ?book bne:OP3008 ?subject }
?subject rdfs:label ?subjectLabel
}
limit 50
group by ?book
order by ?date
Does someone know where I am making a mistake?
Thanks!
Edit:
I was doing one thing wrong: as #AKSW said, I have to group all the variables at the end of the code, or add variables on the select. I have a reduced version of the code for testing this:
PREFIX bne: <http://datos.bne.es/def/>
PREFIX resource: <http://datos.bne.es/resource/>
SELECT ?book ?author ?title (GROUP_CONCAT(DISTINCT ?subject ; separator='//') AS ?subjects)
WHERE
{ ?book a bne:C1003 ;
bne:P3001 "Errata Naturae" ;
bne:P1011 ?author ;
bne:P3002 ?title ;
bne:OP3008 ?subject
}
#group everything here
GROUP BY ?book ?author ?title
#JeenBroekstra, when I run it in a SPARQL Validator, it says it is OK, but when I try to run it in the SPARQL endpoint of the library, it gives me an error:
Virtuoso 37000 Error SP030: SPARQL compiler, line 6: syntax error at 'GROUP_CONCAT' before '('
As noted in comments, the problem is that the target endpoint is running on Virtuoso, Open Source Edition, v06.01.3127 as of 2011-11-16), which did not support GROUP_CONCAT in SPARQL, as SPARQL 1.1 had not yet been finalized.
Upgrading to a current version is strongly recommended!
There is a built-in function in this version of Virtuoso, available as sql:group_concat, as documented, which may serve you for now.

UNION operator in SPARQL updates

I have two SPARQL updates.First one:
INSERT
{ GRAPH <[http://example/bookStore2]> { ?book ?p ?v } }
WHERE
{ GRAPH <[http://example/bookStore]>
{ ?book dc:date ?date .
FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
} }
Second:
INSERT
{ GRAPH <[http://example/bookStore2]> { ?book ?p ?v } }
WHERE
{ GRAPH <[http://example/bookStore3]>
{ ?book dc:date ?date .
FILTER ( ?date > "1980-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
} }
Can i combine them with the UNION operator? And if yes, is it an equivalent result? Is it possible to use UNION in SPARQL updates such as in "Select"?
AndyS's answer is correct; you can combine them, and the description of UNION is found in section 7 Matching Alternatives of the SPARQL specification. The combined query would be:
INSERT {
GRAPH <[http://example/bookStore2]> { ?book ?p ?v }
}
WHERE{
{
GRAPH <[http://example/bookStore]> {
?book dc:date ?date .
FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
}
}
UNION
{
GRAPH <[http://example/bookStore3]> {
?book dc:date ?date .
FILTER ( ?date > "1980-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
}
}
}
In this particular case where the patterns are so similar, you could also just abstract out the differing parts with VALUES:
INSERT {
GRAPH <[http://example/bookStore2]> { ?book ?p ?v }
}
WHERE{
values (?graph ?startDate) {
(<[http://example/bookStore]> "1970-01-01T00:00:00-02:00"^^xsd:dateTime)
(<[http://example/bookStore3]> "1980-01-01T00:00:00-02:00"^^xsd:dateTime)
}
GRAPH ?graph {
?book dc:date ?date .
FILTER ( ?date > ?startDate )
?book ?p ?v
}
}
The WHERE clause is the same as SPARQL Query - you can use UNION.