@Test public void testWriteCroppedCoverage() throws IllegalArgumentException, IOException, UnsupportedOperationException, ParseException, FactoryException, TransformException { final File readdir = TestData.file(GeoTiffWriterTest.class, ""); final File writedir = new File(new StringBuilder(readdir.getAbsolutePath()).append( "/testWriter/").toString()); writedir.mkdir(); final File tiff = new File(readdir, "latlon.tiff"); assert tiff.exists() && tiff.canRead() && tiff.isFile(); if (TestData.isInteractiveTest()) logger.info(tiff.getAbsolutePath()); final GeoTiffFormat format = new GeoTiffFormat(); GridCoverageReader reader = format.getReader(tiff); assertNotNull(reader); GridCoverage2D gc = (GridCoverage2D) reader.read(null); if (TestData.isInteractiveTest()) { logger.info(new StringBuilder("Coverage before: ").append("\n") .append(gc.getCoordinateReferenceSystem().toWKT()) .append(gc.getEnvelope().toString()).toString()); } final CoordinateReferenceSystem sourceCRS = gc.getCoordinateReferenceSystem2D(); final GeneralEnvelope sourceEnvelope = (GeneralEnvelope) gc.getEnvelope(); final GridGeometry2D sourcedGG = (GridGeometry2D) gc.getGridGeometry(); final MathTransform sourceG2W = sourcedGG.getGridToCRS(PixelInCell.CELL_CENTER); double xc = sourceEnvelope.getMedian(0); double yc = sourceEnvelope.getMedian(1); double xl = sourceEnvelope.getSpan(0); double yl = sourceEnvelope.getSpan(1); final GeneralEnvelope cropEnvelope = new GeneralEnvelope(new double[] { xc - xl / 4.0, yc - yl / 4.0 }, new double[] { xc + xl / 4.0, yc + yl / 4.0 }); final CoverageProcessor processor = new CoverageProcessor(); final ParameterValueGroup param = processor.getOperation("CoverageCrop").getParameters(); param.parameter("Source").setValue(gc); param.parameter("Envelope").setValue(cropEnvelope); final GridCoverage2D cropped = (GridCoverage2D) processor.doOperation(param); final GridGeometry2D croppedGG = (GridGeometry2D) cropped.getGridGeometry(); final GridEnvelope2D croppedGR = (GridEnvelope2D) croppedGG.getGridRange(); final MathTransform croppedG2W = croppedGG.getGridToCRS(PixelInCell.CELL_CENTER); final GeneralEnvelope croppedEnvelope = (GeneralEnvelope) cropped.getEnvelope(); assertTrue("min x do not match after crop", 29 == croppedGR.x); assertTrue("min y do not match after crop", 30 == croppedGR.y); assertTrue("max x do not match after crop", 90 == croppedGR.getMaxX()); assertTrue("max y do not match after crop", 91 == croppedGR.getMaxY()); assertTrue("The Grdi2World tranformations of the original and the cropped covearage do not match", sourceG2W.equals(croppedG2W)); final GeneralEnvelope expectedEnvelope = new GeneralEnvelope(croppedGR, PixelInCell.CELL_CENTER, croppedG2W, cropped.getCoordinateReferenceSystem2D()); assertTrue("Expected envelope is different from the computed one", expectedEnvelope.equals(croppedEnvelope, XAffineTransform.getScale((AffineTransform) croppedG2W) / 2.0, false)); final File writeFile = new File(writedir.getAbsolutePath() + File.separatorChar + cropped.getName().toString() + ".tiff"); final GridCoverageWriter writer = format.getWriter(writeFile); try { writer.write(cropped, null); } catch (IOException e) { } finally { try { writer.dispose(); } catch (Throwable e) { } } cropped.dispose(true); gc.dispose(true); if (reader != null) { try { reader.dispose(); } catch (Throwable e) { } } try { reader = new GeoTiffReader(writeFile, null); assertNotNull(reader); gc = (GridCoverage2D) reader.read(null); assertNotNull(gc); final CoordinateReferenceSystem targetCRS = gc.getCoordinateReferenceSystem2D(); assertTrue("Source and Target coordinate reference systems do not match", CRS.equalsIgnoreMetadata(sourceCRS, targetCRS)); assertEquals("Read-back and Cropped envelopes do not match", cropped.getEnvelope(), croppedEnvelope); if (TestData.isInteractiveTest()) { logger.info(new StringBuilder("Coverage after: ").append("\n") .append(gc.getCoordinateReferenceSystem().toWKT()) .append(gc.getEnvelope().toString()).toString()); gc.show(); } else { gc.getRenderedImage().getData(); } } finally { if (reader != null) { try { reader.dispose(); } catch (Throwable e) { } } if (!TestData.isInteractiveTest()) { gc.dispose(true); } } }
public void write(final GridCoverage gc, final GeneralParameterValue[] params) throws IllegalArgumentException, IOException, IndexOutOfBoundsException { GeoToolsWriteParams gtParams = null; boolean writeTfw = GeoTiffFormat.WRITE_TFW.getDefaultValue(); if (params != null) { if (params != null) { Parameter param; final int length = params.length; for (int i = 0; i < length; i++) { param = (Parameter) params[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName())) { gtParams = (GeoToolsWriteParams) param.getValue(); } if (name.equals(GeoTiffFormat.WRITE_TFW.getName())) { writeTfw = (Boolean) param.getValue(); } } } } if (gtParams == null) gtParams = new GeoTiffWriteParams(); final GridGeometry2D gg = (GridGeometry2D) gc.getGridGeometry(); GridEnvelope2D range = gg.getGridRange2D(); final Rectangle sourceRegion = gtParams.getSourceRegion(); if (sourceRegion != null) range = new GridEnvelope2D(sourceRegion); final AffineTransform tr = (AffineTransform) gg.getGridToCRS2D(); final CoordinateReferenceSystem crs = gg.getCoordinateReferenceSystem2D(); final double inNoData = getCandidateNoData(gc); if (crs instanceof ProjectedCRS || crs instanceof GeographicCRS) { final CRS2GeoTiffMetadataAdapter adapter = new CRS2GeoTiffMetadataAdapter(crs); final GeoTiffIIOMetadataEncoder metadata = adapter.parseCoordinateReferenceSystem(); if (!Double.isNaN(inNoData)) metadata.setNoData(inNoData); if (metadataKeyValue != null && !metadataKeyValue.isEmpty()) { metadata.setTiffTagsMetadata(metadataKeyValue); } setGeoReference(crs, metadata, tr, range); writeImage(((GridCoverage2D) gc).geophysics(true).getRenderedImage(), this.outStream, metadata, gtParams); if (writeTfw && (destination instanceof File)) { final File destFile = (File) this.destination; final File tfw = new File(destFile.getParentFile(), destFile.getName().replace( "tif", "tfw")); final BufferedWriter outW = new BufferedWriter(new FileWriter(tfw)); try { outW.write(gc.getCoordinateReferenceSystem().toWKT()); } finally { try { outW.close(); } catch (Exception e) { } } } } else { throw new GeoTiffException( null, "The supplied grid coverage uses an unsupported crs! You are allowed to" + " use only projected and geographic coordinate reference systems", null); } }
GeoTiffWriter extends AbstractGridCoverageWriter implements
		GridCoverageWriter { public void write(final GridCoverage gc, final GeneralParameterValue[] params) throws IllegalArgumentException, IOException, IndexOutOfBoundsException { GeoToolsWriteParams gtParams = null; boolean writeTfw = GeoTiffFormat.WRITE_TFW.getDefaultValue(); if (params != null) { if (params != null) { Parameter param; final int length = params.length; for (int i = 0; i < length; i++) { param = (Parameter) params[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName())) { gtParams = (GeoToolsWriteParams) param.getValue(); } if (name.equals(GeoTiffFormat.WRITE_TFW.getName())) { writeTfw = (Boolean) param.getValue(); } } } } if (gtParams == null) gtParams = new GeoTiffWriteParams(); final GridGeometry2D gg = (GridGeometry2D) gc.getGridGeometry(); GridEnvelope2D range = gg.getGridRange2D(); final Rectangle sourceRegion = gtParams.getSourceRegion(); if (sourceRegion != null) range = new GridEnvelope2D(sourceRegion); final AffineTransform tr = (AffineTransform) gg.getGridToCRS2D(); final CoordinateReferenceSystem crs = gg.getCoordinateReferenceSystem2D(); final double inNoData = getCandidateNoData(gc); if (crs instanceof ProjectedCRS || crs instanceof GeographicCRS) { final CRS2GeoTiffMetadataAdapter adapter = new CRS2GeoTiffMetadataAdapter(crs); final GeoTiffIIOMetadataEncoder metadata = adapter.parseCoordinateReferenceSystem(); if (!Double.isNaN(inNoData)) metadata.setNoData(inNoData); if (metadataKeyValue != null && !metadataKeyValue.isEmpty()) { metadata.setTiffTagsMetadata(metadataKeyValue); } setGeoReference(crs, metadata, tr, range); writeImage(((GridCoverage2D) gc).geophysics(true).getRenderedImage(), this.outStream, metadata, gtParams); if (writeTfw && (destination instanceof File)) { final File destFile = (File) this.destination; final File tfw = new File(destFile.getParentFile(), destFile.getName().replace( "tif", "tfw")); final BufferedWriter outW = new BufferedWriter(new FileWriter(tfw)); try { outW.write(gc.getCoordinateReferenceSystem().toWKT()); } finally { try { outW.close(); } catch (Exception e) { } } } } else { throw new GeoTiffException( null, "The supplied grid coverage uses an unsupported crs! You are allowed to" + " use only projected and geographic coordinate reference systems", null); } } }
GeoTiffWriter extends AbstractGridCoverageWriter implements
		GridCoverageWriter { public void write(final GridCoverage gc, final GeneralParameterValue[] params) throws IllegalArgumentException, IOException, IndexOutOfBoundsException { GeoToolsWriteParams gtParams = null; boolean writeTfw = GeoTiffFormat.WRITE_TFW.getDefaultValue(); if (params != null) { if (params != null) { Parameter param; final int length = params.length; for (int i = 0; i < length; i++) { param = (Parameter) params[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName())) { gtParams = (GeoToolsWriteParams) param.getValue(); } if (name.equals(GeoTiffFormat.WRITE_TFW.getName())) { writeTfw = (Boolean) param.getValue(); } } } } if (gtParams == null) gtParams = new GeoTiffWriteParams(); final GridGeometry2D gg = (GridGeometry2D) gc.getGridGeometry(); GridEnvelope2D range = gg.getGridRange2D(); final Rectangle sourceRegion = gtParams.getSourceRegion(); if (sourceRegion != null) range = new GridEnvelope2D(sourceRegion); final AffineTransform tr = (AffineTransform) gg.getGridToCRS2D(); final CoordinateReferenceSystem crs = gg.getCoordinateReferenceSystem2D(); final double inNoData = getCandidateNoData(gc); if (crs instanceof ProjectedCRS || crs instanceof GeographicCRS) { final CRS2GeoTiffMetadataAdapter adapter = new CRS2GeoTiffMetadataAdapter(crs); final GeoTiffIIOMetadataEncoder metadata = adapter.parseCoordinateReferenceSystem(); if (!Double.isNaN(inNoData)) metadata.setNoData(inNoData); if (metadataKeyValue != null && !metadataKeyValue.isEmpty()) { metadata.setTiffTagsMetadata(metadataKeyValue); } setGeoReference(crs, metadata, tr, range); writeImage(((GridCoverage2D) gc).geophysics(true).getRenderedImage(), this.outStream, metadata, gtParams); if (writeTfw && (destination instanceof File)) { final File destFile = (File) this.destination; final File tfw = new File(destFile.getParentFile(), destFile.getName().replace( "tif", "tfw")); final BufferedWriter outW = new BufferedWriter(new FileWriter(tfw)); try { outW.write(gc.getCoordinateReferenceSystem().toWKT()); } finally { try { outW.close(); } catch (Exception e) { } } } } else { throw new GeoTiffException( null, "The supplied grid coverage uses an unsupported crs! You are allowed to" + " use only projected and geographic coordinate reference systems", null); } } GeoTiffWriter(Object destination);  GeoTiffWriter(Object destination, Hints hints); }
GeoTiffWriter extends AbstractGridCoverageWriter implements
		GridCoverageWriter { public void write(final GridCoverage gc, final GeneralParameterValue[] params) throws IllegalArgumentException, IOException, IndexOutOfBoundsException { GeoToolsWriteParams gtParams = null; boolean writeTfw = GeoTiffFormat.WRITE_TFW.getDefaultValue(); if (params != null) { if (params != null) { Parameter param; final int length = params.length; for (int i = 0; i < length; i++) { param = (Parameter) params[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName())) { gtParams = (GeoToolsWriteParams) param.getValue(); } if (name.equals(GeoTiffFormat.WRITE_TFW.getName())) { writeTfw = (Boolean) param.getValue(); } } } } if (gtParams == null) gtParams = new GeoTiffWriteParams(); final GridGeometry2D gg = (GridGeometry2D) gc.getGridGeometry(); GridEnvelope2D range = gg.getGridRange2D(); final Rectangle sourceRegion = gtParams.getSourceRegion(); if (sourceRegion != null) range = new GridEnvelope2D(sourceRegion); final AffineTransform tr = (AffineTransform) gg.getGridToCRS2D(); final CoordinateReferenceSystem crs = gg.getCoordinateReferenceSystem2D(); final double inNoData = getCandidateNoData(gc); if (crs instanceof ProjectedCRS || crs instanceof GeographicCRS) { final CRS2GeoTiffMetadataAdapter adapter = new CRS2GeoTiffMetadataAdapter(crs); final GeoTiffIIOMetadataEncoder metadata = adapter.parseCoordinateReferenceSystem(); if (!Double.isNaN(inNoData)) metadata.setNoData(inNoData); if (metadataKeyValue != null && !metadataKeyValue.isEmpty()) { metadata.setTiffTagsMetadata(metadataKeyValue); } setGeoReference(crs, metadata, tr, range); writeImage(((GridCoverage2D) gc).geophysics(true).getRenderedImage(), this.outStream, metadata, gtParams); if (writeTfw && (destination instanceof File)) { final File destFile = (File) this.destination; final File tfw = new File(destFile.getParentFile(), destFile.getName().replace( "tif", "tfw")); final BufferedWriter outW = new BufferedWriter(new FileWriter(tfw)); try { outW.write(gc.getCoordinateReferenceSystem().toWKT()); } finally { try { outW.close(); } catch (Exception e) { } } } } else { throw new GeoTiffException( null, "The supplied grid coverage uses an unsupported crs! You are allowed to" + " use only projected and geographic coordinate reference systems", null); } } GeoTiffWriter(Object destination);  GeoTiffWriter(Object destination, Hints hints); @Override void setMetadataValue(String name, String value); Format getFormat(); void write(final GridCoverage gc, final GeneralParameterValue[] params); }
GeoTiffWriter extends AbstractGridCoverageWriter implements
		GridCoverageWriter { public void write(final GridCoverage gc, final GeneralParameterValue[] params) throws IllegalArgumentException, IOException, IndexOutOfBoundsException { GeoToolsWriteParams gtParams = null; boolean writeTfw = GeoTiffFormat.WRITE_TFW.getDefaultValue(); if (params != null) { if (params != null) { Parameter param; final int length = params.length; for (int i = 0; i < length; i++) { param = (Parameter) params[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName())) { gtParams = (GeoToolsWriteParams) param.getValue(); } if (name.equals(GeoTiffFormat.WRITE_TFW.getName())) { writeTfw = (Boolean) param.getValue(); } } } } if (gtParams == null) gtParams = new GeoTiffWriteParams(); final GridGeometry2D gg = (GridGeometry2D) gc.getGridGeometry(); GridEnvelope2D range = gg.getGridRange2D(); final Rectangle sourceRegion = gtParams.getSourceRegion(); if (sourceRegion != null) range = new GridEnvelope2D(sourceRegion); final AffineTransform tr = (AffineTransform) gg.getGridToCRS2D(); final CoordinateReferenceSystem crs = gg.getCoordinateReferenceSystem2D(); final double inNoData = getCandidateNoData(gc); if (crs instanceof ProjectedCRS || crs instanceof GeographicCRS) { final CRS2GeoTiffMetadataAdapter adapter = new CRS2GeoTiffMetadataAdapter(crs); final GeoTiffIIOMetadataEncoder metadata = adapter.parseCoordinateReferenceSystem(); if (!Double.isNaN(inNoData)) metadata.setNoData(inNoData); if (metadataKeyValue != null && !metadataKeyValue.isEmpty()) { metadata.setTiffTagsMetadata(metadataKeyValue); } setGeoReference(crs, metadata, tr, range); writeImage(((GridCoverage2D) gc).geophysics(true).getRenderedImage(), this.outStream, metadata, gtParams); if (writeTfw && (destination instanceof File)) { final File destFile = (File) this.destination; final File tfw = new File(destFile.getParentFile(), destFile.getName().replace( "tif", "tfw")); final BufferedWriter outW = new BufferedWriter(new FileWriter(tfw)); try { outW.write(gc.getCoordinateReferenceSystem().toWKT()); } finally { try { outW.close(); } catch (Exception e) { } } } } else { throw new GeoTiffException( null, "The supplied grid coverage uses an unsupported crs! You are allowed to" + " use only projected and geographic coordinate reference systems", null); } } GeoTiffWriter(Object destination);  GeoTiffWriter(Object destination, Hints hints); @Override void setMetadataValue(String name, String value); Format getFormat(); void write(final GridCoverage gc, final GeneralParameterValue[] params);  }