Sometimes you'd like to have a simple function that gives the geographic coordinates of a city by giving it just the name of that city. You may not be interested in installing any third-party libraries for this simple task.
In this simple screen-scraping example, we use the Google Maps URL to query the latitude and longitude of a city. The URL used to query can be found after making a custom search on the Google Maps page. We can perform the following steps to extract some information from Google Maps.
Let us take the name of a city from the command line using the argparse
module.
We can open the maps search URL using the urlopen()
function of urllib
. This will give an XML output if the URL is correct.
Now, process the XML output in order to get the geographic coordinates of that city.
Listing 6.2 helps finding the geographic coordinates of a city using Google Maps, as shown:
#!/usr/bin/env python # Python Network Programming Cookbook -- Chapter - 6 # This program is optimized for Python 2.7. # It may run on any other version with/without modifications. import argparse import os import urllib ERROR_STRING = '<error>' def find_lat_long(city): """ Find geographic coordinates """ # Encode query string into Google maps URL url = 'http://maps.google.com/?q=' + urllib.quote(city) + '&output=js' print 'Query: %s' % (url) # Get XML location from Google maps xml = urllib.urlopen(url).read() if ERROR_STRING in xml: print ' Google cannot interpret the city.' return else: # Strip lat/long coordinates from XML lat,lng = 0.0,0.0 center = xml[xml.find('{center')+10:xml.find('}',xml.find('{center'))] center = center.replace('lat:','').replace('lng:','') lat,lng = center.split(',') print "Latitude/Longitude: %s/%s " %(lat, lng) if __name__ == '__main__': parser = argparse.ArgumentParser(description='City Geocode Search') parser.add_argument('--city', action="store", dest="city", required=True) given_args = parser.parse_args() print "Finding geographic coordinates of %s" %given_args.city find_lat_long(given_args.city)
If you run this script, you should see something similar to the following:
$ python 6_2_geo_coding_by_google_maps.py --city=London Finding geograhic coordinates of London Query: http://maps.google.com/?q=London&output=js Latitude/Longitude: 51.511214000000002/-0.119824
This recipe takes a name of a city from the command line and passes that to the find_lat_long()
function. This function queries the Google Maps service using the urlopen()
function of urllib
and gets the XML output. Then, the error string '<error>'
is searched for. If that's not present, it means there are some good results.
If you print out the raw XML, it's a long stream of characters produced for the browser. In the browser, it would be interesting to display the layers in maps. But in our case, we just need the latitude and longitude.
From the raw XML, the latitude and longitude is extracted using the string method find()
. This searches for the keyword "center". This list key possesses the geographic coordinates information. But it also contains the additional characters which are removed using the string method replace()
.
You may try this recipe to find out the latitude/longitude of any known city of the world.
3.17.76.175