Revision: 4865 http://sourceforge.net/p/jump-pilot/code/4865 Author: ma15569 Date: 2016-03-26 11:07:43 +0000 (Sat, 26 Mar 2016) Log Message: ----------- Giuseppe Aruta <giuseppe_ar...@yahoo.it> Extended Sextante Raster Capabilities to read Bitmap (BMP) and JP2000 (JP2) raster files
Modified Paths: -------------- core/trunk/ChangeLog core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java core/trunk/src/org/openjump/core/rasterimage/SelectRasterImageFilesPanel.java Modified: core/trunk/ChangeLog =================================================================== --- core/trunk/ChangeLog 2016-03-25 21:29:42 UTC (rev 4864) +++ core/trunk/ChangeLog 2016-03-26 11:07:43 UTC (rev 4865) @@ -2,6 +2,9 @@ # 1. for display continuity sake please use 2 spaces instead of tabs # 2. make sure that lines break at 80 chars for constricted display situations #<-------------------------------- 80 chars ----------------------------------># +2016-03-26 Giuseppe Aruta <giuseppe_ar...@yahoo.it> + * Extended Sextante Raster Capabilities to read Bitmap (BMP) and JP2000 + (JP2) raster files 2016-03-24 mmichaud <m.michael.mich...@orange.fr> * add build-helper-maven-plugin in maven to be able to run tests located @@ -9,13 +12,13 @@ 2016-03-23 Giuseppe Aruta <giuseppe_ar...@yahoo.it> * emhenced version of RasterImageLayerPropertiesPlugIn: - * 1) New info HTML table "a la" GvSIG style - * 2) Faster info checker (all cell statistic related to Sextante raster image - * have been removed as alredy embedded into Raster Statistic plugin - * 3) Added a limited projection checker < see other details into - * org.openjump.core.ui.util.ProjUtils.class - * 4) Added a Raster Transparency panel - * + 1) New info HTML table "a la" GvSIG style + 2) Faster info checker (all cell statistic related to Sextante raster + image have been removed as alredy embedded into Raster Statistic + plugin + 3) Added a limited projection checker < see other details into + org.openjump.core.ui.util.ProjUtils.class + 4) Added a Raster Transparency panel 2016-03-15 mmichaud <m.michael.mich...@orange.fr> * Switch compiler source and target to java 1.7 Modified: core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java =================================================================== --- core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java 2016-03-25 21:29:42 UTC (rev 4864) +++ core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java 2016-03-26 11:07:43 UTC (rev 4865) @@ -1,16 +1,5 @@ package org.openjump.core.rasterimage; -import com.sun.media.jai.codec.FileSeekableStream; -import com.sun.media.jai.codec.TIFFDirectory; -import com.sun.media.jai.codec.TIFFEncodeParam; -import com.sun.media.jai.codec.TIFFField; -import com.sun.media.jai.codecimpl.TIFFCodec; -import com.sun.media.jai.codecimpl.TIFFImageEncoder; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Envelope; -import com.vividsolutions.jump.I18N; -import com.vividsolutions.jump.workbench.WorkbenchContext; -import com.vividsolutions.jump.workbench.ui.Viewport; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; @@ -28,734 +17,855 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; + import javax.imageio.ImageIO; import javax.media.jai.JAI; import javax.media.jai.PlanarImage; import javax.media.jai.RasterFactory; import javax.media.jai.RenderedOp; + import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.formats.tiff.TiffField; import org.apache.commons.imaging.formats.tiff.fieldtypes.FieldType; import org.openjump.core.rasterimage.TiffTags.TiffMetadata; +import com.sun.media.jai.codec.FileSeekableStream; +import com.sun.media.jai.codec.TIFFDirectory; +import com.sun.media.jai.codec.TIFFEncodeParam; +import com.sun.media.jai.codec.TIFFField; +import com.sun.media.jai.codecimpl.TIFFCodec; +import com.sun.media.jai.codecimpl.TIFFImageEncoder; +import com.vividsolutions.jts.geom.Coordinate; +import com.vividsolutions.jts.geom.Envelope; +import com.vividsolutions.jump.I18N; +import com.vividsolutions.jump.workbench.WorkbenchContext; +import com.vividsolutions.jump.workbench.ui.Viewport; + /** * * @author deluca */ public class RasterImageIO { - - public ImageAndMetadata loadImage(WorkbenchContext wbContext, String fileNameOrURL, Stats stats, Envelope viewPortEnvelope, Resolution requestedRes) - throws IOException, NoninvertibleTransformException, FileNotFoundException, TiffTags.TiffReadingException, Exception { - if (fileNameOrURL.toLowerCase().endsWith(".jpg") - || fileNameOrURL.toLowerCase().endsWith(".gif") - || fileNameOrURL.toLowerCase().endsWith(".png")) { - - BufferedImage bImage; - try { - // Try with ImageIO - bImage = ImageIO.read(new File(fileNameOrURL)); - } catch(Exception ex) { - // Try with JAI - bImage = JAI.create("fileload", fileNameOrURL).getAsBufferedImage(); - } - - if(stats == null) { - - ParameterBlock pb = new ParameterBlock(); - pb.addSource(bImage); // The source image - pb.add(null); // null ROI means whole image - pb.add(1); // check every pixel horizontally - pb.add(1); // check every pixel vertically + public ImageAndMetadata loadImage(WorkbenchContext wbContext, + String fileNameOrURL, Stats stats, Envelope viewPortEnvelope, + Resolution requestedRes) throws IOException, + NoninvertibleTransformException, FileNotFoundException, + TiffTags.TiffReadingException, Exception { - // Mean - RenderedImage meanImage = JAI.create("mean", pb, null); - double[] mean = (double[])meanImage.getProperty("mean"); + if (fileNameOrURL.toLowerCase().endsWith(".jpg") + || fileNameOrURL.toLowerCase().endsWith(".gif") + || fileNameOrURL.toLowerCase().endsWith(".png") + || fileNameOrURL.toLowerCase().endsWith(".bmp") + || fileNameOrURL.toLowerCase().endsWith(".jp2")) { - int nCols = bImage.getWidth(); - int nRows = bImage.getHeight(); - int nBands = bImage.getData().getNumBands(); - long nCells = nCols * nRows; + BufferedImage bImage; + try { + // Try with ImageIO + bImage = ImageIO.read(new File(fileNameOrURL)); + } catch (Exception ex) { + // Try with JAI + bImage = JAI.create("fileload", fileNameOrURL) + .getAsBufferedImage(); + } - // StdDev - double[] stdDev = new double[nBands]; - DataBuffer dataBuffer = bImage.getData().getDataBuffer(); - for(int r=0; r<nRows; r++) { - for(int c=0; c<nCols; c++) { - for(int b=0; b<nBands; b++) { + if (stats == null) { - double val = Math.pow(dataBuffer.getElemDouble(b) - mean[b], 2); - stdDev[b] += val; - } - } - } + ParameterBlock pb = new ParameterBlock(); + pb.addSource(bImage); // The source image + pb.add(null); // null ROI means whole image + pb.add(1); // check every pixel horizontally + pb.add(1); // check every pixel vertically - for(int b=0; b<nBands; b++) { - stdDev[b] = Math.sqrt(stdDev[b] / nCells); - } + // Mean + RenderedImage meanImage = JAI.create("mean", pb, null); + double[] mean = (double[]) meanImage.getProperty("mean"); - // Max and min - pb = new ParameterBlock(); - pb.addSource(bImage); + int nCols = bImage.getWidth(); + int nRows = bImage.getHeight(); + int nBands = bImage.getData().getNumBands(); + long nCells = nCols * nRows; - RenderedOp op = JAI.create("extrema", pb); - double[][] extrema = (double[][]) op.getProperty("extrema"); - - stats = new Stats(nBands); - for(int b=0; b<nBands; b++) { - stats.setStatsForBand(b, extrema[0][b], extrema[1][b], mean[b], stdDev[b]); - } - - } - - Envelope envelope = getGeoReferencing(fileNameOrURL, true, new Point(bImage.getWidth(), bImage.getHeight())); - double cellSize = (envelope.getMaxX() - envelope.getMinX()) / bImage.getWidth(); - return new ImageAndMetadata(bImage, - new Metadata( - envelope, envelope, - new Point(bImage.getWidth(), bImage.getHeight()), - new Point(bImage.getWidth(), bImage.getHeight()), - cellSize, cellSize, Double.NaN, stats)); - - } else if (fileNameOrURL.toLowerCase().endsWith(".tif") || fileNameOrURL.toLowerCase().endsWith(".tiff")) { - - TiffMetadata tiffMetadata = TiffTags.readMetadata(new File(fileNameOrURL)); - - int imgWidth = tiffMetadata.getColsCount(); - int imgHeight = tiffMetadata.getRowsCount(); - - Envelope imageEnvelope = getGeoReferencing(fileNameOrURL, true, new Point (imgWidth, imgHeight)); - - Overviews overviews = OverviewsUtils.getOverviews(new File(fileNameOrURL), imageEnvelope); - - return TiffUtils.readImage(new File(fileNameOrURL), viewPortEnvelope, requestedRes, overviews, stats); + // StdDev + double[] stdDev = new double[nBands]; + DataBuffer dataBuffer = bImage.getData().getDataBuffer(); + for (int r = 0; r < nRows; r++) { + for (int c = 0; c < nCols; c++) { + for (int b = 0; b < nBands; b++) { - } else if (fileNameOrURL.toLowerCase().endsWith(".flt")){ + double val = Math.pow(dataBuffer.getElemDouble(b) + - mean[b], 2); + stdDev[b] += val; + } + } + } - GridFloat gf = new GridFloat(fileNameOrURL); - gf.readGrid(null); - - Envelope imageEnvelope = new Envelope( - gf.getXllCorner(), - gf.getXllCorner() + gf.getnCols() * gf.getCellSize(), - gf.getYllCorner(), - gf.getYllCorner() + gf.getnRows() * gf.getCellSize()); - - stats = new Stats(1); - stats.setStatsForBand(0, gf.getMinVal(), gf.getMaxVal(), gf.getMeanVal(), gf.getStDevVal()); - - return new ImageAndMetadata( - gf.getBufferedImage(), - new Metadata(imageEnvelope, imageEnvelope, - new Point(gf.getnCols(), gf.getnRows()), - new Point(gf.getnCols(), gf.getnRows()), - gf.getCellSize(), gf.getCellSize(), gf.getNoData(), - stats)); + for (int b = 0; b < nBands; b++) { + stdDev[b] = Math.sqrt(stdDev[b] / nCells); + } + // Max and min + pb = new ParameterBlock(); + pb.addSource(bImage); - } else if (fileNameOrURL.toLowerCase().endsWith(".asc") || - fileNameOrURL.toLowerCase().endsWith(".txt")){ + RenderedOp op = JAI.create("extrema", pb); + double[][] extrema = (double[][]) op.getProperty("extrema"); - GridAscii ga = new GridAscii(fileNameOrURL); - ga.readGrid(null); + stats = new Stats(nBands); + for (int b = 0; b < nBands; b++) { + stats.setStatsForBand(b, extrema[0][b], extrema[1][b], + mean[b], stdDev[b]); + } - Envelope imageEnvelope = new Envelope( - ga.getXllCorner(), - ga.getXllCorner() + ga.getnCols() * ga.getCellSize(), - ga.getYllCorner(), - ga.getYllCorner() + ga.getnRows() * ga.getCellSize()); - - BufferedImage pImage = ga.getBufferedImage(); - - stats = new Stats(1); - stats.setStatsForBand(0, ga.getMinVal(), ga.getMaxVal(), ga.getMeanVal(), ga.getStDevVal()); - - return new ImageAndMetadata( - pImage, - new Metadata(imageEnvelope, imageEnvelope, - new Point(ga.getnCols(), ga.getnRows()), - new Point(ga.getnCols(), ga.getnRows()), - ga.getCellSize(), ga.getCellSize(), ga.getNoData(), - stats)); + } - } - //logger.printError("unsupported image format"); - return null; - } - - public static Raster loadRasterData(String filenameOrURL, Rectangle subset) throws IOException { + Envelope envelope = getGeoReferencing(fileNameOrURL, true, + new Point(bImage.getWidth(), bImage.getHeight())); + double cellSize = (envelope.getMaxX() - envelope.getMinX()) + / bImage.getWidth(); + return new ImageAndMetadata(bImage, new Metadata(envelope, + envelope, new Point(bImage.getWidth(), bImage.getHeight()), + new Point(bImage.getWidth(), bImage.getHeight()), cellSize, + cellSize, Double.NaN, stats)); - if (filenameOrURL.toLowerCase().endsWith(".gif") || filenameOrURL.toLowerCase().endsWith(".png") - || filenameOrURL.toLowerCase().endsWith(".tif") || filenameOrURL.toLowerCase().endsWith(".tiff")) { - - RenderedOp renderedOp = JAI.create("fileload", filenameOrURL); - return renderedOp.getAsBufferedImage(subset, null).getData(); + } else if (fileNameOrURL.toLowerCase().endsWith(".tif") + || fileNameOrURL.toLowerCase().endsWith(".tiff")) { - } else if (filenameOrURL.toLowerCase().endsWith(".jpg")){ - - BufferedImage bImage; - try { - // Try with ImageIO - bImage = ImageIO.read(new File(filenameOrURL)); - } catch(Exception ex) { - // Try with JAI - bImage = JAI.create("fileload", filenameOrURL).getAsBufferedImage(); - } - if(subset != null) { - - BufferedImage clipping = new BufferedImage(subset.width, subset.height, bImage.getType()); - Graphics2D area = (Graphics2D) clipping.getGraphics().create(); - area.drawImage(bImage, 0, 0, - clipping.getWidth(), clipping.getHeight(), - subset.x, subset.y, - subset.x + subset.width, subset.y + subset.height, null); - area.dispose(); - - return clipping.getData(); -// Raster raster = bImage.getData(subset); -// WritableRaster wRaster = raster.createCompatibleWritableRaster(subset); -// wRaster.setRect(subset.x, subset.y, raster); -// return wRaster; - } else { - return bImage.getData(); - } -// PlanarImage pimage; -// BufferedImage image = ImageIO.read(new File(filenameOrURL)); -// pimage = PlanarImage.wrapRenderedImage(image); -// return pimage.copyData(); + TiffMetadata tiffMetadata = TiffTags.readMetadata(new File( + fileNameOrURL)); + int imgWidth = tiffMetadata.getColsCount(); + int imgHeight = tiffMetadata.getRowsCount(); - } else if (filenameOrURL.toLowerCase().endsWith(".flt")){ + Envelope imageEnvelope = getGeoReferencing(fileNameOrURL, true, + new Point(imgWidth, imgHeight)); - GridFloat gf = new GridFloat(filenameOrURL); - gf.readGrid(subset); + Overviews overviews = OverviewsUtils.getOverviews(new File( + fileNameOrURL), imageEnvelope); - DataBuffer dataBuffer = new DataBufferFloat(gf.getFloatArray(), gf.getFloatArray().length); + return TiffUtils.readImage(new File(fileNameOrURL), + viewPortEnvelope, requestedRes, overviews, stats); - int nCols = gf.getnCols(); - int nRows = gf.getnRows(); - if(subset != null) { - nCols = subset.width; - nRows = subset.height; - } - - return Raster.createWritableRaster( - RasterFactory.createBandedSampleModel(DataBuffer.TYPE_FLOAT, nCols, nRows, 1), - dataBuffer, - new java.awt.Point(0,0)); + } else if (fileNameOrURL.toLowerCase().endsWith(".flt")) { - } else if (filenameOrURL.toLowerCase().endsWith(".asc") || - filenameOrURL.toLowerCase().endsWith(".txt")){ + GridFloat gf = new GridFloat(fileNameOrURL); + gf.readGrid(null); - GridAscii ga = new GridAscii(filenameOrURL); - ga.readGrid(subset); + Envelope imageEnvelope = new Envelope(gf.getXllCorner(), + gf.getXllCorner() + gf.getnCols() * gf.getCellSize(), + gf.getYllCorner(), gf.getYllCorner() + gf.getnRows() + * gf.getCellSize()); - int nCols = ga.getnCols(); - int nRows = ga.getnRows(); - if(subset != null) { - nCols = subset.width; - nRows = subset.height; - } - - DataBuffer dataBuffer = new DataBufferFloat(ga.getFloatArray(), ga.getFloatArray().length); - - return Raster.createWritableRaster(RasterFactory.createBandedSampleModel( - DataBuffer.TYPE_FLOAT, nCols, nRows, 1), dataBuffer, new java.awt.Point(0,0)); + stats = new Stats(1); + stats.setStatsForBand(0, gf.getMinVal(), gf.getMaxVal(), + gf.getMeanVal(), gf.getStDevVal()); + return new ImageAndMetadata(gf.getBufferedImage(), new Metadata( + imageEnvelope, imageEnvelope, new Point(gf.getnCols(), + gf.getnRows()), new Point(gf.getnCols(), + gf.getnRows()), gf.getCellSize(), gf.getCellSize(), + gf.getNoData(), stats)); - } - return null; - } - - public static Double readCellValue(String fileNameOrURL, Coordinate coordinate, int band) throws NoninvertibleTransformException, Exception { - - Point imageDims = getImageDimensions(fileNameOrURL); - - Envelope envelope = getGeoReferencing(fileNameOrURL, true, new Point(imageDims.x, imageDims.y)); - double cellSizeX = (envelope.getMaxX() - envelope.getMinX()) / imageDims.x; - double cellSizeY = (envelope.getMaxY() - envelope.getMinY()) / imageDims.y; - - Point colRow = fromCoordinateToCell(coordinate, new Coordinate(envelope.getMinX(), envelope.getMinY()), imageDims.y, cellSizeX, cellSizeY); - - return readCellValue(fileNameOrURL, colRow.x, colRow.y, band); - - } - - public static Double readCellValue(String filenameOrURL, int col, int row, int band) throws IOException { - - if (filenameOrURL.toLowerCase().endsWith(".gif") || filenameOrURL.toLowerCase().endsWith(".png") - || filenameOrURL.toLowerCase().endsWith(".tif") || filenameOrURL.toLowerCase().endsWith(".tiff")) { + } else if (fileNameOrURL.toLowerCase().endsWith(".asc") + || fileNameOrURL.toLowerCase().endsWith(".txt")) { - RenderedOp renderedOp = javax.media.jai.JAI.create("fileload", filenameOrURL); - Rectangle rectangle = new Rectangle(col, row, 1, 1); - - return renderedOp.getData(rectangle).getSampleDouble(col, row, band); - - //return pImage.copyData().getSampleDouble(col, row, 0); //copy data so we do not get a ref + GridAscii ga = new GridAscii(fileNameOrURL); + ga.readGrid(null); - - } else if (filenameOrURL.toLowerCase().endsWith(".jpg")){ - //PlanarImage pimage; + Envelope imageEnvelope = new Envelope(ga.getXllCorner(), + ga.getXllCorner() + ga.getnCols() * ga.getCellSize(), + ga.getYllCorner(), ga.getYllCorner() + ga.getnRows() + * ga.getCellSize()); - RenderedOp renderedOp = javax.media.jai.JAI.create("fileload", filenameOrURL); - Rectangle rectangle = new Rectangle(col, row, 1, 1); - - return renderedOp.getData(rectangle).getSampleDouble(col, row, band); - -// BufferedImage image = ImageIO.read(new File(filenameOrURL)); -// pimage = PlanarImage.wrapRenderedImage(image); -// return pimage.copyData().getSampleDouble(col, row, 0); //copy data so we do not get a ref + BufferedImage pImage = ga.getBufferedImage(); + stats = new Stats(1); + stats.setStatsForBand(0, ga.getMinVal(), ga.getMaxVal(), + ga.getMeanVal(), ga.getStDevVal()); - } else if (filenameOrURL.toLowerCase().endsWith(".flt")){ + return new ImageAndMetadata(pImage, new Metadata(imageEnvelope, + imageEnvelope, new Point(ga.getnCols(), ga.getnRows()), + new Point(ga.getnCols(), ga.getnRows()), ga.getCellSize(), + ga.getCellSize(), ga.getNoData(), stats)); - GridFloat gf = new GridFloat(filenameOrURL); - return gf.readCellVal(col, row); + } + // logger.printError("unsupported image format"); + return null; + } - } else if (filenameOrURL.toLowerCase().endsWith(".asc") || - filenameOrURL.toLowerCase().endsWith(".txt")){ + public static Raster loadRasterData(String filenameOrURL, Rectangle subset) + throws IOException { - GridAscii ga = new GridAscii(filenameOrURL); - return ga.readCellValue(col, row); + if (filenameOrURL.toLowerCase().endsWith(".gif") + || filenameOrURL.toLowerCase().endsWith(".png") + || filenameOrURL.toLowerCase().endsWith(".tif") + || filenameOrURL.toLowerCase().endsWith(".tiff")) { - } - return null; - - - } - - public static Point getImageDimensions(String filenameOrURL) throws IOException { - - if (!filenameOrURL.toLowerCase().endsWith(".jpg") && !filenameOrURL.toLowerCase().endsWith(".flt") && - !filenameOrURL.toLowerCase().endsWith(".asc") && - !filenameOrURL.toLowerCase().endsWith(".txt")){ + RenderedOp renderedOp = JAI.create("fileload", filenameOrURL); + return renderedOp.getAsBufferedImage(subset, null).getData(); - javax.media.jai.PlanarImage pImage = javax.media.jai.JAI.create("fileload", filenameOrURL); - if (pImage != null) { - return new Point(pImage.getWidth(), pImage.getHeight()); - } + } else if (filenameOrURL.toLowerCase().endsWith(".jpg") + || filenameOrURL.toLowerCase().endsWith(".bmp") + || filenameOrURL.toLowerCase().endsWith(".jp2")) { - }else if(filenameOrURL.toLowerCase().endsWith(".flt")){ + BufferedImage bImage; + try { + // Try with ImageIO + bImage = ImageIO.read(new File(filenameOrURL)); + } catch (Exception ex) { + // Try with JAI + bImage = JAI.create("fileload", filenameOrURL) + .getAsBufferedImage(); + } + if (subset != null) { - GridFloat gf = new GridFloat(filenameOrURL); - return new Point(gf.getnCols(), gf.getnRows()); + BufferedImage clipping = new BufferedImage(subset.width, + subset.height, bImage.getType()); + Graphics2D area = (Graphics2D) clipping.getGraphics().create(); + area.drawImage(bImage, 0, 0, clipping.getWidth(), + clipping.getHeight(), subset.x, subset.y, subset.x + + subset.width, subset.y + subset.height, null); + area.dispose(); + return clipping.getData(); + // Raster raster = bImage.getData(subset); + // WritableRaster wRaster = + // raster.createCompatibleWritableRaster(subset); + // wRaster.setRect(subset.x, subset.y, raster); + // return wRaster; + } else { + return bImage.getData(); + } + // PlanarImage pimage; + // BufferedImage image = ImageIO.read(new File(filenameOrURL)); + // pimage = PlanarImage.wrapRenderedImage(image); + // return pimage.copyData(); - }else if(filenameOrURL.toLowerCase().endsWith(".asc") || - filenameOrURL.toLowerCase().endsWith(".txt")){ + } else if (filenameOrURL.toLowerCase().endsWith(".flt")) { - GridAscii ga = new GridAscii(filenameOrURL); - return new Point(ga.getnCols(), ga.getnRows()); + GridFloat gf = new GridFloat(filenameOrURL); + gf.readGrid(subset); - } else { - - BufferedImage image = ImageIO.read(new File(filenameOrURL)); - return new Point(image.getWidth(), image.getHeight()); - - } - - return null; - } - - public static Envelope getGeoReferencing(String fileName, boolean allwaysLookForTFWExtension, - Point imageDimensions) throws IOException, NoninvertibleTransformException, Exception{ + DataBuffer dataBuffer = new DataBufferFloat(gf.getFloatArray(), + gf.getFloatArray().length); - Envelope env = null; - - WorldFileHandler worldFileHandler = new WorldFileHandler(fileName, allwaysLookForTFWExtension); - - if (imageDimensions == null){ - //logger.printError("can not determine image dimensions"); - //context.getWorkbench().getFrame().warnUser(I18N.get("org.openjump.core.rasterimage.AddRasterImageLayerWizard.can-not-determine-image-dimensions")); -// return null; - throw new Exception(I18N.get("org.openjump.core.rasterimage.AddRasterImageLayerWizard.can-not-determine-image-dimensions")); - } - - if (worldFileHandler.isWorldFileExistentForImage()!=null) { - //logger.printDebug(PirolPlugInMessages.getString("worldfile-found")); - env = worldFileHandler.readWorldFile(imageDimensions.x, imageDimensions.y); - } - - if (env == null) { + int nCols = gf.getnCols(); + int nRows = gf.getnRows(); + if (subset != null) { + nCols = subset.width; + nRows = subset.height; + } - boolean isGeoTiff = false; + return Raster.createWritableRaster(RasterFactory + .createBandedSampleModel(DataBuffer.TYPE_FLOAT, nCols, + nRows, 1), dataBuffer, new java.awt.Point(0, 0)); - if ( fileName.toLowerCase().endsWith(".tif") || fileName.toLowerCase().endsWith(".tiff") ) { - //logger.printDebug("checking for GeoTIFF"); + } else if (filenameOrURL.toLowerCase().endsWith(".asc") + || filenameOrURL.toLowerCase().endsWith(".txt")) { - Coordinate tiePoint = null, pixelOffset = null, pixelScale = null; - double[] doubles; + GridAscii ga = new GridAscii(filenameOrURL); + ga.readGrid(subset); - FileSeekableStream fileSeekableStream = new FileSeekableStream(fileName); - TIFFDirectory tiffDirectory = new TIFFDirectory(fileSeekableStream, 0); + int nCols = ga.getnCols(); + int nRows = ga.getnRows(); + if (subset != null) { + nCols = subset.width; + nRows = subset.height; + } - TIFFField[] availTags = tiffDirectory.getFields(); - - for (TIFFField availTag : availTags) { - if (availTag.getTag() == GeoTiffConstants.ModelTiepointTag) { - doubles = availTag.getAsDoubles(); - if (doubles.length != 6){ - //logger.printError("unsupported value for ModelTiepointTag (" + GeoTiffConstants.ModelTiepointTag + ")"); - //context.getWorkbench().getFrame().warnUser("unsupported value for ModelTiepointTag (" + GeoTiffConstants.ModelTiepointTag + ")"); - //break; - throw new Exception("unsupported value for ModelTiepointTag (" + GeoTiffConstants.ModelTiepointTag + ")"); - } - if (doubles[0]!=0 || doubles[1]!=0 || doubles[2]!=0){ - if (doubles[2]==0) - pixelOffset = new Coordinate(doubles[0],doubles[1]); - else - pixelOffset = new Coordinate(doubles[0],doubles[1],doubles[2]); - } - if (doubles[5]==0) - tiePoint = new Coordinate(doubles[3],doubles[4]); - else - tiePoint = new Coordinate(doubles[3],doubles[4],doubles[5]); + DataBuffer dataBuffer = new DataBufferFloat(ga.getFloatArray(), + ga.getFloatArray().length); - //logger.printDebug("ModelTiepointTag (po): " + pixelOffset); - //logger.printDebug("ModelTiepointTag (tp): " + tiePoint); - } else if (availTag.getTag() == GeoTiffConstants.ModelPixelScaleTag) { - // Karteneinheiten pro pixel x bzw. y - doubles = availTag.getAsDoubles(); - if (doubles[2]==0) - pixelScale = new Coordinate(doubles[0],doubles[1]); - else - pixelScale = new Coordinate(doubles[0],doubles[1],doubles[2]); + return Raster.createWritableRaster(RasterFactory + .createBandedSampleModel(DataBuffer.TYPE_FLOAT, nCols, + nRows, 1), dataBuffer, new java.awt.Point(0, 0)); - //logger.printDebug("ModelPixelScaleTag (ps): " + pixelScale); - } else { - //logger.printDebug("tiff field: " + availTags[i].getType() + ", "+ availTags[i].getTag() + ", "+ availTags[i].getCount()); - } - } + } + return null; + } - fileSeekableStream.close(); + public static Double readCellValue(String fileNameOrURL, + Coordinate coordinate, int band) + throws NoninvertibleTransformException, Exception { - if (tiePoint!=null && pixelScale!=null){ - isGeoTiff = true; - Coordinate upperLeft; - Coordinate lowerRight; + Point imageDims = getImageDimensions(fileNameOrURL); - if (pixelOffset==null){ - upperLeft = tiePoint; - } else { - upperLeft = new Coordinate( tiePoint.x - (pixelOffset.x * pixelScale.x), tiePoint.y - (pixelOffset.y * pixelScale.y)); - } + Envelope envelope = getGeoReferencing(fileNameOrURL, true, new Point( + imageDims.x, imageDims.y)); + double cellSizeX = (envelope.getMaxX() - envelope.getMinX()) + / imageDims.x; + double cellSizeY = (envelope.getMaxY() - envelope.getMinY()) + / imageDims.y; - lowerRight = new Coordinate( upperLeft.x + (imageDimensions.x * pixelScale.x), upperLeft.y - (imageDimensions.y * pixelScale.y)); + Point colRow = fromCoordinateToCell(coordinate, + new Coordinate(envelope.getMinX(), envelope.getMinY()), + imageDims.y, cellSizeX, cellSizeY); - //logger.printDebug("upperLeft: " + upperLeft); - //logger.printDebug("lowerRight: " + lowerRight); + return readCellValue(fileNameOrURL, colRow.x, colRow.y, band); - env = new Envelope(upperLeft, lowerRight); - } + } - }else if(fileName.toLowerCase().endsWith(".flt")){ - isGeoTiff = true; - GridFloat gf = new GridFloat(fileName); + public static Double readCellValue(String filenameOrURL, int col, int row, + int band) throws IOException { - Coordinate upperLeft = new Coordinate(gf.getXllCorner(), gf.getYllCorner() + gf.getnRows() * gf.getCellSize()); - Coordinate lowerRight = new Coordinate(gf.getXllCorner() + gf.getnCols() * gf.getCellSize(), gf.getYllCorner()); + if (filenameOrURL.toLowerCase().endsWith(".gif") + || filenameOrURL.toLowerCase().endsWith(".png") + || filenameOrURL.toLowerCase().endsWith(".tif") + || filenameOrURL.toLowerCase().endsWith(".tiff")) { - env = new Envelope(upperLeft, lowerRight); + RenderedOp renderedOp = javax.media.jai.JAI.create("fileload", + filenameOrURL); + Rectangle rectangle = new Rectangle(col, row, 1, 1); - }else if(fileName.toLowerCase().endsWith(".asc") || - fileName.toLowerCase().endsWith(".txt")){ - isGeoTiff = true; - GridAscii ga = new GridAscii(fileName); + return renderedOp.getData(rectangle) + .getSampleDouble(col, row, band); - Coordinate upperLeft = new Coordinate(ga.getXllCorner(), ga.getYllCorner() + ga.getnRows() * ga.getCellSize()); - Coordinate lowerRight = new Coordinate(ga.getXllCorner() + ga.getnCols() * ga.getCellSize(), ga.getYllCorner()); + // return pImage.copyData().getSampleDouble(col, row, 0); //copy + // data so we do not get a ref - env = new Envelope(upperLeft, lowerRight); - } + } else if (filenameOrURL.toLowerCase().endsWith(".jpg")) { + // PlanarImage pimage; - if (!isGeoTiff || env==null){ - //logger.printDebug(PirolPlugInMessages.getString("no-worldfile-found")); -// context.getWorkbench().getFrame().warnUser(I18N.get("org.openjump.core.rasterimage.AddRasterImageLayerWizard.no-worldfile-found")); - throw new Exception(I18N.get("org.openjump.core.rasterimage.AddRasterImageLayerWizard.no-worldfile-found")); -// WizardDialog d = new WizardDialog( -// context.getWorkbench().getFrame(), -// I18N.get("RasterImagePlugIn.34") -// + worldFileHandler.getWorldFileName() -// + I18N.get("RasterImagePlugIn.35"), -// context.getErrorHandler()); -// d.init(new WizardPanel[] { new RasterImageWizardPanel() }); -// //Set size after #init, because #init calls #pack. [Jon Aquino] -// d.setSize(500, 400); -// GUIUtil.centreOnWindow(d); -// d.setVisible(true); -// -// if (!d.wasFinishPressed()) { -// //logger.printWarning("user canceled"); -// return null; -// } -// try { -// minx = Double.parseDouble((String) d -// .getData(RasterImageWizardPanel.MINX_KEY)); -// maxx = Double.parseDouble((String) d -// .getData(RasterImageWizardPanel.MAXX_KEY)); -// miny = Double.parseDouble((String) d -// .getData(RasterImageWizardPanel.MINY_KEY)); -// maxy = Double.parseDouble((String) d -// .getData(RasterImageWizardPanel.MAXY_KEY)); -// -// env = new Envelope(minx, maxx, miny, maxy); -// } -// catch(java.lang.NumberFormatException e) { -// Viewport viewport = context.getLayerViewPanel().getViewport(); -// Rectangle visibleRect = viewport.getPanel().getVisibleRect(); -// -// int visibleX1 = visibleRect.x; -// int visibleY1 = visibleRect.y; -// int visibleX2 = visibleX1 + visibleRect.width; -// int visibleY2 = visibleY1 + visibleRect.height; -// Coordinate upperLeftVisible = viewport.toModelCoordinate(new Point(0,0)); -// Coordinate lowerRightVisible = viewport.toModelCoordinate(new Point(visibleX2, visibleY2)); -// env = new Envelope(upperLeftVisible.x, lowerRightVisible.x, upperLeftVisible.y, lowerRightVisible.y); -// } -// -// } -// -// // creating world file -// worldFileHandler = new WorldFileHandler(fileName, allwaysLookForTFWExtension); -// worldFileHandler.writeWorldFile(env, imageDimensions.x, imageDimensions.y); - } - } - return env; - } - - public static CellSizeXY getCellSize(String fileNameOrURL) throws NoninvertibleTransformException, Exception { - - Point imageDims = getImageDimensions(fileNameOrURL); - - Envelope envelope = getGeoReferencing(fileNameOrURL, true, new Point(imageDims.x, imageDims.y)); - double cellSizeX = (envelope.getMaxX() - envelope.getMinX()) / imageDims.x; - double cellSizeY = (envelope.getMaxY() - envelope.getMinY()) / imageDims.y; - - return new RasterImageIO().new CellSizeXY(cellSizeX, cellSizeY); - - } - - public static Double getNoData(String fileNameOrURL) throws IOException, ImageReadException { - - if(fileNameOrURL.toLowerCase().endsWith(".asc") || - fileNameOrURL.toLowerCase().endsWith(".txt")) { - - GridAscii gridAscii = new GridAscii(fileNameOrURL); - gridAscii.readHeader(); - return gridAscii.getNoData(); - - } else if(fileNameOrURL.toLowerCase().endsWith(".flt")) { - - GridFloat gf = new GridFloat(fileNameOrURL); - return gf.getNoData(); - - } else if(fileNameOrURL.toLowerCase().endsWith(".tif")) { - - TiffField field = TiffTags.readField(new File(fileNameOrURL), 42113); - if(field.getFieldType() == FieldType.DOUBLE) { - return field.getDoubleValue(); - } else if (field.getFieldType() == FieldType.FLOAT) { - return field.getDoubleValue(); - } else if (field.getFieldType() == FieldType.ASCII) { - return Double.parseDouble(field.getStringValue()); - } - - } - return null; - - } - - public static Envelope getViewingEnvelope(Viewport viewport) throws NoninvertibleTransformException { - - Rectangle visible = viewport.getPanel().getVisibleRect(); - int visibleX1 = visible.x; - int visibleY1 = visible.y; - int visibleX2 = visibleX1 + visible.width; - int visibleY2 = visibleY1 + visible.height; - - Coordinate upperLeftVisible = viewport.toModelCoordinate(new Point(visibleX1, visibleY1)); - Coordinate lowerRightVisible = viewport.toModelCoordinate(new Point(visibleX2, visibleY2)); - - return new Envelope(upperLeftVisible, lowerRightVisible); - - } - - public static Rectangle getDrawingRectangle(int imgWidth, int imgHeight, Envelope wholeImageEnvelope, - Envelope viewportEnvelope, Resolution subsetResolution) throws NoninvertibleTransformException{ - - if(viewportEnvelope == null || viewportEnvelope.contains(wholeImageEnvelope)) { - Rectangle rect = new Rectangle(0, 0, imgWidth, imgHeight); - return rect; - } else if (viewportEnvelope.intersects(wholeImageEnvelope)) { - - Coordinate upperLeftVisible = new Coordinate(viewportEnvelope.getMinX(), viewportEnvelope.getMaxY()); - Coordinate lowerRightVisible = new Coordinate(viewportEnvelope.getMaxX(), viewportEnvelope.getMinY()); - - java.awt.Point upperLeft = fromCoordinateToCell( - upperLeftVisible, new Coordinate( - wholeImageEnvelope.getMinX(), wholeImageEnvelope.getMinY()), (int)imgHeight, subsetResolution.getX(), subsetResolution.getY()); - java.awt.Point lowerRight = fromCoordinateToCell( - lowerRightVisible, new Coordinate( - wholeImageEnvelope.getMinX(), wholeImageEnvelope.getMinY()), (int)imgHeight, subsetResolution.getX(), subsetResolution.getY()); - - int xOffset = Math.max(0, upperLeft.x); - int yOffset = Math.max(0, upperLeft.y); - - int width = lowerRight.x - upperLeft.x; - int height = lowerRight.y - upperLeft.y; - - Rectangle rect = new Rectangle(xOffset, yOffset, width, height); - return rect; - } else { - return null; - } - - } - - protected Rectangle getVisibleImageCoordinatesOfImage( double imgWidth, double imgHeight, Envelope visible, Envelope imageEnv ){ - - double minVisibleX = Math.max(visible.getMinX(), imageEnv.getMinX()); - double minVisibleY = Math.max(visible.getMinY(), imageEnv.getMinY()); - - double maxVisibleX = Math.min(visible.getMaxX(), imageEnv.getMaxX()); - double maxVisibleY = Math.min(visible.getMaxY(), imageEnv.getMaxY()); - - double offset2VisibleX = imageEnv.getMinX() - visible.getMinX(); - double offset2VisibleY = visible.getMaxY() - imageEnv.getMaxY(); - - double scaleX = imgWidth / imageEnv.getWidth(); - double scaleY = imgHeight / imageEnv.getHeight(); - - int xOffset; - int yOffset; - - if (offset2VisibleX >= 0){ - xOffset = 0; - } else { - xOffset = (int)(-offset2VisibleX * scaleX); - } - - if (offset2VisibleY >= 0){ - yOffset = 0; - } else { - yOffset = (int)(-offset2VisibleY * scaleY); - } - - int width = (int)((maxVisibleX-minVisibleX) * scaleX); - int height = (int)((maxVisibleY-minVisibleY) * scaleY); - - if (width < imgWidth && height < imgHeight){ - width += 1; - height += 1; - } - - - if (width <= 0 || height <= 0){ - return null; - } - - return new Rectangle(xOffset, yOffset, width, height); - } - - public static java.awt.Point fromCoordinateToCell(Coordinate coord, Coordinate lowerLeftCoord, int rowCount, double cellSizeX, double cellSizeY) { - - java.awt.Point point = new java.awt.Point(); - point.x = (int)Math.floor((coord.x - lowerLeftCoord.x) / cellSizeX); - point.y = rowCount - (int)Math.floor((coord.y - lowerLeftCoord.y) / cellSizeY) - 1; + RenderedOp renderedOp = javax.media.jai.JAI.create("fileload", + filenameOrURL); + Rectangle rectangle = new Rectangle(col, row, 1, 1); - return point; - - } - - public static Coordinate fromCellToCoordinate(java.awt.Point cell, Coordinate lowerLeftCoord, double cellSize, int rowCount) { - - Coordinate coord = new Coordinate(); - coord.x = lowerLeftCoord.x + cell.x * cellSize + 0.5 * cellSize; - coord.y = lowerLeftCoord.y + (rowCount - cell.y) * cellSize - 0.5 * cellSize; - return coord; - - } + return renderedOp.getData(rectangle) + .getSampleDouble(col, row, band); - public void writeImage( - File outFile, - Raster raster, - Envelope envelope, - CellSizeXY cellSize, double noData) throws FileNotFoundException, IOException { - - // Delete old .xml.aux statistics file - File auxXmlFile = new File(outFile.getParent(), outFile.getName() + ".aux.xml"); - if(auxXmlFile.exists() && auxXmlFile.canWrite()) { - try { - auxXmlFile.delete(); - } catch(Exception ex) { - ex.printStackTrace(System.out); - } - } - - SampleModel sm = raster.getSampleModel(); - ColorModel colorModel = PlanarImage.createColorModel(sm); - BufferedImage image = new BufferedImage(colorModel, (WritableRaster) raster, false, null); + // BufferedImage image = ImageIO.read(new File(filenameOrURL)); + // pimage = PlanarImage.wrapRenderedImage(image); + // return pimage.copyData().getSampleDouble(col, row, 0); //copy + // data so we do not get a ref + } else if (filenameOrURL.toLowerCase().endsWith(".bmp")) { + BufferedImage image = ImageIO.read(new File(filenameOrURL)); + PlanarImage pimage = PlanarImage.wrapRenderedImage(image); + return pimage.copyData().getSampleDouble(col, row, 0); - TIFFEncodeParam param = new TIFFEncodeParam(); - param.setCompression(TIFFEncodeParam.COMPRESSION_NONE); + } else if (filenameOrURL.toLowerCase().endsWith(".jp2")) { + BufferedImage image = ImageIO.read(new File(filenameOrURL)); + PlanarImage pimage = PlanarImage.wrapRenderedImage(image); + return pimage.copyData().getSampleDouble(col, row, 0); - TIFFField[] tiffFields = new TIFFField[3]; - - // Cell size - tiffFields[0] = new TIFFField(GeoTiffConstants.ModelPixelScaleTag, TIFFField.TIFF_DOUBLE, 2, new double[]{cellSize.cellSizeX, cellSize.cellSizeY}); - - // No data - String noDataS = Double.toString(noData); - byte[] bytes = noDataS.getBytes(); - tiffFields[1] = new TIFFField(TiffTags.TIFFTAG_GDAL_NODATA, TIFFField.TIFF_BYTE, noDataS.length(), bytes); + } else if (filenameOrURL.toLowerCase().endsWith(".flt")) { - // Tie point - tiffFields[2] = new TIFFField(GeoTiffConstants.ModelTiepointTag, TIFFField.TIFF_DOUBLE, 6, - new double[]{0, 0, 0, envelope.getMinX(), envelope.getMaxY(), 0}); - - param.setExtraFields(tiffFields); + GridFloat gf = new GridFloat(filenameOrURL); + return gf.readCellVal(col, row); - FileOutputStream tifOut = new FileOutputStream(outFile); - TIFFImageEncoder encoder = (TIFFImageEncoder) TIFFCodec.createImageEncoder("tiff", tifOut, param); - encoder.encode(image); - tifOut.close(); + } else if (filenameOrURL.toLowerCase().endsWith(".asc") + || filenameOrURL.toLowerCase().endsWith(".txt")) { - WorldFileHandler worldFileHandler = new WorldFileHandler(outFile.getAbsolutePath(), false); - worldFileHandler.writeWorldFile(envelope, image.getWidth(), image.getHeight()); - - } - - public static Resolution calcRequestedResolution(Viewport viewport) { - - double xRes = viewport.getEnvelopeInModelCoordinates().getWidth() / (double) viewport.getPanel().getVisibleRect().width; - double yRes = viewport.getEnvelopeInModelCoordinates().getHeight() / (double) viewport.getPanel().getVisibleRect().height; - - Resolution requestedRes = new Resolution(xRes, yRes); - return requestedRes; - } - - public class CellSizeXY { + GridAscii ga = new GridAscii(filenameOrURL); + return ga.readCellValue(col, row); - public CellSizeXY(double cellSizeX, double cellSizeY) { - this.cellSizeX = cellSizeX; - this.cellSizeY = cellSizeY; - } + } + return null; - public double getCellSizeX() { - return cellSizeX; - } + } - public double getCellSizeY() { - return cellSizeY; - } - - public double getAverageCellSize() { - return (cellSizeX + cellSizeY) * 0.5; - } - - private final double cellSizeX; - private final double cellSizeY; - - } - + public static Point getImageDimensions(String filenameOrURL) + throws IOException { + + if (!filenameOrURL.toLowerCase().endsWith(".jpg") + && !filenameOrURL.toLowerCase().endsWith(".flt") + && !filenameOrURL.toLowerCase().endsWith(".asc") + && !filenameOrURL.toLowerCase().endsWith(".txt") + && !filenameOrURL.toLowerCase().endsWith(".jp2")) { + + javax.media.jai.PlanarImage pImage = javax.media.jai.JAI.create( + "fileload", filenameOrURL); + if (pImage != null) { + return new Point(pImage.getWidth(), pImage.getHeight()); + } + + } else if (filenameOrURL.toLowerCase().endsWith(".flt")) { + + GridFloat gf = new GridFloat(filenameOrURL); + return new Point(gf.getnCols(), gf.getnRows()); + + } else if (filenameOrURL.toLowerCase().endsWith(".asc") + || filenameOrURL.toLowerCase().endsWith(".txt")) { + + GridAscii ga = new GridAscii(filenameOrURL); + return new Point(ga.getnCols(), ga.getnRows()); + + } else { + + BufferedImage image = ImageIO.read(new File(filenameOrURL)); + return new Point(image.getWidth(), image.getHeight()); + + } + + return null; + } + + public static Envelope getGeoReferencing(String fileName, + boolean allwaysLookForTFWExtension, Point imageDimensions) + throws IOException, NoninvertibleTransformException, Exception { + + Envelope env = null; + + WorldFileHandler worldFileHandler = new WorldFileHandler(fileName, + allwaysLookForTFWExtension); + + if (imageDimensions == null) { + // logger.printError("can not determine image dimensions"); + // context.getWorkbench().getFrame().warnUser(I18N.get("org.openjump.core.rasterimage.AddRasterImageLayerWizard.can-not-determine-image-dimensions")); + // return null; + throw new Exception( + I18N.get("org.openjump.core.rasterimage.AddRasterImageLayerWizard.can-not-determine-image-dimensions")); + } + + if (worldFileHandler.isWorldFileExistentForImage() != null) { + // logger.printDebug(PirolPlugInMessages.getString("worldfile-found")); + env = worldFileHandler.readWorldFile(imageDimensions.x, + imageDimensions.y); + } + + if (env == null) { + + boolean isGeoTiff = false; + + if (fileName.toLowerCase().endsWith(".tif") + || fileName.toLowerCase().endsWith(".tiff")) { + // logger.printDebug("checking for GeoTIFF"); + + Coordinate tiePoint = null, pixelOffset = null, pixelScale = null; + double[] doubles; + + FileSeekableStream fileSeekableStream = new FileSeekableStream( + fileName); + TIFFDirectory tiffDirectory = new TIFFDirectory( + fileSeekableStream, 0); + + TIFFField[] availTags = tiffDirectory.getFields(); + + for (TIFFField availTag : availTags) { + if (availTag.getTag() == GeoTiffConstants.ModelTiepointTag) { + doubles = availTag.getAsDoubles(); + if (doubles.length != 6) { + // logger.printError("unsupported value for ModelTiepointTag (" + // + GeoTiffConstants.ModelTiepointTag + ")"); + // context.getWorkbench().getFrame().warnUser("unsupported value for ModelTiepointTag (" + // + GeoTiffConstants.ModelTiepointTag + ")"); + // break; + throw new Exception( + "unsupported value for ModelTiepointTag (" + + GeoTiffConstants.ModelTiepointTag + + ")"); + } + if (doubles[0] != 0 || doubles[1] != 0 + || doubles[2] != 0) { + if (doubles[2] == 0) + pixelOffset = new Coordinate(doubles[0], + doubles[1]); + else + pixelOffset = new Coordinate(doubles[0], + doubles[1], doubles[2]); + } + if (doubles[5] == 0) + tiePoint = new Coordinate(doubles[3], doubles[4]); + else + tiePoint = new Coordinate(doubles[3], doubles[4], + doubles[5]); + + // logger.printDebug("ModelTiepointTag (po): " + + // pixelOffset); + // logger.printDebug("ModelTiepointTag (tp): " + + // tiePoint); + } else if (availTag.getTag() == GeoTiffConstants.ModelPixelScaleTag) { + // Karteneinheiten pro pixel x bzw. y + doubles = availTag.getAsDoubles(); + if (doubles[2] == 0) + pixelScale = new Coordinate(doubles[0], doubles[1]); + else + pixelScale = new Coordinate(doubles[0], doubles[1], + doubles[2]); + + // logger.printDebug("ModelPixelScaleTag (ps): " + + // pixelScale); + } else { + // logger.printDebug("tiff field: " + + // availTags[i].getType() + ", "+ availTags[i].getTag() + // + ", "+ availTags[i].getCount()); + } + } + + fileSeekableStream.close(); + + if (tiePoint != null && pixelScale != null) { + isGeoTiff = true; + Coordinate upperLeft; + Coordinate lowerRight; + + if (pixelOffset == null) { + upperLeft = tiePoint; + } else { + upperLeft = new Coordinate(tiePoint.x + - (pixelOffset.x * pixelScale.x), tiePoint.y + - (pixelOffset.y * pixelScale.y)); + } + + lowerRight = new Coordinate(upperLeft.x + + (imageDimensions.x * pixelScale.x), upperLeft.y + - (imageDimensions.y * pixelScale.y)); + + // logger.printDebug("upperLeft: " + upperLeft); + // logger.printDebug("lowerRight: " + lowerRight); + + env = new Envelope(upperLeft, lowerRight); + } + + } else if (fileName.toLowerCase().endsWith(".flt")) { + isGeoTiff = true; + GridFloat gf = new GridFloat(fileName); + + Coordinate upperLeft = new Coordinate(gf.getXllCorner(), + gf.getYllCorner() + gf.getnRows() * gf.getCellSize()); + Coordinate lowerRight = new Coordinate(gf.getXllCorner() + + gf.getnCols() * gf.getCellSize(), gf.getYllCorner()); + + env = new Envelope(upperLeft, lowerRight); + + } else if (fileName.toLowerCase().endsWith(".asc") + || fileName.toLowerCase().endsWith(".txt")) { + isGeoTiff = true; + GridAscii ga = new GridAscii(fileName); + + Coordinate upperLeft = new Coordinate(ga.getXllCorner(), + ga.getYllCorner() + ga.getnRows() * ga.getCellSize()); + Coordinate lowerRight = new Coordinate(ga.getXllCorner() + + ga.getnCols() * ga.getCellSize(), ga.getYllCorner()); + + env = new Envelope(upperLeft, lowerRight); + } + + if (!isGeoTiff || env == null) { + // logger.printDebug(PirolPlugInMessages.getString("no-worldfile-found")); + // context.getWorkbench().getFrame().warnUser(I18N.get("org.openjump.core.rasterimage.AddRasterImageLayerWizard.no-worldfile-found")); + throw new Exception( + I18N.get("org.openjump.core.rasterimage.AddRasterImageLayerWizard.no-worldfile-found")); + // WizardDialog d = new WizardDialog( + // context.getWorkbench().getFrame(), + // I18N.get("RasterImagePlugIn.34") + // + worldFileHandler.getWorldFileName() + // + I18N.get("RasterImagePlugIn.35"), + // context.getErrorHandler()); + // d.init(new WizardPanel[] { new RasterImageWizardPanel() }); + // //Set size after #init, because #init calls #pack. [Jon + // Aquino] + // d.setSize(500, 400); + // GUIUtil.centreOnWindow(d); + // d.setVisible(true); + // + // if (!d.wasFinishPressed()) { + // //logger.printWarning("user canceled"); + // return null; + // } + // try { + // minx = Double.parseDouble((String) d + // .getData(RasterImageWizardPanel.MINX_KEY)); + // maxx = Double.parseDouble((String) d + // .getData(RasterImageWizardPanel.MAXX_KEY)); + // miny = Double.parseDouble((String) d + // .getData(RasterImageWizardPanel.MINY_KEY)); + // maxy = Double.parseDouble((String) d + // .getData(RasterImageWizardPanel.MAXY_KEY)); + // + // env = new Envelope(minx, maxx, miny, maxy); + // } + // catch(java.lang.NumberFormatException e) { + // Viewport viewport = + // context.getLayerViewPanel().getViewport(); + // Rectangle visibleRect = viewport.getPanel().getVisibleRect(); + // + // int visibleX1 = visibleRect.x; + // int visibleY1 = visibleRect.y; + // int visibleX2 = visibleX1 + visibleRect.width; + // int visibleY2 = visibleY1 + visibleRect.height; + // Coordinate upperLeftVisible = viewport.toModelCoordinate(new + // Point(0,0)); + // Coordinate lowerRightVisible = viewport.toModelCoordinate(new + // Point(visibleX2, visibleY2)); + // env = new Envelope(upperLeftVisible.x, lowerRightVisible.x, + // upperLeftVisible.y, lowerRightVisible.y); + // } + // + // } + // + // // creating world file + // worldFileHandler = new WorldFileHandler(fileName, + // allwaysLookForTFWExtension); + // worldFileHandler.writeWorldFile(env, imageDimensions.x, + // imageDimensions.y); + } + } + return env; + } + + public static CellSizeXY getCellSize(String fileNameOrURL) + throws NoninvertibleTransformException, Exception { + + Point imageDims = getImageDimensions(fileNameOrURL); + + Envelope envelope = getGeoReferencing(fileNameOrURL, true, new Point( + imageDims.x, imageDims.y)); + double cellSizeX = (envelope.getMaxX() - envelope.getMinX()) + / imageDims.x; + double cellSizeY = (envelope.getMaxY() - envelope.getMinY()) + / imageDims.y; + + return new RasterImageIO().new CellSizeXY(cellSizeX, cellSizeY); + + } + + public static Double getNoData(String fileNameOrURL) throws IOException, + ImageReadException { + + if (fileNameOrURL.toLowerCase().endsWith(".asc") + || fileNameOrURL.toLowerCase().endsWith(".txt")) { + + GridAscii gridAscii = new GridAscii(fileNameOrURL); + gridAscii.readHeader(); + return gridAscii.getNoData(); + + } else if (fileNameOrURL.toLowerCase().endsWith(".flt")) { + + GridFloat gf = new GridFloat(fileNameOrURL); + return gf.getNoData(); + + } else if (fileNameOrURL.toLowerCase().endsWith(".tif")) { + + TiffField field = TiffTags + .readField(new File(fileNameOrURL), 42113); + if (field.getFieldType() == FieldType.DOUBLE) { + return field.getDoubleValue(); + } else if (field.getFieldType() == FieldType.FLOAT) { + return field.getDoubleValue(); + } else if (field.getFieldType() == FieldType.ASCII) { + return Double.parseDouble(field.getStringValue()); + } + + } + return null; + + } + + public static Envelope getViewingEnvelope(Viewport viewport) + throws NoninvertibleTransformException { + + Rectangle visible = viewport.getPanel().getVisibleRect(); + int visibleX1 = visible.x; + int visibleY1 = visible.y; + int visibleX2 = visibleX1 + visible.width; + int visibleY2 = visibleY1 + visible.height; + + Coordinate upperLeftVisible = viewport.toModelCoordinate(new Point( + visibleX1, visibleY1)); + Coordinate lowerRightVisible = viewport.toModelCoordinate(new Point( + visibleX2, visibleY2)); + + return new Envelope(upperLeftVisible, lowerRightVisible); + + } + + public static Rectangle getDrawingRectangle(int imgWidth, int imgHeight, + Envelope wholeImageEnvelope, Envelope viewportEnvelope, + Resolution subsetResolution) throws NoninvertibleTransformException { + + if (viewportEnvelope == null + || viewportEnvelope.contains(wholeImageEnvelope)) { + Rectangle rect = new Rectangle(0, 0, imgWidth, imgHeight); + return rect; + } else if (viewportEnvelope.intersects(wholeImageEnvelope)) { + + Coordinate upperLeftVisible = new Coordinate( + viewportEnvelope.getMinX(), viewportEnvelope.getMaxY()); + Coordinate lowerRightVisible = new Coordinate( + viewportEnvelope.getMaxX(), viewportEnvelope.getMinY()); + + java.awt.Point upperLeft = fromCoordinateToCell(upperLeftVisible, + new Coordinate(wholeImageEnvelope.getMinX(), + wholeImageEnvelope.getMinY()), (int) imgHeight, + subsetResolution.getX(), subsetResolution.getY()); + java.awt.Point lowerRight = fromCoordinateToCell(lowerRightVisible, + new Coordinate(wholeImageEnvelope.getMinX(), + wholeImageEnvelope.getMinY()), (int) imgHeight, + subsetResolution.getX(), subsetResolution.getY()); + + int xOffset = Math.max(0, upperLeft.x); + int yOffset = Math.max(0, upperLeft.y); + + int width = lowerRight.x - upperLeft.x; + int height = lowerRight.y - upperLeft.y; + + Rectangle rect = new Rectangle(xOffset, yOffset, width, height); + return rect; + } else { + return null; + } + + } + + protected Rectangle getVisibleImageCoordinatesOfImage(double imgWidth, + double imgHeight, Envelope visible, Envelope imageEnv) { + + double minVisibleX = Math.max(visible.getMinX(), imageEnv.getMinX()); + double minVisibleY = Math.max(visible.getMinY(), imageEnv.getMinY()); + + double maxVisibleX = Math.min(visible.getMaxX(), imageEnv.getMaxX()); + double maxVisibleY = Math.min(visible.getMaxY(), imageEnv.getMaxY()); + + double offset2VisibleX = imageEnv.getMinX() - visible.getMinX(); + double offset2VisibleY = visible.getMaxY() - imageEnv.getMaxY(); + + double scaleX = imgWidth / imageEnv.getWidth(); + double scaleY = imgHeight / imageEnv.getHeight(); + + int xOffset; + int yOffset; + + if (offset2VisibleX >= 0) { + xOffset = 0; + } else { + xOffset = (int) (-offset2VisibleX * scaleX); + } + + if (offset2VisibleY >= 0) { + yOffset = 0; + } else { + yOffset = (int) (-offset2VisibleY * scaleY); + } + + int width = (int) ((maxVisibleX - minVisibleX) * scaleX); + int height = (int) ((maxVisibleY - minVisibleY) * scaleY); + + if (width < imgWidth && height < imgHeight) { + width += 1; + height += 1; + } + + if (width <= 0 || height <= 0) { + return null; + } + + return new Rectangle(xOffset, yOffset, width, height); + } + + public static java.awt.Point fromCoordinateToCell(Coordinate coord, + Coordinate lowerLeftCoord, int rowCount, double cellSizeX, + double cellSizeY) { + + java.awt.Point point = new java.awt.Point(); + point.x = (int) Math.floor((coord.x - lowerLeftCoord.x) / cellSizeX); + point.y = rowCount + - (int) Math.floor((coord.y - lowerLeftCoord.y) / cellSizeY) + - 1; + + return point; + + } + + public static Coordinate fromCellToCoordinate(java.awt.Point cell, + Coordinate lowerLeftCoord, double cellSize, int rowCount) { + + Coordinate coord = new Coordinate(); + coord.x = lowerLeftCoord.x + cell.x * cellSize + 0.5 * cellSize; + coord.y = lowerLeftCoord.y + (rowCount - cell.y) * cellSize - 0.5 + * cellSize; + return coord; + + } + + public void writeImage(File outFile, Raster raster, Envelope envelope, + CellSizeXY cellSize, double noData) throws FileNotFoundException, + IOException { + + // Delete old .xml.aux statistics file + File auxXmlFile = new File(outFile.getParent(), outFile.getName() + + ".aux.xml"); + if (auxXmlFile.exists() && auxXmlFile.canWrite()) { + try { + auxXmlFile.delete(); + } catch (Exception ex) { + ex.printStackTrace(System.out); + } + } + + SampleModel sm = raster.getSampleModel(); + ColorModel colorModel = PlanarImage.createColorModel(sm); + BufferedImage image = new BufferedImage(colorModel, + (WritableRaster) raster, false, null); + + TIFFEncodeParam param = new TIFFEncodeParam(); + param.setCompression(TIFFEncodeParam.COMPRESSION_NONE); + + TIFFField[] tiffFields = new TIFFField[3]; + + // Cell size + tiffFields[0] = new TIFFField(GeoTiffConstants.ModelPixelScaleTag, + TIFFField.TIFF_DOUBLE, 2, new double[] { cellSize.cellSizeX, + cellSize.cellSizeY }); + + // No data + String noDataS = Double.toString(noData); + byte[] bytes = noDataS.getBytes(); + tiffFields[1] = new TIFFField(TiffTags.TIFFTAG_GDAL_NODATA, + TIFFField.TIFF_BYTE, noDataS.length(), bytes); + + // Tie point + tiffFields[2] = new TIFFField(GeoTiffConstants.ModelTiepointTag, + TIFFField.TIFF_DOUBLE, 6, new double[] { 0, 0, 0, + envelope.getMinX(), envelope.getMaxY(), 0 }); + + param.setExtraFields(tiffFields); + + FileOutputStream tifOut = new FileOutputStream(outFile); + TIFFImageEncoder encoder = (TIFFImageEncoder) TIFFCodec + .createImageEncoder("tiff", tifOut, param); + encoder.encode(image); + tifOut.close(); + + WorldFileHandler worldFileHandler = new WorldFileHandler( + outFile.getAbsolutePath(), false); + worldFileHandler.writeWorldFile(envelope, image.getWidth(), + image.getHeight()); + + } + + public static Resolution calcRequestedResolution(Viewport viewport) { + + double xRes = viewport.getEnvelopeInModelCoordinates().getWidth() + / (double) viewport.getPanel().getVisibleRect().width; + double yRes = viewport.getEnvelopeInModelCoordinates().getHeight() + / (double) viewport.getPanel().getVisibleRect().height; + + Resolution requestedRes = new Resolution(xRes, yRes); + return requestedRes; + } + + public class CellSizeXY { + + public CellSizeXY(double cellSizeX, double cellSizeY) { + this.cellSizeX = cellSizeX; + this.cellSizeY = cellSizeY; + } + + public double getCellSizeX() { + return cellSizeX; + } + + public double getCellSizeY() { + return cellSizeY; + } + + public double getAverageCellSize() { + return (cellSizeX + cellSizeY) * 0.5; + } + + private final double cellSizeX; + private final double cellSizeY; + + } + } Modified: core/trunk/src/org/openjump/core/rasterimage/SelectRasterImageFilesPanel.java =================================================================== --- core/trunk/src/org/openjump/core/rasterimage/SelectRasterImageFilesPanel.java 2016-03-25 21:29:42 UTC (rev 4864) +++ core/trunk/src/org/openjump/core/rasterimage/SelectRasterImageFilesPanel.java 2016-03-26 11:07:43 UTC (rev 4865) @@ -49,107 +49,119 @@ import com.vividsolutions.jump.workbench.ui.wizard.WizardDialog; import com.vividsolutions.jump.workbench.ui.wizard.WizardPanel; -public class SelectRasterImageFilesPanel extends JFCWithEnterAction implements WizardPanel { +public class SelectRasterImageFilesPanel extends JFCWithEnterAction implements + WizardPanel { - public static final String KEY = SelectRasterImageFilesPanel.class.getName(); - - public static final String FILE_CHOOSER_DIRECTORY_KEY = KEY + " - FILE CHOOSER DIRECTORY"; + public static final String KEY = SelectRasterImageFilesPanel.class + .getName(); - public static final String TITLE = I18N.get("org.openjump.core.rasterimage.SelectRasterImageFilesPanel.Select-Raster-Image"); + public static final String FILE_CHOOSER_DIRECTORY_KEY = KEY + + " - FILE CHOOSER DIRECTORY"; - public static final String INSTRUCTIONS = I18N.get("org.openjump.core.ui.plugin.file.open.SelectFileOptionsPanel.instructions"); + public static final String TITLE = I18N + .get("org.openjump.core.rasterimage.SelectRasterImageFilesPanel.Select-Raster-Image"); - public static final String ALL_FILES = I18N.get("org.openjump.core.ui.plugin.file.open.SelectFilesPanel.all-files"); + public static final String INSTRUCTIONS = I18N + .get("org.openjump.core.ui.plugin.file.open.SelectFileOptionsPanel.instructions"); - private Set<InputChangedListener> listeners = new LinkedHashSet<InputChangedListener>(); + public static final String ALL_FILES = I18N + .get("org.openjump.core.ui.plugin.file.open.SelectFilesPanel.all-files"); - private Blackboard blackboard; - private ActionListener dialogActionListener; + private Set<InputChangedListener> listeners = new LinkedHashSet<InputChangedListener>(); - public SelectRasterImageFilesPanel(final WorkbenchContext context) { - setDialogType(JFileChooser.OPEN_DIALOG); - - if (PersistentBlackboardPlugIn.get(context).get(FILE_CHOOSER_DIRECTORY_KEY) != null) { - setCurrentDirectory(new File((String)PersistentBlackboardPlugIn - .get(context) - .get(FILE_CHOOSER_DIRECTORY_KEY))); - } - - setFileSelectionMode(JFileChooser.FILES_ONLY); - setMultiSelectionEnabled(true); - GUIUtil.removeChoosableFileFilters(this); - /* - FileFilter GEOTIFF_FILE_FILTER = GUIUtil.createFileFilter("GeoTIFF", new String[]{ "tif", "tiff" }); - FileFilter GIF_FILE_FILTER = GUIUtil.createFileFilter("GIF", new String[]{ "gif"}); - FileFilter JPG_FILE_FILTER = GUIUtil.createFileFilter("JPEG", new String[]{ "jpg"}); - FileFilter PNG_FILE_FILTER = GUIUtil.createFileFilter("PNG", new String[]{ "png"}); - addChoosableFileFilter(GEOTIFF_FILE_FILTER); - addChoosableFileFilter(GIF_FILE_FILTER); - addChoosableFileFilter(JPG_FILE_FILTER); - addChoosableFileFilter(PNG_FILE_FILTER); - */ - FileFilter JAI_IMAGE_FILE_FILTER = GUIUtil.createFileFilter(I18N.get("org.openjump.core.rasterimage.SelectRasterImageFilesPanel.supported-raster-image-formats"), - new String[]{ "tif", "tiff", "gif", "jpg", "png", "flt", "asc", "txt" }); - addChoosableFileFilter(JAI_IMAGE_FILE_FILTER); - addChoosableFileFilter(GUIUtil.ALL_FILES_FILTER); - - setFileFilter(JAI_IMAGE_FILE_FILTER); + private Blackboard blackboard; + private ActionListener dialogActionListener; - setControlButtonsAreShown(false); + public SelectRasterImageFilesPanel(final WorkbenchContext context) { + setDialogType(JFileChooser.OPEN_DIALOG); - addPropertyChangeListener(new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent evt) { - PersistentBlackboardPlugIn.get(context) - .put(FILE_CHOOSER_DIRECTORY_KEY, getCurrentDirectory().toString()); - fireInputChanged(); - } - }); - } + if (PersistentBlackboardPlugIn.get(context).get( + FILE_CHOOSER_DIRECTORY_KEY) != null) { + setCurrentDirectory(new File((String) PersistentBlackboardPlugIn + .get(context).get(FILE_CHOOSER_DIRECTORY_KEY))); + } - public void setDialog(WizardDialog dialog) { - removeActionListener(dialogActionListener); - dialogActionListener = new InvokeMethodActionListener(dialog, "next"); - addActionListener(dialogActionListener); - - } - public void enteredFromLeft(final Map dataMap) { - rescanCurrentDirectory(); - } + setFileSelectionMode(JFileChooser.FILES_ONLY); + setMultiSelectionEnabled(true); + GUIUtil.removeChoosableFileFilters(this); + /* + * FileFilter GEOTIFF_FILE_FILTER = GUIUtil.createFileFilter("GeoTIFF", + * new String[]{ "tif", "tiff" }); FileFilter GIF_FILE_FILTER = + * GUIUtil.createFileFilter("GIF", new String[]{ "gif"}); FileFilter + * JPG_FILE_FILTER = GUIUtil.createFileFilter("JPEG", new String[]{ + * "jpg"}); FileFilter PNG_FILE_FILTER = GUIUtil.createFileFilter("PNG", + * new String[]{ "png"}); addChoosableFileFilter(GEOTIFF_FILE_FILTER); + * addChoosableFileFilter(GIF_FILE_FILTER); + * addChoosableFileFilter(JPG_FILE_FILTER); + * addChoosableFileFilter(PNG_FILE_FILTER); + */ + FileFilter JAI_IMAGE_FILE_FILTER = GUIUtil + .createFileFilter( + I18N.get("org.openjump.core.rasterimage.SelectRasterImageFilesPanel.supported-raster-image-formats"), + new String[] { "tif", "tiff", "gif", "jpg", "jp2", + "png", "flt", "bmp", "asc", "txt" }); + addChoosableFileFilter(JAI_IMAGE_FILE_FILTER); + addChoosableFileFilter(GUIUtil.ALL_FILES_FILTER); - public void exitingToRight() throws Exception { - } + setFileFilter(JAI_IMAGE_FILE_FILTER); - public String getID() { - return getClass().getName(); - } + setControlButtonsAreShown(false); - public String getInstructions() { - return INSTRUCTIONS; - } + addPropertyChangeListener(new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent evt) { + PersistentBlackboardPlugIn.get(context).put( + FILE_CHOOSER_DIRECTORY_KEY, + getCurrentDirectory().toString()); + fireInputChanged(); + } + }); + } - public String getNextID() { - return null; - } + public void setDialog(WizardDialog dialog) { + removeActionListener(dialogActionListener); + dialogActionListener = new InvokeMethodActionListener(dialog, "next"); + addActionListener(dialogActionListener); - public String getTitle() { - return TITLE; - } + } - public boolean isInputValid() { - return getSelectedFile() != null; - } + public void enteredFromLeft(final Map dataMap) { + rescanCurrentDirectory(); + } - public void add(InputChangedListener listener) { - listeners.add(listener); - } + public void exitingToRight() throws Exception { + } - public void remove(InputChangedListener listener) { - listeners.remove(listener); - } + public String getID() { + return getClass().getName(); + } - private void fireInputChanged() { - for (InputChangedListener listener : listeners) { - listener.inputChanged(); - } - } + public String getInstructions() { + return INSTRUCTIONS; + } + + public String getNextID() { + return null; + } + + public String getTitle() { + return TITLE; + } + + public boolean isInputValid() { + return getSelectedFile() != null; + } + + public void add(InputChangedListener listener) { + listeners.add(listener); + } + + public void remove(InputChangedListener listener) { + listeners.remove(listener); + } + + private void fireInputChanged() { + for (InputChangedListener listener : listeners) { + listener.inputChanged(); + } + } } ------------------------------------------------------------------------------ Transform Data into Opportunity. Accelerate data analysis in your applications with Intel Data Analytics Acceleration Library. Click to learn more. http://pubads.g.doubleclick.net/gampad/clk?id=278785351&iu=/4140 _______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel