Python:地理编码和提取经度,纬度,城市

时间:2017-09-19 11:04:13

标签: python geocoding arcgis nominatim

我有一个包含地址的数据框,我使用下面的代码分别提取经度和纬度。有没有办法一起提取经度和纬度,以及使用同样的方法提取城市?

在我的“添加”数据框的地址栏中,我的地址格式如下:“35 Turkey Hill Rd Rt 21 Ste 101,Belchertown,MA 01007”

我使用的是Python 3,Spyder IDE和Windows 10桌面。

示例数据:
地址
415 E Main St,Westfield,MA 01085
200 Silver St,Agawam,MA 01001
35 Turkey Hill Rd Rt 21 Ste 101,Belchertown,MA 01007

from geopy.geocoders import Nominatim, ArcGIS, GoogleV3
#from geopy.exc import GeocoderTimedOut

arc=ArcGIS(timeout=100)
nom = Nominatim(timeout=100)
goo = GoogleV3(timeout=100)

geocoders = [arc,nom, goo]

# Capture Longitude
def geocodelng(address):
    i = 0
    try:
        while i < len(geocoders):
            # try to geocode using a service
            location = geocoders[i].geocode(address)

            # if it returns a location
            if location != None:

                # return those values
                return location.longitude
            else:
                # otherwise try the next one
                i += 1
    except:
        # catch whatever errors, likely timeout, and return null values
        print sys.exc_info()[0]
        return ['null','null']

    # if all services have failed to geocode, return null values
    return ['null','null']


#Extract co-ordinates
add['longitude']=add['Address'].apply(geocodelng)


# Capture Latitude
def geocodelat(address):
    i = 0
    try:
        while i < len(geocoders):
            # try to geocode using a service
            location = geocoders[i].geocode(address)

            # if it returns a location
            if location != None:

                # return those values
                return location.latitude
            else:
                # otherwise try the next one
                i += 1
    except:
        # catch whatever errors, likely timeout, and return null values
        print sys.exc_info()[0]
        return ['null','null']

    # if all services have failed to geocode, return null values
    return ['null','null']


#Extract co-ordinates
add['latitude']=add['Address'].apply(geocodelat)    

0 个答案:

没有答案