在尝试使用geopy和ArcGIS时出现502错误。

问题描述 投票:0回答:1

所以我一直遇到一个问题,我的代码得到一个HTTP错误502:坏网关。我有基本相同的代码,但工作正常,只是有一个较小的数据文件。是数据量太大,还是我创造了另一个问题。谢谢你的帮助

    import pandas
    from geopy.geocoders import ArcGIS
    nom = ArcGIS(timeout=300)
    info = pandas.read_csv('file here')
    info['Address'] = info['city'] + ', ' + info['state'] + ', ' + 'USA'
    info['Coordinates'] = info['Address'].apply(nom.geocode)
    print(info)
        Traceback (most recent call last):
        File "C:\Users\roger\AppData\Local\Programs\Python\Python38\lib\site-packages\geopy\geocoders\base.py", line 367, in _call_geocoder
    page = requester(req, timeout=timeout, **kwargs)
        File "C:\Users\roger\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
        File "C:\Users\roger\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 640, in http_response
    response = self.parent.error(
        File "C:\Users\roger\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 569, in error
    return self._call_chain(*args)
       File "C:\Users\roger\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
       File "C:\Users\roger\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
    urllib.error.HTTPError: HTTP Error 502: Bad Gateway

During handling of the above exception, another exception occurred:

       Traceback (most recent call last):
       File "C:/Users/roger/PycharmProjects/", line 9, in <module>
    info['Coordinates'] = info['Address'].apply(nom.geocode)
       File "C:\Users\roger\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 3848, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
       File "pandas\_libs\lib.pyx", line 2329, in pandas._libs.lib.map_infer
       File "C:\Users\roger\AppData\Local\Programs\Python\Python38\lib\site-packages\geopy\geocoders\arcgis.py", line 197, in geocode
    response = self._call_geocoder(url, timeout=timeout)
       File "C:\Users\roger\AppData\Local\Programs\Python\Python38\lib\site-packages\geopy\geocoders\base.py", line 389, in _call_geocoder
    raise ERROR_CODE_MAP[http_code](message)
       geopy.exc.GeocoderServiceError: HTTP Error 502: Bad Gateway

python-3.x arcgis geopy
1个回答
0
投票

我最终通过将数据以50组的形式批量化解决了这个问题。这是我能做的最多的事情,没有ArcGIS给我一个502错误。希望这能帮到你

    info['Address'] = info['city'] + ', ' + info['state'] + ', ' + 'USA'
    total_length = len(info)
    def make_file(data):
         data.to_csv('file here', mode='a')
    query_start = 0
    query_end = query_start + 50
    while query_start < total_length + 1:
        brevinfo = info[query_start:query_end]
        brevinfo['Coordinates'] = brevinfo['Address'].apply(nom.geocode)
        brevinfo["Latitude"] = brevinfo["Coordinates"].apply(lambda x: 
                               x.latitude if x != None else None)
        brevinfo["Longitude"] = brevinfo["Coordinates"].apply(lambda x: 
                                x.longitude if x != None else None)
        print(brevinfo)
        make_file(brevinfo)
        query_start += 50
        query_end += 50
        time.sleep(1)
© www.soinside.com 2019 - 2024. All rights reserved.