0

I am using the following code:

from geopy import geocoders   

def main():
    gn = geocoders.GeoNames()
    city = 'roman'
    place, (lat, lng) = gn.geocode_url('http://www.geonames.org/advanced-search.html?q='+city+'&country=FR&featureClass=A&continentCode=&fuzzy=0.6')
    location, (lat, lon) = geocodes[0]
    print lat, lon

I want to print the first result returned from the Geopy website for a city, given the specific URL configurations (in France, feature = A, and fuzzy = .6)

However, I keep getting a "No JSON Object can be decoded" error from the above code. What's the issue?

Parseltongue
  • 11,157
  • 30
  • 95
  • 160

2 Answers2

1

You're supposed to use the JSON webservice:

url = 'http://ws.geonames.org/searchJSON?q=%s&country=FR&featureClass=A&continentCode=&fuzzy=0.6'
gn.geocode_url(url % city)

The proper way to add more params is to use urlencode and the url geocode uses:

from urllib import urlencode
params = {
    'q': 'roman',
    'featureClass': 'A',
    'fuzzy': '0.6',
    'country': 'FR'
}
gn.geocode_url(gn.url % urlencode(params))
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
0

I'm not directly familiar with geopy or the GeoNames database, but did you mean to be requesting something from their web service? The URL you've given seems to return a normal webpage rather than a JSON response, unless there's something tricky going on.

The API seems to have similar arguments, though it does require a username/account. Perhaps you meant:

http://api.geonames.org/searchJSON?q=paris&country=FR&featureClass=A&continentCode=&fuzzy=0.6&maxRows=10&username=demo
Michael C. O'Connor
  • 9,742
  • 3
  • 37
  • 49
  • `ws.geonames.org` doesn't require credentials. – Pavel Anossov Feb 25 '13 at 22:47
  • Strangely, it appears to be deprecated in favour of `api.` for more than a year, but still works and is used by `geopy`. – Pavel Anossov Feb 25 '13 at 22:49
  • @PavelAnossov Interesting. It looks like they're in the process of transitioning to new system (with rate limits), and the `ws.` hostname may be discontinued at some indeterminate point in the future: http://forum.geonames.org/gforum/posts/list/2206.page – Michael C. O'Connor Feb 25 '13 at 22:59