How To‎ > ‎

SPARQL


Sample queries using http://DBPedia.org/sparql endpoint

List cities, towns and villages in the UK


PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbp-ont: <http://dbpedia.org/ontology/>
select *
{
?location ?locatedIn dbpedia:United_Kingdom filter (?locatedIn!=dbp-ont:wikiPageWikiLink) .
?location a ?type filter (?type=dbp-ont:City || ?type=dbp-ont:Town || ?type=dbp-ont:Village)
} limit 1000

List all locations/populated places


PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbp-ont: <http://dbpedia.org/ontology/>
select distinct ?location ?locatedIn ?type
{
?location ?locatedIn dbpedia:United_Kingdom filter (?locatedIn!=dbp-ont:wikiPageWikiLink) .
?location a ?type filter (?type=dbp-ont:PopulatedPlace)
} limit 1000

All locations from within the UK


PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbp-ont: <http://dbpedia.org/ontology/>
select ?location, str(?label), ?type
{
?location <http://dbpedia.org/ontology/country> dbpedia:United_Kingdom .
?location a ?type filter ( ?type = dbp-ont:PopulatedPlace || ?type=dbp-ont:Country || ?type=dbp-ont:City || ?type=dbp-ont:Town || ?type=dbp-ont:Village || ?type=dbp-ont:AdministrativeRegion || ?type=dbp-ont:Island  || ?type=dbp-ont:Continent) . 
?location foaf:name ?label  filter (lang(?label)="en").
} order by ?location 

DBPedia cuts the output at 10000 so we need to repeat the query with different offset:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbp-ont: <http://dbpedia.org/ontology/>
select ?location, str(?label), ?type
{
?location <http://dbpedia.org/ontology/country> dbpedia:United_Kingdom .
?location a ?type filter ( ?type = dbp-ont:PopulatedPlace || ?type=dbp-ont:Country || ?type=dbp-ont:City || ?type=dbp-ont:Town || ?type=dbp-ont:Village || ?type=dbp-ont:AdministrativeRegion || ?type=dbp-ont:Island  || ?type=dbp-ont:Continent) . 
?location foaf:name ?label  filter (lang(?label)="en").
} order by ?location limit 10000 offset 20001


Adding longitude and latitude to the UK locations


PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbp-ont: <http://dbpedia.org/ontology/>
PREFIX geo:  <http://www.w3.org/2003/01/geo/wgs84_pos#>
select distinct ?locationuri, str(?label), ?type, ?latitude, ?longitude
{
?locationuri <http://dbpedia.org/ontology/country> dbpedia:United_Kingdom .
?locationuri a ?type filter ( ?type = dbp-ont:PopulatedPlace || ?type=dbp-ont:Country || ?type=dbp-ont:City || ?type=dbp-ont:Town || ?type=dbp-ont:Village || ?type=dbp-ont:AdministrativeRegion || ?type=dbp-ont:Island  || ?type=dbp-ont:Continent) . 
?locationuri foaf:name ?label  filter (lang(?label)="en").
?locationuri geo:lat ?latitude .
?locationuri geo:long ?longitude
} order by ?locationuri limit 10000 offset 1

then change the last part of the line twice:
 limit 10000 offset 10001
 limit 10000 offset 20001
this is because the dbpedia endpoint at the moment (8 jan 2012) has limit set to 10000

Retrieve Named Entities with DBpedia


Now this long sparql assumes that no reasoning is used (not even rdfs) - if rdfs reasoning is used the current version of dbpedia makes many incorrect assumptions so it is better to get the list this way in my opinion:

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbp: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?label ?inst ?cls  
  WHERE {
?inst ?nameprop ?label .
FILTER (?nameprop=foaf:name || ?nameprop=rdfs:label) .
FILTER (lang(?label) = "en") .
?inst a ?cls .
FILTER (?cls = <http://dbpedia.org/ontology/Actor>
|| ?cls = <http://dbpedia.org/ontology/AdministrativeRegion>
|| ?cls = <http://dbpedia.org/ontology/AdultActor>
|| ?cls = <http://dbpedia.org/ontology/Airline>
|| ?cls = <http://dbpedia.org/ontology/Airport>
|| ?cls = <http://dbpedia.org/ontology/Ambassador>
|| ?cls = <http://dbpedia.org/ontology/AmericanFootballLeague>
|| ?cls = <http://dbpedia.org/ontology/AmericanFootballPlayer>
|| ?cls = <http://dbpedia.org/ontology/AmericanFootballTeam>
|| ?cls = <http://dbpedia.org/ontology/Architect>
|| ?cls = <http://dbpedia.org/ontology/ArchitecturalStructure>
|| ?cls = <http://dbpedia.org/ontology/Arena>
|| ?cls = <http://dbpedia.org/ontology/Artist>
|| ?cls = <http://dbpedia.org/ontology/Astronaut>
|| ?cls = <http://dbpedia.org/ontology/Athlete>
|| ?cls = <http://dbpedia.org/ontology/Atoll>
|| ?cls = <http://dbpedia.org/ontology/AustralianFootballLeague>
|| ?cls = <http://dbpedia.org/ontology/AustralianRulesFootballPlayer>
|| ?cls = <http://dbpedia.org/ontology/AutoRacingLeague>
|| ?cls = <http://dbpedia.org/ontology/BadmintonPlayer>
|| ?cls = <http://dbpedia.org/ontology/Band>
|| ?cls = <http://dbpedia.org/ontology/BaseballLeague>
|| ?cls = <http://dbpedia.org/ontology/BaseballPlayer>
|| ?cls = <http://dbpedia.org/ontology/BasketballLeague>
|| ?cls = <http://dbpedia.org/ontology/BasketballPlayer>
|| ?cls = <http://dbpedia.org/ontology/BasketballTeam>
|| ?cls = <http://dbpedia.org/ontology/BodyOfWater>
|| ?cls = <http://dbpedia.org/ontology/BowlingLeague>
|| ?cls = <http://dbpedia.org/ontology/Boxer>
|| ?cls = <http://dbpedia.org/ontology/BoxingLeague>
|| ?cls = <http://dbpedia.org/ontology/Bridge>
|| ?cls = <http://dbpedia.org/ontology/BritishRoyalty>
|| ?cls = <http://dbpedia.org/ontology/BroadcastNetwork>
|| ?cls = <http://dbpedia.org/ontology/Broadcaster>
|| ?cls = <http://dbpedia.org/ontology/Building>
|| ?cls = <http://dbpedia.org/ontology/CanadianFootballLeague>
|| ?cls = <http://dbpedia.org/ontology/CanadianFootballPlayer>
|| ?cls = <http://dbpedia.org/ontology/CanadianFootballTeam>
|| ?cls = <http://dbpedia.org/ontology/Canal>
|| ?cls = <http://dbpedia.org/ontology/Cardinal>
|| ?cls = <http://dbpedia.org/ontology/Cave>
|| ?cls = <http://dbpedia.org/ontology/Celebrity>
|| ?cls = <http://dbpedia.org/ontology/Chancellor>
|| ?cls = <http://dbpedia.org/ontology/ChessPlayer>
|| ?cls = <http://dbpedia.org/ontology/ChristianBishop>
|| ?cls = <http://dbpedia.org/ontology/City>
|| ?cls = <http://dbpedia.org/ontology/Cleric>
|| ?cls = <http://dbpedia.org/ontology/College>
|| ?cls = <http://dbpedia.org/ontology/CollegeCoach>
|| ?cls = <http://dbpedia.org/ontology/Comedian>
|| ?cls = <http://dbpedia.org/ontology/ComicsCharacter>
|| ?cls = <http://dbpedia.org/ontology/ComicsCreator>
|| ?cls = <http://dbpedia.org/ontology/Company>
|| ?cls = <http://dbpedia.org/ontology/Congressman>
|| ?cls = <http://dbpedia.org/ontology/Continent>
|| ?cls = <http://dbpedia.org/ontology/Country>
|| ?cls = <http://dbpedia.org/ontology/CricketLeague>
|| ?cls = <http://dbpedia.org/ontology/Cricketer>
|| ?cls = <http://dbpedia.org/ontology/Criminal>
|| ?cls = <http://dbpedia.org/ontology/CurlingLeague>
|| ?cls = <http://dbpedia.org/ontology/CyclingLeague>
|| ?cls = <http://dbpedia.org/ontology/Cyclist>
|| ?cls = <http://dbpedia.org/ontology/Deputy>
|| ?cls = <http://dbpedia.org/ontology/EducationalInstitution>
|| ?cls = <http://dbpedia.org/ontology/FictionalCharacter>
|| ?cls = <http://dbpedia.org/ontology/FieldHockeyLeague>
|| ?cls = <http://dbpedia.org/ontology/FigureSkater>
|| ?cls = <http://dbpedia.org/ontology/FormulaOneRacer>
|| ?cls = <http://dbpedia.org/ontology/GaelicGamesPlayer>
|| ?cls = <http://dbpedia.org/ontology/GeopoliticalOrganisation>
|| ?cls = <http://dbpedia.org/ontology/GolfLeague>
|| ?cls = <http://dbpedia.org/ontology/GolfPlayer>
|| ?cls = <http://dbpedia.org/ontology/GovernmentAgency>
|| ?cls = <http://dbpedia.org/ontology/Governor>
|| ?cls = <http://dbpedia.org/ontology/GridironFootballPlayer>
|| ?cls = <http://dbpedia.org/ontology/HandballLeague>
|| ?cls = <http://dbpedia.org/ontology/HistoricBuilding>
|| ?cls = <http://dbpedia.org/ontology/HistoricPlace>
|| ?cls = <http://dbpedia.org/ontology/HockeyTeam>
|| ?cls = <http://dbpedia.org/ontology/Hospital>
|| ?cls = <http://dbpedia.org/ontology/Hotel>
|| ?cls = <http://dbpedia.org/ontology/IceHockeyLeague>
|| ?cls = <http://dbpedia.org/ontology/IceHockeyPlayer>
|| ?cls = <http://dbpedia.org/ontology/Infrastructure>
|| ?cls = <http://dbpedia.org/ontology/InlineHockeyLeague>
|| ?cls = <http://dbpedia.org/ontology/Island>
|| ?cls = <http://dbpedia.org/ontology/Journalist>
|| ?cls = <http://dbpedia.org/ontology/Judge>
|| ?cls = <http://dbpedia.org/ontology/LacrosseLeague>
|| ?cls = <http://dbpedia.org/ontology/Lake>
|| ?cls = <http://dbpedia.org/ontology/LaunchPad>
|| ?cls = <http://dbpedia.org/ontology/LawFirm>
|| ?cls = <http://dbpedia.org/ontology/Legislature>
|| ?cls = <http://dbpedia.org/ontology/Library>
|| ?cls = <http://dbpedia.org/ontology/Lieutenant>
|| ?cls = <http://dbpedia.org/ontology/Lighthouse>
|| ?cls = <http://dbpedia.org/ontology/LunarCrater>
|| ?cls = <http://dbpedia.org/ontology/MartialArtist>
|| ?cls = <http://dbpedia.org/ontology/Mayor>
|| ?cls = <http://dbpedia.org/ontology/MemberOfParliament>
|| ?cls = <http://dbpedia.org/ontology/MilitaryPerson>
|| ?cls = <http://dbpedia.org/ontology/MilitaryUnit>
|| ?cls = <http://dbpedia.org/ontology/MixedMartialArtsLeague>
|| ?cls = <http://dbpedia.org/ontology/Model>
|| ?cls = <http://dbpedia.org/ontology/Monarch>
|| ?cls = <http://dbpedia.org/ontology/Monument>
|| ?cls = <http://dbpedia.org/ontology/MotorcycleRacingLeague>
|| ?cls = <http://dbpedia.org/ontology/Mountain>
|| ?cls = <http://dbpedia.org/ontology/MountainPass>
|| ?cls = <http://dbpedia.org/ontology/MountainRange>
|| ?cls = <http://dbpedia.org/ontology/Museum>
|| ?cls = <http://dbpedia.org/ontology/MusicalArtist>
|| ?cls = <http://dbpedia.org/ontology/NascarDriver>
|| ?cls = <http://dbpedia.org/ontology/NationalCollegiateAthleticAssociationAthlete>
|| ?cls = <http://dbpedia.org/ontology/NaturalPlace>
|| ?cls = <http://dbpedia.org/ontology/Non-ProfitOrganisation>
|| ?cls = <http://dbpedia.org/ontology/OfficeHolder>
|| ?cls = <http://dbpedia.org/ontology/Organisation>
|| ?cls = <http://dbpedia.org/ontology/PaintballLeague>
|| ?cls = <http://dbpedia.org/ontology/Park>
|| ?cls = <http://dbpedia.org/ontology/Person>
|| ?cls = <http://dbpedia.org/ontology/Philosopher>
|| ?cls = <http://dbpedia.org/ontology/Place>
|| ?cls = <http://dbpedia.org/ontology/PlayboyPlaymate>
|| ?cls = <http://dbpedia.org/ontology/PokerPlayer>
|| ?cls = <http://dbpedia.org/ontology/PolishKing>
|| ?cls = <http://dbpedia.org/ontology/PoliticalParty>
|| ?cls = <http://dbpedia.org/ontology/Politician>
|| ?cls = <http://dbpedia.org/ontology/PoloLeague>
|| ?cls = <http://dbpedia.org/ontology/Pope>
|| ?cls = <http://dbpedia.org/ontology/PopulatedPlace>
|| ?cls = <http://dbpedia.org/ontology/PowerStation>
|| ?cls = <http://dbpedia.org/ontology/President>
|| ?cls = <http://dbpedia.org/ontology/Priest>
|| ?cls = <http://dbpedia.org/ontology/PrimeMinister>
|| ?cls = <http://dbpedia.org/ontology/ProtectedArea>
|| ?cls = <http://dbpedia.org/ontology/PublicTransitSystem>
|| ?cls = <http://dbpedia.org/ontology/RadioControlledRacingLeague>
|| ?cls = <http://dbpedia.org/ontology/RadioStation>
|| ?cls = <http://dbpedia.org/ontology/RailwayLine>
|| ?cls = <http://dbpedia.org/ontology/RailwayTunnel>
|| ?cls = <http://dbpedia.org/ontology/RecordLabel>
|| ?cls = <http://dbpedia.org/ontology/Restaurant>
|| ?cls = <http://dbpedia.org/ontology/River>
|| ?cls = <http://dbpedia.org/ontology/Road>
|| ?cls = <http://dbpedia.org/ontology/RoadJunction>
|| ?cls = <http://dbpedia.org/ontology/RoadTunnel>
|| ?cls = <http://dbpedia.org/ontology/RouteOfTransportation>
|| ?cls = <http://dbpedia.org/ontology/Royalty>
|| ?cls = <http://dbpedia.org/ontology/RugbyLeague>
|| ?cls = <http://dbpedia.org/ontology/RugbyPlayer>
|| ?cls = <http://dbpedia.org/ontology/Saint>
|| ?cls = <http://dbpedia.org/ontology/School>
|| ?cls = <http://dbpedia.org/ontology/Scientist>
|| ?cls = <http://dbpedia.org/ontology/Senator>
|| ?cls = <http://dbpedia.org/ontology/Settlement>
|| ?cls = <http://dbpedia.org/ontology/ShoppingMall>
|| ?cls = <http://dbpedia.org/ontology/SiteOfSpecialScientificInterest>
|| ?cls = <http://dbpedia.org/ontology/SkiArea>
|| ?cls = <http://dbpedia.org/ontology/Skyscraper>
|| ?cls = <http://dbpedia.org/ontology/SnookerChamp>
|| ?cls = <http://dbpedia.org/ontology/SnookerPlayer>
|| ?cls = <http://dbpedia.org/ontology/SoccerClub>
|| ?cls = <http://dbpedia.org/ontology/SoccerLeague>
|| ?cls = <http://dbpedia.org/ontology/SoccerManager>
|| ?cls = <http://dbpedia.org/ontology/SoccerPlayer>
|| ?cls = <http://dbpedia.org/ontology/SoftballLeague>
|| ?cls = <http://dbpedia.org/ontology/SpeedwayLeague>
|| ?cls = <http://dbpedia.org/ontology/SpeedwayTeam>
|| ?cls = <http://dbpedia.org/ontology/SportsLeague>
|| ?cls = <http://dbpedia.org/ontology/SportsTeam>
|| ?cls = <http://dbpedia.org/ontology/Stadium>
|| ?cls = <http://dbpedia.org/ontology/Station>
|| ?cls = <http://dbpedia.org/ontology/Stream>
|| ?cls = <http://dbpedia.org/ontology/TelevisionStation>
|| ?cls = <http://dbpedia.org/ontology/TennisLeague>
|| ?cls = <http://dbpedia.org/ontology/TennisPlayer>
|| ?cls = <http://dbpedia.org/ontology/Theatre>
|| ?cls = <http://dbpedia.org/ontology/Town>
|| ?cls = <http://dbpedia.org/ontology/TradeUnion>
|| ?cls = <http://dbpedia.org/ontology/Tunnel>
|| ?cls = <http://dbpedia.org/ontology/University>
|| ?cls = <http://dbpedia.org/ontology/Valley>
|| ?cls = <http://dbpedia.org/ontology/VicePresident>
|| ?cls = <http://dbpedia.org/ontology/VicePrimeMinister>
|| ?cls = <http://dbpedia.org/ontology/VideogamesLeague>
|| ?cls = <http://dbpedia.org/ontology/Village>
|| ?cls = <http://dbpedia.org/ontology/VoiceActor>
|| ?cls = <http://dbpedia.org/ontology/VolleyballLeague>
|| ?cls = <http://dbpedia.org/ontology/VolleyballPlayer>
|| ?cls = <http://dbpedia.org/ontology/WaterwayTunnel>
|| ?cls = <http://dbpedia.org/ontology/WineRegion>
|| ?cls = <http://dbpedia.org/ontology/WorldHeritageSite>
|| ?cls = <http://dbpedia.org/ontology/Wrestler>
|| ?cls = <http://dbpedia.org/ontology/Writer>
|| ?cls = <http://xmlns.com/foaf/0.1/Person>
)
  }




PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbp-ont: <http://dbpedia.org/ontology/>
PREFIX geo:  <http://www.w3.org/2003/01/geo/wgs84_pos#>
select distinct ?locationuri ?label ?latitude ?longitude
{
?locationuri ?relation ?uk .
?locationuri a ?type . filter ( ?type = dbp-ont:PopulatedPlace || ?type=dbp-ont:Country || ?type=dbp-ont:City || ?type=dbp-ont:Town || ?type=dbp-ont:Village || ?type=dbp-ont:AdministrativeRegion || ?type=dbp-ont:Island  || ?type=dbp-ont:Continent) . 
?locationuri foaf:name ?label . filter (lang(?label)="en").
optional {
?locationuri geo:lat ?latitude .
?locationuri geo:long ?longitude .
}
filter (?relation= <http://dbpedia.org/ontology/country> || ?relation = <http://dbpedia.org/property/subdivisionName>) .
filter (?uk = dbpedia:United_Kingdom || ?uk = "United Kingdom"@en)

} order by ?locationuri

Find geonames URIs for each DBpedia URI


PREFIX owl:<http://www.w3.org/2002/07/owl#>
select ?x
{
<http://dbpedia.org/resource/Paris> owl:sameAs ?x
filter regex(str(?x), "geonames", "i")
}


Comments