Servers by jDrones

Scripting using gdal commands

Hi, I need some help with creating a python script, that I would like to run from missionplanner. The purpose is to determine the snow depth below the copter using a rangefinder. To do this I have to subtract the rangefinders measurement and the terrain height at the copters current position from the vehicles GPS altitude.

I would like to use gdallocationinfo to get the terrain height, but don´t know how to execute it from MissionPlanners IronPython (how to import gdal). Any help would be appreciated.

Here is the first sketch:

#!/usr/bin/ipy
import clr
#from osgeo import gdal

clr.AddReference(“MissionPlanner”)
import MissionPlanner
clr.AddReference(“MissionPlanner.Utilities”) # includes the Utilities class
clr.AddReference(“MAVLink”) # includes the Utilities class
import MAVLink

######################

print ‘Script gestartet’

#check if copter is armed
while True:

if (cs.armed):
    #get position from drone lon, lat, alt
    pos = [cs.lat, cs.lng, cs.alt]
    
    #get laser distance or just use rope length
    ropeLen = 20
    
    #get alt from geotiff at current location
    tiffalt = gdallocationinfo -valonly -wgs84 file.tif pos[1] pos[0]
    #tiffalt = 123
    
    #calculate and print snow depth
    snow = pos[2] - ropeLen - tiffalt
    print snow
    
    Script.Sleep(1000)
else:
    print 'Nicht im Flug'
    Script.Sleep(10000)

print ‘Script beendet’

gdallocationinfo is a gdal application, is is not available
you could start an external process and capture the console response

or you could use the srtm library and include your geotiff in the mission planner srtm cache

import MissionPlanner.Utilities
alt = srtm.getAltitude(lat,lng)

Thanks for the reply.

I already created a customm geotiff for flying for my country with a 10m elevation data, but this resolution is way too low for my purpose.

Could you give me hint on how to start an external process and capture the console response?

Ok, I have found a way to use GDAL. I had to install gdal in windows and add all variables to path (make sure you can call the exact same gdal function in cmd).

Now I can call the command and store the result like this:

#!/usr/bin/ipy
import clr
import sys
import os

map = ‘example.tif’
lat = 47.3
lng = 13.1
result = os.popen(‘gdallocationinfo -valonly -wgs84 %s %s %s’ % (map, lng, lat)).read()

yep, thats the method to call it externally, i dont see any issues doing it this way, only question is performance

Servers by jDrones