Skip to content

Commit

Permalink
gdal_calc.py: Add * from gdalnumeric to gdal_calc.py eval namespace a…
Browse files Browse the repository at this point in the history
…gain, now in a clean way, to fix 2.0 regression that made for examle 'log10(A)' to no longer work (patch by Piers Titus van der Torren, OSGeo/gdal#121)

git-svn-id: https://svn.osgeo.org/gdal/trunk/gdal@34034 f0d54148-0727-0410-94bb-9a71ac55c965
  • Loading branch information
rouault committed Apr 20, 2016
1 parent 7b80c34 commit a0c9918
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions swig/python/scripts/gdal_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def doit(opts, args):
if opts.debug:
print("gdal_calc.py starting calculation %s" %(opts.calc))

# set up global namespace for eval with all functions of gdalnumeric
global_namespace = dict([(key, getattr(gdalnumeric, key))
for key in dir(gdalnumeric) if not key.startswith('__')])

################################################################
# fetch details of input layers
################################################################
Expand Down Expand Up @@ -249,6 +253,9 @@ def doit(opts, args):
myNDVs=numpy.zeros(myBufSize)
myNDVs.shape=(nYValid,nXValid)

# modules available to calculation
local_namespace = {}

# fetch data for each input layer
for i,Alpha in enumerate(myAlphaList):

Expand All @@ -264,14 +271,14 @@ def doit(opts, args):
# fill in nodata values
myNDVs=1*numpy.logical_or(myNDVs==1, myval==myNDV[i])

# create an array of values for this block
exec("%s=myval" %Alpha)
# add an array of values for this block to the eval namespace
local_namespace[Alpha] = myval
myval=None


# try the calculation on the array blocks
try:
myResult = eval(opts.calc)
myResult = eval(opts.calc, global_namespace, local_namespace)
except:
print("evaluation of calculation %s failed" %(opts.calc))
raise
Expand Down

0 comments on commit a0c9918

Please sign in to comment.