Python module for getting latitude and longitude from the name of a US city
Asked Answered
R

3

32

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.

Redraft answered 3/12, 2012 at 15:8 Comment(1)
medium.com/python-in-plain-english/… there's a detailed tutorial here.Irreformable
F
45

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))
Fieldfare answered 3/12, 2012 at 15:9 Comment(6)
I had to create an account in geonames.org/login, enable free web services for my account and issue gn = geocoders.GeoNames(username='account_username')Granlund
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?Salesperson
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)Salesperson
@Salesperson I'm trying to find it too, here somewhere? geonames.org/exportGreenstein
@Salesperson yes, it's beneath the login box if you clicked your username on the right top corner of the website.Iq
For me the enable button was at geonames.org/manageaccountHolmquist
B
38

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'}
Brewton answered 29/8, 2014 at 14:8 Comment(2)
Giving me the error : Using Nominatim with the default "geopy/1.17.0" user_agent is strongly discouraged, as it violates Nominatim's ToS 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"Phore
Still works great in 2022, no API key required.Rudyrudyard
S
3

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.

Soandso answered 29/1, 2022 at 13:6 Comment(1)
Bing Maps! Excellent answer. +1Hydrophyte

© 2022 - 2024 — McMap. All rights reserved.