def execute(self, args): try: dX = [ 1, 1, 1, 0, -1, -1, -1, 0 ] dY = [ -1, 0, 1, 1, 1, 0, -1, -1 ] if len(args) != 2: pluginHost.showFeedback("Incorrect number of arguments given to tool.") return # read the input parameters inputfile = args[0] outputfile = args[1] # read the input image inputraster = WhiteboxRaster(inputfile, 'r') nodata = inputraster.getNoDataValue() rows = inputraster.getNumberRows() cols = inputraster.getNumberColumns() # initialize the output image outputraster = WhiteboxRaster(outputfile, "rw", inputfile, DataType.FLOAT, nodata) outputraster.setPreferredPalette(inputraster.getPreferredPalette()) '''perform the analysis This code loops through a raster and performs a 3 x 3 mean filter.''' oldprogress = -1 for row in xrange(0, rows): for col in xrange(0, cols): z = inputraster.getValue(row, col) if z != nodata: mean = z numneighbours = 1 for n in xrange(0, 8): zn = inputraster.getValue(row + dY[n], col + dX[n]) if zn != nodata: mean += zn numneighbours += 1 outputraster.setValue(row, col, mean / numneighbours) progress = (int)(100.0 * row / (rows - 1)) if progress > oldprogress: oldprogress = progress pluginHost.updateProgress(progress) if pluginHost.isRequestForOperationCancelSet(): pluginHost.showFeedback("Operation cancelled") return inputraster.close() outputraster.addMetadataEntry("Created by the " + descriptiveName + " tool.") outputraster.addMetadataEntry("Created on " + time.asctime()) outputraster.close() # display the output image pluginHost.returnData(outputfile) except Exception, e: print e pluginHost.showFeedback("An error has occurred during operation. See log file for details.") pluginHost.logException("Error in " + descriptiveName, e) return
def execute(self, args): try: dX = [1, 1, 1, 0, -1, -1, -1, 0] dY = [-1, 0, 1, 1, 1, 0, -1, -1] if len(args) != 2: pluginHost.showFeedback( "Incorrect number of arguments given to tool.") return # read the input parameters inputfile = args[0] outputfile = args[1] # read the input image inputraster = WhiteboxRaster(inputfile, 'r') nodata = inputraster.getNoDataValue() rows = inputraster.getNumberRows() cols = inputraster.getNumberColumns() # initialize the output image outputraster = WhiteboxRaster(outputfile, "rw", inputfile, DataType.FLOAT, nodata) outputraster.setPreferredPalette(inputraster.getPreferredPalette()) '''perform the analysis This code loops through a raster and performs a 3 x 3 mean filter.''' oldprogress = -1 for row in xrange(0, rows): for col in xrange(0, cols): z = inputraster.getValue(row, col) if z != nodata: mean = z numneighbours = 1 for n in xrange(0, 8): zn = inputraster.getValue(row + dY[n], col + dX[n]) if zn != nodata: mean += zn numneighbours += 1 outputraster.setValue(row, col, mean / numneighbours) progress = (int)(100.0 * row / (rows - 1)) if progress > oldprogress: oldprogress = progress pluginHost.updateProgress(progress) if pluginHost.isRequestForOperationCancelSet(): pluginHost.showFeedback("Operation cancelled") return inputraster.close() outputraster.addMetadataEntry("Created by the " + descriptiveName + " tool.") outputraster.addMetadataEntry("Created on " + time.asctime()) outputraster.close() # display the output image pluginHost.returnData(outputfile) except Exception, e: print e pluginHost.showFeedback( "An error has occurred during operation. See log file for details." ) pluginHost.logException("Error in " + descriptiveName, e) return