Revision: 4356 http://sourceforge.net/p/jump-pilot/code/4356 Author: ma15569 Date: 2015-03-27 13:02:49 +0000 (Fri, 27 Mar 2015) Log Message: ----------- 2015-03-22 Giuseppe Aruta (ma15569) * Added ChangeRangeValuesToNoDataPlugIn. * This plugin allowas to change a range of cell values (of a Sextante single * band raster layer) to nodata value. An option allowas to extraxt only the * input range of value (and set the other values to nodata)
Modified Paths: -------------- core/trunk/ChangeLog core/trunk/scripts/default-plugins.xml core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeNoDataValuePlugIn.java core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeValueToNoDataPlugIn.java Added Paths: ----------- core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeRangeValuesToNoDataPlugIn.java Modified: core/trunk/ChangeLog =================================================================== --- core/trunk/ChangeLog 2015-03-27 12:53:34 UTC (rev 4355) +++ core/trunk/ChangeLog 2015-03-27 13:02:49 UTC (rev 4356) @@ -1,4 +1,10 @@ # for display continuity sake please use 2 spaces instead of tabs +2015-03-22 Giuseppe Aruta (ma15569) + * Added ChangeRangeValuesToNoDataPlugIn. + * This plugin allowas to change a range of cell values (of a Sextante single + * band raster layer) to nodata value. An option allowas to extraxt only the + * input range of value (and set the other values to nodata) + 2015-03-22 Giuseppe Aruta (ma15569) * RasterImageLayer Plugin: added Raster Layer statistics tab with several info about cell values * DEMStaristics: added header with the number of selected raster layers Modified: core/trunk/scripts/default-plugins.xml =================================================================== --- core/trunk/scripts/default-plugins.xml 2015-03-27 12:53:34 UTC (rev 4355) +++ core/trunk/scripts/default-plugins.xml 2015-03-27 13:02:49 UTC (rev 4356) @@ -812,6 +812,9 @@ </menus> </plug-in> <plug-in> + org.openjump.core.ui.plugin.raster.nodata.ChangeRangeValuesToNoDataPlugIn + </plug-in> + <plug-in> org.openjump.core.ui.plugin.raster.nodata.ChangeNoDataValuePlugIn </plug-in> Modified: core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeNoDataValuePlugIn.java =================================================================== --- core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeNoDataValuePlugIn.java 2015-03-27 12:53:34 UTC (rev 4355) +++ core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeNoDataValuePlugIn.java 2015-03-27 13:02:49 UTC (rev 4356) @@ -59,8 +59,31 @@ import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller; public class ChangeNoDataValuePlugIn extends AbstractPlugIn { + /** + * + * @author Giuseppe Aruta + * @date 2015_3_25 This class allows to change nodata value of a single band + * file The output is a ESRI float file + */ + // Language codes:9 + private String SUBMENU = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.menu"); public static final String PLUGINNAME = I18N .get("org.openjump.core.ui.plugin.raster.nodata.ChangeNoDataValuePlugIn.name"); + private String OUTPUT_FILE = I18N.get("driver.DriverManager.file-to-save") + + ": "; + private static String FROM = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.from"); + private static String TO = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.to"); + private static String STATISTICS = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.CellStatistics"); + private static String NODATA = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.nodata"); + private static String MIN = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.min"); + private static String MAX = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.max"); private static final Logger LOGGER = Logger .getLogger(ChangeNoDataValuePlugIn.class); @@ -69,10 +92,6 @@ private static String propertiesFile = LoadSextanteRasterImagePlugIn .getPropertiesFile(); NumberFormat cellFormat = null; - private String OUTPUT_FILE = I18N.get("driver.DriverManager.file-to-save") - + ": "; - private String SUBMENU = I18N - .get("org.openjump.core.ui.plugin.raster.nodata.menu"); private static ImageIcon icon16 = IconLoader .icon("fugue/folder-horizontal-open_16.png"); @@ -100,13 +119,14 @@ reportNothingToUndoYet(context); RasterImageLayer rLayer = (RasterImageLayer) LayerTools .getSelectedLayerable(context, RasterImageLayer.class); + String nome = getName() + " (" + rLayer.getName() + ")"; + MultiInputDialog dialog = new MultiInputDialog( - context.getWorkbenchFrame(), getName(), true); + context.getWorkbenchFrame(), nome, true); // Top panel. Visualize nodata/max/min cell values JPanel jPanel1 = new JPanel(new GridBagLayout()); - jPanel1.setBorder(BorderFactory.createTitledBorder(I18N - .get("ui.plugin.LayerStatisticsPlugIn.layer-statistics"))); + jPanel1.setBorder(BorderFactory.createTitledBorder(STATISTICS)); OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); rstLayer.create(rLayer); JTextField nd = new JTextField(String.valueOf(rLayer.getNoDataValue())); @@ -115,23 +135,21 @@ max.setEditable(false); JTextField min = new JTextField(String.valueOf(rstLayer.getMinValue())); min.setEditable(false); - JLabel nd_label = new JLabel("nodata:"); - JLabel min_label = new JLabel("min:"); - JLabel max_label = new JLabel("max:"); + JLabel nd_label = new JLabel(NODATA); + JLabel min_label = new JLabel(MIN); + JLabel max_label = new JLabel(MAX); FormUtils.addRowInGBL(jPanel1, 1, 0, nd_label, nd); FormUtils.addRowInGBL(jPanel1, 1, 2, min_label, min); FormUtils.addRowInGBL(jPanel1, 1, 4, max_label, max); // Main Panel. Set range source-target no data value JPanel mainPanel = new JPanel(new GridBagLayout()); - JLabel source_NoData_label = new JLabel( - I18N.get("org.openjump.core.ui.plugin.raster.nodata.ChangeNoDataValuePlugIn.from")); - JLabel target_NoData_label = new JLabel( - I18N.get("org.openjump.core.ui.plugin.raster.nodata.ChangeNoDataValuePlugIn.to")); + JLabel source_NoData_label = new JLabel(FROM); + JLabel target_NoData_label = new JLabel(TO); JTextField source_nodata = new JTextField(String.valueOf(rLayer .getNoDataValue())); source_nodata.setEditable(false); - JTextField target_nodata = new JTextField(String.valueOf("-99999")); + JTextField target_nodata = new JTextField(String.valueOf("-9999")); source_nodata.addKeyListener(new java.awt.event.KeyAdapter() { @Override public void keyTyped(KeyEvent e) { Added: core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeRangeValuesToNoDataPlugIn.java =================================================================== --- core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeRangeValuesToNoDataPlugIn.java (rev 0) +++ core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeRangeValuesToNoDataPlugIn.java 2015-03-27 13:02:49 UTC (rev 4356) @@ -0,0 +1,518 @@ +package org.openjump.core.ui.plugin.raster.nodata; + +import it.betastudio.adbtoolbox.libs.ExtensionFilter; +import it.betastudio.adbtoolbox.libs.FileOperations; + +import java.awt.Dimension; +import java.awt.GridBagLayout; +import java.awt.event.KeyEvent; +import java.awt.geom.NoninvertibleTransformException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.FileChannel; +import java.text.NumberFormat; +import java.util.Properties; + +import javax.swing.BorderFactory; +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JFileChooser; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; + +import org.apache.log4j.Logger; +import org.openjump.core.apitools.LayerTools; +import org.openjump.core.rasterimage.GridFloat; +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.sextante.OpenJUMPSextanteRasterLayer; +import org.openjump.core.rasterimage.sextante.rasterWrappers.GridWrapperNotInterpolated; +import org.openjump.core.ui.plugin.layer.pirolraster.LoadSextanteRasterImagePlugIn; +import org.saig.core.gui.swing.sldeditor.util.FormUtils; + +import com.vividsolutions.jts.geom.Envelope; +import com.vividsolutions.jump.I18N; +import com.vividsolutions.jump.workbench.WorkbenchContext; +import com.vividsolutions.jump.workbench.model.Category; +import com.vividsolutions.jump.workbench.model.StandardCategoryNames; +import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn; +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.ui.GUIUtil; +import com.vividsolutions.jump.workbench.ui.GenericNames; +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.images.IconLoader; +import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller; + +public class ChangeRangeValuesToNoDataPlugIn extends AbstractPlugIn { + /** + * + * @author Giuseppe Aruta + * @date 2015_3_25 This class allows to change a range allows values to + * nodata Reverse operation extracts the input range of value and set + * the others to nodata The output is a ESRI float file + */ + private static final Logger LOGGER = Logger + .getLogger(ChangeNoDataValuePlugIn.class); + private Properties properties = null; + private String byteOrder = "LSBFIRST"; + private static String propertiesFile = LoadSextanteRasterImagePlugIn + .getPropertiesFile(); + NumberFormat cellFormat = null; + + private boolean reverse = false; + + JTextField jTextField_RasterOut = new JTextField(); + + // Language codes: 12 + + private static String INVERSE = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.Inverse"); + private static String REVERSE_TOOLTIP = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.ChangeRangeValuesToNoDataPlugIn.name"); + private static String CHANGE = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.ChangeRangeValuesToNoDataPlugIn.change"); + private static String PLUGINNAME = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.ChangeRangeValuesToNoDataPlugIn.name"); + private static String FROM = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.from"); + private static String TO = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.to"); + private String SUBMENU = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.menu"); + private static String OUTPUT_FILE = I18N + .get("driver.DriverManager.file-to-save") + ": "; + + private static String STATISTICS = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.CellStatistics"); + private static String NODATA = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.nodata"); + private static String MIN = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.min"); + private static String MAX = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.max"); + + private static ImageIcon icon16 = IconLoader + .icon("fugue/folder-horizontal-open_16.png"); + + public ChangeRangeValuesToNoDataPlugIn() { + + } + + @Override + public String getName() { + return PLUGINNAME; + } + + public static MultiEnableCheck createEnableCheck( + WorkbenchContext workbenchContext) { + EnableCheckFactory checkFactory = new EnableCheckFactory( + workbenchContext); + MultiEnableCheck multiEnableCheck = new MultiEnableCheck(); + + multiEnableCheck.add( + checkFactory.createExactlyNLayerablesMustBeSelectedCheck(1, + RasterImageLayer.class)).add( + checkFactory + .createRasterImageLayerExactlyNBandsMustExistCheck(1)); + return multiEnableCheck; + } + + @Override + public void initialize(PlugInContext context) throws Exception { + WorkbenchContext workbenchContext = context.getWorkbenchContext(); + new FeatureInstaller(workbenchContext); + + context.getFeatureInstaller().addMainMenuPlugin(this, + new String[] { MenuNames.RASTER, SUBMENU }, getName(), false, + null, createEnableCheck(context.getWorkbenchContext())); + } + + @Override + public boolean execute(PlugInContext context) throws Exception { + reportNothingToUndoYet(context); + RasterImageLayer rLayer = (RasterImageLayer) LayerTools + .getSelectedLayerable(context, RasterImageLayer.class); + String nome = getName() + " (" + rLayer.getName() + ")"; + MultiInputDialog dialog = new MultiInputDialog( + context.getWorkbenchFrame(), nome, true); + + // Top panel. Visualize nodata/max/min cell values + JPanel jPanel1 = new JPanel(new GridBagLayout()); + jPanel1.setBorder(BorderFactory.createTitledBorder(STATISTICS)); + OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); + rstLayer.create(rLayer); + JTextField nd = new JTextField(String.valueOf(rLayer.getNoDataValue())); + nd.setEditable(false); + JTextField max = new JTextField(String.valueOf(rstLayer.getMaxValue())); + max.setEditable(false); + JTextField min = new JTextField(String.valueOf(rstLayer.getMinValue())); + min.setEditable(false); + JLabel nd_label = new JLabel(NODATA); + JLabel min_label = new JLabel(MIN); + JLabel max_label = new JLabel(MAX); + FormUtils.addRowInGBL(jPanel1, 1, 0, nd_label, nd); + FormUtils.addRowInGBL(jPanel1, 1, 2, min_label, min); + FormUtils.addRowInGBL(jPanel1, 1, 4, max_label, max); + + // middle panel: input fields for upper/lower values of the range + + JPanel jPanel2 = new JPanel(new GridBagLayout()); + JTextField min_nodata = new JTextField(); + min_nodata.addKeyListener(new java.awt.event.KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + char vChar = e.getKeyChar(); + if (!(Character.isDigit(vChar) || (vChar == KeyEvent.VK_PERIOD) + || (vChar == KeyEvent.VK_BACK_SPACE) + || (vChar == KeyEvent.VK_DELETE) || (vChar == KeyEvent.VK_MINUS))) { + e.consume(); + } + } + }); + JTextField max_nodata = new JTextField(); + max_nodata.addKeyListener(new java.awt.event.KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + char vChar = e.getKeyChar(); + if (!(Character.isDigit(vChar) || (vChar == KeyEvent.VK_PERIOD) + || (vChar == KeyEvent.VK_BACK_SPACE) + || (vChar == KeyEvent.VK_DELETE) || (vChar == KeyEvent.VK_MINUS))) { + e.consume(); + } + } + }); + JLabel max_NoData_label = new JLabel(TO); + JLabel min_NoData_label = new JLabel(FROM); + // JCheckBox checkBox = new JCheckBox(REVERSE, false); + // checkBox.setToolTipText(REVERSE_TOOLTIP); + jPanel2.setBorder(BorderFactory.createTitledBorder(CHANGE)); + FormUtils.addRowInGBL(jPanel2, 2, 0, min_NoData_label, min_nodata); + FormUtils.addRowInGBL(jPanel2, 2, 2, max_NoData_label, max_nodata); + // FormUtils.addRowInGBL(jPanel2, 3, 0, checkBox); + + // Lower panel: choose output raster file + JPanel jPanel3 = new JPanel(new GridBagLayout()); + jPanel3 = new javax.swing.JPanel(); + JLabel jLabel3 = new javax.swing.JLabel(); + jTextField_RasterOut = new javax.swing.JTextField(); + JButton jButton_Dir = new javax.swing.JButton(); + jTextField_RasterOut.setText(""); + jButton_Dir.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton_RasterOutActionPerformed(evt); + } + }); + jLabel3.setText(OUTPUT_FILE); + jTextField_RasterOut.setEditable(true); + jButton_Dir.setIcon(icon16); + jTextField_RasterOut.setPreferredSize(new Dimension(250, 20)); + FormUtils.addRowInGBL(jPanel3, 3, 0, OUTPUT_FILE, jTextField_RasterOut); + FormUtils.addRowInGBL(jPanel3, 3, 2, jButton_Dir); + + // Build the dialog. Add the panels + dialog.addRow(jPanel1); + dialog.addRow(jPanel2); + dialog.addCheckBox(INVERSE, reverse, REVERSE_TOOLTIP); + dialog.addRow(jPanel3); + // Build the dialog. Add a checkbox for a reverse operation (extraxt the + // range) + + GUIUtil.centreOnWindow(dialog); + dialog.setVisible(true); + if (!dialog.wasOKPressed()) { + return false; + } else { + // Get the path of file + String path = jTextField_RasterOut.getText(); + // Set the band + int band = 0; + // Set the extension of the files FLT/HDR + File flt_outFile = new File(path.concat(".flt")); + File hdr_outFile = new File(path.concat(".hdr")); + // Set range (min/max data) and nodata + float mindata = Float.parseFloat(min_nodata.getText()); + float maxdata = Float.parseFloat(max_nodata.getText()); + float nodata1 = (float) rLayer.getNoDataValue(); + + if (dialog.getCheckBox(INVERSE).isSelected()) + // A)Reverse operation: extract the range of cell values + { + saveFLT_extract(flt_outFile, context, rLayer, band, mindata, + maxdata, nodata1); + + } + // B)Mask the range of cell values + else { + saveFLT_mask(flt_outFile, context, rLayer, band, mindata, + maxdata, nodata1); + } + // Save Header + double sNoDataVal = rLayer.getNoDataValue(); + saveHDR(hdr_outFile, context, rLayer, sNoDataVal); + min_nodata.setText(""); + max_nodata.setText(""); + // Reload result file to working category + + loadFLT(flt_outFile, context); + } + return true; + } + + // This code derives from AdBToolbox 1.7 - Set the output file name + private void jButton_RasterOutActionPerformed(java.awt.event.ActionEvent evt) { + File outputPathFile = null; + JFileChooser chooser = new JFileChooser(); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + chooser.setSelectedFile(FileOperations.lastVisitedFolder); + chooser.setDialogType(JFileChooser.SAVE_DIALOG); + ExtensionFilter filter = new ExtensionFilter(); + filter.setDescription("ESRI Binary grid"); + filter.addExtension("flt"); + chooser.setFileFilter(filter); + int ret = chooser.showOpenDialog(null); + if (ret == JFileChooser.APPROVE_OPTION) { + outputPathFile = chooser.getSelectedFile(); + jTextField_RasterOut.setText(outputPathFile.getPath()); + FileOperations.lastVisitedFolder = outputPathFile; + } + + } + + private void saveHDR(File outFile, PlugInContext context, + RasterImageLayer rLayer, double nodata) throws IOException { + OutputStream out = null; + try { + OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); + rstLayer.create(rLayer); + LOGGER.debug(getClass()); + out = new FileOutputStream(outFile); + this.cellFormat = NumberFormat.getNumberInstance(); + this.cellFormat.setMaximumFractionDigits(3); + this.cellFormat.setMinimumFractionDigits(0); + this.properties = new Properties(); + try { + FileInputStream fis = new FileInputStream(propertiesFile); + this.properties.load(fis); + this.properties + .getProperty(LoadSextanteRasterImagePlugIn.KEY_PATH); + fis.close(); + } catch (FileNotFoundException localFileNotFoundException) { + } catch (IOException e) { + context.getWorkbenchFrame().warnUser(GenericNames.ERROR); + } + PrintStream o = new PrintStream(out); + o.println("ncols " + rLayer.getOrigImageWidth()); + + o.println("nrows " + rLayer.getOrigImageHeight()); + + o.println("xllcorner " + rLayer.getWholeImageEnvelope().getMinX()); + + o.println("yllcorner " + rLayer.getWholeImageEnvelope().getMinY()); + + o.println("cellsize " + rstLayer.getLayerCellSize()); + + String sNoDataVal = Double.toString(nodata); + + o.println("NODATA_value " + sNoDataVal); + o.println("byteorder " + byteOrder); + o.close(); + } catch (Exception e) { + context.getWorkbenchFrame() + .warnUser( + I18N.get("org.openjump.core.ui.plugin.mousemenu.SaveDatasetsPlugIn.Error-See-Output-Window")); + context.getWorkbenchFrame().getOutputFrame().createNewDocument(); + context.getWorkbenchFrame() + .getOutputFrame() + .addText( + "SaveImageToRasterPlugIn Exception:Export Part of FLT/ASC or modify raster to ASC not yet implemented. Please Use Sextante Plugin"); + } finally { + if (out != null) + out.close(); + } + } + + private void saveFLT_mask(File outFile, PlugInContext context, + RasterImageLayer rLayer, int band, float mindata, float maxdata, + float nodata) throws IOException { + FileOutputStream out = null; + try { + OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); + rstLayer.create(rLayer); + + LOGGER.debug(getClass()); + out = new FileOutputStream(outFile); + this.cellFormat = NumberFormat.getNumberInstance(); + this.cellFormat.setMaximumFractionDigits(3); + this.cellFormat.setMinimumFractionDigits(0); + this.properties = new Properties(); + try { + FileInputStream fis = new FileInputStream(propertiesFile); + this.properties.load(fis); + this.properties + .getProperty(LoadSextanteRasterImagePlugIn.KEY_PATH); + fis.close(); + } catch (FileNotFoundException localFileNotFoundException) { + } catch (IOException e) { + context.getWorkbenchFrame().warnUser(GenericNames.ERROR); + } + FileChannel fileChannelOut = out.getChannel(); + GridWrapperNotInterpolated gwrapper = new GridWrapperNotInterpolated( + rstLayer, rstLayer.getLayerGridExtent()); + int nx = rstLayer.getLayerGridExtent().getNX(); + int ny = rstLayer.getLayerGridExtent().getNY(); + ByteBuffer bb = ByteBuffer.allocateDirect(nx * 4); + bb.order(ByteOrder.LITTLE_ENDIAN); + + for (int y = 0; y < ny; y++) { + for (int x = 0; x < nx; x++) { + float value = gwrapper.getCellValueAsFloat(x, y, band); + if (bb.hasRemaining()) { + if (value >= mindata && value <= maxdata) { + bb.putFloat(nodata); + } else { + bb.putFloat(value); + } + } else { + x--; + // c--; + bb.compact(); + fileChannelOut.write(bb); + bb.clear(); + } + } + } + bb.compact(); + fileChannelOut.write(bb); + bb.clear(); + } catch (Exception e) { + context.getWorkbenchFrame() + .warnUser( + I18N.get("org.openjump.core.ui.plugin.mousemenu.SaveDatasetsPlugIn.Error-See-Output-Window")); + context.getWorkbenchFrame().getOutputFrame().createNewDocument(); + context.getWorkbenchFrame() + .getOutputFrame() + .addText( + "SaveImageToRasterPlugIn Exception:Export Part of FLT/ASC or modify raster to ASC not yet implemented. Please Use Sextante Plugin"); + } finally { + if (out != null) + out.close(); + } + } + + private void saveFLT_extract(File outFile, PlugInContext context, + RasterImageLayer rLayer, int band, float mindata, float maxdata, + float nodata) throws IOException { + FileOutputStream out = null; + try { + OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); + rstLayer.create(rLayer); + + LOGGER.debug(getClass()); + out = new FileOutputStream(outFile); + this.cellFormat = NumberFormat.getNumberInstance(); + this.cellFormat.setMaximumFractionDigits(3); + this.cellFormat.setMinimumFractionDigits(0); + this.properties = new Properties(); + try { + FileInputStream fis = new FileInputStream(propertiesFile); + this.properties.load(fis); + this.properties + .getProperty(LoadSextanteRasterImagePlugIn.KEY_PATH); + fis.close(); + } catch (FileNotFoundException localFileNotFoundException) { + } catch (IOException e) { + context.getWorkbenchFrame().warnUser(GenericNames.ERROR); + } + FileChannel fileChannelOut = out.getChannel(); + GridWrapperNotInterpolated gwrapper = new GridWrapperNotInterpolated( + rstLayer, rstLayer.getLayerGridExtent()); + int nx = rstLayer.getLayerGridExtent().getNX(); + int ny = rstLayer.getLayerGridExtent().getNY(); + ByteBuffer bb = ByteBuffer.allocateDirect(nx * 4); + bb.order(ByteOrder.LITTLE_ENDIAN); + + for (int y = 0; y < ny; y++) { + for (int x = 0; x < nx; x++) { + float value = gwrapper.getCellValueAsFloat(x, y, band); + if (bb.hasRemaining()) { + if (value >= mindata && value <= maxdata) { + bb.putFloat(value); + } else { + bb.putFloat(nodata); + } + } else { + x--; + // c--; + bb.compact(); + fileChannelOut.write(bb); + bb.clear(); + } + } + } + bb.compact(); + fileChannelOut.write(bb); + bb.clear(); + } catch (Exception e) { + context.getWorkbenchFrame() + .warnUser( + I18N.get("org.openjump.core.ui.plugin.mousemenu.SaveDatasetsPlugIn.Error-See-Output-Window")); + context.getWorkbenchFrame().getOutputFrame().createNewDocument(); + context.getWorkbenchFrame() + .getOutputFrame() + .addText( + "SaveImageToRasterPlugIn Exception:Export Part of FLT/ASC or modify raster to ASC not yet implemented. Please Use Sextante Plugin"); + } finally { + if (out != null) + out.close(); + } + } + + private void loadFLT(File flt_outFile, PlugInContext context) + throws NoninvertibleTransformException, TiffReadingException, + Exception { + + RasterImageIO rasterImageIO = new RasterImageIO(); + Viewport viewport = context.getWorkbenchContext().getLayerViewPanel() + .getViewport(); + Resolution requestedRes = RasterImageIO + .calcRequestedResolution(viewport); + ImageAndMetadata imageAndMetadata = rasterImageIO.loadImage( + context.getWorkbenchContext(), flt_outFile.getAbsolutePath(), + null, viewport.getEnvelopeInModelCoordinates(), requestedRes); + + GridFloat gf = new GridFloat(flt_outFile.getAbsolutePath()); + + Envelope imageEnvelope = new Envelope(gf.getXllCorner(), + gf.getXllCorner() + gf.getnCols() * gf.getCellSize(), + gf.getYllCorner(), gf.getYllCorner() + gf.getnRows() + * gf.getCellSize()); + + RasterImageLayer ril = new RasterImageLayer(flt_outFile.getName(), + context.getWorkbenchContext().getLayerManager(), + flt_outFile.getAbsolutePath(), imageAndMetadata.getImage(), + imageEnvelope); + String catName = StandardCategoryNames.WORKING; + try { + catName = ((Category) context.getLayerNamePanel() + .getSelectedCategories().toArray()[0]).getName(); + } catch (RuntimeException e1) { + } + context.getLayerManager().addLayerable(catName, ril); + } + +} \ No newline at end of file Property changes on: core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeRangeValuesToNoDataPlugIn.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Modified: core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeValueToNoDataPlugIn.java =================================================================== --- core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeValueToNoDataPlugIn.java 2015-03-27 12:53:34 UTC (rev 4355) +++ core/trunk/src/org/openjump/core/ui/plugin/raster/nodata/ChangeValueToNoDataPlugIn.java 2015-03-27 13:02:49 UTC (rev 4356) @@ -27,7 +27,6 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; -import javax.swing.JTextPane; import org.apache.log4j.Logger; import org.openjump.core.apitools.LayerTools; @@ -59,15 +58,14 @@ import com.vividsolutions.jump.workbench.ui.images.IconLoader; import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller; -public class ChangeValueToNoDataPlugIn extends AbstractPlugIn { - public static final String PLUGINNAME = I18N - .get("org.openjump.core.ui.plugin.raster.nodata.ChangeValueToNoDataPlugIn.name"); - private String SUBMENU = I18N - .get("org.openjump.core.ui.plugin.raster.nodata.menu"); - private String CHANGE = I18N - .get("org.openjump.core.ui.plugin.raster.nodata.ChangeValueToNoDataPlugIn.change"); - private String TONODATA = I18N - .get("org.openjump.core.ui.plugin.raster.nodata.ChangeValueToNoDataPlugIn.tonodata"); +public class ChangeRangeValuesToNoDataPlugIn extends AbstractPlugIn { + /** + * + * @author Giuseppe Aruta + * @date 2015_3_25 This class allows to change a range allows values to + * nodata Reverse operation extracts the input range of value and set + * the others to nodata The output is a ESRI float file + */ private static final Logger LOGGER = Logger .getLogger(ChangeNoDataValuePlugIn.class); private Properties properties = null; @@ -75,19 +73,65 @@ private static String propertiesFile = LoadSextanteRasterImagePlugIn .getPropertiesFile(); NumberFormat cellFormat = null; - private String OUTPUT_FILE = I18N.get("driver.DriverManager.file-to-save") - + ": "; + + private boolean reverse = false; + + JTextField jTextField_RasterOut = new JTextField(); + + // Language codes: 12 + + private static String INVERSE = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.Inverse"); + private static String REVERSE_TOOLTIP = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.ChangeRangeValuesToNoDataPlugIn.name"); + private static String CHANGE = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.ChangeRangeValuesToNoDataPlugIn.change"); + private static String PLUGINNAME = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.ChangeRangeValuesToNoDataPlugIn.name"); + private static String FROM = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.from"); + private static String TO = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.to"); + private String SUBMENU = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.menu"); + private static String OUTPUT_FILE = I18N + .get("driver.DriverManager.file-to-save") + ": "; + + private static String STATISTICS = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.CellStatistics"); + private static String NODATA = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.nodata"); + private static String MIN = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.min"); + private static String MAX = I18N + .get("org.openjump.core.ui.plugin.raster.nodata.max"); + private static ImageIcon icon16 = IconLoader .icon("fugue/folder-horizontal-open_16.png"); - public ChangeValueToNoDataPlugIn() { + public ChangeRangeValuesToNoDataPlugIn() { } + @Override public String getName() { return PLUGINNAME; } + public static MultiEnableCheck createEnableCheck( + WorkbenchContext workbenchContext) { + EnableCheckFactory checkFactory = new EnableCheckFactory( + workbenchContext); + MultiEnableCheck multiEnableCheck = new MultiEnableCheck(); + + multiEnableCheck.add( + checkFactory.createExactlyNLayerablesMustBeSelectedCheck(1, + RasterImageLayer.class)).add( + checkFactory + .createRasterImageLayerExactlyNBandsMustExistCheck(1)); + return multiEnableCheck; + } + @Override public void initialize(PlugInContext context) throws Exception { WorkbenchContext workbenchContext = context.getWorkbenchContext(); @@ -98,17 +142,18 @@ null, createEnableCheck(context.getWorkbenchContext())); } + @Override public boolean execute(PlugInContext context) throws Exception { reportNothingToUndoYet(context); RasterImageLayer rLayer = (RasterImageLayer) LayerTools .getSelectedLayerable(context, RasterImageLayer.class); + String nome = getName() + " (" + rLayer.getName() + ")"; MultiInputDialog dialog = new MultiInputDialog( - context.getWorkbenchFrame(), getName(), true); + context.getWorkbenchFrame(), nome, true); // Top panel. Visualize nodata/max/min cell values JPanel jPanel1 = new JPanel(new GridBagLayout()); - jPanel1.setBorder(BorderFactory.createTitledBorder(I18N - .get("ui.plugin.LayerStatisticsPlugIn.layer-statistics"))); + jPanel1.setBorder(BorderFactory.createTitledBorder(STATISTICS)); OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); rstLayer.create(rLayer); JTextField nd = new JTextField(String.valueOf(rLayer.getNoDataValue())); @@ -117,35 +162,52 @@ max.setEditable(false); JTextField min = new JTextField(String.valueOf(rstLayer.getMinValue())); min.setEditable(false); - JLabel nd_label = new JLabel("nodata:"); - JLabel min_label = new JLabel("min:"); - JLabel max_label = new JLabel("max:"); + JLabel nd_label = new JLabel(NODATA); + JLabel min_label = new JLabel(MIN); + JLabel max_label = new JLabel(MAX); FormUtils.addRowInGBL(jPanel1, 1, 0, nd_label, nd); FormUtils.addRowInGBL(jPanel1, 1, 2, min_label, min); FormUtils.addRowInGBL(jPanel1, 1, 4, max_label, max); - // Main Panel. Set range source-target no data value - JPanel secondPanel = new JPanel(new GridBagLayout()); - JTextPane text = new JTextPane(); - text.setOpaque(false); - text.setText(String.valueOf(rLayer.getNoDataValue())); - JTextField changing_data = new JTextField(); - changing_data.addKeyListener(new java.awt.event.KeyAdapter() { + // middle panel: input fields for upper/lower values of the range + + JPanel jPanel2 = new JPanel(new GridBagLayout()); + JTextField min_nodata = new JTextField(); + min_nodata.addKeyListener(new java.awt.event.KeyAdapter() { @Override public void keyTyped(KeyEvent e) { char vChar = e.getKeyChar(); if (!(Character.isDigit(vChar) || (vChar == KeyEvent.VK_PERIOD) - || (vChar == KeyEvent.VK_BACK_SPACE) || (vChar == KeyEvent.VK_DELETE))) { + || (vChar == KeyEvent.VK_BACK_SPACE) + || (vChar == KeyEvent.VK_DELETE) || (vChar == KeyEvent.VK_MINUS))) { e.consume(); } } }); - FormUtils.addRowInGBL(secondPanel, 2, 0, CHANGE, changing_data); - FormUtils.addRowInGBL(secondPanel, 3, 0, TONODATA, text); + JTextField max_nodata = new JTextField(); + max_nodata.addKeyListener(new java.awt.event.KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + char vChar = e.getKeyChar(); + if (!(Character.isDigit(vChar) || (vChar == KeyEvent.VK_PERIOD) + || (vChar == KeyEvent.VK_BACK_SPACE) + || (vChar == KeyEvent.VK_DELETE) || (vChar == KeyEvent.VK_MINUS))) { + e.consume(); + } + } + }); + JLabel max_NoData_label = new JLabel(TO); + JLabel min_NoData_label = new JLabel(FROM); + // JCheckBox checkBox = new JCheckBox(REVERSE, false); + // checkBox.setToolTipText(REVERSE_TOOLTIP); + jPanel2.setBorder(BorderFactory.createTitledBorder(CHANGE)); + FormUtils.addRowInGBL(jPanel2, 2, 0, min_NoData_label, min_nodata); + FormUtils.addRowInGBL(jPanel2, 2, 2, max_NoData_label, max_nodata); + // FormUtils.addRowInGBL(jPanel2, 3, 0, checkBox); - // Lower panel - JPanel jPanel2 = new JPanel(new GridBagLayout()); - jPanel2 = new javax.swing.JPanel(); + // Lower panel: choose output raster file + JPanel jPanel3 = new JPanel(new GridBagLayout()); + jPanel3 = new javax.swing.JPanel(); JLabel jLabel3 = new javax.swing.JLabel(); jTextField_RasterOut = new javax.swing.JTextField(); JButton jButton_Dir = new javax.swing.JButton(); @@ -159,39 +221,80 @@ jTextField_RasterOut.setEditable(true); jButton_Dir.setIcon(icon16); jTextField_RasterOut.setPreferredSize(new Dimension(250, 20)); - FormUtils.addRowInGBL(jPanel2, 3, 0, OUTPUT_FILE, jTextField_RasterOut); - FormUtils.addRowInGBL(jPanel2, 3, 2, jButton_Dir); + FormUtils.addRowInGBL(jPanel3, 3, 0, OUTPUT_FILE, jTextField_RasterOut); + FormUtils.addRowInGBL(jPanel3, 3, 2, jButton_Dir); + // Build the dialog. Add the panels dialog.addRow(jPanel1); - dialog.addRow(secondPanel); dialog.addRow(jPanel2); + dialog.addCheckBox(INVERSE, reverse, REVERSE_TOOLTIP); + dialog.addRow(jPanel3); + // Build the dialog. Add a checkbox for a reverse operation (extraxt the + // range) GUIUtil.centreOnWindow(dialog); dialog.setVisible(true); - if (!dialog.wasOKPressed()) { return false; } else { - + // Get the path of file String path = jTextField_RasterOut.getText(); - + // Set the band int band = 0; - + // Set the extension of the files FLT/HDR File flt_outFile = new File(path.concat(".flt")); File hdr_outFile = new File(path.concat(".hdr")); - float olddata = Float.parseFloat(changing_data.getText()); - float newdata = (float) rLayer.getNoDataValue(); - saveFLT(flt_outFile, context, rLayer, band, olddata, newdata); - saveHDR(hdr_outFile, context, rLayer); + // Set range (min/max data) and nodata + float mindata = Float.parseFloat(min_nodata.getText()); + float maxdata = Float.parseFloat(max_nodata.getText()); + float nodata1 = (float) rLayer.getNoDataValue(); + + if (dialog.getCheckBox(INVERSE).isSelected()) + // A)Reverse operation: extract the range of cell values + { + saveFLT_extract(flt_outFile, context, rLayer, band, mindata, + maxdata, nodata1); + + } + // B)Mask the range of cell values + else { + saveFLT_mask(flt_outFile, context, rLayer, band, mindata, + maxdata, nodata1); + } + // Save Header + double sNoDataVal = rLayer.getNoDataValue(); + saveHDR(hdr_outFile, context, rLayer, sNoDataVal); + min_nodata.setText(""); + max_nodata.setText(""); + // Reload result file to working category + loadFLT(flt_outFile, context); - } return true; + } + // This code derives from AdBToolbox 1.7 - Set the output file name + private void jButton_RasterOutActionPerformed(java.awt.event.ActionEvent evt) { + File outputPathFile = null; + JFileChooser chooser = new JFileChooser(); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + chooser.setSelectedFile(FileOperations.lastVisitedFolder); + chooser.setDialogType(JFileChooser.SAVE_DIALOG); + ExtensionFilter filter = new ExtensionFilter(); + filter.setDescription("ESRI Binary grid"); + filter.addExtension("flt"); + chooser.setFileFilter(filter); + int ret = chooser.showOpenDialog(null); + if (ret == JFileChooser.APPROVE_OPTION) { + outputPathFile = chooser.getSelectedFile(); + jTextField_RasterOut.setText(outputPathFile.getPath()); + FileOperations.lastVisitedFolder = outputPathFile; + } + } private void saveHDR(File outFile, PlugInContext context, - RasterImageLayer rLayer) throws IOException { + RasterImageLayer rLayer, double nodata) throws IOException { OutputStream out = null; try { OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); @@ -223,7 +326,7 @@ o.println("cellsize " + rstLayer.getLayerCellSize()); - String sNoDataVal = Double.toString(rstLayer.getNoDataValue()); + String sNoDataVal = Double.toString(nodata); o.println("NODATA_value " + sNoDataVal); o.println("byteorder " + byteOrder); @@ -243,13 +346,14 @@ } } - private void saveFLT(File outFile, PlugInContext context, - RasterImageLayer rLayer, int band, float oldnodata, float newnodata) - throws IOException { + private void saveFLT_mask(File outFile, PlugInContext context, + RasterImageLayer rLayer, int band, float mindata, float maxdata, + float nodata) throws IOException { FileOutputStream out = null; try { OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); rstLayer.create(rLayer); + LOGGER.debug(getClass()); out = new FileOutputStream(outFile); this.cellFormat = NumberFormat.getNumberInstance(); @@ -278,8 +382,8 @@ for (int x = 0; x < nx; x++) { float value = gwrapper.getCellValueAsFloat(x, y, band); if (bb.hasRemaining()) { - if (value == oldnodata) { - bb.putFloat(newnodata); + if (value >= mindata && value <= maxdata) { + bb.putFloat(nodata); } else { bb.putFloat(value); } @@ -310,6 +414,74 @@ } } + private void saveFLT_extract(File outFile, PlugInContext context, + RasterImageLayer rLayer, int band, float mindata, float maxdata, + float nodata) throws IOException { + FileOutputStream out = null; + try { + OpenJUMPSextanteRasterLayer rstLayer = new OpenJUMPSextanteRasterLayer(); + rstLayer.create(rLayer); + + LOGGER.debug(getClass()); + out = new FileOutputStream(outFile); + this.cellFormat = NumberFormat.getNumberInstance(); + this.cellFormat.setMaximumFractionDigits(3); + this.cellFormat.setMinimumFractionDigits(0); + this.properties = new Properties(); + try { + FileInputStream fis = new FileInputStream(propertiesFile); + this.properties.load(fis); + this.properties + .getProperty(LoadSextanteRasterImagePlugIn.KEY_PATH); + fis.close(); + } catch (FileNotFoundException localFileNotFoundException) { + } catch (IOException e) { + context.getWorkbenchFrame().warnUser(GenericNames.ERROR); + } + FileChannel fileChannelOut = out.getChannel(); + GridWrapperNotInterpolated gwrapper = new GridWrapperNotInterpolated( + rstLayer, rstLayer.getLayerGridExtent()); + int nx = rstLayer.getLayerGridExtent().getNX(); + int ny = rstLayer.getLayerGridExtent().getNY(); + ByteBuffer bb = ByteBuffer.allocateDirect(nx * 4); + bb.order(ByteOrder.LITTLE_ENDIAN); + + for (int y = 0; y < ny; y++) { + for (int x = 0; x < nx; x++) { + float value = gwrapper.getCellValueAsFloat(x, y, band); + if (bb.hasRemaining()) { + if (value >= mindata && value <= maxdata) { + bb.putFloat(value); + } else { + bb.putFloat(nodata); + } + } else { + x--; + // c--; + bb.compact(); + fileChannelOut.write(bb); + bb.clear(); + } + } + } + bb.compact(); + fileChannelOut.write(bb); + bb.clear(); + } catch (Exception e) { + context.getWorkbenchFrame() + .warnUser( + I18N.get("org.openjump.core.ui.plugin.mousemenu.SaveDatasetsPlugIn.Error-See-Output-Window")); + context.getWorkbenchFrame().getOutputFrame().createNewDocument(); + context.getWorkbenchFrame() + .getOutputFrame() + .addText( + "SaveImageToRasterPlugIn Exception:Export Part of FLT/ASC or modify raster to ASC not yet implemented. Please Use Sextante Plugin"); + } finally { + if (out != null) + out.close(); + } + } + private void loadFLT(File flt_outFile, PlugInContext context) throws NoninvertibleTransformException, TiffReadingException, Exception { @@ -343,41 +515,4 @@ context.getLayerManager().addLayerable(catName, ril); } - public static MultiEnableCheck createEnableCheck( - WorkbenchContext workbenchContext) { - EnableCheckFactory checkFactory = new EnableCheckFactory( - workbenchContext); - MultiEnableCheck multiEnableCheck = new MultiEnableCheck(); - - multiEnableCheck.add( - checkFactory.createExactlyNLayerablesMustBeSelectedCheck(1, - RasterImageLayer.class)).add( - checkFactory - .createRasterImageLayerExactlyNBandsMustExistCheck(1)); - - return multiEnableCheck; - } - - JTextField jTextField_RasterOut = new JTextField(); - - private void jButton_RasterOutActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_RasterOutActionPerformed - - File outputPathFile = null; - JFileChooser chooser = new JFileChooser(); - chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - chooser.setSelectedFile(FileOperations.lastVisitedFolder); - chooser.setDialogType(JFileChooser.SAVE_DIALOG); - ExtensionFilter filter = new ExtensionFilter(); - filter.addExtension("flt"); - chooser.setFileFilter(filter); - int ret = chooser.showOpenDialog(null); - if (ret == JFileChooser.APPROVE_OPTION) { - outputPathFile = chooser.getSelectedFile(); - // outputpathString = outputpathFile.getPath(); - jTextField_RasterOut.setText(outputPathFile.getPath()); - FileOperations.lastVisitedFolder = outputPathFile; - } - - } - } \ No newline at end of file ------------------------------------------------------------------------------ Dive into the World of Parallel Programming The Go Parallel Website, sponsored by Intel and developed in partnership with Slashdot Media, is your hub for all things parallel software development, from weekly thought leadership blogs to news, videos, case studies, tutorials and more. Take a look and join the conversation now. http://goparallel.sourceforge.net/ _______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel