I often mark great locations on Google Earth Pro & Mars with placemarks and export them to kml files (and then convert to CSV). At this time, the kml files include the coordinates of locations but not the elevations.
In the case of locations marked on Earth, I can get their elevation via Elevation API of Google Map Platform with Python (https://developers.google.com/maps/documentation/elevation/requests-elevation), referring to the coordinates included in 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()
In the case of locations marked on Mars, Could I get the elevations the same as the above way of Elevation requests? Could you recommend ways for doing this.