【问题标题】:Error 502 while trying to use geopy and ArcGIS尝试使用 geopy 和 ArcGIS 时出现错误 502
【发布时间】:2023-03-13 14:59:01
【问题描述】:

所以我一直遇到我的代码收到 HTTP 错误 502:Bad Gateway 的问题。我有基本上相同的代码可以正常工作,只是数据文件更小。只是数据量搞砸了还是我创建了另一个问题。感谢您的帮助!

    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】:

我最终解决了这个问题,将数据分成 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-23
    • 2011-06-04
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多