Search code examples
wikipediageonames

Use GeoNames Java API to retrieve coordinates given Wikipedia article title


Say I have the title of a Wikipedia article, e.g. Philadelphia, and I want the coordinates of that place. I don't want to get this from Wikipedia because it looks like I'd have to retrieve the whole article. And I don't want to just loosely query GeoNames for places "like philadelphia" because that will have lots of results, and I know I want the specific Wikipedia article [[Philadelphia]]'s coordinates.

It seems like the GeoNames API allows you to do this with the class WikipediaArticle and its methods like setTitle() and getLatitude(). But I've been fiddling around with that class and WebService and haven't been able to retrieve the coordinates that way. I won't bore you with all the combinations I've tried, but I can't find an example of how to do it. Something like:

WebService.setUserName("whateveritis");
List<WikipediaArticle> wikiResults = WebService.wikipediaSearchForTitle(
                            "Philadelphia","en");
for (WikipediaArticle r : wikiResults) {
    System.out.println(r.getLatitude());
}

That gives a lot of errors, that I can share, but make me think I'm just using it wrong. Or maybe:

WikipediaArticle myPlace = new WikipediaArticle();
myPlace.setTitle("Philadelphia");
Double lat = myPlace.getLatitude();

This returns 0.0. Is anyone familiar with API and can tell me where I'm going wrong? I do think it's meant to do stuff like this because the web version does it: http://api.geonames.org/wikipediaSearch?title=Philadelphia&maxRows=1&username=demo


Solution

  • The first way, using WebService.wikipediaSearchForTitle, is the way to do this, but you have to have jdom set up properly in the classpath--which I thought I had, but didn't. However, note that the function returns any article with "Philadelphia" in the title, not just exactly Phildelphia. So the function, even when working, didn't do quite what I needed.