Friday, 8 January 2016

gdal - gdaldem -alpha flag in Python bindings is missing


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?



Answer



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)

No comments:

Post a Comment