@Test public void testWriteCoverage() throws IOException, InvalidRangeException { String gridOut = tempFolder.newFile().getAbsolutePath(); logger.debug("geotiff read grid {} ({}) from {} write {}", field, type, filename, gridOut); Array dtArray; try (GridDataset gds = GridDataset.open(filename)) { GridDatatype grid = gds.findGridByName(field); assert grid != null; int rtindex = -1; int tindex = -1; CoordinateAxis timeAxis = grid.getCoordinateSystem().getTimeAxis(); if (timeAxis instanceof CoordinateAxis2D) { int[] shape = timeAxis.getShape(); rtindex = shape[0] - 1; tindex = shape[1] - 1; } else { CoordinateAxis rtimeAxis = grid.getCoordinateSystem().getRunTimeAxis(); if (rtimeAxis != null) rtindex = (int) rtimeAxis.getSize() - 1; timeAxis = grid.getCoordinateSystem().getTimeAxis1D(); if (timeAxis != null) tindex = (int) timeAxis.getSize() - 1; } dtArray = grid.readDataSlice(rtindex, -1, tindex, 0, -1, -1); try (GeotiffWriter writer = new GeotiffWriter(gridOut)) { writer.writeGrid(gds, grid, dtArray, true); } } try (GeoTiff geotiff = new GeoTiff(gridOut)) { geotiff.read(); logger.debug("{}", geotiff.showInfo()); String gridOut2 = tempFolder.newFile().getAbsolutePath(); logger.debug("geotiff2 read coverage {} write {}", filename, gridOut2); GeoReferencedArray covArray; try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(filename)) { assert cc != null; CoverageCollection gcd = cc.findCoverageDataset(type); Assert.assertNotNull(type.toString(), gcd); int pos = field.indexOf("/"); String covName = (pos > 0) ? field.substring(pos + 1) : field; Coverage coverage = gcd.findCoverage(covName); CoverageCoordAxis1D z = (CoverageCoordAxis1D) coverage.getCoordSys().getZAxis(); SubsetParams params = new SubsetParams().set(SubsetParams.timePresent, true); if (z != null) params.set(SubsetParams.vertCoord, z.getCoordMidpoint(0)); Assert.assertNotNull(covName, coverage); covArray = coverage.readData(params); try (GeotiffWriter writer = new GeotiffWriter(gridOut2)) { writer.writeGrid(covArray, true); } } CompareNetcdf2.compareData(field, dtArray, covArray.getData()); try (GeoTiff geotiff2 = new GeoTiff(gridOut2)) { geotiff2.read(); logger.debug("{}", geotiff2.showInfo()); Formatter out = new Formatter(); CompareNetcdf2.compareLists(geotiff.getTags(), geotiff2.getTags(), out); logger.debug("{}", out.toString()); } File file1 = new File(gridOut); File file2 = new File(gridOut2); assert FileUtils.contentEquals(file1, file2); } }
public void writeGrid(GridDataset dataset, GridDatatype grid, Array data, boolean greyScale) throws IOException { GridCoordSystem gcs = grid.getCoordinateSystem(); if (!gcs.isRegularSpatial()) { throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName()); } CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis(); CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis(); double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0; double xStart = xaxis.getCoordEdge(0) * scaler; double yStart = yaxis.getCoordEdge(0) * scaler; double xInc = xaxis.getIncrement() * scaler; double yInc = Math.abs(yaxis.getIncrement()) * scaler; if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) { data = data.flip(0); yStart = yaxis.getCoordEdge((int) yaxis.getSize()) * scaler; } if (!xaxis.isRegular() || !yaxis.isRegular()) { throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName()); } if (pageNumber > 1) { geotiff.initTags(); } writeGrid(grid, data, greyScale, xStart, yStart, xInc, yInc, pageNumber); pageNumber++; }
GeotiffWriter implements Closeable { public void writeGrid(GridDataset dataset, GridDatatype grid, Array data, boolean greyScale) throws IOException { GridCoordSystem gcs = grid.getCoordinateSystem(); if (!gcs.isRegularSpatial()) { throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName()); } CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis(); CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis(); double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0; double xStart = xaxis.getCoordEdge(0) * scaler; double yStart = yaxis.getCoordEdge(0) * scaler; double xInc = xaxis.getIncrement() * scaler; double yInc = Math.abs(yaxis.getIncrement()) * scaler; if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) { data = data.flip(0); yStart = yaxis.getCoordEdge((int) yaxis.getSize()) * scaler; } if (!xaxis.isRegular() || !yaxis.isRegular()) { throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName()); } if (pageNumber > 1) { geotiff.initTags(); } writeGrid(grid, data, greyScale, xStart, yStart, xInc, yInc, pageNumber); pageNumber++; } }
GeotiffWriter implements Closeable { public void writeGrid(GridDataset dataset, GridDatatype grid, Array data, boolean greyScale) throws IOException { GridCoordSystem gcs = grid.getCoordinateSystem(); if (!gcs.isRegularSpatial()) { throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName()); } CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis(); CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis(); double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0; double xStart = xaxis.getCoordEdge(0) * scaler; double yStart = yaxis.getCoordEdge(0) * scaler; double xInc = xaxis.getIncrement() * scaler; double yInc = Math.abs(yaxis.getIncrement()) * scaler; if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) { data = data.flip(0); yStart = yaxis.getCoordEdge((int) yaxis.getSize()) * scaler; } if (!xaxis.isRegular() || !yaxis.isRegular()) { throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName()); } if (pageNumber > 1) { geotiff.initTags(); } writeGrid(grid, data, greyScale, xStart, yStart, xInc, yInc, pageNumber); pageNumber++; } GeotiffWriter(String fileOut); }
GeotiffWriter implements Closeable { public void writeGrid(GridDataset dataset, GridDatatype grid, Array data, boolean greyScale) throws IOException { GridCoordSystem gcs = grid.getCoordinateSystem(); if (!gcs.isRegularSpatial()) { throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName()); } CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis(); CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis(); double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0; double xStart = xaxis.getCoordEdge(0) * scaler; double yStart = yaxis.getCoordEdge(0) * scaler; double xInc = xaxis.getIncrement() * scaler; double yInc = Math.abs(yaxis.getIncrement()) * scaler; if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) { data = data.flip(0); yStart = yaxis.getCoordEdge((int) yaxis.getSize()) * scaler; } if (!xaxis.isRegular() || !yaxis.isRegular()) { throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName()); } if (pageNumber > 1) { geotiff.initTags(); } writeGrid(grid, data, greyScale, xStart, yStart, xInc, yInc, pageNumber); pageNumber++; } GeotiffWriter(String fileOut); void close(); void writeGrid(GridDataset dataset, GridDatatype grid, Array data, boolean greyScale); void writeGrid(GeoReferencedArray array, boolean greyScale); }
GeotiffWriter implements Closeable { public void writeGrid(GridDataset dataset, GridDatatype grid, Array data, boolean greyScale) throws IOException { GridCoordSystem gcs = grid.getCoordinateSystem(); if (!gcs.isRegularSpatial()) { throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName()); } CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis(); CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis(); double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0; double xStart = xaxis.getCoordEdge(0) * scaler; double yStart = yaxis.getCoordEdge(0) * scaler; double xInc = xaxis.getIncrement() * scaler; double yInc = Math.abs(yaxis.getIncrement()) * scaler; if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) { data = data.flip(0); yStart = yaxis.getCoordEdge((int) yaxis.getSize()) * scaler; } if (!xaxis.isRegular() || !yaxis.isRegular()) { throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName()); } if (pageNumber > 1) { geotiff.initTags(); } writeGrid(grid, data, greyScale, xStart, yStart, xInc, yInc, pageNumber); pageNumber++; } GeotiffWriter(String fileOut); void close(); void writeGrid(GridDataset dataset, GridDatatype grid, Array data, boolean greyScale); void writeGrid(GeoReferencedArray array, boolean greyScale);  }