Revision: 6097 http://sourceforge.net/p/jump-pilot/code/6097 Author: ma15569 Date: 2019-01-18 17:16:02 +0000 (Fri, 18 Jan 2019) Log Message: ----------- Correct bug. Now users don't have to manually add extension to output file. Added also .tif output as defautt
Modified Paths: -------------- plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/rastertools/RasterConversionsPlugIn.java plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/rastertools/RasterConverter.java Modified: plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/rastertools/RasterConversionsPlugIn.java =================================================================== --- plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/rastertools/RasterConversionsPlugIn.java 2019-01-17 18:12:32 UTC (rev 6096) +++ plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/rastertools/RasterConversionsPlugIn.java 2019-01-18 17:16:02 UTC (rev 6097) @@ -1,5 +1,6 @@ package com.geomaticaeambiente.klemgui.plugin.rastertools; +import com.geomaticaeambiente.klemgui.plugin.rastertools.RasterConverter; import com.geomaticaeambiente.klemgui.utils.PluginUtils; import com.geomaticaeambiente.klemgui.ui.CustomComboBox.RasterComboBox; import com.geomaticaeambiente.klemgui.ui.GUIUtils; @@ -13,13 +14,16 @@ import com.vividsolutions.jump.util.StringUtil; import com.vividsolutions.jump.workbench.plugin.PlugInContext; import com.vividsolutions.jump.workbench.ui.ErrorDialog; + import java.awt.Cursor; import java.io.File; import java.io.IOException; + import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; + import org.openjump.core.rasterimage.RasterImageLayer; /** @@ -71,56 +75,81 @@ @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 { - super.getInitialDialog().setCursor(new Cursor(Cursor.WAIT_CURSOR)); - + super.getInitialDialog().setCursor( + new Cursor(Cursor.WAIT_CURSOR)); + // Input values - String rasterSelected = GUIUtils.getStringValue(componentsWithActions.getComponent("00", GUIUtils.INPUT, 1)); - String toSelected = GUIUtils.getStringValue(componentsWithActions.getComponent("01", GUIUtils.INPUT, 1)); + final String rasterSelected = GUIUtils + .getStringValue(componentsWithActions.getComponent( + "00", GUIUtils.INPUT, 1)); + final String toSelected = GUIUtils + .getStringValue(componentsWithActions.getComponent( + "01", GUIUtils.INPUT, 1)); // Get output raster name - String outRasterName = GUIUtils.getStringValue(componentsWithActions.getComponent("00", GUIUtils.OUTPUT, 1)); + final String outRasterName = GUIUtils + .getStringValue(componentsWithActions.getComponent( + "00", GUIUtils.OUTPUT, 1)); // Check input and output values checkValues(rasterSelected, outRasterName); //extract raster selected from combobox - RasterImageLayer inputRasterSelected = PluginUtils.getRasterImageLayerSelected((RasterComboBox) componentsWithActions.getComponent("00", GUIUtils.INPUT, 1)); - File inputFile = new File(inputRasterSelected.getImageFileName()); - - File outputFile = new File(outRasterName); - - RasterConverter.convert( - inputFile, - outputFile, - RasterConverter.Format.valueOf(toSelected)); + final RasterImageLayer inputRasterSelected = PluginUtils + .getRasterImageLayerSelected((RasterComboBox) componentsWithActions + .getComponent("00", GUIUtils.INPUT, 1)); + final File inputFile = new File( + inputRasterSelected.getImageFileName()); - JOptionPane.showMessageDialog(super.getInitialDialog(), - PluginUtils.getResources().getString("SetWorkspacePlugin.Done.message"), PluginUtils.plugInName, JOptionPane.INFORMATION_MESSAGE); + final File outputFile = new File(outRasterName); + if (!toSelected.isEmpty()) { + RasterConverter.convert(inputFile, outputFile, + RasterConverter.Format.valueOf(toSelected)); + } else { + //Added TIF if no extension is choosen + RasterConverter.convert(inputFile, outputFile, + RasterConverter.Format.TIFF); + } - } catch (Exception ex) { - super.getInitialDialog().setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); - ex.printStackTrace(System.out); - ErrorDialog.show( + JOptionPane.showMessageDialog( super.getInitialDialog(), + PluginUtils.getResources().getString( + "SetWorkspacePlugin.Done.message"), PluginUtils.plugInName, - ex.toString(), + JOptionPane.INFORMATION_MESSAGE); + + } catch (final Exception ex) { + super.getInitialDialog().setCursor( + new Cursor(Cursor.DEFAULT_CURSOR)); + ex.printStackTrace(System.out); + ErrorDialog.show(super.getInitialDialog(), + PluginUtils.plugInName, ex.toString(), StringUtil.stackTrace(ex)); } finally { - super.getInitialDialog().setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + super.getInitialDialog().setCursor( + new Cursor(Cursor.DEFAULT_CURSOR)); } } + @Override public void leftButton() { Modified: plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/rastertools/RasterConverter.java =================================================================== --- plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/rastertools/RasterConverter.java 2019-01-17 18:12:32 UTC (rev 6096) +++ plug-ins/OpenKLEM/OpenKLEMOpenJUMP/trunk/src/com/geomaticaeambiente/klemgui/plugin/rastertools/RasterConverter.java 2019-01-18 17:16:02 UTC (rev 6097) @@ -1,9 +1,12 @@ package com.geomaticaeambiente.klemgui.plugin.rastertools; import com.geomaticaeambiente.klemgui.exceptions.WarningException; +import com.geomaticaeambiente.klemgui.plugin.rastertools.RasterConverter.Format; import com.geomaticaeambiente.klemgui.utils.FileUtils; import com.geomaticaeambiente.klemgui.utils.PluginUtils; import com.vividsolutions.jts.geom.Envelope; +import com.vividsolutions.jump.util.FileUtil; + import java.awt.Point; import java.awt.image.Raster; import java.io.File; @@ -10,6 +13,8 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.nio.ByteOrder; + +import org.apache.commons.io.FilenameUtils; import org.openjump.core.rasterimage.GridAscii; import org.openjump.core.rasterimage.GridFloat; import org.openjump.core.rasterimage.RasterImageIO; @@ -22,54 +27,66 @@ */ public class RasterConverter { - public static void convert(File input, - File output, Format outputFormat) - throws IOException, FileNotFoundException, TiffTags.TiffReadingException, Exception { - + public static void convert(File input, File output, Format outputFormat) + throws IOException, FileNotFoundException, + TiffTags.TiffReadingException, Exception { + // Just copy - Format inputFormat = findFormat(input); - if(inputFormat == null) { - throw new WarningException(PluginUtils.getResources().getString("RasterConverter.UnknownInputFormat")); + final Format inputFormat = findFormat(input); + if (inputFormat == null) { + throw new WarningException(PluginUtils.getResources().getString( + "RasterConverter.UnknownInputFormat")); } - if(inputFormat == outputFormat) { - FileUtils.copy(input, output); + if (inputFormat == outputFormat) { + final String ext = FilenameUtils.getExtension(input + .getAbsolutePath()); + final File outputfile = new File(output.getAbsolutePath() + .concat(".").concat(ext)); + FileUtils.copy(input, outputfile); + } else { + final Point imageDimensions = RasterImageIO + .getImageDimensions(input.getAbsolutePath()); + final Envelope envelope = RasterImageIO.getGeoReferencing( + input.getAbsolutePath(), true, imageDimensions); + final CellSizeXY cellSize = RasterImageIO.getCellSize(input + .getAbsolutePath()); + Double noData = RasterImageIO.getNoData(input.getAbsolutePath()); + if (noData == null) { + noData = -9999.; + } + + if (outputFormat == Format.ESRI_ASCII) { + output = FileUtil.addExtensionIfNone(output, "asc"); + final GridAscii ga = new GridAscii(output.getAbsolutePath(), + imageDimensions.x, imageDimensions.y, true, + envelope.getMinX(), envelope.getMinY(), + cellSize.getAverageCellSize(), noData); + ga.setRas(RasterImageIO.loadRasterData(input.getAbsolutePath(), + null)); + ga.writeGrid(); + + } else if (outputFormat == Format.ESRI_FLT) { + output = FileUtil.addExtensionIfNone(output, "flt"); + final GridFloat gf = new GridFloat(output.getAbsolutePath(), + imageDimensions.x, imageDimensions.y, true, + envelope.getMinX(), envelope.getMinY(), + cellSize.getAverageCellSize(), noData, + ByteOrder.LITTLE_ENDIAN); + gf.setRas(RasterImageIO.loadRasterData(input.getAbsolutePath(), + null)); + gf.writeGrid(); + + } else if (outputFormat == Format.TIFF) { + output = FileUtil.addExtensionIfNone(output, "tif"); + final RasterImageIO rasterImageIO = new RasterImageIO(); + final Raster raster = RasterImageIO.loadRasterData( + input.getAbsolutePath(), null); + + rasterImageIO.writeImage(output, raster, envelope, cellSize, + noData); + + } } - - Point imageDimensions = RasterImageIO.getImageDimensions(input.getAbsolutePath()); - Envelope envelope = RasterImageIO.getGeoReferencing(input.getAbsolutePath(), true, imageDimensions); - CellSizeXY cellSize = RasterImageIO.getCellSize(input.getAbsolutePath()); - Double noData = RasterImageIO.getNoData(input.getAbsolutePath()); - if(noData == null) { - noData = -9999.; - } - - if(outputFormat == Format.ESRI_ASCII) { - - GridAscii ga = new GridAscii(output.getAbsolutePath(), - imageDimensions.x, imageDimensions.y, - true, envelope.getMinX(), envelope.getMinY(), - cellSize.getAverageCellSize(), noData); - ga.setRas(RasterImageIO.loadRasterData(input.getAbsolutePath(), null)); - ga.writeGrid(); - - } else if(outputFormat == Format.ESRI_FLT) { - - GridFloat gf = new GridFloat(output.getAbsolutePath(), - imageDimensions.x, imageDimensions.y, - true, envelope.getMinX(), envelope.getMinY(), - cellSize.getAverageCellSize(), noData, ByteOrder.LITTLE_ENDIAN); - gf.setRas(RasterImageIO.loadRasterData(input.getAbsolutePath(), null)); - gf.writeGrid(); - - } else if (outputFormat == Format.TIFF) { - - RasterImageIO rasterImageIO = new RasterImageIO(); - Raster raster = RasterImageIO.loadRasterData(input.getAbsolutePath(), null); - - rasterImageIO.writeImage(output, raster, envelope, cellSize, noData); - - } - } public static Format findFormat(File file) { _______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel