Search code examples
rdfsparqljenageonamesopendata

How do I get a place (city, province, country) information from Geonames having the place's name using JENA and SPARQL?


Could someone help me with a SPARQL query to Geonames database to find a place's information, i.e., population? I have just the place's name. I am using JENA.


Solution

  • Solved using the Geonames Java Library (see http://www.geonames.org/source-code/). Here is the solution that worked for me:

    //First, must be registered on Geonames. Then, place the user here...
    WebService.setUserName("demo");
    
    ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria();
    searchCriteria.setQ("My City Name");
    searchCriteria.setStyle(Style.FULL);
    ToponymSearchResult searchResult;
    Long population;
    
    try {
        searchResult = WebService.search(searchCriteria);
        for (Toponym toponym : searchResult.getToponyms()) {
        population = toponym.getPopulation();
        }
    
    } catch (Exception e) {     
        e.printStackTrace();
    }
    
    return population;