Question

I am trying to use Twitter API in Python, and for the streaming API I need to pass how to find pair of longitude and latitude for a city or country or any location to it.

Is there a way to find this Pair in Python, or is there a file that has the pair of longitude and latitude for cities/countries/etc?

I need a pair of longitude and latitude, i.e. two longitudes and two latitudes. One for north-east of the location and one for south-west of that location (assuming the city can be represented with a rectangle).

Was it helpful?

Solution

Using the ever useful, BeautifulSoup.

Code:

import re
import urllib2

# Import Custom libraries
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup

def render_google_uri(idict):
    '''
    Render the appropriate Google Map Api request uri
    '''
    base_url = "http://maps.googleapis.com/maps/api/geocode/xml?"
    options = [(key,re.sub("\s+", "+", value)) for (key, value) in idict.items()]

    options = map(lambda x: "=".join(x), options)
    options = "&".join(options)
    url = base_url + options
    return url

def get_street_position(*args):
    '''
    Longitude and Latitude from Street Address
    '''
    def is_result(itag):
        if itag.name == "result":
            if itag.type.text == "locality":
                return True
        return False

    ret_list = []
    for address in args:
        google_api_dict = \
        {
            "address" : address,
            "sensor"  : "false",
        }
        request_uri = render_google_uri(google_api_dict)
        request = urllib2.Request(request_uri, None, {})

        try:
            response = urllib2.urlopen(request)
            the_page = response.read()
        except Exception:
            the_page = ""

        if the_page:
            pool = BeautifulStoneSoup(the_page)
            result = pool.find(is_result)

            if result:
                bounds = result.find("bounds")

                if bounds:

                    cur_dict = \
                    {
                        "Address"        : address,
                        "Google Address" : result.formatted_address.text,
                        "Bounds"         : \
                        {
                            "SW" :
                            {
                                "Longitude" : bounds.southwest.lng.text,
                                "Latitude"  : bounds.southwest.lat.text
                            },
                            "NE" :
                            {
                                "Longitude" : bounds.northeast.lng.text,
                                "Latitude"  : bounds.northeast.lat.text
                            }
                        }
                    }
                    ret_list += [cur_dict]

    return ret_list

if __name__ == "__main__":
    print get_street_position("Sydney", "Marrakech")

OTHER TIPS

A quick google revealed: http://www.kindle-maps.com/blog/csv-location-of-all-the-cities-in-the-world.html

Should be trivial to read this CSV into memory.

Alternatively since you're already online you could use Google Maps or some other API to pull the info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top