Mihiro Key
Mihiro Key

Reputation: 21

Getting the elevations of placemarks on Mars (mark with Google Earth Pro)with Elevation API

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.

Upvotes: 1

Views: 397

Answers (1)

Mihiro Key
Mihiro Key

Reputation: 21

Problem solved by self!

I will share the solution.

I decided to refer to the Mars MGS MOLA - MEX HRSC Blended DEM(.tif).

Using the QGIS, I converted the .tif file to ndarray:

from osgeo import gdal
import numpy as np

path = '/PATH/Mars_HRSC_MOLA_BlendDEM_Global_200mp_v2.tif'
src = gdal.Open(path, gdal.GA_ReadOnly)
elv = src.GetRasterBand(1)
elv_arr = elv.ReadAsArray()
print(elv_arr.shape)

np.save('/PATH/MOLA-HRSC_elevation_ndarray', elv_arr)

After converting, I referred to elevation values in ndarray with coordinates from a .kml file rescaling based on the DEM Geospatial Information:

import numpy as np
import xml.etree.ElementTree as ET
import csv
import unicodedata


# Load KML
tree = ET.parse('placemarks_mars.kml')
root = tree.getroot()

# CSV format
placeDict = dict.fromkeys(["placemarks_name","latitude","longitude","elevation"],"")

# Load MOLA-HRSC_DEM ndarray
elemap = np.load('PATH/MOLA-HRSC_elevation_ndarray.npy')
# Center of MOLA-HRSC_DEM ndarray as xy origin
pixel_origin = np.array([elemap.shape[0]//2, elemap.shape[1]//2])

f = open('placemarks_mars.csv', "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]

        ### ndarray of Lat&Lon at locations
        LatLon_coord = np.array([coordArray[1], coordArray[0]], dtype='float64')
        ### y-axis(Lat): Positive Down(Positive South) to y-axis(Lat): Positive Up(Positive North). *x-axis(Lon) is Positive Right(Positive East)
        np.put(LatLon_coord,[0],LatLon_coord[0] * -1)

        ### Lat&Lon at locations to xy
        ### Scale(pixels/degree) = 296.3735
        ### https://astrogeology.usgs.gov/search/map/Mars/Topography/HRSC_MOLA_Blend/Mars_HRSC_MOLA_BlendDEM_Global_200mp_v2
        datapoint = np.floor(pixel_origin + LatLon_coord * 296.3735)
        datapoint = np.array (datapoint, dtype='int64')

        ### See elevations
        placeDict["elevation"] = elemap[datapoint[0], datapoint[1]]
   
        w.writerow(placeDict)  ### to CSV
        
f.close()

Thanks!

Upvotes: 1

Related Questions