From a0c9918fb52aa100ba9f14a09ef00ff714a74d86 Mon Sep 17 00:00:00 2001 From: rouault Date: Wed, 20 Apr 2016 14:13:54 +0000 Subject: [PATCH] gdal_calc.py: Add * from gdalnumeric to gdal_calc.py eval namespace again, 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, https://github.com/OSGeo/gdal/pull/121) git-svn-id: https://svn.osgeo.org/gdal/trunk/gdal@34034 f0d54148-0727-0410-94bb-9a71ac55c965 --- swig/python/scripts/gdal_calc.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/swig/python/scripts/gdal_calc.py b/swig/python/scripts/gdal_calc.py index de35ac6bd8..7f1d766ba8 100755 --- a/swig/python/scripts/gdal_calc.py +++ b/swig/python/scripts/gdal_calc.py @@ -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 ################################################################ @@ -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): @@ -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