1

I've been trying to get the python bindings to work in order to keep the processes in memory, making it faster.

I have previously used:

subprocess.call('gdaldem color-relief file1.tif -alpha hramp.txt file2.tif')

which has worked the way I have wanted it to, however I want to move this command over to the python api. My problem is that the call to the color-relief in the python api doesnt add the alpha channel

This is my working code, with the test generations commented out

warp_options = gdal.WarpOptions(dstAlpha=True, srcSRS="EPSG:4326", dstSRS="EPSG:3857", format="MEM")

for file in files:
    try:
        original_file = 'downloads/' + file
        filename = 'combined/' + file[:-4] 

        src_ds = gdal.Open(original_file)

        warp = gdal.Warp(destNameOrDestDS='',srcDSOrSrcDSTab=src_ds, options=warp_options)

        hillshade = gdal.DEMProcessing('', warp, 'hillshade', format = 'MEM', zFactor=3)
        hillshade_colored = gdal.DEMProcessing('', hillshade, "color-relief", colorFilename='hramp.txt', format='MEM')


        #gdal.Translate(filename + '_hill.tif', hillshade)

        #I want these to output the same file, however the inmemory hillshade doesnt have an alpha channel
        #gdal.Translate(filename + '_hillcolor1.tif', hillshade_colored, creationOptions=['compress=LZW', 'alpha=yes'])

        #This works the way I want it to
        #subprocess.call('gdaldem color-relief -q -co compress=lzw -of GTiff {0} -alpha hramp.txt  {1}'.format(filename + '_hill.tif', filename + '_hillcolor2.tif'))

        colors = gdal.DEMProcessing('', warp, 'color-relief', colorFilename='ramp.txt', format='MEM')

        overlaid = gdal.Warp('', [colors, hillshade_colored], format='MEM')
        gdal.Translate(filename + '.tif', overlaid, creationOptions=['compress=LZW', 'alpha=yes'])
        ...

I have checked the test page, and the python api page, but they don't seem to mention what the alpha flag in the console command actually does. It is mentioned here, but I'm not sure how to apply it to the python api calls.

So my question is, how do I implement the -alpha flag in the gdal demprocessing python api calls?

  • Does the warp result warp = gdal.Warp(destNameOrDestDS='',srcDSOrSrcDSTab=src_ds, options=warp_options) contain the alpha Band ? – Hugo Roussaffa - GeoDatup Sep 30 '17 at 12:30
  • Yes, doing gdalinfo onthe files generated from gdal.Transform('warp.tif', warp), gives two bands, grayscale and alpha – AlexVestin Sep 30 '17 at 12:33
  • can you do the same command to other (hillshade and hillshade_colored) please – Hugo Roussaffa - GeoDatup Sep 30 '17 at 12:35
  • hill.tif has one band grey, hillcolor1.tif has three bands with ColorInterp=undefined, and hillcolor2.tif has 4 bands,red green blue and alpha – AlexVestin Sep 30 '17 at 12:38
  • sorry but I don't understant what is hill.tiff, hillcolor1.tif and hillcolor2.tif (which command created them) – Hugo Roussaffa - GeoDatup Sep 30 '17 at 12:40
4

It seem to be missed unfortunatly by Gdal's Python dev team (or not, don't know if it's work!).
As temporary hack, edit the gdal.py script and add setAlpha=False, argument and the wrapping to -alpha option.

like that :

def DEMProcessingOptions(options = [], colorFilename = None, format = 'GTiff',
          creationOptions = None, computeEdges = False, alg = 'Horn', band = 1,
          zFactor = None, scale = None, azimuth = None, altitude = None, combined = False,
          slopeFormat = None, trigonometric = False, zeroForFlat = False, setAlpha=False,
          callback = None, callback_data = None):
""" Create a DEMProcessingOptions() object that can be passed to gdal.DEMProcessing()
    Keyword arguments are :
      options --- can be be an array of strings, a string or let empty and filled from other keywords.
      colorFilename --- (mandatory for "color-relief") name of file that contains palette definition for the "color-relief" processing.
      format --- output format ("GTiff", etc...)
      creationOptions --- list of creation options
      computeEdges --- whether to compute values at raster edges.
      alg --- 'ZevenbergenThorne' or 'Horn'
      band --- source band number to use
      zFactor --- (hillshade only) vertical exaggeration used to pre-multiply the elevations.
      scale --- ratio of vertical units to horizontal.
      azimuth --- (hillshade only) azimuth of the light, in degrees. 0 if it comes from the top of the raster, 90 from the east, ... The default value, 315, should rarely be changed as it is the value generally used to generate shaded maps.
      altitude ---(hillshade only) altitude of the light, in degrees. 90 if the light comes from above the DEM, 0 if it is raking light.
      combined --- (hillshade only) whether to compute combined shading, a combination of slope and oblique shading.
      slopeformat --- (slope only) "degree" or "percent".
      trigonometric --- (aspect only) whether to return trigonometric angle instead of azimuth. Thus 0deg means East, 90deg North, 180deg West, 270deg South.
      zeroForFlat --- (aspect only) whether to return 0 for flat areas with slope=0, instead of -9999.
      callback --- callback method
      callback_data --- user data for callback
"""
import copy

if _is_str_or_unicode(options):
    new_options = ParseCommandLine(options)
else:
    new_options = copy.copy(options)
    new_options += ['-of', format]
    if creationOptions is not None:
        for opt in creationOptions:
            new_options += ['-co', opt ]
    if computeEdges:
        new_options += ['-compute_edges' ]
    if alg ==  'ZevenbergenThorne':
        new_options += ['-alg', 'ZevenbergenThorne']
    new_options += ['-b', str(band) ]
    if zFactor is not None:
        new_options += ['-z', str(zFactor) ]
    if scale is not None:
        new_options += ['-s', str(scale) ]
    if azimuth is not None:
        new_options += ['-az', str(azimuth) ]
    if altitude is not None:
        new_options += ['-alt', str(altitude) ]
    if combined:
        new_options += ['-combined' ]
    if slopeFormat == 'percent':
        new_options += ['-p' ]
    if trigonometric:
        new_options += ['-trigonometric' ]
    if zeroForFlat:
        new_options += ['-zero_for_flat' ]
    if setAlpha == True:
        new_options += ['-alpha' ]

return (GDALDEMProcessingOptions(new_options), colorFilename, callback, callback_data)
  • Ohh, that looks promising! I'm having a hard time locating gdal.py, any hints to where it can be? – AlexVestin Sep 30 '17 at 15:53
  • gdal.__file__ – Hugo Roussaffa - GeoDatup Sep 30 '17 at 16:02
  • You are welcome, happy to helped. We need to make a push request – Hugo Roussaffa - GeoDatup Oct 1 '17 at 12:05
3

addAlpha=true option added to gdal.DEMProcessing() per https://trac.osgeo.org/gdal/changeset/40477

  • Evan Rouault did changeset 40477 get implemented yet? I can't tell. – bucricket Jan 24 '18 at 21:41
  • My God that Trac software is terrible. It's in league with Launchpad – four43 Jun 19 '18 at 19:04
  • It didn't make it into 2.2 but did make it into 2.3 – user697576 Jun 22 '18 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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