Revision: 6540 http://sourceforge.net/p/jump-pilot/code/6540 Author: ma15569 Date: 2020-09-27 06:02:33 +0000 (Sun, 27 Sep 2020) Log Message: ----------- Added enhanced method to save the TIF with an external aux.xml file which contains statistics of raster and srs. Worldfile is not saved as geographic position of file are already stored into the TIF file. Statistics are calculated from raster
Modified Paths: -------------- core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java Modified: core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java =================================================================== --- core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java 2020-09-26 18:47:14 UTC (rev 6539) +++ core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java 2020-09-27 06:02:33 UTC (rev 6540) @@ -22,10 +22,15 @@ import javax.media.jai.PlanarImage; import javax.media.jai.RasterFactory; import javax.media.jai.RenderedOp; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; 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.ccordsys.utils.SRSInfo; +import org.xml.sax.SAXException; import com.sun.media.jai.codec.FileSeekableStream; import com.sun.media.jai.codec.TIFFDirectory; @@ -866,6 +871,104 @@ } + /** + * Enhanced method to save the TIF with an external aux.xml file + * which contains statistics of raster and srs. Worldfile is not saved as + * geographic position of file are already stored into the TIF file. + * Statistics are calculated from raster + * @param outFile + * @param raster + * @param envelope + * @param cellSizeX + * @param cellSizeY + * @param noData + * @param srsInfo + * @throws IOException + * @throws TransformerConfigurationException + * @throws ParserConfigurationException + * @throws TransformerException + * @throws SAXException + */ + + public void writeImage(File outFile, Raster raster, Envelope envelope, + double cellSizeX, double cellSizeY, double noData, SRSInfo srsInfo) + throws IOException, TransformerConfigurationException, ParserConfigurationException, TransformerException, SAXException { + + SampleModel sm = raster.getSampleModel(); + ColorModel colorModel = PlanarImage.createColorModel(sm); + BufferedImage bufferedImage = 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[] { cellSizeX, + 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(bufferedImage); + tifOut.close(); + int bandCount = bufferedImage.getRaster().getNumBands(); + double minValue[] = new double[bandCount]; + double maxValue[] = new double[bandCount]; + double sum[] = new double[bandCount]; + double sumSquare[] = new double[bandCount]; + long cellsCount[] = new long[bandCount]; + + for(int b=0; b<bandCount; b++) { + minValue[b] = Double.MAX_VALUE; + maxValue[b] = -Double.MAX_VALUE; + } + + for(int r=0; r<bufferedImage.getHeight(); r++) { + Raster raster2 = bufferedImage.getData(new Rectangle(0, r, bufferedImage.getWidth(), 1)); + for(int c=0; c<bufferedImage.getWidth(); c++) { + for(int b=0; b<bandCount; b++) { + double value = raster2.getSampleDouble(c, r, b); + if(value != noData && (float)value != (float)noData && + !Double.isNaN(value) && !Double.isInfinite(value)) { + if(value < minValue[b]) minValue[b] = value; + if(value > maxValue[b]) maxValue[b] = value; + cellsCount[b]++; + sum[b] += value; + sumSquare[b] += value * value; + } + } + } + } + Stats stats = new Stats(bandCount); + for(int b=0; b<bandCount; b++) { + double meanValue = sum[b] / cellsCount[b]; + double stdDevValue = Math.sqrt(sumSquare[b] / cellsCount[b] - meanValue * meanValue); + stats.setStatsForBand(b, minValue[b], maxValue[b], meanValue, stdDevValue); + } + File auxXmlFile = new File(outFile.getParent(), outFile.getName() + + ".aux.xml"); + GDALPamDataset gPam = new GDALPamDataset(); + gPam.writeStatisticsAndSRS(auxXmlFile, srsInfo, stats); + } + + + public static Resolution calcRequestedResolution(Viewport viewport) { double xRes = viewport.getEnvelopeInModelCoordinates().getWidth() _______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel