Revision: 6103 http://sourceforge.net/p/jump-pilot/code/6103 Author: ma15569 Date: 2019-01-19 08:10:42 +0000 (Sat, 19 Jan 2019) Log Message: ----------- added monitoring to raster generation
Modified Paths: -------------- plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/HydroDistPlugIn.java plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/RoutingTimePlugIn.java plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/UpslopeAreaPlugIn.java plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/WatershedPlugIn.java Modified: plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/HydroDistPlugIn.java =================================================================== --- plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/HydroDistPlugIn.java 2019-01-19 08:00:37 UTC (rev 6102) +++ plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/HydroDistPlugIn.java 2019-01-19 08:10:42 UTC (rev 6103) @@ -1,17 +1,26 @@ package com.geomaticaeambiente.klemgui.plugin.hydrology; -import com.geomaticaeambiente.klemgui.utils.PluginUtils; +import java.io.File; + +import javax.swing.JButton; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; + +import org.openjump.core.rasterimage.RasterImageLayer; + import com.geomaticaeambiente.klemgui.ui.CustomComboBox; import com.geomaticaeambiente.klemgui.ui.GUIUtils; import com.geomaticaeambiente.klemgui.ui.InitialDialog; +import com.geomaticaeambiente.klemgui.ui.LayerablesList; import com.geomaticaeambiente.klemgui.ui.MainPanel; -import com.geomaticaeambiente.klemgui.ui.LayerablesList; import com.geomaticaeambiente.klemgui.utils.AbstractInputKlemPlugin; import com.geomaticaeambiente.klemgui.utils.ActionObject; import com.geomaticaeambiente.klemgui.utils.ComponentsTreeMap; import com.geomaticaeambiente.klemgui.utils.GeometryUtils; +import com.geomaticaeambiente.klemgui.utils.InitialData; +import com.geomaticaeambiente.klemgui.utils.PluginUtils; import com.geomaticaeambiente.klemgui.utils.RasterUtils; -import com.geomaticaeambiente.klemgui.utils.InitialData; import com.geomaticaeambiente.openjump.klem.flowdir.FlowDirsCalculator; import com.geomaticaeambiente.openjump.klem.flowdir.FlowDirsStripe; import com.geomaticaeambiente.openjump.klem.grid.DoubleBasicGrid; @@ -18,17 +27,15 @@ import com.geomaticaeambiente.openjump.klem.grid.FlowDirBasicGrid; import com.geomaticaeambiente.openjump.klem.hydrodistance.HydroDistCalculator; import com.vividsolutions.jts.geom.LineString; +import com.vividsolutions.jump.task.TaskMonitor; import com.vividsolutions.jump.util.StringUtil; +import com.vividsolutions.jump.workbench.Logger; import com.vividsolutions.jump.workbench.model.Layer; +import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn; import com.vividsolutions.jump.workbench.plugin.PlugInContext; +import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn; import com.vividsolutions.jump.workbench.ui.ErrorDialog; -import java.awt.geom.NoninvertibleTransformException; -import java.io.File; -import javax.swing.JButton; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JTextField; -import org.openjump.core.rasterimage.RasterImageLayer; +import com.vividsolutions.jump.workbench.ui.task.TaskMonitorManager; /** * @@ -36,7 +43,8 @@ */ public class HydroDistPlugIn extends AbstractInputKlemPlugin { - public HydroDistPlugIn(PlugInContext context, InitialDialog initialDialog, LayerablesList layerablesList) { + public HydroDistPlugIn(PlugInContext context, InitialDialog initialDialog, + LayerablesList layerablesList) { super(context, initialDialog); this.context = context; this.layerablesList = layerablesList; @@ -45,95 +53,153 @@ @Override public InitialData setInitialData() { -// ar_rasterImageLayers = PluginUtils.getRasterImageLayers(context); + // ar_rasterImageLayers = PluginUtils.getRasterImageLayers(context); - InitialData initialData = new InitialData(); + final InitialData initialData = new InitialData(); //input data - initialData.setParam_Label_TextBox(GUIUtils.setGUILabel(RASTER_DEM_LABEL), PluginUtils.getRasterImageLayers(layerablesList.getLayerables()), GUIUtils.INPUT);//combobox with rasterImageLayer + initialData.setParam_Label_TextBox(GUIUtils + .setGUILabel(RASTER_DEM_LABEL), PluginUtils + .getRasterImageLayers(layerablesList.getLayerables()), + GUIUtils.INPUT);//combobox with rasterImageLayer initialData.setParam_Label_TextBox( - GUIUtils.setGUILabel(PluginUtils.getResources().getString("HydrographKlemPlugin.Bluelines.label")), - PluginUtils.getLayers(layerablesList.getLayerables()), GUIUtils.INPUT); - + GUIUtils.setGUILabel(PluginUtils.getResources().getString( + "HydrographKlemPlugin.Bluelines.label")), + PluginUtils.getLayers(layerablesList.getLayerables()), + GUIUtils.INPUT); + //output data - initialData.setParam_Label_TextBox_Button(GUIUtils.setGUILabel(HYDRODIST_LABEL), "", new ActionObject(""), GUIUtils.OUTPUT); + initialData.setParam_Label_TextBox_Button( + GUIUtils.setGUILabel(HYDRODIST_LABEL), "", + new ActionObject(""), GUIUtils.OUTPUT); return initialData; } @Override - public ComponentsTreeMap setComponentsActions(ComponentsTreeMap personalTreeMap) { + public ComponentsTreeMap setComponentsActions( + ComponentsTreeMap personalTreeMap) { - final JTextField outputTextField = (JTextField) personalTreeMap.getComponent("00", GUIUtils.OUTPUT, 1); - JButton outputButton = (JButton) personalTreeMap.getComponent("00", GUIUtils.OUTPUT, 2); + final JTextField outputTextField = (JTextField) personalTreeMap + .getComponent("00", GUIUtils.OUTPUT, 1); + final JButton outputButton = (JButton) personalTreeMap.getComponent( + "00", GUIUtils.OUTPUT, 2); outputButton.setIcon(PluginUtils.getFolderIcon()); - outputButton.addActionListener(GUIUtils.setSaveRasterTif(outputTextField)); + outputButton.addActionListener(GUIUtils + .setSaveRasterTif(outputTextField)); return personalTreeMap; } + public void hydroDistCommand(final ComponentsTreeMap componentsWithActions) + throws Exception { + //get input raster names + final String rasterDem = GUIUtils.getStringValue(componentsWithActions + .getComponent("00", GUIUtils.INPUT, 1)); + + //get output raster name + final String outRasterName = GUIUtils + .getStringValue(componentsWithActions.getComponent("00", + GUIUtils.OUTPUT, 1)); + + //check input values + GUIUtils.checkStringValue(rasterDem, RASTER_DEM_LABEL); + + //check output values + GUIUtils.checkFileValue(outRasterName, GUIUtils.getOutputRasterString()); + + //get input raster as rasterImageLayer from string + final RasterImageLayer inputRasterSelected = PluginUtils + .getRasterImageLayerSelected((CustomComboBox.RasterComboBox) componentsWithActions + .getComponent("00", GUIUtils.INPUT, 1)); + final DoubleBasicGrid demGrid = RasterUtils + .getDoubleBasicGrid(inputRasterSelected); + + //Flow direction + + final Layer bluelinesLayer = PluginUtils + .getLayerSelected((CustomComboBox.LayerComboBox) componentsWithActions + .getComponent("01", GUIUtils.INPUT, 1)); + LineString[] bluelines = null; + if (bluelinesLayer != null) { + bluelines = GeometryUtils.getLineStringsFromFeatures(bluelinesLayer + .getFeatureCollectionWrapper()); + } + + final FlowDirsCalculator flowDirsCalculator = new FlowDirsCalculator( + demGrid, FlowDirsStripe.FlowDirAlgorithm.D8, bluelines, 100d); + final FlowDirBasicGrid flowDirsGrid = flowDirsCalculator.calculate(); + + //Calculate hydrography distance + final HydroDistCalculator hydroDistCalc = new HydroDistCalculator(); + final DoubleBasicGrid hydroDistGrid = hydroDistCalc + .calcD8(flowDirsGrid); + + //Create the output file and display on OJ + //Save grid as tiff + RasterUtils.saveOutputRasterAsTiff(hydroDistGrid, new File( + outRasterName)); + //Display raster on OJ from file + RasterUtils.displayRasterFileOnOJ(context.getWorkbenchContext(), + new File(outRasterName), null); + + JOptionPane.showMessageDialog(super.getInitialDialog(), PluginUtils + .getResources().getString("SetWorkspacePlugin.Done.message"), + PluginUtils.plugInName, JOptionPane.INFORMATION_MESSAGE); + + } + @Override public JPanel buildPluginPanel(final ComponentsTreeMap componentsWithActions) { - if(this.mainPanel != null) { - return this.mainPanel; + if (mainPanel != null) { + return mainPanel; } - this.mainPanel = new MainPanel(super.getInitialDialog(), componentsWithActions, false, false, true, - PluginUtils.getResources().getString("MainPanel.ExecuteButton.text"), layerablesList) { + mainPanel = new MainPanel(super.getInitialDialog(), + componentsWithActions, false, false, true, PluginUtils + .getResources().getString( + "MainPanel.ExecuteButton.text"), layerablesList) { + /** + * + */ + private static final long serialVersionUID = 1L; + @Override public void rightButton() { try { - //get input raster names - String rasterDem = GUIUtils.getStringValue(componentsWithActions.getComponent("00", GUIUtils.INPUT, 1)); + AbstractPlugIn.toActionListener(new ThreadedBasePlugIn() { + @Override + public String getName() { + return null; + } - //get output raster name - String outRasterName = GUIUtils.getStringValue(componentsWithActions.getComponent("00", GUIUtils.OUTPUT, 1)); + @Override + public boolean execute(PlugInContext context) + throws Exception { + return true; + } - //check input values - GUIUtils.checkStringValue(rasterDem, RASTER_DEM_LABEL); + @Override + public void run(TaskMonitor monitor, + PlugInContext context) throws Exception { + monitor.report(PluginUtils.getResources() + .getString("OpenKlem.executing-process")); + // monitor.allowCancellationRequests(); + reportNothingToUndoYet(context); + try { + hydroDistCommand(componentsWithActions); + } catch (final Exception ex) { + Logger.error(getName(), ex); + } + } + }, context.getWorkbenchContext(), new TaskMonitorManager()) + .actionPerformed(null); - //check output values - GUIUtils.checkFileValue(outRasterName, GUIUtils.getOutputRasterString()); - - //get input raster as rasterImageLayer from string - RasterImageLayer inputRasterSelected = PluginUtils.getRasterImageLayerSelected( - (CustomComboBox.RasterComboBox) componentsWithActions.getComponent("00", GUIUtils.INPUT, 1)); - DoubleBasicGrid demGrid = RasterUtils.getDoubleBasicGrid(inputRasterSelected); - - //Flow direction - - Layer bluelinesLayer = PluginUtils.getLayerSelected((CustomComboBox.LayerComboBox) componentsWithActions.getComponent("01", GUIUtils.INPUT, 1)); - LineString[] bluelines = null; - if(bluelinesLayer != null) { - bluelines = GeometryUtils.getLineStringsFromFeatures(bluelinesLayer.getFeatureCollectionWrapper()); - } - - FlowDirsCalculator flowDirsCalculator = new FlowDirsCalculator(demGrid, FlowDirsStripe.FlowDirAlgorithm.D8, bluelines, 100d); - FlowDirBasicGrid flowDirsGrid = flowDirsCalculator.calculate(); - - //Calculate hydrography distance - HydroDistCalculator hydroDistCalc = new HydroDistCalculator(); - DoubleBasicGrid hydroDistGrid = hydroDistCalc.calcD8(flowDirsGrid); - - //Create the output file and display on OJ - //Save grid as tiff - RasterUtils.saveOutputRasterAsTiff(hydroDistGrid, new File(outRasterName)); - //Display raster on OJ from file - RasterUtils.displayRasterFileOnOJ( - context.getWorkbenchContext(), - new File(outRasterName), - null); - - JOptionPane.showMessageDialog(super.getInitialDialog(), - PluginUtils.getResources().getString("SetWorkspacePlugin.Done.message"), PluginUtils.plugInName, JOptionPane.INFORMATION_MESSAGE); - - } catch (Exception ex) { - ErrorDialog.show( - super.getInitialDialog(), - PluginUtils.plugInName, - ex.toString(), + } catch (final Exception ex) { + ErrorDialog.show(super.getInitialDialog(), + PluginUtils.plugInName, ex.toString(), StringUtil.stackTrace(ex)); } } @@ -147,18 +213,21 @@ } }; - return this.mainPanel; + return mainPanel; } @Override public String toString() { - return PluginUtils.getResources().getString("HydroDistancePlugIn.PlugInName.label"); + return PluginUtils.getResources().getString( + "HydroDistancePlugIn.PlugInName.label"); } private MainPanel mainPanel; private final PlugInContext context; - private final String RASTER_DEM_LABEL = PluginUtils.getResources().getString("KlemGUI.InputFilledDem.label"); - private final String HYDRODIST_LABEL = PluginUtils.getResources().getString("HydroDistancePlugIn.PlugInName.label"); + private final String RASTER_DEM_LABEL = PluginUtils.getResources() + .getString("KlemGUI.InputFilledDem.label"); + private final String HYDRODIST_LABEL = PluginUtils.getResources() + .getString("HydroDistancePlugIn.PlugInName.label"); private final LayerablesList layerablesList; } Modified: plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/RoutingTimePlugIn.java =================================================================== --- plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/RoutingTimePlugIn.java 2019-01-19 08:00:37 UTC (rev 6102) +++ plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/RoutingTimePlugIn.java 2019-01-19 08:10:42 UTC (rev 6103) @@ -1,19 +1,25 @@ package com.geomaticaeambiente.klemgui.plugin.hydrology; -import com.geomaticaeambiente.klemgui.exceptions.WarningException; -import com.geomaticaeambiente.klemgui.utils.PluginUtils; +import java.io.File; + +import javax.swing.JButton; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; + import com.geomaticaeambiente.klemgui.ui.CustomComboBox; import com.geomaticaeambiente.klemgui.ui.GUIUtils; import com.geomaticaeambiente.klemgui.ui.InitialDialog; +import com.geomaticaeambiente.klemgui.ui.LayerablesList; import com.geomaticaeambiente.klemgui.ui.MainPanel; -import com.geomaticaeambiente.klemgui.ui.LayerablesList; import com.geomaticaeambiente.klemgui.utils.AbstractInputKlemPlugin; import com.geomaticaeambiente.klemgui.utils.ActionObject; import com.geomaticaeambiente.klemgui.utils.ComponentsTreeMap; import com.geomaticaeambiente.klemgui.utils.GeometryUtils; import com.geomaticaeambiente.klemgui.utils.HydroUtils; +import com.geomaticaeambiente.klemgui.utils.InitialData; +import com.geomaticaeambiente.klemgui.utils.PluginUtils; import com.geomaticaeambiente.klemgui.utils.RasterUtils; -import com.geomaticaeambiente.klemgui.utils.InitialData; import com.geomaticaeambiente.openjump.klem.grid.DoubleBasicGrid; import com.geomaticaeambiente.openjump.klem.routing.RoutingTimeCalculator; import com.geomaticaeambiente.openjump.klem.routing.RoutingTimeParameters; @@ -20,15 +26,15 @@ import com.geomaticaeambiente.openjump.klem.units.Area; import com.geomaticaeambiente.openjump.klem.units.Length; import com.vividsolutions.jts.geom.LineString; +import com.vividsolutions.jump.task.TaskMonitor; import com.vividsolutions.jump.util.StringUtil; +import com.vividsolutions.jump.workbench.Logger; import com.vividsolutions.jump.workbench.model.Layer; +import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn; import com.vividsolutions.jump.workbench.plugin.PlugInContext; +import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn; import com.vividsolutions.jump.workbench.ui.ErrorDialog; -import java.io.File; -import javax.swing.JButton; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JTextField; +import com.vividsolutions.jump.workbench.ui.task.TaskMonitorManager; /** * @@ -36,7 +42,8 @@ */ public class RoutingTimePlugIn extends AbstractInputKlemPlugin { - public RoutingTimePlugIn(PlugInContext context, InitialDialog initialDialog, LayerablesList layerablesList) { + public RoutingTimePlugIn(PlugInContext context, + InitialDialog initialDialog, LayerablesList layerablesList) { super(context, initialDialog); this.context = context; this.layerablesList = layerablesList; @@ -45,117 +52,265 @@ @Override public InitialData setInitialData() { -// rasterImageLayers = PluginUtils.getRasterImageLayers(context); + // rasterImageLayers = PluginUtils.getRasterImageLayers(context); - InitialData initialData = new InitialData(); + final InitialData initialData = new InitialData(); //input data - initialData.setParam_Label_TextBox(GUIUtils.setGUILabel(DEM_LABEL), PluginUtils.getRasterImageLayers(layerablesList.getLayerables()), GUIUtils.INPUT);//dem - initialData.setParam_Label_TextBox( - GUIUtils.setGUILabel(PluginUtils.getResources().getString("HydrographKlemPlugin.Bluelines.label")), - PluginUtils.getLayers(layerablesList.getLayerables()), GUIUtils.INPUT); // Bluelines - initialData.setParam_Label_TextBox_Label(GUIUtils.setGUILabel(SLOPE_VELOCITY_LABEL), "0.02", M_S, GUIUtils.INPUT); //slope velocity - initialData.setParam_Label_TextBox_Label(GUIUtils.setGUILabel(CHANNEL_VELOCITY_LABEL), "2.0", M_S, GUIUtils.INPUT); //channel velocity - initialData.setParam_Label_TextBox_Label(GUIUtils.setGUILabel(MIN_THRESHOLD_LABEL), "0.01", KMQ, GUIUtils.INPUT); //min threshold - initialData.setParam_Label_TextBox_Label(GUIUtils.setGUILabel(MAX_THRESHOLD_LABEL), "100000000", KMQ, GUIUtils.INPUT); //max threshold - initialData.setParam_Label_TextBox(GUIUtils.setGUILabel(EXPONENT), "0", GUIUtils.INPUT); //exponent + initialData + .setParam_Label_TextBox(GUIUtils.setGUILabel(DEM_LABEL), + PluginUtils.getRasterImageLayers(layerablesList + .getLayerables()), GUIUtils.INPUT);//dem + initialData.setParam_Label_TextBox( + GUIUtils.setGUILabel(PluginUtils.getResources().getString( + "HydrographKlemPlugin.Bluelines.label")), + PluginUtils.getLayers(layerablesList.getLayerables()), + GUIUtils.INPUT); // Bluelines + initialData.setParam_Label_TextBox_Label( + GUIUtils.setGUILabel(SLOPE_VELOCITY_LABEL), "0.02", M_S, + GUIUtils.INPUT); //slope velocity + initialData.setParam_Label_TextBox_Label( + GUIUtils.setGUILabel(CHANNEL_VELOCITY_LABEL), "2.0", M_S, + GUIUtils.INPUT); //channel velocity + initialData.setParam_Label_TextBox_Label( + GUIUtils.setGUILabel(MIN_THRESHOLD_LABEL), "0.01", KMQ, + GUIUtils.INPUT); //min threshold + initialData.setParam_Label_TextBox_Label( + GUIUtils.setGUILabel(MAX_THRESHOLD_LABEL), "100000000", KMQ, + GUIUtils.INPUT); //max threshold + initialData.setParam_Label_TextBox(GUIUtils.setGUILabel(EXPONENT), "0", + GUIUtils.INPUT); //exponent //output data - initialData.setParam_Label_TextBox_Button(GUIUtils.setGUILabel(ROUTINGTIME_LABEL), "", new ActionObject(""), GUIUtils.OUTPUT); //output raster + initialData.setParam_Label_TextBox_Button(GUIUtils + .setGUILabel(ROUTINGTIME_LABEL), "", new ActionObject(""), + GUIUtils.OUTPUT); //output raster return initialData; } @Override - public ComponentsTreeMap setComponentsActions(ComponentsTreeMap personalTreeMap) { + public ComponentsTreeMap setComponentsActions( + ComponentsTreeMap personalTreeMap) { - final JTextField outputTextField = (JTextField) personalTreeMap.getComponent("00", GUIUtils.OUTPUT, 1); - JButton outputButton = (JButton) personalTreeMap.getComponent("00", GUIUtils.OUTPUT, 2); + final JTextField outputTextField = (JTextField) personalTreeMap + .getComponent("00", GUIUtils.OUTPUT, 1); + final JButton outputButton = (JButton) personalTreeMap.getComponent( + "00", GUIUtils.OUTPUT, 2); outputButton.setIcon(PluginUtils.getFolderIcon()); - outputButton.addActionListener(GUIUtils.setSaveRasterTif(outputTextField)); + outputButton.addActionListener(GUIUtils + .setSaveRasterTif(outputTextField)); return personalTreeMap; } + public void routingTimeCommand(final ComponentsTreeMap componentsWithActions) + throws Exception { + //get input raster names + final String demRasterSelected = GUIUtils + .getStringValue(componentsWithActions.getComponent("00", + GUIUtils.INPUT, 1)); + final Layer bluelinesLayer = PluginUtils + .getLayerSelected((CustomComboBox.LayerComboBox) componentsWithActions + .getComponent("01", GUIUtils.INPUT, 1)); + + //get input values + final double slopeVelValue = GUIUtils + .getDoubleValue(componentsWithActions.getComponent("02", + GUIUtils.INPUT, 1)); + final double channelVelValue = GUIUtils + .getDoubleValue(componentsWithActions.getComponent("03", + GUIUtils.INPUT, 1)); + final double minThresholdValue = GUIUtils + .getDoubleValue(componentsWithActions.getComponent("04", + GUIUtils.INPUT, 1)); + final double maxThresholdValue = GUIUtils + .getDoubleValue(componentsWithActions.getComponent("05", + GUIUtils.INPUT, 1)); + final double exponent = GUIUtils.getDoubleValue(componentsWithActions + .getComponent("06", GUIUtils.INPUT, 1)); + + //get output raster name + final String outRasterName = GUIUtils + .getStringValue(componentsWithActions.getComponent("00", + GUIUtils.OUTPUT, 1)); + + //checks + GUIUtils.checkStringValue(demRasterSelected, DEM_LABEL); + GUIUtils.checkFileValue(outRasterName, GUIUtils.getOutputRasterString()); + //convert string values in correct objects + //get input dem + final DoubleBasicGrid demGrid = RasterUtils + .getDoubleBasicGrid((CustomComboBox.RasterComboBox) componentsWithActions + .getComponent("00", GUIUtils.INPUT, 1)); + + //get Parameters + final RoutingTimeParameters routingTimeParameters = new RoutingTimeParameters( + HydroUtils.calculateSpeed(slopeVelValue), + HydroUtils.calculateSpeed(channelVelValue), new Area( + minThresholdValue, Length.LengthUnit.km), new Area( + maxThresholdValue, Length.LengthUnit.km), exponent); + + LineString[] bluelines = null; + if (bluelinesLayer != null) { + bluelines = GeometryUtils.getLineStringsFromFeatures(bluelinesLayer + .getFeatureCollectionWrapper()); + } + + //Routing time + final RoutingTimeCalculator routingTimeCalculator = new RoutingTimeCalculator(); + final DoubleBasicGrid routingTimeGrid = routingTimeCalculator.calcD8( + demGrid, bluelines, 100d, routingTimeParameters); + + //Create the output file and display on OJ + //Save grid as tiff + RasterUtils.saveOutputRasterAsTiff(routingTimeGrid, new File( + outRasterName)); + //Display raster on OJ from file + RasterUtils.displayRasterFileOnOJ(context.getWorkbenchContext(), + new File(outRasterName), null); + + JOptionPane.showMessageDialog(super.getInitialDialog(), PluginUtils + .getResources().getString("SetWorkspacePlugin.Done.message"), + PluginUtils.plugInName, JOptionPane.INFORMATION_MESSAGE); + } + @Override public JPanel buildPluginPanel(final ComponentsTreeMap componentsWithActions) { - if(this.mainPanel != null) { - return this.mainPanel; + if (mainPanel != null) { + return mainPanel; } - this.mainPanel = new MainPanel(super.getInitialDialog(), componentsWithActions, false, false, true, - PluginUtils.getResources().getString("MainPanel.ExecuteButton.text"), layerablesList) { + mainPanel = new MainPanel(super.getInitialDialog(), + componentsWithActions, false, false, true, PluginUtils + .getResources().getString( + "MainPanel.ExecuteButton.text"), layerablesList) { + /** + * + */ + private static final long serialVersionUID = 1L; + @Override public void rightButton() { try { + AbstractPlugIn.toActionListener(new ThreadedBasePlugIn() { + @Override + public String getName() { + return null; + } + + @Override + public boolean execute(PlugInContext context) + throws Exception { + return true; + } + + @Override + public void run(TaskMonitor monitor, + PlugInContext context) throws Exception { + monitor.report(PluginUtils.getResources() + .getString("OpenKlem.executing-process")); + // monitor.allowCancellationRequests(); + reportNothingToUndoYet(context); + try { + routingTimeCommand(componentsWithActions); + } catch (final Exception ex) { + Logger.error(getName(), ex); + } + } + }, context.getWorkbenchContext(), new TaskMonitorManager()) + .actionPerformed(null); + //get input raster names - String demRasterSelected = GUIUtils.getStringValue(componentsWithActions.getComponent("00", GUIUtils.INPUT, 1)); - Layer bluelinesLayer = PluginUtils.getLayerSelected((CustomComboBox.LayerComboBox) componentsWithActions.getComponent("01", GUIUtils.INPUT, 1)); - - //get input values - double slopeVelValue = GUIUtils.getDoubleValue(componentsWithActions.getComponent("02", GUIUtils.INPUT, 1)); - double channelVelValue = GUIUtils.getDoubleValue(componentsWithActions.getComponent("03", GUIUtils.INPUT, 1)); - double minThresholdValue = GUIUtils.getDoubleValue(componentsWithActions.getComponent("04", GUIUtils.INPUT, 1)); - double maxThresholdValue = GUIUtils.getDoubleValue(componentsWithActions.getComponent("05", GUIUtils.INPUT, 1)); - double exponent = GUIUtils.getDoubleValue(componentsWithActions.getComponent("06", GUIUtils.INPUT, 1)); + /* final String demRasterSelected = GUIUtils + .getStringValue(componentsWithActions.getComponent( + "00", GUIUtils.INPUT, 1)); + final Layer bluelinesLayer = PluginUtils + .getLayerSelected((CustomComboBox.LayerComboBox) componentsWithActions + .getComponent("01", GUIUtils.INPUT, 1)); - //get output raster name - String outRasterName = GUIUtils.getStringValue(componentsWithActions.getComponent("00", GUIUtils.OUTPUT, 1)); + //get input values + final double slopeVelValue = GUIUtils + .getDoubleValue(componentsWithActions.getComponent( + "02", GUIUtils.INPUT, 1)); + final double channelVelValue = GUIUtils + .getDoubleValue(componentsWithActions.getComponent( + "03", GUIUtils.INPUT, 1)); + final double minThresholdValue = GUIUtils + .getDoubleValue(componentsWithActions.getComponent( + "04", GUIUtils.INPUT, 1)); + final double maxThresholdValue = GUIUtils + .getDoubleValue(componentsWithActions.getComponent( + "05", GUIUtils.INPUT, 1)); + final double exponent = GUIUtils + .getDoubleValue(componentsWithActions.getComponent( + "06", GUIUtils.INPUT, 1)); - //checks - GUIUtils.checkStringValue(demRasterSelected, DEM_LABEL); - GUIUtils.checkFileValue(outRasterName, GUIUtils.getOutputRasterString()); - //convert string values in correct objects - //get input dem - DoubleBasicGrid demGrid = RasterUtils.getDoubleBasicGrid( - (CustomComboBox.RasterComboBox) componentsWithActions.getComponent("00", GUIUtils.INPUT, 1)); + //get output raster name + final String outRasterName = GUIUtils + .getStringValue(componentsWithActions.getComponent( + "00", GUIUtils.OUTPUT, 1)); - //get Parameters - RoutingTimeParameters routingTimeParameters = new RoutingTimeParameters( - HydroUtils.calculateSpeed(slopeVelValue), - HydroUtils.calculateSpeed(channelVelValue), - new Area(minThresholdValue, Length.LengthUnit.km), - new Area(maxThresholdValue, Length.LengthUnit.km), - exponent); + //checks + GUIUtils.checkStringValue(demRasterSelected, DEM_LABEL); + GUIUtils.checkFileValue(outRasterName, + GUIUtils.getOutputRasterString()); + //convert string values in correct objects + //get input dem + final DoubleBasicGrid demGrid = RasterUtils + .getDoubleBasicGrid((CustomComboBox.RasterComboBox) componentsWithActions + .getComponent("00", GUIUtils.INPUT, 1)); - LineString[] bluelines = null; - if(bluelinesLayer != null) { - bluelines = GeometryUtils.getLineStringsFromFeatures(bluelinesLayer.getFeatureCollectionWrapper()); - } - - //Routing time - RoutingTimeCalculator routingTimeCalculator = new RoutingTimeCalculator(); - DoubleBasicGrid routingTimeGrid = routingTimeCalculator.calcD8(demGrid, bluelines, 100d, routingTimeParameters); + //get Parameters + final RoutingTimeParameters routingTimeParameters = new RoutingTimeParameters( + HydroUtils.calculateSpeed(slopeVelValue), + HydroUtils.calculateSpeed(channelVelValue), + new Area(minThresholdValue, Length.LengthUnit.km), + new Area(maxThresholdValue, Length.LengthUnit.km), + exponent); - //Create the output file and display on OJ - //Save grid as tiff - RasterUtils.saveOutputRasterAsTiff(routingTimeGrid, new File(outRasterName)); - //Display raster on OJ from file - RasterUtils.displayRasterFileOnOJ( - context.getWorkbenchContext(), - new File(outRasterName), - null); + LineString[] bluelines = null; + if (bluelinesLayer != null) { + bluelines = GeometryUtils + .getLineStringsFromFeatures(bluelinesLayer + .getFeatureCollectionWrapper()); + } - JOptionPane.showMessageDialog(super.getInitialDialog(), - PluginUtils.getResources().getString("SetWorkspacePlugin.Done.message"), PluginUtils.plugInName, JOptionPane.INFORMATION_MESSAGE); - - } catch (WarningException ex) { - JOptionPane.showMessageDialog( - super.getInitialDialog(), - ex.getMessage(), - PluginUtils.plugInName, - JOptionPane.WARNING_MESSAGE); - } catch (Exception ex) { - ErrorDialog.show( - super.getInitialDialog(), - PluginUtils.plugInName, - ex.toString(), + //Routing time + final RoutingTimeCalculator routingTimeCalculator = new RoutingTimeCalculator(); + final DoubleBasicGrid routingTimeGrid = routingTimeCalculator + .calcD8(demGrid, bluelines, 100d, + routingTimeParameters); + + //Create the output file and display on OJ + //Save grid as tiff + RasterUtils.saveOutputRasterAsTiff(routingTimeGrid, + new File(outRasterName)); + //Display raster on OJ from file + RasterUtils.displayRasterFileOnOJ(context + .getWorkbenchContext(), new File(outRasterName), + null); + + JOptionPane.showMessageDialog( + super.getInitialDialog(), + PluginUtils.getResources().getString( + "SetWorkspacePlugin.Done.message"), + PluginUtils.plugInName, + JOptionPane.INFORMATION_MESSAGE); + + } catch (final WarningException ex) { + JOptionPane.showMessageDialog(super.getInitialDialog(), + ex.getMessage(), PluginUtils.plugInName, + JOptionPane.WARNING_MESSAGE);*/ + } catch (final Exception ex) { + ErrorDialog.show(super.getInitialDialog(), + PluginUtils.plugInName, ex.toString(), StringUtil.stackTrace(ex)); } }; - + @Override public void leftButton() { } @@ -165,27 +320,35 @@ } }; - return this.mainPanel; + return mainPanel; } @Override public String toString() { - return PluginUtils.getResources().getString("RoutingTimePlugIn.RoutingTime.label"); + return PluginUtils.getResources().getString( + "RoutingTimePlugIn.RoutingTime.label"); } private MainPanel mainPanel; private final PlugInContext context; - private final String DEM_LABEL = PluginUtils.getResources().getString("KlemGUI.InputFilledDem.label"); + private final String DEM_LABEL = PluginUtils.getResources().getString( + "KlemGUI.InputFilledDem.label"); //private final String UPSLOPE_LABEL = PluginUtils.getResources().getString("KlemGUI.InputUpslope.label"); - private final String SLOPE_VELOCITY_LABEL = PluginUtils.getResources().getString("RoutingTimePlugIn.SlopeVelociti.label"); - private final String CHANNEL_VELOCITY_LABEL = PluginUtils.getResources().getString("RoutingTimePlugIn.ChannelVelocity.label"); - private final String MIN_THRESHOLD_LABEL = PluginUtils.getResources().getString("RoutingTimePlugIn.MinThreshold.label"); - private final String MAX_THRESHOLD_LABEL = PluginUtils.getResources().getString("RoutingTimePlugIn.MaxThreshold.label"); - private final String EXPONENT = PluginUtils.getResources().getString("RoutingTimePlugIn.Exponent.label"); + private final String SLOPE_VELOCITY_LABEL = PluginUtils.getResources() + .getString("RoutingTimePlugIn.SlopeVelociti.label"); + private final String CHANNEL_VELOCITY_LABEL = PluginUtils.getResources() + .getString("RoutingTimePlugIn.ChannelVelocity.label"); + private final String MIN_THRESHOLD_LABEL = PluginUtils.getResources() + .getString("RoutingTimePlugIn.MinThreshold.label"); + private final String MAX_THRESHOLD_LABEL = PluginUtils.getResources() + .getString("RoutingTimePlugIn.MaxThreshold.label"); + private final String EXPONENT = PluginUtils.getResources().getString( + "RoutingTimePlugIn.Exponent.label"); private final String M_S = "[m/s]"; private final String KMQ = "[km2]"; - private final String ROUTINGTIME_LABEL = PluginUtils.getResources().getString("RoutingTimePlugIn.RoutingTime.label"); - + private final String ROUTINGTIME_LABEL = PluginUtils.getResources() + .getString("RoutingTimePlugIn.RoutingTime.label"); + private final LayerablesList layerablesList; } Modified: plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/UpslopeAreaPlugIn.java =================================================================== --- plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/UpslopeAreaPlugIn.java 2019-01-19 08:00:37 UTC (rev 6102) +++ plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/UpslopeAreaPlugIn.java 2019-01-19 08:10:42 UTC (rev 6103) @@ -1,34 +1,41 @@ package com.geomaticaeambiente.klemgui.plugin.hydrology; -import com.geomaticaeambiente.klemgui.utils.PluginUtils; +import java.io.File; + +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; + +import org.openjump.core.rasterimage.RasterImageLayer; + import com.geomaticaeambiente.klemgui.ui.CustomComboBox; import com.geomaticaeambiente.klemgui.ui.GUIUtils; import com.geomaticaeambiente.klemgui.ui.InitialDialog; +import com.geomaticaeambiente.klemgui.ui.LayerablesList; import com.geomaticaeambiente.klemgui.ui.MainPanel; -import com.geomaticaeambiente.klemgui.ui.LayerablesList; import com.geomaticaeambiente.klemgui.ui.Symbologies; import com.geomaticaeambiente.klemgui.utils.AbstractInputKlemPlugin; import com.geomaticaeambiente.klemgui.utils.ActionObject; import com.geomaticaeambiente.klemgui.utils.ComponentsTreeMap; import com.geomaticaeambiente.klemgui.utils.GeometryUtils; +import com.geomaticaeambiente.klemgui.utils.InitialData; +import com.geomaticaeambiente.klemgui.utils.PluginUtils; import com.geomaticaeambiente.klemgui.utils.RasterUtils; -import com.geomaticaeambiente.klemgui.utils.InitialData; import com.geomaticaeambiente.openjump.klem.flowdir.FlowDirsStripe.FlowDirAlgorithm; import com.geomaticaeambiente.openjump.klem.grid.DoubleBasicGrid; import com.geomaticaeambiente.openjump.klem.upslopearea.UpslopeAreaCalculator; import com.vividsolutions.jts.geom.LineString; +import com.vividsolutions.jump.task.TaskMonitor; import com.vividsolutions.jump.util.StringUtil; +import com.vividsolutions.jump.workbench.Logger; import com.vividsolutions.jump.workbench.model.Layer; +import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn; import com.vividsolutions.jump.workbench.plugin.PlugInContext; +import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn; import com.vividsolutions.jump.workbench.ui.ErrorDialog; -import java.awt.geom.NoninvertibleTransformException; -import java.io.File; -import javax.swing.JButton; -import javax.swing.JComboBox; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JTextField; -import org.openjump.core.rasterimage.RasterImageLayer; +import com.vividsolutions.jump.workbench.ui.task.TaskMonitorManager; /** * @@ -36,7 +43,8 @@ */ public class UpslopeAreaPlugIn extends AbstractInputKlemPlugin { - public UpslopeAreaPlugIn(PlugInContext context, InitialDialog initialDialog, LayerablesList layerablesList) { + public UpslopeAreaPlugIn(PlugInContext context, + InitialDialog initialDialog, LayerablesList layerablesList) { super(context, initialDialog); this.context = context; this.layerablesList = layerablesList; @@ -46,107 +54,162 @@ public InitialData setInitialData() { // Input data - InitialData initialData = new InitialData(); + final InitialData initialData = new InitialData(); + initialData + .setParam_Label_TextBox(GUIUtils.setGUILabel(DEM_LABEL), + PluginUtils.getRasterImageLayers(layerablesList + .getLayerables()), GUIUtils.INPUT); initialData.setParam_Label_TextBox( - GUIUtils.setGUILabel(DEM_LABEL), - PluginUtils.getRasterImageLayers(layerablesList.getLayerables()), + GUIUtils.setGUILabel(PluginUtils.getResources().getString( + "HydrographKlemPlugin.Bluelines.label")), + PluginUtils.getLayers(layerablesList.getLayerables()), GUIUtils.INPUT); + + final String[] hyetoTypes = { D8_LABEL, MF_LABEL }; initialData.setParam_Label_TextBox( - GUIUtils.setGUILabel(PluginUtils.getResources().getString("HydrographKlemPlugin.Bluelines.label")), - PluginUtils.getLayers(layerablesList.getLayerables()), GUIUtils.INPUT); - - String[] hyetoTypes = { - D8_LABEL, - MF_LABEL - }; - initialData.setParam_Label_TextBox(GUIUtils.setGUILabel(FLOWALGO_LABEL), hyetoTypes, GUIUtils.INPUT); - + GUIUtils.setGUILabel(FLOWALGO_LABEL), hyetoTypes, + GUIUtils.INPUT); + // Output data - initialData.setParam_Label_TextBox_Button(GUIUtils.setGUILabel(UPSLOPEAREA_LABEL), "", new ActionObject(""), GUIUtils.OUTPUT); + initialData.setParam_Label_TextBox_Button(GUIUtils + .setGUILabel(UPSLOPEAREA_LABEL), "", new ActionObject(""), + GUIUtils.OUTPUT); return initialData; } @Override - public ComponentsTreeMap setComponentsActions(ComponentsTreeMap personalTreeMap) { - final JTextField outputTextField = (JTextField) personalTreeMap.getComponent("00", GUIUtils.OUTPUT, 1); - JButton outputButton = (JButton) personalTreeMap.getComponent("00", GUIUtils.OUTPUT, 2); + public ComponentsTreeMap setComponentsActions( + ComponentsTreeMap personalTreeMap) { + final JTextField outputTextField = (JTextField) personalTreeMap + .getComponent("00", GUIUtils.OUTPUT, 1); + final JButton outputButton = (JButton) personalTreeMap.getComponent( + "00", GUIUtils.OUTPUT, 2); outputButton.setIcon(PluginUtils.getFolderIcon()); - outputButton.addActionListener(GUIUtils.setSaveRasterTif(outputTextField)); + outputButton.addActionListener(GUIUtils + .setSaveRasterTif(outputTextField)); return personalTreeMap; } + public void upSlopeAreaCommand(final ComponentsTreeMap componentsWithActions) + throws Exception { + //get input raster names + final String flowDirRaster = GUIUtils + .getStringValue(componentsWithActions.getComponent("00", + GUIUtils.INPUT, 1)); + + final JComboBox jComboBox_Algo = (JComboBox) componentsWithActions + .getComponent("02", GUIUtils.INPUT, 1); + final String selectedAlgo = jComboBox_Algo.getSelectedItem().toString(); + + FlowDirAlgorithm flowDirAlgo = FlowDirAlgorithm.D8; + if (selectedAlgo.equals(D8_LABEL)) { + flowDirAlgo = FlowDirAlgorithm.D8; + } else if (selectedAlgo.equals(MF_LABEL)) { + flowDirAlgo = FlowDirAlgorithm.MultiFlow; + } + + //get output raster name + final String outRasterName = GUIUtils + .getStringValue(componentsWithActions.getComponent("00", + GUIUtils.OUTPUT, 1)); + + //check input values + GUIUtils.checkStringValue(flowDirRaster, DEM_LABEL); + + //check output values + GUIUtils.checkStringValue(outRasterName, + GUIUtils.getOutputRasterString()); + + //get input raster as rasterImageLayer from string + final RasterImageLayer inputRasterSelected = PluginUtils + .getRasterImageLayerSelected((CustomComboBox.RasterComboBox) componentsWithActions + .getComponent("00", GUIUtils.INPUT, 1)); + final DoubleBasicGrid demGrid = RasterUtils + .getDoubleBasicGrid(inputRasterSelected); + + final Layer bluelinesLayer = PluginUtils + .getLayerSelected((CustomComboBox.LayerComboBox) componentsWithActions + .getComponent("01", GUIUtils.INPUT, 1)); + + LineString[] bluelines = null; + if (bluelinesLayer != null) { + bluelines = GeometryUtils.getLineStringsFromFeatures(bluelinesLayer + .getFeatureCollectionWrapper()); + } + + //Calculate upslope area + final UpslopeAreaCalculator upslopeArea = new UpslopeAreaCalculator( + demGrid, bluelines, 100d); + final DoubleBasicGrid upslopeGrid = upslopeArea.calc(flowDirAlgo); + + //Create the output file and display on OJ + //Save grid as tiff + RasterUtils + .saveOutputRasterAsTiff(upslopeGrid, new File(outRasterName)); + //Display raster on OJ from file + RasterUtils.displayRasterFileOnOJ(context.getWorkbenchContext(), + new File(outRasterName), + Symbologies.getUpslopeAreaSymb(upslopeGrid.getCellSize())); + + JOptionPane.showMessageDialog(super.getInitialDialog(), PluginUtils + .getResources().getString("SetWorkspacePlugin.Done.message"), + PluginUtils.plugInName, JOptionPane.INFORMATION_MESSAGE); + } + @Override public JPanel buildPluginPanel(final ComponentsTreeMap componentsWithActions) { - - if(this.mainPanel != null) { - return this.mainPanel; + + if (mainPanel != null) { + return mainPanel; } - this.mainPanel = new MainPanel(super.getInitialDialog(), componentsWithActions, false, false, true, - PluginUtils.getResources().getString("MainPanel.ExecuteButton.text"), layerablesList) { + mainPanel = new MainPanel(super.getInitialDialog(), + componentsWithActions, false, false, true, PluginUtils + .getResources().getString( + "MainPanel.ExecuteButton.text"), layerablesList) { + /** + * + */ + private static final long serialVersionUID = 1L; + @Override public void rightButton() { try { - //get input raster names - String flowDirRaster = GUIUtils.getStringValue(componentsWithActions.getComponent("00", GUIUtils.INPUT, 1)); + AbstractPlugIn.toActionListener(new ThreadedBasePlugIn() { + @Override + public String getName() { + return null; + } - JComboBox jComboBox_Algo = (JComboBox) componentsWithActions.getComponent("02", GUIUtils.INPUT, 1); - String selectedAlgo = jComboBox_Algo.getSelectedItem().toString(); - - FlowDirAlgorithm flowDirAlgo = FlowDirAlgorithm.D8; - if(selectedAlgo.equals(D8_LABEL)) { - flowDirAlgo = FlowDirAlgorithm.D8; - } else if(selectedAlgo.equals(MF_LABEL)) { - flowDirAlgo = FlowDirAlgorithm.MultiFlow; - } - - - //get output raster name - String outRasterName = GUIUtils.getStringValue(componentsWithActions.getComponent("00", GUIUtils.OUTPUT, 1)); + @Override + public boolean execute(PlugInContext context) + throws Exception { + return true; + } - //check input values - GUIUtils.checkStringValue(flowDirRaster, DEM_LABEL); + @Override + public void run(TaskMonitor monitor, + PlugInContext context) throws Exception { + monitor.report(PluginUtils.getResources() + .getString("OpenKlem.executing-process")); + // monitor.allowCancellationRequests(); + reportNothingToUndoYet(context); + try { + upSlopeAreaCommand(componentsWithActions); + } catch (final Exception ex) { + Logger.error(getName(), ex); + } + } + }, context.getWorkbenchContext(), new TaskMonitorManager()) + .actionPerformed(null); - //check output values - GUIUtils.checkStringValue(outRasterName, GUIUtils.getOutputRasterString()); - - //get input raster as rasterImageLayer from string - RasterImageLayer inputRasterSelected = PluginUtils.getRasterImageLayerSelected( - (CustomComboBox.RasterComboBox) componentsWithActions.getComponent("00", GUIUtils.INPUT, 1)); - DoubleBasicGrid demGrid = RasterUtils.getDoubleBasicGrid(inputRasterSelected); - - Layer bluelinesLayer = PluginUtils.getLayerSelected((CustomComboBox.LayerComboBox) componentsWithActions.getComponent("01", GUIUtils.INPUT, 1)); - - LineString[] bluelines = null; - if(bluelinesLayer != null) { - bluelines = GeometryUtils.getLineStringsFromFeatures(bluelinesLayer.getFeatureCollectionWrapper()); - } - - //Calculate upslope area - UpslopeAreaCalculator upslopeArea = new UpslopeAreaCalculator(demGrid, bluelines, 100d); - DoubleBasicGrid upslopeGrid = upslopeArea.calc(flowDirAlgo); - - //Create the output file and display on OJ - //Save grid as tiff - RasterUtils.saveOutputRasterAsTiff(upslopeGrid, new File(outRasterName)); - //Display raster on OJ from file - RasterUtils.displayRasterFileOnOJ( - context.getWorkbenchContext(), - new File(outRasterName), - Symbologies.getUpslopeAreaSymb(upslopeGrid.getCellSize())); - - JOptionPane.showMessageDialog(super.getInitialDialog(), - PluginUtils.getResources().getString("SetWorkspacePlugin.Done.message"), PluginUtils.plugInName, JOptionPane.INFORMATION_MESSAGE); - - } catch (Exception ex) { - ErrorDialog.show( - super.getInitialDialog(), - PluginUtils.plugInName, - ex.toString(), + } catch (final Exception ex) { + ErrorDialog.show(super.getInitialDialog(), + PluginUtils.plugInName, ex.toString(), StringUtil.stackTrace(ex)); } @@ -163,20 +226,26 @@ } }; - return this.mainPanel; + return mainPanel; } @Override public String toString() { - return PluginUtils.getResources().getString("UpslopeAreaPlugin.PlugInName.label"); + return PluginUtils.getResources().getString( + "UpslopeAreaPlugin.PlugInName.label"); } private MainPanel mainPanel; private final PlugInContext context; - private final String DEM_LABEL = PluginUtils.getResources().getString("KlemGUI.InputFilledDem.label"); - private final String FLOWALGO_LABEL = PluginUtils.getResources().getString("KlemGUI.InputFlowDirAlgo.label"); - private final String D8_LABEL = PluginUtils.getResources().getString("FlowDirectionsPlugIn.FlowDirModel.D8.label"); - private final String MF_LABEL = PluginUtils.getResources().getString("FlowDirectionsPlugIn.FlowDirModel.MF.label"); - private final String UPSLOPEAREA_LABEL = PluginUtils.getResources().getString("UpslopeAreaPlugin.PlugInName.label"); + private final String DEM_LABEL = PluginUtils.getResources().getString( + "KlemGUI.InputFilledDem.label"); + private final String FLOWALGO_LABEL = PluginUtils.getResources().getString( + "KlemGUI.InputFlowDirAlgo.label"); + private final String D8_LABEL = PluginUtils.getResources().getString( + "FlowDirectionsPlugIn.FlowDirModel.D8.label"); + private final String MF_LABEL = PluginUtils.getResources().getString( + "FlowDirectionsPlugIn.FlowDirModel.MF.label"); + private final String UPSLOPEAREA_LABEL = PluginUtils.getResources() + .getString("UpslopeAreaPlugin.PlugInName.label"); private final LayerablesList layerablesList; } Modified: plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/WatershedPlugIn.java =================================================================== --- plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/WatershedPlugIn.java 2019-01-19 08:00:37 UTC (rev 6102) +++ plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/hydrology/WatershedPlugIn.java 2019-01-19 08:10:42 UTC (rev 6103) @@ -1,7 +1,26 @@ package com.geomaticaeambiente.klemgui.plugin.hydrology; -import com.geomaticaeambiente.klemgui.exceptions.WarningException; -import com.geomaticaeambiente.klemgui.utils.PluginUtils; +import java.awt.Dialog; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JRadioButton; +import javax.swing.JTextField; + +import org.openjump.core.rasterimage.RasterImageLayer; + import com.geomaticaeambiente.klemgui.plugin.hydrology.hydrographs.klem.WatershedInformation; import com.geomaticaeambiente.klemgui.ui.CustomComboBox; import com.geomaticaeambiente.klemgui.ui.CustomComboBox.LayerComboBox; @@ -8,14 +27,15 @@ import com.geomaticaeambiente.klemgui.ui.CustomComboBox.RasterComboBox; import com.geomaticaeambiente.klemgui.ui.GUIUtils; import com.geomaticaeambiente.klemgui.ui.InitialDialog; +import com.geomaticaeambiente.klemgui.ui.LayerablesList; import com.geomaticaeambiente.klemgui.ui.MainPanel; -import com.geomaticaeambiente.klemgui.ui.LayerablesList; import com.geomaticaeambiente.klemgui.utils.AbstractInputKlemPlugin; import com.geomaticaeambiente.klemgui.utils.ActionObject; import com.geomaticaeambiente.klemgui.utils.ComponentsTreeMap; import com.geomaticaeambiente.klemgui.utils.GeometryUtils; +import com.geomaticaeambiente.klemgui.utils.InitialData; +import com.geomaticaeambiente.klemgui.utils.PluginUtils; import com.geomaticaeambiente.klemgui.utils.RasterUtils; -import com.geomaticaeambiente.klemgui.utils.InitialData; import com.geomaticaeambiente.openjump.klem.flowdir.FlowDirsCalculator; import com.geomaticaeambiente.openjump.klem.flowdir.FlowDirsStripe; import com.geomaticaeambiente.openjump.klem.grid.DoubleBasicGrid; @@ -26,28 +46,15 @@ import com.vividsolutions.jts.geom.LineString; import com.vividsolutions.jts.geom.Point; import com.vividsolutions.jump.feature.Feature; +import com.vividsolutions.jump.task.TaskMonitor; import com.vividsolutions.jump.util.StringUtil; +import com.vividsolutions.jump.workbench.Logger; import com.vividsolutions.jump.workbench.model.Layer; +import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn; import com.vividsolutions.jump.workbench.plugin.PlugInContext; +import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn; import com.vividsolutions.jump.workbench.ui.ErrorDialog; -import java.awt.Dialog; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import javax.swing.JButton; -import javax.swing.JComboBox; -import javax.swing.JComponent; -import javax.swing.JLabel; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JRadioButton; -import javax.swing.JTextField; -import org.openjump.core.rasterimage.RasterImageLayer; +import com.vividsolutions.jump.workbench.ui.task.TaskMonitorManager; /** * @@ -55,7 +62,8 @@ */ public class WatershedPlugIn extends AbstractInputKlemPlugin { - public WatershedPlugIn(PlugInContext context, InitialDialog initialDialog, LayerablesList layerablesList) { + public WatershedPlugIn(PlugInContext context, InitialDialog initialDialog, + LayerablesList layerablesList) { super(context, initialDialog); this.context = context; this.initialDialog = initialDialog; @@ -66,38 +74,55 @@ @Override public InitialData setInitialData() { -// layers = PluginUtils.getLayers(context); -// ar_rasterImageLayers = PluginUtils.getRasterImageLayers(context); - String[] attributes = new String[0]; + // layers = PluginUtils.getLayers(context); + // ar_rasterImageLayers = PluginUtils.getRasterImageLayers(context); + final String[] attributes = new String[0]; - InitialData initialData = new InitialData(); + final InitialData initialData = new InitialData(); //input data + initialData + .setParam_Label_TextBox(GUIUtils.setGUILabel(DEM_LABEL), + PluginUtils.getRasterImageLayers(layerablesList + .getLayerables()), GUIUtils.INPUT);//dem combobox with rasterImageLayer initialData.setParam_Label_TextBox( - GUIUtils.setGUILabel(DEM_LABEL), - PluginUtils.getRasterImageLayers(layerablesList.getLayerables()), - GUIUtils.INPUT);//dem combobox with rasterImageLayer - initialData.setParam_Label_TextBox( - GUIUtils.setGUILabel(PluginUtils.getResources().getString("HydrographKlemPlugin.Bluelines.label")), - PluginUtils.getLayers(layerablesList.getLayerables()), GUIUtils.INPUT); - + GUIUtils.setGUILabel(PluginUtils.getResources().getString( + "HydrographKlemPlugin.Bluelines.label")), + PluginUtils.getLayers(layerablesList.getLayerables()), + GUIUtils.INPUT); + //other data - initialData.setParam_Labels(new String[]{PluginUtils.getResources().getString("WatershedPlugin.CloseCoordSection.label")}, GUIUtils.OTHER); - initialData.setParam_Action(new ActionObject( - new String[]{PluginUtils.getResources().getString("WatershedPlugin.MouseRadioButton.label"), - PluginUtils.getResources().getString("WatershedPlugin.RadioButtonLayer.label")}), GUIUtils.OTHER);//radio button da mouse da layer + initialData.setParam_Labels(new String[] { PluginUtils.getResources() + .getString("WatershedPlugin.CloseCoordSection.label") }, + GUIUtils.OTHER); + initialData.setParam_Action( + new ActionObject(new String[] { + PluginUtils.getResources().getString( + "WatershedPlugin.MouseRadioButton.label"), + PluginUtils.getResources().getString( + "WatershedPlugin.RadioButtonLayer.label") }), + GUIUtils.OTHER);//radio button da mouse da layer // coordinata x da mouse - initialData.setParam_Label_TextBox_Button(GUIUtils.setGUILabel(XCOORD_LABEL), "", - new ActionObject(PluginUtils.getResources().getString("WatershedPlugin.ChooseButton.label")), GUIUtils.OTHER); - initialData.setParam_Label_TextBox(GUIUtils.setGUILabel(YCOORD_LABEL), "", GUIUtils.OTHER);// coordinata y da mouse + initialData.setParam_Label_TextBox_Button( + GUIUtils.setGUILabel(XCOORD_LABEL), + "", + new ActionObject(PluginUtils.getResources().getString( + "WatershedPlugin.ChooseButton.label")), GUIUtils.OTHER); + initialData.setParam_Label_TextBox(GUIUtils.setGUILabel(YCOORD_LABEL), + "", GUIUtils.OTHER);// coordinata y da mouse - initialData.setParam_Label_TextBox(GUIUtils.setGUILabel(LAYER_LABEL), PluginUtils.getLayers(layerablesList.getLayerables()), GUIUtils.OTHER);// lista layer vettoriali -// initialData.setParam_Label_TextBox(GUIUtils.setGUILabel("Attribute"), attributes);// lista layer vettoriali + initialData.setParam_Label_TextBox(GUIUtils.setGUILabel(LAYER_LABEL), + PluginUtils.getLayers(layerablesList.getLayerables()), + GUIUtils.OTHER);// lista layer vettoriali + // initialData.setParam_Label_TextBox(GUIUtils.setGUILabel("Attribute"), attributes);// lista layer vettoriali //output data // Check box: - initialData.setParam_Action(new ActionObject(true, CLIP_OUTPUT), GUIUtils.OUTPUT); - initialData.setParam_Label_TextBox_Button(GUIUtils.setGUILabel(WATERSHED_LABEL), "", new ActionObject(""), GUIUtils.OUTPUT); //JTextField + initialData.setParam_Action(new ActionObject(true, CLIP_OUTPUT), + GUIUtils.OUTPUT); + initialData.setParam_Label_TextBox_Button( + GUIUtils.setGUILabel(WATERSHED_LABEL), "", + new ActionObject(""), GUIUtils.OUTPUT); //JTextField return initialData; @@ -104,38 +129,49 @@ } @Override - public ComponentsTreeMap setComponentsActions(final ComponentsTreeMap personalTreeMap) { + public ComponentsTreeMap setComponentsActions( + final ComponentsTreeMap personalTreeMap) { //radio button components - final JRadioButton jRadioButton_mouse = (JRadioButton) personalTreeMap.getComponent("01", GUIUtils.OTHER, 0); //radio button from mouse - final JRadioButton jRadioButton_layer = (JRadioButton) personalTreeMap.getComponent("01", GUIUtils.OTHER, 1); // radio button from layer + final JRadioButton jRadioButton_mouse = (JRadioButton) personalTreeMap + .getComponent("01", GUIUtils.OTHER, 0); //radio button from mouse + final JRadioButton jRadioButton_layer = (JRadioButton) personalTreeMap + .getComponent("01", GUIUtils.OTHER, 1); // radio button from layer //x coord components - final JLabel jLabel_xCoord = (JLabel) personalTreeMap.getComponent("02", GUIUtils.OTHER, 0); // label coord x - final JTextField jTextField_xCoord = (JTextField) personalTreeMap.getComponent("02", GUIUtils.OTHER, 1); //jtext field coord x - final JButton jButton_Choose = (JButton) personalTreeMap.getComponent("02", GUIUtils.OTHER, 2); //button choose + final JLabel jLabel_xCoord = (JLabel) personalTreeMap.getComponent( + "02", GUIUtils.OTHER, 0); // label coord x + final JTextField jTextField_xCoord = (JTextField) personalTreeMap + .getComponent("02", GUIUtils.OTHER, 1); //jtext field coord x + final JButton jButton_Choose = (JButton) personalTreeMap.getComponent( + "02", GUIUtils.OTHER, 2); //button choose //y coord components - final JLabel jLabel_yCoord = (JLabel) personalTreeMap.getComponent("03", GUIUtils.OTHER, 0); // label coord y - final JTextField jTextField_yCoord = (JTextField) personalTreeMap.getComponent("03", GUIUtils.OTHER, 1); //jtext field coord y + final JLabel jLabel_yCoord = (JLabel) personalTreeMap.getComponent( + "03", GUIUtils.OTHER, 0); // label coord y + final JTextField jTextField_yCoord = (JTextField) personalTreeMap + .getComponent("03", GUIUtils.OTHER, 1); //jtext field coord y //layer components - final JLabel jLabel_layer = (JLabel) personalTreeMap.getComponent("04", GUIUtils.OTHER, 0); // label layer + final JLabel jLabel_layer = (JLabel) personalTreeMap.getComponent("04", + GUIUtils.OTHER, 0); // label layer jLabel_layer.setEnabled(false); - final CustomComboBox.LayerComboBox jComboBox_layer = (CustomComboBox.LayerComboBox) (JComboBox) personalTreeMap.getComponent("04", GUIUtils.OTHER, 1); //jtext field layer + final CustomComboBox.LayerComboBox jComboBox_layer = (CustomComboBox.LayerComboBox) (JComboBox) personalTreeMap + .getComponent("04", GUIUtils.OTHER, 1); //jtext field layer jComboBox_layer.setEnabled(false); //attribute components > NOT USED, ONLY GEOMETRY IS USEFUL -// final JLabel jLabel_attribute = (JLabel) personalTreeMap.getComponent("Other5", 0); // label attribute -// jLabel_attribute.setEnabled(false); -// final JComboBox jComboBox_attribute= (JComboBox) personalTreeMap.getComponent("Other5", 1); //jtext field attribute -// jComboBox_attribute.setEnabled(false); + // final JLabel jLabel_attribute = (JLabel) personalTreeMap.getComponent("Other5", 0); // label attribute + // jLabel_attribute.setEnabled(false); + // final JComboBox jComboBox_attribute= (JComboBox) personalTreeMap.getComponent("Other5", 1); //jtext field attribute + // jComboBox_attribute.setEnabled(false); jRadioButton_mouse.addActionListener(new ActionListener() { //enable/disable components @Override public void actionPerformed(ActionEvent e) { - enableElements(jRadioButton_mouse, jRadioButton_layer, jLabel_xCoord, - jTextField_xCoord, jButton_Choose, jLabel_yCoord, jTextField_yCoord, jLabel_layer, + enableElements(jRadioButton_mouse, jRadioButton_layer, + jLabel_xCoord, jTextField_xCoord, jButton_Choose, + jLabel_yCoord, jTextField_yCoord, jLabel_layer, jComboBox_layer); } }); @@ -144,8 +180,9 @@ //enable/disable components @Override public void actionPerformed(ActionEvent e) { - enableElements(jRadioButton_mouse, jRadioButton_layer, jLabel_xCoord, - jTextField_xCoord, jButton_Choose, jLabel_yCoord, jTextField_yCoord, jLabel_layer, + enableElements(jRadioButton_mouse, jRadioButton_layer, + jLabel_xCoord, jTextField_xCoord, jButton_Choose, + jLabel_yCoord, jTextField_yCoord, jLabel_layer, jComboBox_layer); } }); @@ -155,146 +192,369 @@ @Override public void actionPerformed(ActionEvent e) { - - RasterImageLayer inputRasterSelected = PluginUtils.getRasterImageLayerSelected((CustomComboBox.RasterComboBox) personalTreeMap.getComponent("00", GUIUtils.INPUT, 1)); - - WatershedInformation wi = new WatershedInformation(context, jTextField_xCoord, jTextField_yCoord); + + final RasterImageLayer inputRasterSelected = PluginUtils + .getRasterImageLayerSelected((CustomComboBox.RasterComboBox) personalTreeMap + .getComponent("00", GUIUtils.INPUT, 1)); + + final WatershedInformation wi = new WatershedInformation( + context, jTextField_xCoord, jTextField_yCoord); wi.getCoordinate(); - wi.setRasterEnvelope(inputRasterSelected.getWholeImageEnvelope()); + wi.setRasterEnvelope(inputRasterSelected + .getWholeImageEnvelope()); } }); jRadioButton_mouse.setSelected(true); - final JTextField outputTextField = (JTextField) personalTreeMap.getComponent("01", GUIUtils.OUTPUT, 1); - JButton outputButton = (JButton) personalTreeMap.getComponent("01", GUIUtils.OUTPUT, 2); + final JTextField outputTextField = (JTextField) personalTreeMap + .getComponent("01", GUIUtils.OUTPUT, 1); + final JButton outputButton = (JButton) personalTreeMap.getComponent( + "01", GUIUtils.OUTPUT, 2); outputButton.setIcon(PluginUtils.getFolderIcon()); - outputButton.addActionListener(GUIUtils.setSaveRasterTif(outputTextField)); + outputButton.addActionListener(GUIUtils + .setSaveRasterTif(outputTextField)); return personalTreeMap; } + public void watershedCommand(final ComponentsTreeMap componentsWithActions) + throws Exception { + //get input raster names + final String flowDirRaster = GUIUtils + .getStringValue(componentsWithActions.getComponent("00", + GUIUtils.INPUT, 1)); + + //get other information + final boolean selectionMouse = GUIUtils + .componentIsSelected(componentsWithActions.getComponent("01", + GUIUtils.OTHER, 0)); + String xCoord = null; + String yCoord = null; + String layerSelected = null; + + if (selectionMouse) { + xCoord = GUIUtils.getStringValue(componentsWithActions + .getComponent("02", GUIUtils.OTHER, 1));//xCoord value + yCoord = GUIUtils.getStringValue(componentsWithActions + .getComponent("03", GUIUtils.OTHER, 1));//yCoord value + } else { + layerSelected = GUIUtils.getStringValue(componentsWithActions + .getComponent("04", GUIUtils.OTHER, 1)); //layer + } + + //get output raster name + final boolean clipOutput = GUIUtils + .getBooleanValue(componentsWithActions.getComponent("00", + GUIUtils.OUTPUT, 0)); + final String outRasterName = GUIUtils + .getStringValue(componentsWithActions.getComponent("01", + GUIUtils.OUTPUT, 1));//output raster name + + //ckeck + checkValues(mainPanel, flowDirRaster, selectionMouse, xCoord, yCoord, + layerSelected, outRasterName); + + final List coords = new ArrayList<Coordinate>(); + + //convert string values in correct objects + //get input flow dir + final DoubleBasicGrid demGrid = RasterUtils + .getDoubleBasicGrid((RasterComboBox) componentsWithActions + .getComponent("00", GUIUtils.INPUT, 1)); + //Calculate flow dir envelope + final double xMax = demGrid.getLowerLeftCoord().x + + (demGrid.getCellSize() * demGrid.getColumnCount()); + final double yMax = demGrid.getLowerLeftCoord().y + + (demGrid.getCellSize() * demGrid.getRowCount()); + + final Envelope env = new Envelope(demGrid.getLowerLeftCoord().x, xMax, + demGrid.getLowerLeftCoord().y, yMax); + + if (selectionMouse) { + + coords.add(new Coordinate(Double.parseDouble(xCoord), Double + .parseDouble(yCoord))); + + } else { //get Layer + //from name to Layer + final Layer layer = PluginUtils + .getLayerSelected((LayerComboBox) componentsWithActions + .getComponent("04", GUIUtils.OTHER, 1)); + + // Get selected features, or all features if none selected, or return error if none present + Collection features = context.getLayerViewPanel() + .getSelectionManager().getFeatureSelection() + .getFeaturesWithSelectedItems(layer); + + if (features.isEmpty()) { + features = layer.getFeatureCollectionWrapper().getFeatures(); + } + + Feature feature; + final Iterator iter = features.iterator(); + final int count = 0; + while (iter.hasNext()) { + feature = (Feature) iter.next(); + // Check feature to be point + if (feature.getGeometry().getGeometryType().toUpperCase() + .equals("POINT")) { + final Point point = (Point) feature.getGeometry(); + //check if coordinate is inside raster. Only inside coordinate are added to the list + if (env.contains(point.getCoordinate())) { + coords.add(new Coordinate(point.getCoordinate().x, + point.getCoordinate().y)); + } + break; + } + } + } + + if (coords.isEmpty()) { + throw new NullPointerException(PluginUtils.getResources() + .getString("WatershedPlugin.CoordinateOutside.label")); + } + + //convert arrayList to array + final Coordinate[] ar_coords = new Coordinate[coords.size()]; + for (int n = 0; n < coords.size(); n++) { + ar_coords[n] = (Coordinate) coords.get(n); + } + + //execute + final WatershedExtractor watershedExtractor = new WatershedExtractor(); + + final Layer bluelinesLayer = PluginUtils + .getLayerSelected((CustomComboBox.LayerComboBox) componentsWithActions + .getComponent("01", GUIUtils.INPUT, 1)); + LineString[] bluelines = null; + if (bluelinesLayer != null) { + bluelines = GeometryUtils.getLineStringsFromFeatures(bluelinesLayer + .getFeatureCollectionWrapper()); + } + + final FlowDirsCalculator flowDirCalc = new FlowDirsCalculator(demGrid, + FlowDirsStripe.FlowDirAlgorithm.D8, bluelines, 100d); + final FlowDirBasicGrid flowDirGrid = flowDirCalc.calculate(); + + final DoubleBasicGrid watershedGrid = watershedExtractor.extract( + flowDirGrid, ar_coords, clipOutput); + + //Create the output file and display on OJ + //Save grid as tiff + RasterUtils.saveOutputRasterAsTiff(watershedGrid, new File( + outRasterName)); + //Display raster on OJ from file + RasterUtils.displayRasterFileOnOJ(context.getWorkbenchContext(), + new File(outRasterName), null); + + JOptionPane.showMessageDialog(super.getInitialDialog(), PluginUtils + .getResources().getString("SetWorkspacePlugin.Done.message"), + PluginUtils.plugInName, JOptionPane.INFORMATION_MESSAGE); + } + @Override public JPanel buildPluginPanel(final ComponentsTreeMap componentsWithActions) { - if(this.mainPanel != null) { - return this.mainPanel; + if (mainPanel != null) { + return mainPanel; } - this.mainPanel = new MainPanel(super.getInitialDialog(), componentsWithActions, false, false, true, - PluginUtils.getResources().getString("MainPanel.ExecuteButton.text"), layerablesList) { + mainPanel = new MainPanel(super.getInitialDialog(), + componentsWithActions, false, false, true, PluginUtils + .getResources().getString( + "MainPanel.ExecuteButton.text"), layerablesList) { + /** + * + */ + private static final long serialVersionUID = 2414155540209210362L; + @Override public void rightButton() { try { + AbstractPlugIn.toActionListener(new ThreadedBasePlugIn() { + @Override + public String getName() { + return null; + } + + @Override + public boolean execute(PlugInContext context) + throws Exception { + return true; + } + + @Override + public void run(TaskMonitor monitor, + PlugInContext context) throws Exception { + monitor.report(PluginUtils.getResources() + .getString("OpenKlem.executing-process")); + // monitor.allowCancellationRequests(); + reportNothingToUndoYet(context); + try { + watershedCommand(componentsWithActions); + } catch (final Exception ex) { + Logger.error(getName(), ex); + } + } + }, context.getWorkbenchContext(), new TaskMonitorManager()) + .actionPerformed(null); + //get input raster names - String flowDirRaster = GUIUtils.getStringValue(componentsWithActions.getComponent("00", GUIUtils.INPUT, 1)); + /* final String flowDirRaster = GUIUtils + .getStringValue(componentsWithActions.getComponent( + "00", GUIUtils.INPUT, 1)); - //get other information - boolean selectionMouse = GUIUtils.componentIsSelected(componentsWithActions.getComponent("01", GUIUtils.OTHER, 0)); - String xCoord = null; - String yCoord = null; - String layerSelected = null; + //get other information + final boolean selectionMouse = GUIUtils + .componentIsSelected(componentsWithActions + .getComponent("01", GUIUtils.OTHER, 0)); + String xCoord = null; + String yCoord = null; + String layerSelected = null; - if (selectionMouse) { - xCoord = GUIUtils.getStringValue(componentsWithActions.getComponent("02", GUIUtils.OTHER, 1));//xCoord value - yCoord = GUIUtils.getStringValue(componentsWithActions.getComponent("03", GUIUtils.OTHER, 1));//yCoord value - } else { - layerSelected = GUIUtils.getStringValue(componentsWithActions.getComponent("04", GUIUtils.OTHER, 1)); //layer - } + if (selectionMouse) { + xCoord = GUIUtils.getStringValue(componentsWithActions + .getComponent("02", GUIUtils.OTHER, 1));//xCoord value + yCoord = GUIUtils.getStringValue(componentsWithActions + .getComponent("03", GUIUtils.OTHER, 1));//yCoord value + } else { + layerSelected = GUIUtils + .getStringValue(componentsWithActions + .getComponent("04", GUIUtils.OTHER, 1)); //layer + } - //get output raster name - boolean clipOutput = GUIUtils.getBooleanValue(componentsWithActions.getComponent("00", GUIUtils.OUTPUT, 0)); - String outRasterName = GUIUtils.getStringValue(componentsWithActions.getComponent("01", GUIUtils.OUTPUT, 1));//output raster name + //get output raster name + final boolean clipOutput = GUIUtils + .getBooleanValue(componentsWithActions + .getComponent("00", GUIUtils.OUTPUT, 0)); + final String outRasterName = GUIUtils + .getStringValue(componentsWithActions.getComponent( + "01", GUIUtils.OUTPUT, 1));//output raster name - //ckeck - checkValues(this, flowDirRaster, selectionMouse, xCoord, yCoord, layerSelected, outRasterName); + //ckeck + checkValues(this, flowDirRaster, selectionMouse, xCoord, + yCoord, layerSelected, outRasterName); - List coords = new ArrayList<Coordinate>(); + final List coords = new ArrayList<Coordinate>(); - //convert string values in correct objects - //get input flow dir - DoubleBasicGrid demGrid = RasterUtils.getDoubleBasicGrid((RasterComboBox) componentsWithActions.getComponent("00", GUIUtils.INPUT, 1)); - //Calculate flow dir envelope - double xMax = demGrid.getLowerLeftCoord().x + (demGrid.getCellSize() * demGrid.getColumnCount()); - double yMax = demGrid.getLowerLeftCoord().y + (demGrid.getCellSize() * demGrid.getRowCount()); - - Envelope env = new Envelope(demGrid.getLowerLeftCoord().x, xMax, demGrid.getLowerLeftCoord().y, yMax); + //convert string values in correct objects + //get input flow dir + final DoubleBasicGrid demGrid = RasterUtils + .getDoubleBasicGrid((RasterComboBox) componentsWithActions + .getComponent("00", GUIUtils.INPUT, 1)); + //Calculate flow dir envelope + final double xMax = demGrid.getLowerLeftCoord().x + + (demGrid.getCellSize() * demGrid.getColumnCount()); + final double yMax = demGrid.getLowerLeftCoord().y + + (demGrid.getCellSize() * demGrid.getRowCount()); - if (selectionMouse) { + final Envelope env = new Envelope( + demGrid.getLowerLeftCoord().x, xMax, + demGrid.getLowerLeftCoord().y, yMax); - coords.add(new Coordinate(Double.parseDouble(xCoord), Double.parseDouble(yCoord))); + if (selectionMouse) { - } else { //get Layer - //from name to Layer - Layer layer = PluginUtils.getLayerSelected((LayerComboBox)componentsWithActions.getComponent("04", GUIUtils.OTHER, 1)); + coords.add(new Coordinate(Double.parseDouble(xCoord), + Double.parseDouble(yCoord))); - // Get selected features, or all features if none selected, or return error if none present - Collection features = context.getLayerViewPanel().getSelectionManager().getFeatureSelection().getFeaturesWithSelectedItems(layer); + } else { //get Layer + //from name to Layer + final Layer layer = PluginUtils + .getLayerSelected((LayerComboBox) componentsWithActions + .getComponent("04", GUIUtils.OTHER, 1)); - if (features.isEmpty()) { - features = layer.getFeatureCollectionWrapper().getFeatures(); - } + // Get selected features, or all features if none selected, or return error if none present + Collection features = context.getLayerViewPanel() + .getSelectionManager().getFeatureSelection() + .getFeaturesWithSelectedItems(layer); - Feature feature; - Iterator iter = features.iterator(); - int count = 0; - while (iter.hasNext()) { - feature = (Feature) iter.next(); - // Check feature to be point - if (feature.getGeometry().getGeometryType().toUpperCase().equals("POINT")) { - Point point = (Point) feature.getGeometry(); - //check if coordinate is inside raster. Only inside coordinate are added to the list - if(env.contains(point.getCoordinate())){ - coords.add(new Coordinate(point.getCoordinate().x, point.getCoordinate().y)); - } - break; - } - } - } - - if(coords.isEmpty()) throw new NullPointerException(PluginUtils.getResources().getString("WatershedPlugin.CoordinateOutside.label")); + if (features.isEmpty()) { + features = layer.getFeatureCollectionWrapper() + .getFeatures(); + } - //convert arrayList to array - Coordinate[] ar_coords = new Coordinate[coords.size()]; - for (int n = 0; n < coords.size(); n++) { - ar_coords[n] = (Coordinate) coords.get(n); - } + Feature feature; + final Iterator iter = features.iterator(); + final int count = 0; + while (iter.hasNext()) { + feature = (Feature) iter.next(); + // Check feature to be point + if (feature.getGeometry().getGeometryType() + .toUpperCase().equals("POINT")) { + final Point point = (Point) feature + .getGeometry(); + //check if coordinate is inside raster. Only inside coordinate are added to the list + if (env.contains(point.getCoordinate())) { + coords.add(new Coordinate(point + .getCoordinate().x, point + .getCoordinate().y)); + } + break; + } + } + } - //execute - WatershedExtractor watershedExtractor = new WatershedExtractor(); - - Layer bluelinesLayer = PluginUtils.getLayerSelected((CustomComboBox.LayerComboBox) componentsWithActions.getComponent("01", GUIUtils.INPUT, 1)); - LineString[] bluelines = null; - if(bluelinesLayer != null) { - bluelines = GeometryUtils.getLineStringsFromFeatures(bluelinesLayer.getFeatureCollectionWrapper()); - } - - - FlowDirsCalculator flowDirCalc = new FlowDirsCalculator(demGrid, FlowDirsStripe.FlowDirAlgorithm.D8, bluelines, 100d); - FlowDirBasicGrid flowDirGrid = flowDirCalc.calculate(); - - DoubleBasicGrid watershedGrid = watershedExtractor.extract(flowDirGrid, ar_coords, clipOutput); + if (coords.isEmpty()) { + throw new NullPointerException( + PluginUtils + .getResources() + .getString( + "WatershedPlugin.CoordinateOutside.label")); + } - //Create the output file and display on OJ - //Save grid as tiff - RasterUtils.saveOutputRasterAsTiff(watershedGrid, new File(outRasterName)); - //Display raster on OJ from file - RasterUtils.displayRasterFileOnOJ( - context.getWorkbenchContext(), - new File(outRasterName), - null); + //convert arrayList to array + final Coordinate[] ar_coords = new Coordinate[coords.size()]; + for (int n = 0; n < coords.size(); n++) { + ar_coords[n] = (Coordinate) coords.get(n); + } - JOptionPane.showMessageDialog(super.getInitialDialog(), - PluginUtils.getResources().getString("SetWorkspacePlugin.Done.message"), PluginUtils.plugInName, JOptionPane.INFORMATION_MESSAGE); - - } catch (WarningException ex) { - JOptionPane.showMessageDialog(super.getInitialDialog(), ex.getMessage(), PluginUtils.plugInName, JOptionPane.WARNING_MESSAGE); - } catch (Exception ex) { - ErrorDialog.show( - super.getInitialDialog(), - PluginUtils.plugInName, - ex.toString(), + //execute + final WatershedExtractor watershedExtractor = new WatershedExtractor(); + + final Layer bluelinesLayer = PluginUtils + .getLayerSelected((CustomComboBox.LayerComboBox) componentsWithActions + .getComponent("01", GUIUtils.INPUT, 1)); + LineString[] bluelines = null; + if (bluelinesLayer != null) { + bluelines = GeometryUtils + .getLineStringsFromFeatures(bluelinesLayer + .getFeatureCollectionWrapper()); + } + + final FlowDirsCalculator flowDirCalc = new FlowDirsCalculator( + demGrid, FlowDirsStripe.FlowDirAlgorithm.D8, + bluelines, 100d); + final FlowDirBasicGrid flowDirGrid = flowDirCalc + .calculate(); + + final DoubleBasicGrid watershedGrid = watershedExtractor + .extract(flowDirGrid, ar_coords, clipOutput); + + //Create the output file and display on OJ + //Save grid as tiff + RasterUtils.saveOutputRasterAsTiff(watershedGrid, new File( + outRasterName)); + //Display raster on OJ from file + RasterUtils.displayRasterFileOnOJ(context + .getWorkbenchContext(), new File(outRasterName), + null); + + JOptionPane.showMessageDialog( + super.getInitialDialog(), + PluginUtils.getResources().getString( + "SetWorkspacePlugin.Done.message"), + PluginUtils.plugInName, + JOptionPane.INFORMATION_MESSAGE); + + } catch (final WarningException ex) { + JOptionPane.showMessageDialog(super.getInitialDialog(), + ex.getMessage(), PluginUtils.plugInName, + JOptionPane.WARNING_MESSAGE);*/ + } catch (final Exception ex) { + ErrorDialog.show(super.getInitialDialog(), + PluginUtils.plugInName, ex.toString(), StringUtil.stackTrace(ex)); } } @@ -310,17 +570,18 @@ } }; - return this.mainPanel; + return mainPanel; } @Override public String toString() { - return PluginUtils.getResources().getString("WatershedPlugin.PlugInName.label"); + return PluginUtils.getResources().getString( + "WatershedPlugin.PlugInName.label"); } - private void enableElements(JRadioButton mouse, JRadioButton layer, JLabel xcoordL, - JTextField xcoordF, JButton choose, JLabel ycoordL, JTextField ycoordF, - JLabel layerL, JComboBox layerF) { + private void enableElements(JRadioButton mouse, JRadioButton layer, + JLabel xcoordL, JTextField xcoordF, JButton choose, JLabel ycoordL, + JTextField ycoordF, JLabel layerL, JComboBox layerF) { xcoordL.setEnabled(mouse.isSelected()); xcoordF.setEnabled(mouse.isSelected()); @@ -333,8 +594,9 @@ layerF.setEnabled(layer.isSelected()); } - private void checkValues(JComponent component, String dem, boolean mouseSel, String xcoord, String ycoord, - String layer, String outRaster) throws IOException, Exception { + private void checkValues(JComponent component, String dem, + boolean mouseSel, String xcoord, String ycoord, String layer, + String outRaster) throws IOException, Exception { GUIUtils.checkStringValue(dem, DEM_LABEL); if (mouseSel) { @@ -359,16 +621,20 @@ private MainPanel mainPanel; private final PlugInContext context; -// private RasterImageLayer[] ar_rasterImageLayers; + // private RasterImageLayer[] ar_rasterImageLayers; private final InitialDialog initialDialog; -// private Layer[] layers; - private final String DEM_LABEL = PluginUtils.getResources().getString("KlemGUI.InputFilledDem.label"); -// private final String OUT_RASTER_LABEL = PluginUtils.getResources().getString("KlemGUI.OutputRaster.label"); + // private Layer[] layers; + private final String DEM_LABEL = PluginUtils.getResources().getString( + "KlemGUI.InputFilledDem.label"); + // private final String OUT_RASTER_LABEL = PluginUtils.getResources().getString("KlemGUI.OutputRaster.label"); private final String XCOORD_LABEL = "x"; private final String YCOORD_LABEL = "y"; - private final String LAYER_LABEL = PluginUtils.getResources().getString("KlemGUI.Layer.label"); - private final String CLIP_OUTPUT = PluginUtils.getResources().getString("WatershedPlugin.ClipOutput"); - private final String WATERSHED_LABEL = PluginUtils.getResources().getString("WatershedPlugin.PlugInName.label"); - + private final String LAYER_LABEL = PluginUtils.getResources().getString( + "KlemGUI.Layer.label"); + private final String CLIP_OUTPUT = PluginUtils.getResources().getString( + "WatershedPlugin.ClipOutput"); + private final String WATERSHED_LABEL = PluginUtils.getResources() + .getString("WatershedPlugin.PlugInName.label"); + private final LayerablesList layerablesList; } _______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel