1

I could use some help using the google maps Elevation API directly in QGIS for downloading elevation data in order to create a high resolution DEM for various terrains ranging from 10 to 100 ha.

I tried the OpenTopography DEM donwloader plugin that can get elevation from various sources such as SRTM, EU DEM, Copernicus etc, but Google elevation values that I can see in google Earth seem to be very significantly more accurate for my region.

Currently I am creating regular point grids in QGIS (with 1-5 meter distance). By copy pasting the (10000+) coordinates into the GPS visualizer to eventually get the elevation. (see: https://www.gpsvisualizer.com/geocoder/elevation.html) This works, but due to data limits from google the process takes many hours and often my browser becomes unresponsive and crashes...I could subdivide larger grids into smaller grids, to reduce the amount of coordinates per request, but this is tedious.

Ideally I would like to use the API directly in QGIS so that I can let it run, go to bed and wake up the next morning with 25000 elevation points loaded to the attribute table of the point grid.

As I am not really a coder, I have no idea how to go about using the google elevation API directly in QGIS, but I am pretty sure it is possible. Google in any case has extensive instructions on how to use the API: https://developers.google.com/maps/documentation/elevation/start#maps_http_elevation_locations-py

My idea would be to use the Google elevation API somehow in the field calculator, so that it could automatically be applied for an entire attribute table with the coordinates in LAT/LON format.

I guess it should be in Python, looking something like this: enter image description here

Would this be possible and any tips on how to realise this?

1

1 Answer 1

0

You are not so far.

I once produced a simple piece of code that took a file and use every row in latitude|longitude format to produce a X|Y|Z grid that you could use as a DEM in Qgis with a simple import .csv as grid

here it is :

import requests
import csv
import json


GOOGLE_MAPS_API_URL = 'https://maps.googleapis.com/maps/api/elevation/json'

# file must be lat // long

def google_elevation(file, resfile):

    with open (resfile, "w+") as file_write:

        with open(file, 'r') as file_input :
            csvreader = csv.reader(file_input, delimiter="\t")
            for row in csvreader:
                print(row)

                params = {
                'locations': str(row[1]) +","+ str(row[0]),
                'key' : 'your_key'
                }

                req = requests.get(GOOGLE_MAPS_API_URL, params=params)

                if req.status_code == 200 :
                    # print(req)
                    res = req.json()
                    # print (res)
                    loc = res["results"][0]["location"]

                    file_write.write(str(loc["lat"])+";"+str(loc["lng"])+";"+str(res["results"][0]["elevation"])+"\n")

if __name__ == "__main__":

    google_elevation('file_with_points', 'result_file_elevation.txt')

You can adapt this to Qgis env and directly write results in columns or just use it and then import the file.

However ; you must know that this usage is not within the acceptable usage term for google API as it is perceived as scraping of their data.

This approach would also be very expensive very soon to create large DEM, and most of the time you will end up with SRTM data that is free otherwise (they just do a better job at interpolating it). So maybe don't do it ...

4
  • To add on the aspect regarding data-scraping: Have you checked other free resources, such as the MERIT-DEM: hydro.iis.u-tokyo.ac.jp/~yamadai/MERIT_DEM or JAXA_ALOS_AW3D30_V3_2: developers.google.com/earth-engine/datasets/catalog/…
    – Vincé
    Commented Jun 19, 2023 at 14:59
  • Thanks for your answer, I am gonna try to make use of your code. As I am not a coder, I will probably still need guidance to use your code in the field calculator. Perhaps anyone has experience with this? The idea would be to create an empty column with elevation and use an expression to update all features (coordinates) in the table one time only.
    – Adamski
    Commented Jun 19, 2023 at 16:30
  • Regarding the use policy of google data, I am not trying to get elevation for entire regions, just for individual farm properties, As I am paying google for access to their API, I think it will be ok with them. Also I am not planning to share this data or anything, its just used to help me design water retention and agroforestry on the property.
    – Adamski
    Commented Jun 19, 2023 at 16:31
  • I was not aware of the MERIT-DEM, I should check it out and compare it I guess.. but after anaylizing elevation and comparing it with reality, I am suspecting that in my Area, google is using LIDAR data or some other more accurate method than just interpolation of SRTM. There is really a big difference between SRTM / Copernicus and the data I can get from Google with their API.
    – Adamski
    Commented Jun 19, 2023 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.