到达县时出现几何问题连接

问题描述

我有什么?

我得到了以下df:

    id       latitude        longitude
0    0       31.23333         34.78333
1    1         nan               nan
2    2       42.70704        -71.16311
.
.
.

我该怎么办?

如果纬度/经度不是nan,我想添加一列国家名称

    id       latitude        longitude     country 
0    0       31.23333         34.78333      Israel
1    1         nan               nan
2    2       42.70704        -71.16311        USA
.
.
.


我尝试了什么?

    df['country'] = ''

    for index,row in df.iterrows():    
        print((row['latitude'],row['longitude']))
        if math.isnan(row['latitude']) or math.isnan(row['longitude']):
             continue
        else:
             geolocator = Nominatim(user_agent="row_{}".format(index))
             location = geolocator.reverse((row['latitude'],row['longitude']))
             row['country'] = location.raw['address']['country']

出了什么问题?

我遇到以下错误

requests.exceptions.SSLError: HTTPSConnectionPool(host='nominatim.openstreetmap.org',port=443): Max retries exceeded with url: /reverse?lat=31.23333&lon=34.78333&format=json&addressdetails=1 (Caused by SSLError(SSLCertVerificationError(1,'[SSL: CERTIFICATE_VERIFY_Failed] certificate verify Failed: self signed certificate in certificate chain (_ssl.c:1123)')))

我该如何解决这个问题?

解决方法

geopy文档包含熊猫用法的示例:https://geopy.readthedocs.io/en/stable/#module-geopy.extra.rate_limiter

import math

import pandas as pd
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter

df = pd.DataFrame([
    {"id": 0,"latitude": 31.23333,"longitude": 34.78333},{"id": 1,"latitude": float("nan"),"longitude": float("nan")},{"id": 2,"latitude": 42.70704,"longitude": -71.16311},])


geolocator = Nominatim(user_agent="specify_your_app_name_here")
reverse = RateLimiter(geolocator.reverse,min_delay_seconds=1)


def reverse_country(row):
    if math.isnan(row['latitude']) or math.isnan(row['longitude']):
        return ''
    else:
         location = reverse((row['latitude'],row['longitude']),language='en')
         if location is None:
            return ''
         return location.raw['address']['country']


df['country'] = df.apply(reverse_country,axis=1)

这将产生以下数据帧:

   id  latitude  longitude                   country
0   0  31.23333   34.78333                    Israel
1   1       NaN        NaN
2   2  42.70704  -71.16311  United States of America