Python 查找城市或国家的经纬度对

Python 查找城市或国家的经纬度对,python,twitter,streaming,Python,Twitter,Streaming,我正在尝试在Python中使用Twitter API,对于流式API,我需要向它传递如何查找城市、国家或任何位置的经度和纬度对 有没有办法在Python中找到这个对,或者有一个文件包含城市/国家/等的经度和纬度对 我需要一对经度和纬度,即两个经度和两个纬度。一个用于该位置的东北部,一个用于该位置的西南部(假设该城市可以用矩形表示)。一个快速谷歌显示: 将此CSV读入内存应该很简单 或者,由于您已经在线,您可以使用谷歌地图或其他API获取信息。使用非常有用的 代码: 谢谢你的回答。然而,这不是我

我正在尝试在Python中使用Twitter API,对于流式API,我需要向它传递如何查找城市、国家或任何位置的经度和纬度对

有没有办法在Python中找到这个,或者有一个文件包含城市/国家/等的经度和纬度对

我需要一对经度和纬度,即两个经度和两个纬度。一个用于该位置的东北部,一个用于该位置的西南部(假设该城市可以用矩形表示)。

一个快速谷歌显示:

将此CSV读入内存应该很简单

或者,由于您已经在线,您可以使用谷歌地图或其他API获取信息。

使用非常有用的

代码:


谢谢你的回答。然而,这不是我要问的。正如我所说,我需要一对经度和纬度,即两个经度和两个纬度。一个用于该位置的东北部,一个用于该位置的西南部(假设城市可以用矩形表示)。假设立交桥api可以提供城市边界信息:然后提供最大/最小lat/lon以获得边界框,你能给我举一个例子,比如如何找到伦敦的这对吗?看一看:实际上还没有测试,但这应该是一个好的开始。谢谢,这非常有用。只是一件小事,这段代码不适用于国家,你知道为什么吗?它使用谷歌地图API。我想限制在于此。注意:在没有注册的情况下,谷歌限制在给定时间段内的请求数量;如果超过这个标准,他们可能会将IP列入黑名单。
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")