Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 334
Deepak B Avatar asked Dec 03 '12 15:12

Deepak B


People also ask

How do I get the latitude and longitude of a city in Python?

Import Nominatim geocoder from geopy module. Initialize the Nominatim API and use the geocode method to get the location of the input string. Finally, get the latitude and longitude of the location by location. latitude and location.

How do I find the latitude and longitude for an entire address?

The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);


2 Answers

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)) 
like image 154
eumiro Avatar answered Sep 26 '22 05:09

eumiro


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'} 
like image 31
jmunsch Avatar answered Sep 25 '22 05:09

jmunsch