使用 Elevation API 获取火星上地标的海拔高度(标记为 Google Earth Pro)
Getting the elevations of placemarks on Mars (mark with Google Earth Pro)with Elevation API
我经常在 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()
对于标记在火星上的位置,我可以像上面的海拔请求方式一样获得海拔吗?你能推荐一些方法吗。
问题自己解决了!
我会分享解决方案。
我决定参考the Mars MGS MOLA - MEX HRSC Blended DEM(.tif)。
使用 QGIS,我将 .tif 文件转换为 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)
转换后,我参考了 ndarray 中的高程值和来自基于 DEM 地理空间信息重新缩放的 .kml 文件的坐标:
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()
谢谢!
我经常在 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()
对于标记在火星上的位置,我可以像上面的海拔请求方式一样获得海拔吗?你能推荐一些方法吗。
问题自己解决了!
我会分享解决方案。
我决定参考the Mars MGS MOLA - MEX HRSC Blended DEM(.tif)。
使用 QGIS,我将 .tif 文件转换为 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)
转换后,我参考了 ndarray 中的高程值和来自基于 DEM 地理空间信息重新缩放的 .kml 文件的坐标:
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()
谢谢!