29

I am looking for a python module which can take in the name of the city as the input and return the latitude and longitude of the input.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Deepak B
  • 2,245
  • 5
  • 26
  • 28

3 Answers3

44

Have a look at geopy. In the "getting started" documentation it shows:

>>> from geopy import geocoders  
>>> gn = geocoders.GeoNames()

>>> print gn.geocode("Cleveland, OH 44106")
(u'Cleveland, OH, US', (41.4994954, -81.6954088))

>>> gn.geocode("Cleveland, OH", exactly_one=False)[0]
(u'Cleveland, OH, US', (41.4994954, -81.6954088))
jmunsch
  • 22,771
  • 11
  • 93
  • 114
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 21
    I had to create an account in http://www.geonames.org/login, enable free web services for my account and issue `gn = geocoders.GeoNames(username='account_username')` – Mr_and_Mrs_D Feb 02 '18 at 12:04
  • I've been looking for quite a while but can't figure out the "enable free web services" step. Any idea where this is on the website? – sapo_cosmico Mar 30 '21 at 00:28
  • 2
    Edit: found it. It's a bit counter intuitive, it's a link beneath the login and password boxes (looks as if you need to login, but in fact it happens outside) – sapo_cosmico Mar 30 '21 at 00:55
  • @sapo_cosmico I'm trying to find it too, here somewhere? http://www.geonames.org/export/ – Olli May 04 '22 at 12:04
  • @sapo_cosmico yes, it's beneath the login box if you clicked your username on the right top corner of the website. – Youth overturn May 26 '22 at 09:00
  • For me the enable button was at https://www.geonames.org/manageaccount – kevinlinxc Oct 11 '22 at 22:14
37

An example:

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent='myapplication')
location = geolocator.geocode("Chicago Illinois")
print(location.address)

Available info:

>>> location.raw
{u'display_name': u'Chicago, Cook County, Illinois, United States of America', u
'importance': 1.0026476104889, u'place_id': u'97957568', u'lon': u'-87.6243706',
 u'lat': u'41.8756208', u'osm_type': u'relation', u'licence': u'Data \xa9 OpenSt
reetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright', u'osm_i
d': u'122604', u'boundingbox': [u'41.6439170837402', u'42.0230255126953', u'-87.
9401016235352', u'-87.5239791870117'], u'type': u'city', u'class': u'place', u'i
con': u'http://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.p
ng'}
jmunsch
  • 22,771
  • 11
  • 93
  • 114
  • Giving me the error : Using Nominatim with the default "geopy/1.17.0" `user_agent` is strongly discouraged, as it violates Nominatim's ToS https://operations.osmfoundation.org/policies/nominatim/ and may possibly cause 403 and 429 HTTP errors. Please specify a custom `user_agent` with `Nominatim(user_agent="my-application")` or by overriding the default `user_agent`: `geopy.geocoders.options.default_user_agent = "my-application"` – Sarang Manjrekar Nov 16 '18 at 13:44
  • 3
    Still works great in 2022, no API key required. – squarespiral Jun 12 '22 at 13:12
2

You can also use geocoder with Bing Maps API. The API gets latitude and longitude data for all addresses for me (unlike Nominatim) and it has a pretty good free version for non-commercial uses (max 125000 requests per year for free). To get free API, go here and click "Get a free Basic key". After getting your API, you can use it in the code below:

import geocoder # pip install geocoder
g = geocoder.bing('Tokyo', key='<API KEY>')
results = g.json
print(results['lat'], results['lng'])

Output:

>>> 35.68696212768555 139.7494659423828

The results contains much more information than longitude and latitude. Check it out.

Sayyor Y
  • 1,130
  • 2
  • 14
  • 27