Revision: 6165 http://sourceforge.net/p/jump-pilot/code/6165 Author: ma15569 Date: 2019-04-05 15:58:02 +0000 (Fri, 05 Apr 2019) Log Message: ----------- Added some raster tools to complete Raster to Vector conversion (Raster to Polygon, Raster to Contours) partially done by Stefan
Added Paths: ----------- core/trunk/src/org/openjump/core/ui/plugin/raster/RasterMenuNames.java core/trunk/src/org/openjump/core/ui/plugin/raster/Utils.java core/trunk/src/org/openjump/core/ui/plugin/raster/VectorizeToContoursPlugIn.java core/trunk/src/org/openjump/core/ui/plugin/raster/VectorizeToPolygonsPlugIn.java Added: core/trunk/src/org/openjump/core/ui/plugin/raster/RasterMenuNames.java =================================================================== --- core/trunk/src/org/openjump/core/ui/plugin/raster/RasterMenuNames.java (rev 0) +++ core/trunk/src/org/openjump/core/ui/plugin/raster/RasterMenuNames.java 2019-04-05 15:58:02 UTC (rev 6165) @@ -0,0 +1,97 @@ +package org.openjump.core.ui.plugin.raster; + +import com.vividsolutions.jump.I18N; + +public class RasterMenuNames { + + /** + * Many terminologies are used by several plugin. This calss allows to reorganize them + * TODO create new lang strings + */ + + /** + * Common to all the raster plugins + */ + public static String Source_Layer = I18N + .get("ui.GenericNames.Source-Layer"); + public static String Output_file = I18N + .get("driver.DriverManager.file-to-save"); + + public static String Choose_an_action = I18N + .get("org.openjump.core.rasterimage.plugin.Choose-an-action"); + public static String Check_field = I18N + .get("org.openjump.core.rasterimage.plugin.Check_value_into_the_field"); + public static String PROCESSING = I18N + .get("jump.plugin.edit.NoderPlugIn.processing"); + + /** + * ManageDataPlugIn + */ + + public static String DATA_NAME = "Manage raster data"; + public static String CHANGE_NODATA_TIP = "Change both nodata values and reference number (nodata tag)"; + public static String CHANGE_INTERVAL_TO_NODATA_TIP = "Mask the raster using nodata value. This tool sets a the defined range of values of the raster to nodata value. The values outside the range will be preserved"; + public static String EXTRACT_INTERVAL_TIP = "Extract a range of data. This tool extracts a defined range of values of the raster. The values outside the range will be set to nodata value"; + public static String EXTRACT_INTERVAL = "Extract a range of values"; + public static String RESET_NODATA_TAG_TIP = "Stretch raster changing only nodata reference value to min or to max value. This tool tries to repair a raster which has partial area coverd by nodata cells and where the tag has been accidentally deleted"; + public static String RESET_NODATA_TAG = "Stretch raster"; + public static String SET_DECIMAL = "Set data decimals"; + public static String SET_DECIMAL_TIP = "This tool allows to reduce/optimize the number of decimal of the data. Set to 0 to have data with integer values"; + public static String RESET_TO_MIN = "Stretch raster to min value"; + public static String RESET_TO_MAX = "Stretch raster to max value"; + public static String SINGLE_BAND_EXIST = "At least one single banded layer should exist"; + + /** + * HistogramPlugIn + */ + + public static String H_CONST_RANGE = "Divide the range with a constant interval"; + public static String H_ANALISYS_INTERVAL = "Define the range of data"; + public static String H_HIST_AS_LINE = "Show histogram as line"; + public static String H_UNIQUE_VALUES = "Continuous histogram /unique values"; + public static String H_AUTO = "Automatic"; + + /** + * CropWarpPlugIn + */ + public static String C_NAME = "Crop/Warp raster"; + public static String C_TARGET_OBJECT = "Target Object:"; + public static String C_CROP_RASTER = "Crop raster to an envelope"; + public static String C_CROP_RASTER_TIP = "Crop a raster layer using as mask the envelope extension of selected geometries, of mask layer (vector, raster or wms) or of the current view"; + public static String C_WARP_RASTER = "Warp raster to an envelope"; + public static String C_WARP_RASTER_TIP = "Warp a raster layer to the envelope of selected geometries, of mask layer (vector, raster or wms) or of the current view"; + public static String C_CUT_LAYER = "Cutting layer: "; + public static String C_WARP_LAYER = "Warping layer: "; + public static String C_NO_INTERSECTION = "Target object doesn't intersect with the raster layer or it is empty (no geometry selected)"; + + /** + * KernelAnalysisPlugIn + */ + + public static String KERNEL_NAME = "Kernel Analysis"; + public static String KERNEL_DEFAULT = "Default kernels"; + + /** + * Vectorize + * + * + */ + public static String errorReadingRaster = "Error on reading raster"; + public static String errorWhileCalculating = "Error while calculating"; + public static String unablePerformance = "Unable to perform the calculation: parameters"; + public static String VectorizeToPolygon = "Create polygons"; + public static String VectorizeToLinestrings = "Create linestrings"; + public static String VectorizeToContours = "Create contours"; + public static String ExplodeMultipolygons = "Explode multipolygons"; + public static String ApplyStyle = "Apply a random color style to output"; + public static String contour_baseContour = "Base contour"; + public static String contour_distanceContours = "Distance contours"; + public static String contour_zeroElevation = "Zero elevation"; + public static String contour_minContour = "min contour"; + public static String contour_maxcontour = "max contour"; + public static String contour_contourNumber = "contour number"; + public static String contour_properties = "contours properties"; + public static String contour_range = "Renge elevation"; + public static String Value = I18N + .get("org.openjump.core.ui.plugin.raster.RasterQueryPlugIn.value"); +} Property changes on: core/trunk/src/org/openjump/core/ui/plugin/raster/RasterMenuNames.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: core/trunk/src/org/openjump/core/ui/plugin/raster/Utils.java =================================================================== --- core/trunk/src/org/openjump/core/ui/plugin/raster/Utils.java (rev 0) +++ core/trunk/src/org/openjump/core/ui/plugin/raster/Utils.java 2019-04-05 15:58:02 UTC (rev 6165) @@ -0,0 +1,142 @@ +package org.openjump.core.ui.plugin.raster; + +import java.awt.Color; +import java.awt.geom.NoninvertibleTransformException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeMap; + +import org.openjump.core.rasterimage.RasterImageLayer; + +import com.vividsolutions.jump.feature.Feature; +import com.vividsolutions.jump.feature.FeatureCollectionWrapper; +import com.vividsolutions.jump.geom.EnvelopeUtil; +import com.vividsolutions.jump.util.ColorUtil; +import com.vividsolutions.jump.workbench.JUMPWorkbench; +import com.vividsolutions.jump.workbench.model.Layer; +import com.vividsolutions.jump.workbench.ui.WorkbenchFrame; +import com.vividsolutions.jump.workbench.ui.renderer.style.BasicStyle; +import com.vividsolutions.jump.workbench.ui.renderer.style.ColorScheme; +import com.vividsolutions.jump.workbench.ui.renderer.style.ColorThemingStyle; + +public class Utils { + + public static WorkbenchFrame frame = JUMPWorkbench.getInstance().getFrame(); + + /** + * Zoom to the raster layer + * @param rLayer + * @throws NoninvertibleTransformException + */ + public static void zoom(RasterImageLayer rLayer) + throws NoninvertibleTransformException { + frame.getContext() + .getLayerViewPanel() + .getViewport() + .zoom(EnvelopeUtil.bufferByFraction( + rLayer.getWholeImageEnvelope(), 0.03)); + } + + /** + * Apply a random generated color set style according to an attribute + * @param layer to apply the style + * @param attribute to use for the color classification + * @throws Exception + */ + + public static void applyRandomGradualStyle(Layer layer, String attribute) + throws Exception { + final FeatureCollectionWrapper featDataset = layer + .getFeatureCollectionWrapper(); + final ColorScheme colorScheme = ColorUtil + .createRandomColorSchema(featDataset.size()); + final Map<Object, BasicStyle> attributeToStyleMap = new TreeMap<Object, BasicStyle>(); + for (final Iterator<Feature> i = featDataset.iterator(); i.hasNext();) { + final Feature feature = i.next(); + attributeToStyleMap.put(feature.getAttribute(attribute), + new BasicStyle(colorScheme.next())); + } + layer.getBasicStyle().setEnabled(false); + final ColorThemingStyle themeStyle = new ColorThemingStyle(attribute, + attributeToStyleMap, new BasicStyle(Color.gray)); + themeStyle.setEnabled(true); + layer.addStyle(themeStyle); + ColorThemingStyle.get(layer).setEnabled(true); + layer.removeStyle(ColorThemingStyle.get(layer)); + ColorThemingStyle.get(layer).setEnabled(true); + layer.getBasicStyle().setEnabled(false); + layer.fireAppearanceChanged(); + } + + /** + * Apply a random generated color set style according to an attribute, defining a start color (lower value) and an end color (upper value) + * @param layer + * @param attribute + * @param startColor + * @param endColor + * @throws Exception + */ + public static void applyGradualStyle(Layer layer, String attribute, + Color startColor, Color endColor) throws Exception { + final FeatureCollectionWrapper featDataset = layer + .getFeatureCollectionWrapper(); + final ColorScheme colorScheme = ColorUtil.createColorSchema( + featDataset.size(), startColor, endColor); + + final Map<Object, BasicStyle> attributeToStyleMap = new TreeMap<Object, BasicStyle>(); + for (final Iterator<Feature> i = featDataset.iterator(); i.hasNext();) { + final Feature feature = i.next(); + attributeToStyleMap.put(feature.getAttribute(attribute), + new BasicStyle(colorScheme.next())); + } + layer.getBasicStyle().setEnabled(false); + final ColorThemingStyle themeStyle = new ColorThemingStyle(attribute, + attributeToStyleMap, new BasicStyle(Color.gray)); + themeStyle.setEnabled(true); + layer.addStyle(themeStyle); + ColorThemingStyle.get(layer).setEnabled(true); + layer.removeStyle(ColorThemingStyle.get(layer)); + ColorThemingStyle.get(layer).setEnabled(true); + layer.getBasicStyle().setEnabled(false); + layer.fireAppearanceChanged(); + } + + public static void applyAlternateStyle(Layer layer, String attribute, + int interval, int subInterval, Color color1, Color color2) + throws Exception { + final FeatureCollectionWrapper featDataset = layer + .getFeatureCollectionWrapper(); + + final ArrayList<Color> arrayColor = new ArrayList<Color>(); + + for (int c = 1; c < interval; c++) { + + if (c % subInterval == 0) { + arrayColor.add(color1); + } else { + arrayColor.add(color2); + } + } + + final ColorScheme colorScheme = new ColorScheme("test", arrayColor); + final Map<Object, BasicStyle> attributeToStyleMap = new TreeMap<Object, BasicStyle>(); + for (final Iterator<Feature> i = featDataset.iterator(); i.hasNext();) { + final Feature feature = i.next(); + attributeToStyleMap.put(feature.getAttribute(attribute), + new BasicStyle(colorScheme.next())); + } + layer.getBasicStyle().setEnabled(false); + final ColorThemingStyle themeStyle = new ColorThemingStyle(attribute, + attributeToStyleMap, new BasicStyle(Color.gray)); + themeStyle.setEnabled(true); + layer.addStyle(themeStyle); + ColorThemingStyle.get(layer).setEnabled(true); + layer.removeStyle(ColorThemingStyle.get(layer)); + ColorThemingStyle.get(layer).setEnabled(true); + layer.getBasicStyle().setEnabled(false); + layer.fireAppearanceChanged(); + + } + +} Property changes on: core/trunk/src/org/openjump/core/ui/plugin/raster/Utils.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: core/trunk/src/org/openjump/core/ui/plugin/raster/VectorizeToContoursPlugIn.java =================================================================== --- core/trunk/src/org/openjump/core/ui/plugin/raster/VectorizeToContoursPlugIn.java (rev 0) +++ core/trunk/src/org/openjump/core/ui/plugin/raster/VectorizeToContoursPlugIn.java 2019-04-05 15:58:02 UTC (rev 6165) @@ -0,0 +1,389 @@ +package org.openjump.core.ui.plugin.raster; + +import static com.vividsolutions.jump.I18N.get; + +import java.awt.Point; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.geom.NoninvertibleTransformException; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JTextField; + +import org.openjump.core.apitools.LayerTools; +import org.openjump.core.rasterimage.ImageAndMetadata; +import org.openjump.core.rasterimage.RasterImageIO; +import org.openjump.core.rasterimage.RasterImageLayer; +import org.openjump.core.rasterimage.Resolution; +import org.openjump.core.rasterimage.TiffTags.TiffReadingException; +import org.openjump.core.rasterimage.algorithms.VectorizeAlgorithm; +import org.openjump.core.rasterimage.sextante.OpenJUMPSextanteRasterLayer; +import org.openjump.core.rasterimage.sextante.rasterWrappers.GridWrapperNotInterpolated; +import org.openjump.core.ui.util.LayerableUtil; + +import com.vividsolutions.jts.geom.Envelope; +import com.vividsolutions.jump.I18N; +import com.vividsolutions.jump.feature.AttributeType; +import com.vividsolutions.jump.feature.FeatureCollection; +import com.vividsolutions.jump.feature.FeatureDataset; +import com.vividsolutions.jump.feature.FeatureSchema; +import com.vividsolutions.jump.task.TaskMonitor; +import com.vividsolutions.jump.workbench.WorkbenchContext; +import com.vividsolutions.jump.workbench.model.Category; +import com.vividsolutions.jump.workbench.model.Layer; +import com.vividsolutions.jump.workbench.model.StandardCategoryNames; +import com.vividsolutions.jump.workbench.plugin.EnableCheck; +import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory; +import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck; +import com.vividsolutions.jump.workbench.plugin.PlugInContext; +import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn; +import com.vividsolutions.jump.workbench.ui.GUIUtil; +import com.vividsolutions.jump.workbench.ui.MenuNames; +import com.vividsolutions.jump.workbench.ui.MultiInputDialog; +import com.vividsolutions.jump.workbench.ui.Viewport; +import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller; + +public class VectorizeToContoursPlugIn extends ThreadedBasePlugIn { + + private final String PROCESSING = I18N + .get("jump.plugin.edit.NoderPlugIn.processing"); + + String sLayer = RasterMenuNames.Source_Layer; + String sExplode = RasterMenuNames.ExplodeMultipolygons; + String NAME = RasterMenuNames.VectorizeToContours; + String sStyle = RasterMenuNames.ApplyStyle; + String sValue = RasterMenuNames.Value; + public static String contour_baseContour = RasterMenuNames.contour_baseContour; + public static String contour_distanceContours = RasterMenuNames.contour_distanceContours; + public static String contour_zeroElevation = RasterMenuNames.contour_zeroElevation; + public static String contour_minContour = RasterMenuNames.contour_minContour; + public static String contour_maxcontour = RasterMenuNames.contour_maxcontour; + public static String contour_contourNumber = RasterMenuNames.contour_contourNumber; + + private final String MIN = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.min"); + private final String MAX = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.max"); + + JTextField jTextField_ContBase, jTextField_ContIntv, jTextField_ContMin, + jTextField_ContMax, jTextField_ContCount, max, min; + + private double min_value, max_value; + + private int contCount = -1; + private double contBase = 0; + private int contIntv = 100; + private double contMin = -1; + private double contMax = -1; + boolean applystyleb = false; + + List<RasterImageLayer> fLayers = new ArrayList<RasterImageLayer>(); + JComboBox<RasterImageLayer> layerableComboBox = new JComboBox<RasterImageLayer>(); + + RasterImageLayer layer; + + @Override + public String getName() { + return NAME; + } + + @Override + public boolean execute(PlugInContext context) throws Exception { + reportNothingToUndoYet(context); + final MultiInputDialog dialog = new MultiInputDialog( + context.getWorkbenchFrame(), NAME, true); + setDialogValues(dialog, context); + if (fLayers.isEmpty()) { + return false; + } + GUIUtil.centreOnWindow(dialog); + dialog.setVisible(true); + if (!dialog.wasOKPressed()) { + return false; + } else if (dialog.wasOKPressed()) { + getDialogValues(dialog); + return true; + } + return false; + } + + private void setDialogValues(final MultiInputDialog dialog, + PlugInContext context) throws IOException { + dialog.setSideBarDescription(NAME); + if (!context.getLayerNamePanel().selectedNodes(RasterImageLayer.class) + .isEmpty()) { + layer = (RasterImageLayer) LayerTools.getSelectedLayerable(context, + RasterImageLayer.class); + } else { + layer = context.getTask().getLayerManager() + .getLayerables(RasterImageLayer.class).get(0); + } + + findParameters(layer); + fLayers = context.getTask().getLayerManager() + .getLayerables(RasterImageLayer.class); + layerableComboBox = dialog.addLayerableComboBox(sLayer, layer, "", + fLayers); + layerableComboBox.setSize(200, + layerableComboBox.getPreferredSize().height); + layerableComboBox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + layer = (RasterImageLayer) dialog.getLayerable(sLayer); + findParameters(layer); + + dialog.pack(); + dialog.repaint(); + } + }); + + min = dialog.addDoubleField(MIN, min_value, 20); + min.setEditable(false); + max = dialog.addDoubleField(MAX, max_value, 20); + max.setEditable(false); + + jTextField_ContBase = dialog.addDoubleField(contour_baseContour, + contBase, 20); + jTextField_ContBase.addKeyListener(new KeyAdapter() { + @Override + public void keyReleased(KeyEvent evt) { + contIntv = dialog.getInteger(contour_distanceContours); + contBase = dialog.getDouble(contour_baseContour); + findParameters(layer); + + dialog.pack(); + dialog.repaint(); + } + }); + jTextField_ContIntv = dialog.addIntegerField(contour_distanceContours, + contIntv, 20, ""); + jTextField_ContIntv.addKeyListener(new KeyAdapter() { + @Override + public void keyReleased(KeyEvent evt) { + contIntv = dialog.getInteger(contour_distanceContours); + contBase = dialog.getDouble(contour_baseContour); + findParameters(layer); + + dialog.pack(); + dialog.repaint(); + } + }); + + jTextField_ContMin = dialog.addDoubleField(contour_minContour, contMin, + 20); + jTextField_ContMin.setEditable(false); + jTextField_ContMax = dialog.addDoubleField(contour_maxcontour, contMax, + 20); + jTextField_ContMax.setEditable(false); + jTextField_ContCount = dialog.addIntegerField(contour_contourNumber, + contCount, 20, ""); + jTextField_ContCount.setEditable(false); + dialog.addCheckBox(sStyle, true); + + } + + /* + * The following code derives from AdbToolbox RasterToContour and has been modified to work with OpenJUMP + */ + private void findParameters(RasterImageLayer layer) { + + min_value = layer.getMetadata().getStats().getMin(0); + max_value = layer.getMetadata().getStats().getMax(0); + + try { + + // Bad implementation: try and find + + if (contBase < min_value) { + contMin = contBase; + while (contMin < min_value) { + contMin = contMin + contIntv; + } + contMax = contMin; + while (contMax < max_value) { + contMax = contMax + contIntv; + } + contMax = contMax - contIntv; + } else if (contBase == min_value) { + contMin = min_value; + contMax = min_value; + while (contMax < max_value) { + contMax = contMax + contIntv; + } + contMax = contMax - contIntv; + } else if (contBase > min_value && contBase < max_value) { + contMin = contBase; + while (contMin > min_value) { + contMin = contMin - contIntv; + } + contMin = contMin + contIntv; + contMax = contBase; + while (contMax < max_value) { + contMax = contMax + contIntv; + } + contMax = contMax - contIntv; + } else if (contBase == max_value) { + contMin = contMax; + while (contMin > min_value) { + contMin = contMin - contIntv; + } + contMin = contMin + contIntv; + contMax = max_value; + } else if (contBase > max_value) { + contMax = contBase; + while (contMax > max_value) { + contMax = contMax - contIntv; + } + contMin = contMax; + while (contMin > min_value) { + contMin = contMin - contIntv; + } + contMin = contMin + contIntv; + } + + contCount = (int) Math.ceil((contMax - contMin) / contIntv) + 1; + + min.setText(min_value + ""); + max.setText(max_value + ""); + jTextField_ContMin.setText(contMin + ""); + jTextField_ContMax.setText(contMax + ""); + jTextField_ContIntv.setText(contCount + ""); + + } catch (final Exception ex) { + + return; + + } + } + + private void getDialogValues(MultiInputDialog dialog) { + + layer = (RasterImageLayer) dialog.getLayerable(sLayer); + contIntv = dialog.getInteger(contour_distanceContours); + + applystyleb = dialog.getBoolean(sStyle); + + } + + @Override + public void run(TaskMonitor monitor, PlugInContext context) + throws Exception { + monitor.report(PROCESSING); + reportNothingToUndoYet(context); + // Utils.zoom(layer); + final OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); + rstLayer.create(layer, true); + final GridWrapperNotInterpolated gwrapper = new GridWrapperNotInterpolated( + rstLayer, rstLayer.getLayerGridExtent()); + final FeatureSchema fs = new FeatureSchema(); + fs.addAttribute("geometry", AttributeType.GEOMETRY); + fs.addAttribute(sValue, AttributeType.DOUBLE); + FeatureCollection featDataset = new FeatureDataset(fs); + + featDataset = VectorizeAlgorithm.toContours(gwrapper, contMin, contMax, + contIntv, sValue, 0); + + final Layer vlayer = context.addLayer(StandardCategoryNames.WORKING, + rstLayer.getName() + "_" + "vectorized", featDataset); + + if (applystyleb) { + Utils.applyRandomGradualStyle(vlayer, sValue); + + } + + } + + /** + * Load a file into the workbench + * @param inputFile + * eg. "new File(C:/folder/fileName.tif)" + * @param category + * eg. "Working" + * @throws NoninvertibleTransformException + * @throws TiffReadingException + * @throws Exception + */ + public void load(File inputFile, String category, PlugInContext context) + throws NoninvertibleTransformException, TiffReadingException, + Exception { + + final RasterImageIO rasterImageIO = new RasterImageIO(); + final Point point = RasterImageIO.getImageDimensions(inputFile + .getAbsolutePath()); + final Envelope env = RasterImageIO.getGeoReferencing( + inputFile.getAbsolutePath(), true, point); + + final Viewport viewport = context.getLayerViewPanel().getViewport(); + final Resolution requestedRes = RasterImageIO + .calcRequestedResolution(viewport); + final ImageAndMetadata imageAndMetadata = rasterImageIO.loadImage( + context.getWorkbenchContext(), inputFile.getAbsolutePath(), + null, viewport.getEnvelopeInModelCoordinates(), requestedRes); + final RasterImageLayer ril = new RasterImageLayer(inputFile.getName(), + context.getLayerManager(), inputFile.getAbsolutePath(), + imageAndMetadata.getImage(), env); + try { + category = ((Category) context.getLayerableNamePanel() + .getSelectedCategories().toArray()[0]).getName(); + } catch (final RuntimeException e) { + + } + context.getLayerManager().addLayerable(category, ril); + } + + public static MultiEnableCheck createEnableCheck( + WorkbenchContext workbenchContext) { + final EnableCheckFactory checkFactory = new EnableCheckFactory( + workbenchContext); + return new MultiEnableCheck() + .add(checkFactory + .createWindowWithAssociatedTaskFrameMustBeActiveCheck()) + .add(checkFactory.createAtLeastNLayerablesOfTypeMustExistCheck( + 1, RasterImageLayer.class)).add(new EnableCheck() { + @Override + public String check(JComponent component) { + final List<RasterImageLayer> mLayer = new ArrayList<RasterImageLayer>(); + final Collection<RasterImageLayer> rlayers = workbenchContext + .getLayerManager().getLayerables( + RasterImageLayer.class); + for (final RasterImageLayer currentLayer : rlayers) { + if (LayerableUtil.isMonoband(currentLayer)) { + mLayer.add(currentLayer); + } + } + if (!mLayer.isEmpty()) { + return null; + } + String msg = null; + if (mLayer.isEmpty()) { + msg = get(CHECK_FILE); + } + return msg; + } + }); + } + + private final static String CHECK_FILE = RasterMenuNames.SINGLE_BAND_EXIST; + + @Override + public void initialize(PlugInContext context) throws Exception { + + FeatureInstaller.getInstance() + .addMainMenuPlugin( + this, //exe + new String[] { MenuNames.RASTER, + MenuNames.RASTER_VECTORIALIZE }, //menu path + NAME, false, //checkbox + null, //icon + createEnableCheck(context.getWorkbenchContext())); + } + +} Property changes on: core/trunk/src/org/openjump/core/ui/plugin/raster/VectorizeToContoursPlugIn.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: core/trunk/src/org/openjump/core/ui/plugin/raster/VectorizeToPolygonsPlugIn.java =================================================================== --- core/trunk/src/org/openjump/core/ui/plugin/raster/VectorizeToPolygonsPlugIn.java (rev 0) +++ core/trunk/src/org/openjump/core/ui/plugin/raster/VectorizeToPolygonsPlugIn.java 2019-04-05 15:58:02 UTC (rev 6165) @@ -0,0 +1,234 @@ +package org.openjump.core.ui.plugin.raster; + +import static com.vividsolutions.jump.I18N.get; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JComponent; + +import org.openjump.core.apitools.LayerTools; +import org.openjump.core.rasterimage.RasterImageLayer; +import org.openjump.core.rasterimage.algorithms.VectorizeAlgorithm; +import org.openjump.core.rasterimage.sextante.OpenJUMPSextanteRasterLayer; +import org.openjump.core.rasterimage.sextante.rasterWrappers.GridWrapperNotInterpolated; +import org.openjump.core.ui.util.LayerableUtil; + +import com.vividsolutions.jump.I18N; +import com.vividsolutions.jump.feature.AttributeType; +import com.vividsolutions.jump.feature.FeatureCollection; +import com.vividsolutions.jump.feature.FeatureDataset; +import com.vividsolutions.jump.feature.FeatureSchema; +import com.vividsolutions.jump.task.TaskMonitor; +import com.vividsolutions.jump.workbench.WorkbenchContext; +import com.vividsolutions.jump.workbench.model.Layer; +import com.vividsolutions.jump.workbench.model.StandardCategoryNames; +import com.vividsolutions.jump.workbench.plugin.EnableCheck; +import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory; +import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck; +import com.vividsolutions.jump.workbench.plugin.PlugInContext; +import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn; +import com.vividsolutions.jump.workbench.ui.GUIUtil; +import com.vividsolutions.jump.workbench.ui.MenuNames; +import com.vividsolutions.jump.workbench.ui.MultiInputDialog; +import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller; + +public class VectorizeToPolygonsPlugIn extends ThreadedBasePlugIn { + + private final String PROCESSING = I18N + .get("jump.plugin.edit.NoderPlugIn.processing"); + + String sLayer = RasterMenuNames.Source_Layer; + String sExplode = RasterMenuNames.ExplodeMultipolygons; + String NAME = RasterMenuNames.VectorizeToPolygon; + String sStyle = RasterMenuNames.ApplyStyle; + String sValue = RasterMenuNames.Value; + String alg1 = "AdbToolbox"; + String alg2 = "Sextante"; + String algorithms = "Algorithms"; + String choose; + boolean explodeb = true; + boolean applystyleb = false; + + private JCheckBox explode = new JCheckBox(); + private JComboBox<String> comboBox = new JComboBox<String>(); + private List<RasterImageLayer> fLayers = new ArrayList<RasterImageLayer>(); + private JComboBox<RasterImageLayer> layerableComboBox = new JComboBox<RasterImageLayer>(); + + RasterImageLayer layer; + + @Override + public String getName() { + return NAME; + } + + @Override + public boolean execute(PlugInContext context) throws Exception { + reportNothingToUndoYet(context); + final MultiInputDialog dialog = new MultiInputDialog( + context.getWorkbenchFrame(), NAME, true); + setDialogValues(dialog, context); + if (fLayers.isEmpty()) { + return false; + } + GUIUtil.centreOnWindow(dialog); + dialog.setVisible(true); + if (!dialog.wasOKPressed()) { + return false; + } else if (dialog.wasOKPressed()) { + getDialogValues(dialog); + return true; + } + return false; + } + + private void setDialogValues(final MultiInputDialog dialog, + PlugInContext context) throws IOException { + dialog.setSideBarDescription(NAME); + if (!context.getLayerNamePanel().selectedNodes(RasterImageLayer.class) + .isEmpty()) { + layer = (RasterImageLayer) LayerTools.getSelectedLayerable(context, + RasterImageLayer.class); + } else { + layer = context.getTask().getLayerManager() + .getLayerables(RasterImageLayer.class).get(0); + } + fLayers = context.getTask().getLayerManager() + .getLayerables(RasterImageLayer.class); + layerableComboBox = dialog.addLayerableComboBox(sLayer, layer, "", + fLayers); + layerableComboBox.setSize(200, + layerableComboBox.getPreferredSize().height); + final ArrayList<String> srsArray = new ArrayList<String>(); + srsArray.add(alg1); + srsArray.add(alg2); + comboBox = dialog.addComboBox(algorithms, "", srsArray, null); + comboBox.setSize(200, comboBox.getPreferredSize().height); + comboBox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + updateGUI(e, dialog); + + dialog.pack(); + dialog.repaint(); + } + }); + explode = dialog.addCheckBox(sExplode, true); + dialog.addCheckBox(sStyle, true); + + } + + private void updateGUI(ActionEvent evt, MultiInputDialog dialog) { + switch (comboBox.getSelectedIndex()) { + case 0: + explode.setEnabled(true); + break; + case 1: + explode.setEnabled(false); + break; + + } + } + + private void getDialogValues(MultiInputDialog dialog) { + layer = (RasterImageLayer) dialog.getLayerable(sLayer); + explodeb = dialog.getBoolean(sExplode); + applystyleb = dialog.getBoolean(sStyle); + choose = dialog.getText(algorithms); + + } + + @Override + public void run(TaskMonitor monitor, PlugInContext context) + throws Exception { + monitor.report(PROCESSING); + reportNothingToUndoYet(context); + Utils.zoom(layer); + final OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); + rstLayer.create(layer, true); + final GridWrapperNotInterpolated gwrapper = new GridWrapperNotInterpolated( + rstLayer, rstLayer.getLayerGridExtent()); + final FeatureSchema fs = new FeatureSchema(); + fs.addAttribute("geometry", AttributeType.GEOMETRY); + fs.addAttribute(sValue, AttributeType.INTEGER); + FeatureCollection featDataset = new FeatureDataset(fs); + + switch (comboBox.getSelectedIndex()) { + case 0: + if (explodeb = true) { + featDataset = VectorizeAlgorithm.toPolygonsAdbToolBox(gwrapper, + true, sValue, 0); + } else { + featDataset = VectorizeAlgorithm.toPolygonsAdbToolBox(gwrapper, + false, sValue, 0); + } + break; + case 1: + featDataset = VectorizeAlgorithm.toPolygonsSextante(gwrapper, + sValue, 0); + break; + } + final Layer vlayer = context.addLayer(StandardCategoryNames.WORKING, + rstLayer.getName() + "_" + "vectorized", featDataset); + + if (applystyleb) { + Utils.applyRandomGradualStyle(vlayer, sValue); + + } + + } + + public static MultiEnableCheck createEnableCheck( + WorkbenchContext workbenchContext) { + final EnableCheckFactory checkFactory = new EnableCheckFactory( + workbenchContext); + return new MultiEnableCheck() + .add(checkFactory + .createWindowWithAssociatedTaskFrameMustBeActiveCheck()) + .add(checkFactory.createAtLeastNLayerablesOfTypeMustExistCheck( + 1, RasterImageLayer.class)).add(new EnableCheck() { + @Override + public String check(JComponent component) { + final List<RasterImageLayer> mLayer = new ArrayList<RasterImageLayer>(); + final Collection<RasterImageLayer> rlayers = workbenchContext + .getLayerManager().getLayerables( + RasterImageLayer.class); + for (final RasterImageLayer currentLayer : rlayers) { + if (LayerableUtil.isMonoband(currentLayer)) { + mLayer.add(currentLayer); + } + } + if (!mLayer.isEmpty()) { + return null; + } + String msg = null; + if (mLayer.isEmpty()) { + msg = get(CHECK_FILE); + } + return msg; + } + }); + } + + private final static String CHECK_FILE = RasterMenuNames.SINGLE_BAND_EXIST; + + @Override + public void initialize(PlugInContext context) throws Exception { + + FeatureInstaller.getInstance() + .addMainMenuPlugin( + this, //exe + new String[] { MenuNames.RASTER, + MenuNames.RASTER_VECTORIALIZE }, //menu path + NAME, false, //checkbox + null, //icon + createEnableCheck(context.getWorkbenchContext())); + } + +} Property changes on: core/trunk/src/org/openjump/core/ui/plugin/raster/VectorizeToPolygonsPlugIn.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property _______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel