def execute(self, args): try: inputfile = args[0] outputfile = args[1] # fill the depressions in the DEM, being sure to # run on a dedicated thread and supressing # the return data (automatically displayed image) outputfile2 = outputfile.replace(".dep", "_temp.dep") args2 = [inputfile, outputfile2, "0.0"] pluginHost.runPlugin("FillDepressions", args2, False, True) # flag the cells that have been affected by the filling. inputraster = WhiteboxRaster(inputfile, 'r') tempraster = WhiteboxRaster(outputfile2, 'rw') rows = tempraster.getNumberRows() cols = tempraster.getNumberColumns() nodata = inputraster.getNoDataValue() oldprogress = -1 for row in xrange(0, rows): for col in xrange(0, cols): z1 = inputraster.getValue(row, col) z2 = tempraster.getValue(row, col) if z1 != nodata: if z1 < z2: tempraster.setValue(row, col, 1.0) else: tempraster.setValue(row, col, 0.0) else: tempraster.setValue(row, col, nodata) progress = (int)((100.0 * (row + 1.0)) / rows) if progress > oldprogress: oldprogress = progress pluginHost.updateProgress(progress) if pluginHost.isRequestForOperationCancelSet(): pluginHost.showFeedback("Operation cancelled") return tempraster.flush() # clump the filled cells, being sure to # run on a dedicated thread and supressing # the return data (automatically displayed image) args2 = [outputfile2, outputfile, 'true', 'true'] pluginHost.runPlugin("Clump", args2, False, True) inputraster.close() # delete the temporary file os.remove(outputfile2) os.remove(outputfile2.replace(".dep", ".tas")) # display the final output pluginHost.returnData(outputfile) except: pluginHost.showFeedback("Error during script execution.") return
def execute(self, args): try: ''' Make sure that the args array has the expected number of parameters. If the script is being called by another script, this may not be the case, at which point, the script should inform the user of the error and then end gracefully.''' if len(args) != 6: pluginHost.showFeedback("Incorrect number of arguments given to tool.") return ''' Read in the parameters. All parameters are provided as strings and must be converted to their appropriate form for use, e.g. floats etc. ''' inputfile = args[0] outputfile = args[1] inputshapefile = args[2] outputshapefile = args[3] numericalData = float(args[4]) booleanValue = bool(args[5]) ''' The following is an example of how to call another Whitebox plugin from your script. simply create a list of strings to hold the parameters you wish to provide the plugin. Then call the runPlugin method of the pluginHost. pluginHost is a reference to the Whitebox user interface. The last two boolean variables in the runPlugin method are to indicate 1) whether the tool should be run on a dedicated thread, which is not advisable if you would like to run through a workflow sequentially (i.e. complete first task then move onto second task) and 2) whether upon completion, any data that would be automatically returned by the tool (e.g. a displayed image) should be suppressed.''' # args2 = [inputfile, outputfile, "0.0"] # pluginHost.runPlugin("FillDepressions", args2, False, True) ''' The following code sets up an input Whitebox raster for reading. It then reads the number of rows and columns and the nodata value.''' inputraster = WhiteboxRaster(inputfile, 'r') rows = inputraster.getNumberRows() cols = inputraster.getNumberColumns() nodata = inputraster.getNoDataValue() ''' The next line of code will create a new WhiteboxRaster of the name 'ouptutfile'. It will be the same dimensions and extent as the input file. The last parameter is the initial value of the raster grid, here set to nodata.''' outputraster = WhiteboxRaster(outputfile, "rw", inputfile, DataType.FLOAT, nodata) ''' This code can be used to scan through a raster grid. ''' oldprogress = -1 for row in xrange(0, rows): for col in xrange(0, cols): z = inputraster.getValue(row, col) if z != nodata: outputraster.setValue(row, col, z * 1000) else: outputraster.setValue(row, col, nodata) 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.close() ''' Call the returnData method of pluginHost. You can return the file names of raster files and shapefiles and they will be automatically displayed. If you return a text string (other than a file name) it will be displayed in the textbox at the bottom of the Whitebox user interface. If you return html, it will be rendered in a window. If you return a JPanel, it will be placed in a JDialog and displayed.''' pluginHost.returnData(outputfile) ''' The following code is an example of how to work with vector data.''' ''' Open and existing shapefile''' input = ShapeFile(inputshapefile) shapetype = input.getShapeType() table = input.getAttributeTable() ''' reading from the records in a shapefile ''' r = 0 for record in input.records: shapegeometry = record.getGeometry() ''' Read the points into an array ''' points = shapegeometry.getPoints() # x = points[pointnum][0] # y = points[pointnum][1] ''' polylines and polygons can contain multiple parts. The 'parts' array can be used to identify the starting node from the points array for each part in the geometry. ''' parts = shapegeometry.getParts() ''' the following shows how you read the record's attributes and update an entry''' # recData = table.getRecord(r) # recData[recData.length - 1] = new Double(1.0) # table.updateRecord(r, recData) # r += 1 ''' The following code can be used to create a new shapefile. ''' ''' First, set up the fields within the attribute table. ''' field1 = DBFField() field1.setName("FID") field1.setDataType(DBFField.DBFDataType.NUMERIC) field1.setFieldLength(10) field1.setDecimalCount(0) field2 = DBFField() field2.setName("NAME") field2.setDataType(DBFField.DBFDataType.STRING) field2.setFieldLength(20) fields = [field1, field2] ''' Now create the shapefile, feeding the constructor the fields array. ''' output = ShapeFile(outputshapefile, ShapeType.POLYLINE, fields) # outputGeom = Point(x, y) # outputGeom = PolyLine(parts, points) # outputGeom = Polygon(parts, points) ''' Note that for ShapeTypes of M or Z dimensions, there are alternative constructors that allow for setting the M and Z data using a double array. ''' # Object[] rowData = new Object[2] # rowData[0] = (float)FID # rowData[1] = "New feature" # output.addRecord(outputGeom, rowData); output.write() except Exception, e: print e pluginHost.showFeedback("Error during script execution.") ''' alternatively, you many want to send the exception to the pluginHost.logException() method ''' return
def execute(self, args): try: if len(args) != 3: pluginHost.showFeedback( "Incorrect number of arguments given to tool.") return inputfile = args[0] outputfile = args[1] backgroundVal = 0.0 # fill the depressions in the DEM, being sure to # run on a dedicated thread and supressing # the return data (automatically displayed image) args2 = [inputfile, outputfile, "0.0"] pluginHost.runPlugin("FillDepressions", args2, False, True) # measure the depth in sink. inputraster = WhiteboxRaster(inputfile, 'r') outputraster = WhiteboxRaster(outputfile, 'rw') rows = outputraster.getNumberRows() cols = outputraster.getNumberColumns() nodata = inputraster.getNoDataValue() if args[2] == "true": backgroundVal = nodata outputraster.setPreferredPalette("spectrum.plt") else: outputraster.setPreferredPalette( "spectrum_black_background.plt") oldprogress = -1 for row in xrange(0, rows): for col in xrange(0, cols): z1 = inputraster.getValue(row, col) z2 = outputraster.getValue(row, col) if z1 != nodata: if z1 < z2: outputraster.setValue(row, col, z2 - z1) else: outputraster.setValue(row, col, backgroundVal) else: outputraster.setValue(row, col, nodata) 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.flush() outputraster.findMinAndMaxVals() outputraster.setDisplayMinimum(outputraster.getMinimumValue()) outputraster.setDisplayMaximum(outputraster.getMaximumValue()) outputraster.close() # display the final output pluginHost.returnData(outputfile) pluginHost.updateProgress(0) except Exception, e: pluginHost.logException("Error in DepthInSink", e) pluginHost.showFeedback("Error during script execution.") 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
def execute(self, args): try: if len(args) != 3: pluginHost.showFeedback("Incorrect number of arguments given to tool.") return inputfile = args[0] outputfile = args[1] backgroundVal = 0.0 # fill the depressions in the DEM, being sure to # run on a dedicated thread and supressing # the return data (automatically displayed image) args2 = [inputfile, outputfile, "0.0"] pluginHost.runPlugin("FillDepressions", args2, False, True) # measure the depth in sink. inputraster = WhiteboxRaster(inputfile, 'r') outputraster = WhiteboxRaster(outputfile, 'rw') rows = outputraster.getNumberRows() cols = outputraster.getNumberColumns() nodata = inputraster.getNoDataValue() if args[2] == "true": backgroundVal = nodata outputraster.setPreferredPalette("spectrum.plt") else: outputraster.setPreferredPalette("spectrum_black_background.plt") oldprogress = -1 for row in xrange(0, rows): for col in xrange(0, cols): z1 = inputraster.getValue(row, col) z2 = outputraster.getValue(row, col) if z1 != nodata: if z1 < z2: outputraster.setValue(row, col, z2 - z1) else: outputraster.setValue(row, col, backgroundVal) else: outputraster.setValue(row, col, nodata) 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.flush() outputraster.findMinAndMaxVals() outputraster.setDisplayMinimum(outputraster.getMinimumValue()) outputraster.setDisplayMaximum(outputraster.getMaximumValue()) outputraster.close() # display the final output pluginHost.returnData(outputfile) pluginHost.updateProgress(0) except Exception, e: pluginHost.logException("Error in DepthInSink", e) pluginHost.showFeedback("Error during script execution.") return
def execute(self, args): try: ''' Make sure that the args array has the expected number of parameters. If the script is being called by another script, this may not be the case, at which point, the script should inform the user of the error and then end gracefully.''' if len(args) != 6: pluginHost.showFeedback( "Incorrect number of arguments given to tool.") return ''' Read in the parameters. All parameters are provided as strings and must be converted to their appropriate form for use, e.g. floats etc. ''' inputfile = args[0] outputfile = args[1] inputshapefile = args[2] outputshapefile = args[3] numericalData = float(args[4]) booleanValue = bool(args[5]) ''' The following is an example of how to call another Whitebox plugin from your script. simply create a list of strings to hold the parameters you wish to provide the plugin. Then call the runPlugin method of the pluginHost. pluginHost is a reference to the Whitebox user interface. The last two boolean variables in the runPlugin method are to indicate 1) whether the tool should be run on a dedicated thread, which is not advisable if you would like to run through a workflow sequentially (i.e. complete first task then move onto second task) and 2) whether upon completion, any data that would be automatically returned by the tool (e.g. a displayed image) should be suppressed.''' # args2 = [inputfile, outputfile, "0.0"] # pluginHost.runPlugin("FillDepressions", args2, False, True) ''' The following code sets up an input Whitebox raster for reading. It then reads the number of rows and columns and the nodata value.''' inputraster = WhiteboxRaster(inputfile, 'r') rows = inputraster.getNumberRows() cols = inputraster.getNumberColumns() nodata = inputraster.getNoDataValue() ''' The next line of code will create a new WhiteboxRaster of the name 'ouptutfile'. It will be the same dimensions and extent as the input file. The last parameter is the initial value of the raster grid, here set to nodata.''' outputraster = WhiteboxRaster(outputfile, "rw", inputfile, DataType.FLOAT, nodata) ''' This code can be used to scan through a raster grid. ''' oldprogress = -1 for row in xrange(0, rows): for col in xrange(0, cols): z = inputraster.getValue(row, col) if z != nodata: outputraster.setValue(row, col, z * 1000) else: outputraster.setValue(row, col, nodata) 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.close() ''' Call the returnData method of pluginHost. You can return the file names of raster files and shapefiles and they will be automatically displayed. If you return a text string (other than a file name) it will be displayed in the textbox at the bottom of the Whitebox user interface. If you return html, it will be rendered in a window. If you return a JPanel, it will be placed in a JDialog and displayed.''' pluginHost.returnData(outputfile) ''' The following code is an example of how to work with vector data.''' ''' Open and existing shapefile''' input = ShapeFile(inputshapefile) shapetype = input.getShapeType() table = input.getAttributeTable() ''' reading from the records in a shapefile ''' r = 0 for record in input.records: shapegeometry = record.getGeometry() ''' Read the points into an array ''' points = shapegeometry.getPoints() # x = points[pointnum][0] # y = points[pointnum][1] ''' polylines and polygons can contain multiple parts. The 'parts' array can be used to identify the starting node from the points array for each part in the geometry. ''' parts = shapegeometry.getParts() ''' the following shows how you read the record's attributes and update an entry''' # recData = table.getRecord(r) # recData[recData.length - 1] = new Double(1.0) # table.updateRecord(r, recData) # r += 1 ''' The following code can be used to create a new shapefile. ''' ''' First, set up the fields within the attribute table. ''' field1 = DBFField() field1.setName("FID") field1.setDataType(DBFField.DBFDataType.NUMERIC) field1.setFieldLength(10) field1.setDecimalCount(0) field2 = DBFField() field2.setName("NAME") field2.setDataType(DBFField.DBFDataType.STRING) field2.setFieldLength(20) fields = [field1, field2] ''' Now create the shapefile, feeding the constructor the fields array. ''' output = ShapeFile(outputshapefile, ShapeType.POLYLINE, fields) # outputGeom = Point(x, y) # outputGeom = PolyLine(parts, points) # outputGeom = Polygon(parts, points) ''' Note that for ShapeTypes of M or Z dimensions, there are alternative constructors that allow for setting the M and Z data using a double array. ''' # Object[] rowData = new Object[2] # rowData[0] = (float)FID # rowData[1] = "New feature" # output.addRecord(outputGeom, rowData); output.write() except Exception, e: print e pluginHost.showFeedback("Error during script execution.") ''' alternatively, you many want to send the exception to the pluginHost.logException() method ''' 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