【发布时间】:2022-01-27 13:39:51
【问题描述】:
我经常在 Google Earth Pro 和 Mars 上用地标标记好位置,然后将它们导出为 kml 文件(然后转换为 CSV)。此时,kml 文件包含位置坐标,但不包含高程。
对于地球上标记的位置,我可以通过 Google Map Platform 的 Elevation API with Python (https://developers.google.com/maps/documentation/elevation/requests-elevation) 获取它们的海拔高度,参考 kml 中包含的坐标。
import requests
import xml.etree.ElementTree as ET
import csv
import unicodedata
# load KML
tree = ET.parse('placemarks.kml')
root = tree.getroot()
# CSV
placeDict = dict.fromkeys(["placemarks_name","latitude","longitude","altitude"],"")
outputFileName = "placemarks.csv"
f = open(outputFileName, "w")
w = csv.DictWriter(f, placeDict.keys())
w.writeheader()
for line in root.iter('*'):
# placemarks_name
if line.tag == '{http://www.opengis.net/kml/2.2}name':
placeDict["placemarks_name"] = line.text
# coordinates
if line.tag == '{http://www.opengis.net/kml/2.2}coordinates':
coordArray = line.text.split(",")
placeDict["longitude"] = coordArray[0]
placeDict["latitude"] = coordArray[1]
# Google API key
YOUR_API_KEY = 'myapikey'
latitude = coordArray[1]
longitude = coordArray[0]
# Output in xml style
url = 'https://maps.googleapis.com/maps/api/elevation/xml?locations=' + latitude + ',' + longitude + '&key=' + YOUR_API_KEY
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
# Convert to xml
root = ET.fromstring(response.text)
# pick up elevation data
for elevation in root.iter('elevation'):
placeDict["altitude"] = elevation.text
w.writerow(placeDict) # to csv
f.close()
如果是在火星上标记的位置,我可以像上面的海拔请求方式一样获得海拔吗?你能推荐一些方法吗?
【问题讨论】:
标签: python google-earth google-elevation-api