Search code examples
rdfsparqlowldbpedia

Query cities and their country from dbpedia


I'm trying to construct an ontology in OWL for countries and cities(If such an implementation already exist, please point it to me)

To get the data, I'm trying to use sparql with the following code:

SELECT ?c WHERE {
   ?c rdfs:label ?name .
   ?c dbpedia-owl:country <http://dbpedia.org/resource/Country> .
}

I'm a newbie in sparql and dbpedia. The above code is not returning me all cities and the country they are situated in. I really to need to use dbpedia to achieve this.

any suggestion?


Solution

  • SPARQL is case sensitive (because RDF is case sensitive). DBpedia uses the convention that property names are start with a lower case letter (e.g. dbpedia-owl:country for "the country belonging to X is ...") and class names start with an upper case letter (e.g. dbpedia-owl:Country).

    If by works, I mean returns results, this works (City instead of city):

    SELECT DISTINCT ?city ?country 
    WHERE { ?city rdf:type dbpedia-owl:City ; 
                  rdfs:label ?label ; 
                  dbpedia-owl:country ?country 
    }
    

    Be aware, however, that not all places that you and me would call cities are defined as a dbpedia-owl:City - they may be of class dbpedia-owl:Settlement or other classes. So the above query may not yield all cities you want.

    Also: to construct an ontology, you don't need "the data". An ontology defines for example that all cities are populated places, and that all populated places are places. To be able to say that, you don't need to know that Edinburgh is in the United Kingdom.