class ShowGeoTiffTags(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: # Create a dialog for this tool to collect user-specified # tool parameters. self.sd = ScriptDialog(pluginHost, descriptiveName, self) # Specifying the help file will display the html help # file in the help pane. This file should be be located # in the help directory and have the same name as the # class, with an html extension. helpFile = self.__class__.__name__ self.sd.setHelpFile(helpFile) # Specifying the source file allows the 'view code' # button on the tool dialog to be displayed. self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog self.sd.addDialogFile("Input file", "Input GeoTiff File:", "open", "GeoTiff Files (*.tif), TIF, TIFF", True, False) # resize the dialog to the standard size and display it self.sd.setSize(800, 400) self.sd.visible = True def execute(self, args): try: inputfile = args[0] if not os.path.exists(inputfile): pluginHost.showFeedback("File " + inputfile + " does not exist.") return gt = GeoTiff(inputfile) gt.read() tags = gt.showInfo() ret = "" for tag in tags: ret += tag + "\n" pluginHost.returnData(ret) except: pluginHost.showFeedback("Error during script execution.") return def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() self.sd.dispose() t = Thread(target=lambda: self.execute(args)) t.start()
class ShowGeoTiffTags(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: # Create a dialog for this tool to collect user-specified # tool parameters. self.sd = ScriptDialog(pluginHost, descriptiveName, self) # Specifying the help file will display the html help # file in the help pane. This file should be be located # in the help directory and have the same name as the # class, with an html extension. helpFile = self.__class__.__name__ self.sd.setHelpFile(helpFile) # Specifying the source file allows the 'view code' # button on the tool dialog to be displayed. self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog self.sd.addDialogFile("Input file", "Input GeoTiff File:", "open", "GeoTiff Files (*.tif), TIF, TIFF", True, False) # resize the dialog to the standard size and display it self.sd.setSize(800, 400) self.sd.visible = True def execute(self, args): try: inputfile = args[0] if not os.path.exists(inputfile): pluginHost.showFeedback("File " + inputfile + " does not exist.") return gt = GeoTiff(inputfile) gt.read() tags = gt.showInfo() ret = "" for tag in tags: ret += tag + "\n" pluginHost.returnData(ret) except: pluginHost.showFeedback("Error during script execution.") return def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() self.sd.dispose() t = Thread(target=lambda: self.execute(args)) t.start()
class PluginTool(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: ''' Create a dialog for this tool to collect user-specified tool parameters.''' self.sd = ScriptDialog(pluginHost, descriptiveName, self) ''' Specifying the help file will display the html help // file in the help pane. This file should be be located // in the help directory and have the same name as the // class, with an html extension.''' self.sd.setHelpFile(name) ''' Specifying the source file allows the 'view code' // button on the tool dialog to be displayed.''' self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog ''' self.sd.addDialogFile("Input raster file 1 (base)", "Input Base Raster File:", "open", "Raster Files (*.dep), DEP", True, False) self.sd.addDialogFile("Input raster file 2 (comparison)", "Input Comparison Raster File:", "open", "Raster Files (*.dep), DEP", True, False) # Resize the dialog to the standard size and display it ''' self.sd.setSize(800, 400) self.sd.visible = True def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() t = Thread(target=lambda: self.execute(args)) t.start() ''' The execute function is the main part of the tool, where the actual work is completed.''' def execute(self, args): try: if len(args) != 2: pluginHost.showFeedback("Incorrect number of arguments given to tool.") return # read the input parameters inputfile1 = args[0] inputfile2 = args[1] inputraster1 = WhiteboxRaster(inputfile1, 'r') rows = inputraster1.getNumberRows() cols = inputraster1.getNumberColumns() nodata1 = inputraster1.getNoDataValue() inputraster2 = WhiteboxRaster(inputfile2, 'r') if rows != inputraster2.getNumberRows() or cols != inputraster2.getNumberColumns(): pluginHost.showFeedback("The input images must have the same number of rows and columns") nodata2 = inputraster2.getNoDataValue() sumSquares = 0.0 meanVerticalError = 0.0 z1 = 0.0 z2 = 0.0 N = 0.0 progress = -1.0 oldprogress = -1.0 for row in xrange(0, rows): for col in xrange(0, cols): z1 = inputraster1.getValue(row, col) z2 = inputraster2.getValue(row, col) if z1 != nodata1 and z2 != nodata2: sumSquares += (z2 - z1) * (z2 - z1) meanVerticalError += z2 - z1 N += 1.0 progress = (int)(100.0 * row / (rows - 1)) if progress > oldprogress: oldprogress = progress pluginHost.updateProgress(progress) if pluginHost.isRequestForOperationCancelSet(): pluginHost.showFeedback("Operation cancelled") return rmse = math.sqrt(sumSquares / N) meanVerticalError = meanVerticalError / N s = "Vertical Accuracy Analysis\n\n" s += "Base File: {}\n".format(inputfile1) s += "Comparison File: {}\n\n".format(inputfile2) s += "Mean vertical error: {0:.3f}\n".format(meanVerticalError) s += "RMSE: {0:.3f}\n".format(rmse) s += "Accuracy at 95% confidence limit (m): {0:.3f}\n".format(rmse * 1.96) # display the output calculation pluginHost.returnData(s) 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 finally:
class RenameFile(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: # Create a dialog for this tool to collect user-specified # tool parameters. self.sd = ScriptDialog(pluginHost, descriptiveName, self) # Specifying the help file will display the html help # file in the help pane. This file should be be located # in the help directory and have the same name as the # class, with an html extension. helpFile = self.__class__.__name__ self.sd.setHelpFile(helpFile) # Specifying the source file allows the 'view code' # button on the tool dialog to be displayed. self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog self.sd.addDialogFile("Input file", "Input File:", "open", "Whitebox Files (*.shp; *.dep), DEP, SHP", True, False) self.sd.addDialogFile("Output file", "Output File:", "close", "Whitebox Files (*.shp; *.dep), DEP, SHP", True, False) # resize the dialog to the standard size and display it self.sd.setSize(800, 400) self.sd.visible = True def execute(self, args): try: inputfile = args[0] outputfile = args[1] # is the input file a WhiteboxRaster or a shapefile? if inputfile.endswith(".dep"): israster = True elif inputfile.endswith(".shp"): israster = False else: pluginHost.showFeedback("The input file must be a Whitebox raster or shapefile.") return if israster: # make sure that the output file is of the same # file type if not outputfile.endswith(".dep"): fileName, fileExtension = os.path.splitext(outputfile) outputfile = fileName + ".dep" # rename the header file os.rename(inputfile, outputfile) # rename the data file oldfile = inputfile.replace(".dep", ".tas") newfile = outputfile.replace(".dep", ".tas") os.rename(oldfile, newfile) oldfile = inputfile.replace(".dep", ".wstat") newfile = outputfile.replace(".dep", ".wstat") if os.path.exists(oldfile): os.rename(oldfile, newfile) else: # make sure that the output file is of the same # file type if not outputfile.endswith(".shp"): fileName, fileExtension = os.path.splitext(outputfile) outputfile = fileName + ".shp" # .shp file os.rename(inputfile, outputfile) # .dbf file oldfile = inputfile.replace(".shp", ".dbf") newfile = outputfile.replace(".shp", ".dbf") if os.path.exists(oldfile): os.rename(oldfile, newfile) # .shx file oldfile = inputfile.replace(".shp", ".shx") newfile = outputfile.replace(".shp", ".shx") if os.path.exists(oldfile): os.rename(oldfile, newfile) # .prj file oldfile = inputfile.replace(".shp", ".prj") newfile = outputfile.replace(".shp", ".prj") if os.path.exists(oldfile): os.rename(oldfile, newfile) # .sbn file oldfile = inputfile.replace(".shp", ".sbn") newfile = outputfile.replace(".shp", ".sbn") if os.path.exists(oldfile): os.rename(oldfile, newfile) # .sbx file oldfile = inputfile.replace(".shp", ".sbx") newfile = outputfile.replace(".shp", ".sbx") if os.path.exists(oldfile): os.rename(oldfile, newfile) pluginHost.showFeedback("Operation complete") except: pluginHost.showFeedback("File not written properly.") return def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() self.sd.dispose() t = Thread(target=lambda: self.execute(args)) t.start()
class PluginTool(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: ''' Create a dialog for this tool to collect user-specified tool parameters.''' self.sd = ScriptDialog(pluginHost, descriptiveName, self) ''' Specifying the help file will display the html help // file in the help pane. This file should be be located // in the help directory and have the same name as the // class, with an html extension.''' self.sd.setHelpFile(name) ''' Specifying the source file allows the 'view code' // button on the tool dialog to be displayed.''' self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog ''' self.sd.addDialogFile("Input DEM file", "Input DEM:", "open", "Raster Files (*.dep), DEP", True, False) self.sd.addDialogFile("Output DEM file", "Output DEM File:", "save", "Raster Files (*.dep), DEP", True, False) self.sd.addDialogDataInput("Neighbourhood size", "Neighbourhood Size (cells):", "", True, False) self.sd.addDialogDataInput("Slope threshold", "Slope threshold:", "15.0", True, False) # Resize the dialog to the standard size and display it ''' self.sd.setSize(800, 400) self.sd.visible = True def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() t = Thread(target=lambda: self.execute(args)) t.start() ''' The execute function is the main part of the tool, where the actual work is completed.''' def execute(self, args): try: if len(args) != 4: pluginHost.showFeedback("Incorrect number of arguments given to tool.") return # read the input parameters inputfile = args[0] outputfile = args[1] filtersize = int(args[2]) if filtersize < 3: filtersize = 3 slopethreshold = float(args[3]) exe_path = pluginHost.getResourcesDirectory() + "plugins" + os.path.sep os.chdir(exe_path) (release, vendor, vminfo, osinfo) = jav() if "win" in osinfo[0].lower(): ext = '.exe' else: ext = '' cmd = "." + os.path.sep + "NativePlugins" + os.path.sep + "remove_off_terrain_objects{}".format(ext) cmd += ' -i=\"{}\"'.format(inputfile) cmd += ' -o=\"{}\"'.format(outputfile) cmd += ' -filter=\"{}\"'.format(filtersize) cmd += ' -slope=\"{}\"'.format(slopethreshold) cmd += ' -v' ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True) while True: line = ps.stdout.readline() if line != '': if "%" in line: str_array = line.split(" ") label = line.replace(str_array[len(str_array)-1], "") progress = int(str_array[len(str_array)-1].replace("%", "").strip()) pluginHost.updateProgress(label, progress) else: if "error" in line.lower(): pluginHost.showFeedback("Error: {}".format(line)) else: if not line.startswith("*"): pluginHost.updateProgress(line, 0) else: break # 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 finally:
class PythonExamplePlugin(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: ''' Create a dialog for this tool to collect user-specified tool parameters.''' self.sd = ScriptDialog(pluginHost, "Python Example Plugin", self) ''' Specifying the help file will display the html help // file in the help pane. This file should be be located // in the help directory and have the same name as the // class, with an html extension.''' helpFile = self.__class__.__name__ self.sd.setHelpFile(helpFile) ''' Specifying the source file allows the 'view code' // button on the tool dialog to be displayed.''' self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog ''' self.sd.addDialogFile("Input raster file", "Input Raster File:", "open", "Raster Files (*.dep), DEP", True, False) self.sd.addDialogFile("Output raster file", "Output Raster File:", "save", "Raster Files (*.dep), DEP", True, False) # Resize the dialog to the standard size and display it ''' self.sd.setSize(800, 400) self.sd.visible = True def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() t = Thread(target=lambda: self.execute(args)) t.start() ''' The execute function is the main part of the tool, where the actual work is completed.''' 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 finally:
class PluginTool(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: ''' Create a dialog for this tool to collect user-specified tool parameters.''' self.sd = ScriptDialog(pluginHost, descriptiveName, self) ''' Specifying the help file will display the html help // file in the help pane. This file should be be located // in the help directory and have the same name as the // class, with an html extension.''' self.sd.setHelpFile(name) ''' Specifying the source file allows the 'view code' // button on the tool dialog to be displayed.''' self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog ''' self.sd.addDialogFile("Input LAS file", "Input LAS File:", "open", "LAS Files (*.las), LAS", True, False) self.sd.addDialogFile("Output LAS file", "Output LAS File:", "save", "LAS Files (*.las), LAS", True, False) self.sd.addDialogDataInput("Minimum elevation in z-units", "Minimum elevation (Optional):", "", True, True) self.sd.addDialogDataInput("Maximum elevation in z-units", "Maximum elevation (Optional):", "", True, True) self.sd.addDialogDataInput("In-slice class value (0-31)", "In-slice Class Value (Optional):", "", True, True) self.sd.addDialogDataInput("Out-of-slice class value (0-31)", "Out-of-slice Class Value (Optional):", "", True, True) # Resize the dialog to the standard size and display it ''' self.sd.setSize(800, 400) self.sd.visible = True def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() t = Thread(target=lambda: self.execute(args)) t.start() ''' The execute function is the main part of the tool, where the actual work is completed.''' def execute(self, args): try: if len(args) != 6: pluginHost.showFeedback( "Incorrect number of arguments given to tool.") return # read the input parameters inputfile = args[0] outputfile = args[1] min_z = float('-inf') if args[2].lower() != "not specified": min_z = float(args[2]) max_z = float('inf') if args[3].lower() != "not specified": max_z = float(args[3]) in_class = -1 if args[4].lower() != "not specified": in_class = int(args[4]) out_class = -1 if args[5].lower() != "not specified": out_class = int(args[5]) if min_z == float('-inf') and max_z == float('inf'): pluginHost.showFeedback( "You must specify either a min. z, a max. z, or both.") return exe_path = pluginHost.getResourcesDirectory( ) + "plugins" + os.path.sep os.chdir(exe_path) (release, vendor, vminfo, osinfo) = jav() if "win" in osinfo[0].lower(): ext = '.exe' else: ext = '' tool_name = "lidar_elevation_slice" cmd = "." + os.path.sep + "NativePlugins" + os.path.sep + "{}{}".format( tool_name, ext) cmd += ' -i=\"{}\"'.format(inputfile) cmd += ' -o=\"{}\"'.format(outputfile) cmd += ' -minz=\"{}\"'.format(min_z) cmd += ' -maxz=\"{}\"'.format(max_z) if in_class >= 0 and out_class >= 0: cmd += ' -class' cmd += ' -inclassval=\"{}\"'.format(in_class) cmd += ' -outclassval=\"{}\"'.format(out_class) cmd += ' -v' ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True) while True: line = ps.stdout.readline() if line != '': if "%" in line: str_array = line.split(" ") label = line.replace(str_array[len(str_array) - 1], "") progress = int(str_array[len(str_array) - 1].replace( "%", "").strip()) pluginHost.updateProgress(label, progress) else: if "error" in line.lower(): pluginHost.showFeedback("Error: {}".format(line)) else: if not line.startswith("*"): pluginHost.updateProgress(line, 0) else: break # 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 finally:
class PluginTool(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: ''' Create a dialog for this tool to collect user-specified tool parameters.''' self.sd = ScriptDialog(pluginHost, descriptiveName, self) ''' Specifying the help file will display the html help // file in the help pane. This file should be be located // in the help directory and have the same name as the // class, with an html extension.''' self.sd.setHelpFile(name) ''' Specifying the source file allows the 'view code' // button on the tool dialog to be displayed.''' self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog ''' self.sd.addDialogMultiFile( "Select the input Whitebox raster files", "Input Whitebox Raster Files:", "Whitebox Raster (*.dep), DEP") # Resize the dialog to the standard size and display it ''' self.sd.setSize(800, 400) self.sd.visible = True def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() t = Thread(target=lambda: self.execute(args)) t.start() ''' The execute function is the main part of the tool, where the actual work is completed.''' def execute(self, args): try: if len(args) != 1: pluginHost.showFeedback( "Incorrect number of arguments given to tool.") return # read the input parameters input_files = args[0][:-1] exe_path = pluginHost.getResourcesDirectory( ) + "plugins" + os.path.sep os.chdir(exe_path) (release, vendor, vminfo, osinfo) = jav() if "win" in osinfo[0].lower(): ext = '.exe' else: ext = '' exe_name = "go-spatial" tool_name = "whitebox2geotiff" input_files_list = input_files.split(";") num_files = len(input_files_list) for input_file in input_files_list: cmd = "." + os.path.sep + "NativePlugins" + os.path.sep + "{0}{1}".format( exe_name, ext) cmd += ' -run=\"{}\"'.format(tool_name) cmd += ' -args=\"{0};{1}\"'.format( input_file, input_file.replace(".dep", ".tif")) ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True) while True: line = ps.stdout.readline() if line != '': if "%" in line: str_array = line.split(" ") label = line.replace(str_array[len(str_array) - 1], "") progress = int(str_array[len(str_array) - 1].replace("%", "").strip()) pluginHost.updateProgress(label, progress) else: if "error" in line.lower(): pluginHost.showFeedback( "Error: {}".format(line)) else: if not line.startswith("*"): pluginHost.updateProgress(line, 0) else: break pluginHost.showFeedback("Complete!") 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 finally:
class PluginTool(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: ''' Create a dialog for this tool to collect user-specified tool parameters.''' self.sd = ScriptDialog(pluginHost, descriptiveName, self) ''' Specifying the help file will display the html help // file in the help pane. This file should be be located // in the help directory and have the same name as the // class, with an html extension.''' self.sd.setHelpFile(name) ''' Specifying the source file allows the 'view code' // button on the tool dialog to be displayed.''' self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog ''' self.sd.addDialogFile("Input LAS file", "Input LAS File:", "open", "LAS Files (*.las), LAS", True, False) self.sd.addDialogFile("Output LAS file", "Output LAS File:", "save", "LAS Files (*.las), LAS", True, False) self.sd.addDialogDataInput( "Neighbourhood search distance (xy units)", "Search Distance (xy units):", "10.0", True, False) self.sd.addDialogDataInput( "Maximum allowable slope between neighbouring points", "Max. Slope Between Points:", "45.0", True, False) self.sd.addDialogDataInput( "Minimum height above ground (z units) for an off-terrain object", "Min. OTO Height:", "0.15", True, False) self.sd.addDialogDataInput( "The height above ground (z units) beyond which a point is considered to be an off-terrain object regardless of inter-point slopes.", "Max. Height Above Ground:", "2.5", True, False) self.sd.addDialogDataInput( "Elevation (z units) below which points are excluded from analysis (Optional).", "Min. Elevation:", "", True, True) self.sd.addDialogCheckBox("Classify ground points?", "Classify ground points?", False) # Resize the dialog to the standard size and display it ''' self.sd.setSize(800, 400) self.sd.visible = True def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() t = Thread(target=lambda: self.execute(args)) t.start() ''' The execute function is the main part of the tool, where the actual work is completed.''' def execute(self, args): try: if len(args) != 8: pluginHost.showFeedback( "Incorrect number of arguments given to tool.") return # read the input parameters inputfile = args[0] outputfile = args[1] search_dist = float(args[2]) slope = float(args[3]) min_z_diff = float(args[4]) max_z_above_ground = float(args[5]) min_z_str = args[6] min_z = -999999.0 if min_z_str.lower() != "not specified": min_z = float(min_z) classify = False if args[7].lower() == 'true': classify = True exe_path = pluginHost.getResourcesDirectory( ) + "plugins" + os.path.sep os.chdir(exe_path) (release, vendor, vminfo, osinfo) = jav() if "win" in osinfo[0].lower(): ext = '.exe' else: ext = '' tool_name = "lidar_ground_point_separation" cmd = "." + os.path.sep + "NativePlugins" + os.path.sep + "{}{}".format( tool_name, ext) cmd += ' -i=\"{}\"'.format(inputfile) cmd += ' -o=\"{}\"'.format(outputfile) cmd += ' -dist=\"{}\"'.format(search_dist) cmd += ' -slope=\"{}\"'.format(slope) cmd += ' -minzdiff=\"{}\"'.format(min_z_diff) cmd += ' -maxzdiff=\"{}\"'.format(max_z_above_ground) if classify: cmd += ' -class' if min_z_str.lower() != "not specified": cmd += ' -minz=\"{}\"'.format(min_z) cmd += ' -v' ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True) while True: line = ps.stdout.readline() if line != '': if "%" in line: str_array = line.split(" ") label = line.replace(str_array[len(str_array) - 1], "") progress = int(str_array[len(str_array) - 1].replace( "%", "").strip()) pluginHost.updateProgress(label, progress) else: if "error" in line.lower(): pluginHost.showFeedback("Error: {}".format(line)) else: if not line.startswith("*"): pluginHost.updateProgress(line, 0) else: break # 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 finally:
class PluginTool(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: ''' Create a dialog for this tool to collect user-specified tool parameters.''' self.sd = ScriptDialog(pluginHost, descriptiveName, self) ''' Specifying the help file will display the html help // file in the help pane. This file should be be located // in the help directory and have the same name as the // class, with an html extension.''' self.sd.setHelpFile(name) ''' Specifying the source file allows the 'view code' // button on the tool dialog to be displayed.''' self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog ''' self.sd.addDialogFile("Input LAS file", "Input LAS File:", "open", "LAS Files (*.las), LAS", True, False) self.sd.addDialogFile("Output LAS file", "Output LAS File:", "save", "LAS Files (*.las), LAS", True, False) self.sd.addDialogDataInput("Detrending distance (xy units)", "Detrending Distance (xy units):", "20.0", True, False) self.sd.addDialogDataInput( "Neighbourhood search distance (xy units)", "Search Distance (xy units):", "10.0", True, False) self.sd.addDialogDataInput( "Maximum deviation (degrees) in normal vectors", "Max. Normal Vector Difference:", "10.0", True, False) self.sd.addDialogDataInput( "Maximum difference in elevation (z units) between neighbouring points of the same segment", "Max. Elevation Difference:", "0.15", True, False) self.sd.addDialogCheckBox("Classify ground segments?", "Classify ground segments?", False) # Resize the dialog to the standard size and display it ''' self.sd.setSize(800, 400) self.sd.visible = True def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() t = Thread(target=lambda: self.execute(args)) t.start() ''' The execute function is the main part of the tool, where the actual work is completed.''' def execute(self, args): try: if len(args) != 7: pluginHost.showFeedback( "Incorrect number of arguments given to tool.") return # read the input parameters inputfile = args[0] outputfile = args[1] detrending_dist = float(args[2]) search_dist = float(args[3]) normal_dev = float(args[4]) elev_diff = float(args[5]) classify = False if args[6].lower() == 'true': classify = True exe_path = pluginHost.getResourcesDirectory( ) + "plugins" + os.path.sep os.chdir(exe_path) (release, vendor, vminfo, osinfo) = jav() if "win" in osinfo[0].lower(): ext = '.exe' else: ext = '' cmd = "." + os.path.sep + "NativePlugins" + os.path.sep + "lidar_segmentation{}".format( ext) cmd += ' -i=\"{}\"'.format(inputfile) cmd += ' -o=\"{}\"'.format(outputfile) cmd += ' -dist=\"{}\"'.format(search_dist) cmd += ' -detrend=\"{}\"'.format(detrending_dist) cmd += ' -max_norm_angle=\"{}\"'.format(normal_dev) cmd += ' -maxzdiff=\"{}\"'.format(elev_diff) if classify: cmd += ' -class' cmd += ' -v' ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True) while True: line = ps.stdout.readline() if line != '': if "%" in line: str_array = line.split(" ") label = line.replace(str_array[len(str_array) - 1], "") progress = int(str_array[len(str_array) - 1].replace( "%", "").strip()) pluginHost.updateProgress(label, progress) else: if "error" in line.lower(): pluginHost.showFeedback("Error: {}".format(line)) else: if not line.startswith("*"): pluginHost.updateProgress(line, 0) else: break # 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 finally:
class PluginTool(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: ''' Create a dialog for this tool to collect user-specified tool parameters.''' self.sd = ScriptDialog(pluginHost, descriptiveName, self) ''' Specifying the help file will display the html help // file in the help pane. This file should be be located // in the help directory and have the same name as the // class, with an html extension.''' self.sd.setHelpFile(name) ''' Specifying the source file allows the 'view code' // button on the tool dialog to be displayed.''' self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog ''' self.sd.addDialogFile("Input LAS file", "Input LAS File:", "open", "LAS Files (*.las), LAS", True, False) self.sd.addDialogFile("Output Raster file", "Output Raster File:", "save", "Raster Files (*.dep), DEP", True, False) self.sd.addDialogDataInput( "Output raster grid resolution (xy units)", "Grid Resolution (xy units):", "", True, False) # Resize the dialog to the standard size and display it ''' self.sd.setSize(800, 400) self.sd.visible = True def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() t = Thread(target=lambda: self.execute(args)) t.start() ''' The execute function is the main part of the tool, where the actual work is completed.''' def execute(self, args): try: if len(args) != 3: pluginHost.showFeedback( "Incorrect number of arguments given to tool.") return # read the input parameters inputfile = args[0] outputfile = args[1] grid_resolution = float(args[2]) exe_path = pluginHost.getResourcesDirectory( ) + "plugins" + os.path.sep os.chdir(exe_path) (release, vendor, vminfo, osinfo) = jav() if "win" in osinfo[0].lower(): ext = '.exe' else: ext = '' tool_name = "lidar_flightline_overlap" # Hard coded exe directory only for testing. cmd = "/Users/johnlindsay/Documents/programming/Whitebox/trunk/whitebox_tools/target/release/{0}{1}".format( tool_name, ext) # cmd = "." + os.path.sep + "NativePlugins" + os.path.sep + "{0}{1}".format(tool_name, ext) cmd += ' -i=\"{}\"'.format(inputfile) cmd += ' -o=\"{}\"'.format(outputfile) cmd += ' -resolution=\"{}\"'.format(grid_resolution) cmd += ' -palette=\"light_quant.pal\"' cmd += ' -v' ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True) while True: line = ps.stdout.readline() if line != '': if "%" in line: str_array = line.split(" ") label = line.replace(str_array[len(str_array) - 1], "") progress = int(str_array[len(str_array) - 1].replace( "%", "").strip()) pluginHost.updateProgress(label, progress) else: # print line if "error" in line.lower(): pluginHost.showFeedback("Error: {}".format(line)) else: if not line.startswith("*"): pluginHost.updateProgress(line, 0) else: break # 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 finally:
class Sink(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: # Create a dialog for this tool to collect user-specified # tool parameters. self.sd = ScriptDialog(pluginHost, descriptiveName, self) # Specifying the help file will display the html help # file in the help pane. This file should be be located # in the help directory and have the same name as the # class, with an html extension. helpFile = self.__class__.__name__ self.sd.setHelpFile(helpFile) # Specifying the source file allows the 'view code' # button on the tool dialog to be displayed. self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog self.sd.addDialogFile("Input file", "Input DEM File:", "open", "Whitebox Raster Files (*.dep), DEP", True, False) self.sd.addDialogFile("Output file", "Output Raster File:", "close", "Whitebox Raster Files (*.dep), DEP", True, False) # resize the dialog to the standard size and display it self.sd.setSize(800, 400) self.sd.visible = True 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 actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() self.sd.dispose() t = Thread(target=lambda: self.execute(args)) t.start()
class PythonExamplePlugin(ActionListener): def __init__(self, args): if len(args) != 0: self.execute(args) else: ''' Create a dialog for this tool to collect user-specified tool parameters.''' self.sd = ScriptDialog(pluginHost, "Python Example Plugin", self) ''' Specifying the help file will display the html help // file in the help pane. This file should be be located // in the help directory and have the same name as the // class, with an html extension.''' helpFile = self.__class__.__name__ self.sd.setHelpFile(helpFile) ''' Specifying the source file allows the 'view code' // button on the tool dialog to be displayed.''' self.sd.setSourceFile(os.path.abspath(__file__)) # add some components to the dialog ''' self.sd.addDialogFile("Input raster file", "Input Raster File:", "open", "Raster Files (*.dep), DEP", True, False) self.sd.addDialogFile("Output raster file", "Output Raster File:", "save", "Raster Files (*.dep), DEP", True, False) # Resize the dialog to the standard size and display it ''' self.sd.setSize(800, 400) self.sd.visible = True def actionPerformed(self, event): if event.getActionCommand() == "ok": args = self.sd.collectParameters() t = Thread(target=lambda: self.execute(args)) t.start() ''' The execute function is the main part of the tool, where the actual work is completed.''' 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 finally: