使用简单的python脚本从纬度经度坐标中获取高程

问题描述 投票:-3回答:1

我有一个从此question获得的python脚本,该脚本将从USGS高程点查询服务中提取。但是,它会不断超时,并在看似随机的时间之后以及查询结束之前将我踢出局。给定纬度坐标,我需要另一种方法来提取高程数据。

这是我当前的查询:

# ========= pull elev from usgs server ======

# USGS POINT QUERY SERVICE ==================

url = r'https://nationalmap.gov/epqs/pqs.php?'
# ===========================================

# coordinates with known elevation 
lat = [48.633, 48.733, 45.1947, 45.1962]
lon = [-93.9667, -94.6167, -93.3257, -93.2755]

# create df
df = pd.DataFrame({
    'lat': lat,
    'lon': lon
})

def elevation_function(df, lat_column, long_column):
    elevations = []
    counter = 0
    start = time.time()
    for lat, lon in zip(df[lat_column], df[long_column]):

        # define rest query params
        params = {
            'output': 'json',
            'x': lon,
            'y': lat,
            'units': 'Meters'
        }

        # format query string and return query value
        result = requests.get((url + urllib.parse.urlencode(params)))
        elevations.append(result.json()['USGS_Elevation_Point_Query_Service']['Elevation_Query']['Elevation'])
        counter += 1
        print('Proportion of job complete: {}'.format(round(counter/df.shape[0],3)))
        end = time.time()
        print(str(round(end - start)) + " seconds into job\n")
    df['elev'] = elevations
    return elevations

start = time.time()
count = 0
for i in range(100):
    count += 1
    elevations = elevation_function(df, lat_column='lat', long_column='lon')
end = time.time()

print(str(round(end - start)))
python pandas gis
1个回答
0
投票

简化功能并添加错误处理:

  • elevation_function需要写入才能与pandas.DataFrame.apply一起使用
    • pandas.DataFrame.apply一起使用apply,将自动迭代每一行坐标

新功能:

  • axis=1将继续发出请求,直到获得make_remote_request
  • 可选,response并在import time之前添加time.sleep(5),以与远程服务器配合使用。
continue

实现功能

def make_remote_request(url: str, params: dict) -> json:
    """
    Makes the remote request
    Continues making attempts until it succeeds
    """

    count = 1
    while True:
        try:
            response = requests.get((url + urllib.parse.urlencode(params)))
        except (OSError, urllib3.exceptions.ProtocolError) as error:
            print('\n')
            print('*' * 20, 'Error Occured', '*' * 20)
            print(f'Number of tries: {count}')
            print(f'URL: {url}')
            print(error)
            print('\n')
            count += 1
            continue
        break

    return response


def eleveation_function(x):
    url = 'https://nationalmap.gov/epqs/pqs.php?'
    params = {'x': x[1],
              'y': x[0],
              'units': 'Meters',
              'output': 'json'}
    result = make_remote_request(url, params)
    return result.json()['USGS_Elevation_Point_Query_Service']['Elevation_Query']['Elevation']
© www.soinside.com 2019 - 2024. All rights reserved.