在Loop中运行时,反向地理编码失败

时间:2018-04-19 13:40:30

标签: python geocoding

我正在尝试进行反向地理编码并提取很长时间的密码。 .csv文件有大约100万条记录..

以下是我的问题

1. Google API failing to give address for large records, and taking huge amount of time. I will later move it to Batch-Process though.
2. I tried to split the file into chunks and ran few files manually one by one (1000 records in each file after splitting), then i surprisingly get 100% result.
3. Later, I ran in loop one by one, again, Google API fails to give the result

Note: Right now we are looking for free API's only

**Below is my code**
def reverse_geocode(latlng):
    result = {}
    url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng={}'    
    request = url.format(latlng)              
    key= '&key=' + api_key
    request = request + key       
    data = requests.get(request).json()   
    if len(data['results']) > 0:
        result = data['results'][0]
    return result
def parse_postal_code(geocode_data):
    if (not geocode_data is None) and ('formatted_address' in geocode_data):
       for component in geocode_data['address_components']:
            if 'postal_code' in component['types']:
                return component['short_name']
    return None

dfinal = pd.DataFrame(columns=colnames)
dmiss = pd.DataFrame(columns=colnames)

for fl in files:
    df = pd.read_csv(fl)
    print ('Processing file : '  + fl[36:]) 
    df['geocode_data'] = ''        
    df['Pincode'] = ''  
    df['geocode_data']=df['latlng'].map(reverse_geocode)
    df['Pincode'] = df['geocode_data'].map(parse_postal_code)
    if (len(df[df['Pincode'].isnull()]) > 0):
        d0=df[df['Pincode'].isnull()]
        print("Missing Picodes : " + str(len(df[df['Pincode'].isnull()])) + " / " + str(len(df)))
        dmiss.append(d0)
        d0=df[~df['Pincode'].isnull()]
        dfinal.append(d0)
    else:
        dfinal.append(df)

有人可以帮帮我,我的代码中有什么问题?或者如果需要任何其他信息,请告诉我....

1 个答案:

答案 0 :(得分:1)

您已遇到Google API使用限制。