#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+importgooglemaps
+importnumpyasnp
+
+
+defrequest_elevation(xmin,xmax,xinc,ymin,ymax,yinc,maxnum=512):
+"""Request elevation and return them with a list
+
+ Parameter
+ =========
+ xmin : float
+ minimum longitude, unit is degree
+ xmax : float
+ maximum longitude, unit is degree
+ xinc : float
+ increment of longitude
+
+ ymin : float
+ minimum latitude, unit is degree
+ ymax : float
+ maximum latitude, unit is degree
+ yinc : float
+ increment of latitude
+ maxnum : int
+ point number of one request, maximum value is 512
+ """
+
+xlist=np.arange(xmin,xmax,xinc)
+ylist=np.arange(ymin,ymax,yinc)
+points=[(y,x)foryinylistforxinxlist]
+
+requestnum=int(np.floor(len(points)/maxnum)+1)
+npoints=np.array_split(points,requestnum)
+ifrequestnum>2500:
+print("Error: Request number exceed! Change grid or region.")
+
+return[gmaps.elevation(locations=loc.tolist())forlocinnpoints]
+
+
+# 设置要下载高程数据的经度范围、纬度范围以及网格间隔
+xmin,xmax,xinc=120.000,129.005,0.00005
+ymin,ymax,yinc=30.000,30.005,0.00005
+
+gmaps=googlemaps.Client(key='YOU_API_KEY')
+
+# Obtain all elevations
+# 请设置 maxnum 确保每一次请求的数据点数不超过 512 个以控制请求字符串长度
+elevations=request_elevation(xmin,xmax,xinc,ymin,ymax,yinc,maxnum=512)
+
+# output data with format `longitude latitude elevation resolution`
+withopen("Elevations.dat","w")asf:
+forloclistinelevations:
+forsublocinloclist:
+outputstr="{:.5f}{:.5f}{:.5f}{:.5f}\n".format(
+subloc['location']['lng'],
+subloc['location']['lat'],
+subloc['elevation'],
+subloc['resolution'])
+f.writelines(outputstr)
+