boolean visit(GridCoverage2D coverage) throws IOException { MathTransform2D mt = coverage.getGridGeometry().getGridToCRS2D(); // cannot do a comparison if (!(mt instanceof AffineTransform2D)) { return false; } AffineTransform2D at = (AffineTransform2D) mt; double scaleX = Math.abs(at.getScaleX()); double scaleY = Math.abs(at.getScaleY()); if (resolution == null) { this.resolution = new double[2]; this.resolution[0] = scaleX; this.resolution[1] = scaleY; return true; } if (compare(scaleX, scaleY, resolution)) { this.resolution[0] = scaleX; this.resolution[1] = scaleY; return true; } else { return false; } }
@Override public void createIndividuals() { LOG.info("Loading population from raster file {}", sourceFilename); try { File rasterFile = new File(sourceFilename); // determine file format and CRS, then load raster AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile); AbstractGridCoverage2DReader reader = format.getReader(rasterFile); GridCoverage2D coverage = reader.read(null); this.coverageCRS = coverage.getCoordinateReferenceSystem(); GridGeometry2D gridGeometry = coverage.getGridGeometry(); GridEnvelope2D gridEnvelope = gridGeometry.getGridRange2D(); gridGeometry.getGridToCRS(); // because we may want to produce an empty raster rather than loading one, alongside the coverage we // store the row/col dimensions and the referenced envelope in the original coordinate reference system. this.cols = gridEnvelope.width; this.rows = gridEnvelope.height; this.createIndividuals0(); } catch (Exception ex) { throw new IllegalStateException("Error loading population from raster file: ", ex); } LOG.info("Done loading raster from file."); }
ps.println("Grid bounds: " + coverage.getEnvelope()); ps.println("Grid CRS: " + coverage.getCoordinateReferenceSystem()); ps.println("Grid range: " + coverage.getGridGeometry().getGridRange()); ps.println("Grid to world: " + coverage.getGridGeometry().getGridToCRS()); ps.println("Contents:"); RenderedImage ri = coverage.getRenderedImage();
LOGGER.fine("Source tiling:" + tileDimensions.width + "x" + tileDimensions.height); final GridEnvelope gr = sourceCoverage.getGridGeometry().getGridRange(); if (gr.getSpan(0) < tileDimensions.width) { tileDimensions.width = gr.getSpan(0);
private void assertCoverageResolution(GridCoverage2D coverage, double resX, double resY) { AffineTransform2D mt = (AffineTransform2D) coverage.getGridGeometry().getGridToCRS2D(); assertEquals(resX, mt.getScaleX(), 1); assertEquals(resY, Math.abs(mt.getScaleY()), 1); }
GridGeometry2D gg = coverage.getGridGeometry(); GeneralEnvelope padRange = CRS.transform(gg.getCRSToGrid2D(PixelOrientation.UPPER_LEFT), bounds);
private GeoTiffWriteParams buildWriteParams(GridCoverage2D coverage) { final RenderedImage renderedImage = coverage.getRenderedImage(); int tileWidth = renderedImage.getTileWidth(); int tileHeight = renderedImage.getTileHeight(); // avoid tiles bigger than the image final GridEnvelope gr = coverage.getGridGeometry().getGridRange(); if (gr.getSpan(0) < tileWidth) { tileWidth = gr.getSpan(0); } if (gr.getSpan(1) < tileHeight) { tileHeight = gr.getSpan(1); } GeoTiffWriteParams writeParams = new GeoTiffWriteParams(); writeParams.setTilingMode(GeoToolsWriteParams.MODE_EXPLICIT); writeParams.setTiling(tileWidth, tileHeight); return writeParams; }
coverage.getGridGeometry().getGridRange2D(), coverage.getRenderedImage().getSampleModel()); if (actual > limit) {
final long pixelsNumber = computePixelsNumber(gc.getGridGeometry().getGridRange2D());
final GridGeometry geometry = gc.getGridGeometry(); final int dimensions = geometry.getGridRange().getDimension(); String lower = "";
/** * The BandMerge operation takes indexed images and expands them, however in the context of * coverage view band merging we don't normally want that, e.g., raster mask bands are * represented as indexed but we really want to keep them in their binary, single band form. To * do so, the IndexColorModel is replaced by a ComponentColorModel * * @param coverage * @return */ private GridCoverage2D prepareForBandMerge(GridCoverage2D coverage) { RenderedImage ri = coverage.getRenderedImage(); SampleModel sampleModel = ri.getSampleModel(); if (sampleModel.getNumBands() == 1 && ri.getColorModel() instanceof IndexColorModel) { ImageWorker worker = new ImageWorker(ri); worker.removeIndexColorModel(); RenderedImage formatted = worker.getRenderedImage(); return new GridCoverageFactory() .create( coverage.getName(), formatted, coverage.getGridGeometry(), coverage.getSampleDimensions(), new GridCoverage[] {coverage}, coverage.getProperties()); } return coverage; }
/** * Method for checking if Alpha Coverage and Image Coverage have the same dimensions * * @param coverage * @param alpha */ private static void checkAlpha(GridCoverage2D coverage, GridCoverage2D alpha) { // Check GridGeometries if (!coverage.getGridGeometry().equals(alpha.getGridGeometry())) { throw new CoverageProcessingException( "Alpha Coverage and Source Coverage does not have the same dimensions"); } }
/** * Constructs a new {@code BandSelect2D} grid coverage. This grid coverage will use the same * coordinate reference system and the same geometry than the source grid coverage. * * @param source The source coverage. * @param image The image to use. * @param bands The sample dimensions to use. * @param bandIndices The mapping to bands in {@code source}. Not used by this constructor, but * keept for futur reference. * @todo It would be nice if we could use always the "BandSelect" operation without the "Null" * one. But as of JAI-1.1.1, "BandSelect" do not detect by itself the case were no copy is * required. */ private BandSelector2D( final GridCoverage2D source, final PlanarImage image, final GridSampleDimension[] bands, final int[] bandIndices, final Hints hints) { super( source.getName(), // The grid source name image, // The underlying data source.getGridGeometry(), // The grid geometry (unchanged). bands, // The sample dimensions new GridCoverage2D[] {source}, // The source grid coverages. source.getProperties(), hints); // Properties this.bandIndices = bandIndices; assert bandIndices == null || bandIndices.length == bands.length; }
double[] eastNorth = upperCorner.getCoordinate(); GridGeometry2D gridGeometry = gridCoverage.getGridGeometry(); GridEnvelope2D gridRange = gridGeometry.getGridRange2D(); int height = gridRange.height;
private static GridCoverage2D existingCoverage( GridCoverage2D coverage, CoordinateReferenceSystem targetCRS, GridGeometry2D targetGG) { // NoData and ROI must be handled ROI roiProp = CoverageUtilities.getROIProperty(coverage); Object nodataProp = CoverageUtilities.getNoDataProperty(coverage); boolean hasROI = (roiProp != null); boolean hasNoData = (nodataProp != null); if (hasROI || hasNoData) { return null; } while (!equivalent(coverage.getGridGeometry(), targetGG) || (!CRS.equalsIgnoreMetadata(targetCRS, coverage.getCoordinateReferenceSystem()) && !CRS.equalsIgnoreMetadata( targetCRS, coverage.getCoordinateReferenceSystem2D()))) { if (!(coverage instanceof Resampler2D)) { return null; } final List<GridCoverage> sources = coverage.getSources(); assert sources.size() == 1 : sources; coverage = (GridCoverage2D) sources.get(0); } return coverage; }
/** * This method creates a new {@link GridGeometry2D} object based on that of the {@link * GridCoverage2D} defined by the index. * * @param sources * @param index * @return */ private static GridGeometry2D extractFinalGridGeometry( GridCoverage2D[] sources, int index) { // Select the GridGeometry of the first coverage GridGeometry2D gg = sources[index].getGridGeometry(); MathTransform g2w = gg.getGridToCRS2D(PixelOrientation.UPPER_LEFT); // Initial Bounding box Envelope2D bbox = gg.getEnvelope2D(); // Number of the sources to use int numSources = sources.length; // Cycle on all the GridCoverages in order to create the final Bounding box for (int i = 0; i < numSources; i++) { bbox.include(sources[i].getEnvelope2D()); } // Creation of a final GridGeometry containing the final Bounding Box GridGeometry2D finalGG = new GridGeometry2D( PixelInCell.CELL_CORNER, g2w, bbox, GeoTools.getDefaultHints()); return finalGG; } }
public static JGrassRegion getJGrassRegionFromGridCoverage(GridCoverage2D gridCoverage2D) throws InvalidGridGeometryException, TransformException { Envelope2D env = gridCoverage2D.getEnvelope2D(); GridEnvelope2D worldToGrid = gridCoverage2D.getGridGeometry().worldToGrid(env); double xRes = env.getWidth() / worldToGrid.getWidth(); double yRes = env.getHeight() / worldToGrid.getHeight(); JGrassRegion region = new JGrassRegion( env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), xRes, yRes); return region; }
static GridCoverage2D displace( GridCoverage2D coverage, double tx, double ty, GridCoverageFactory gridCoverageFactory) { // let's compute the new grid geometry GridGeometry2D originalGG = coverage.getGridGeometry(); GridEnvelope gridRange = originalGG.getGridRange(); Envelope2D envelope = originalGG.getEnvelope2D(); double minx = envelope.getMinX() + tx; double miny = envelope.getMinY() + ty; double maxx = envelope.getMaxX() + tx; double maxy = envelope.getMaxY() + ty; ReferencedEnvelope translatedEnvelope = new ReferencedEnvelope( minx, maxx, miny, maxy, envelope.getCoordinateReferenceSystem()); GridGeometry2D translatedGG = new GridGeometry2D(gridRange, translatedEnvelope); GridCoverage2D translatedCoverage = gridCoverageFactory.create( coverage.getName(), coverage.getRenderedImage(), translatedGG, coverage.getSampleDimensions(), new GridCoverage2D[] {coverage}, coverage.getProperties()); return translatedCoverage; }
/** * Writes the {@link GridCoverage2D supplied coverage} to disk. * * <p>Note that this also takes care to cloes the file handle after writing to disk. * * @param gridCoverage2D the coverage to write. * @throws IOException */ public void writeRaster(GridCoverage2D gridCoverage2D) throws IOException { try { Envelope2D env = gridCoverage2D.getEnvelope2D(); GridEnvelope2D worldToGrid = gridCoverage2D.getGridGeometry().worldToGrid(env); double xRes = env.getWidth() / worldToGrid.getWidth(); double yRes = env.getHeight() / worldToGrid.getHeight(); JGrassRegion region = new JGrassRegion( env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), xRes, yRes); GrassBinaryImageWriterSpi writerSpi = new GrassBinaryImageWriterSpi(); GrassBinaryImageWriter writer = new GrassBinaryImageWriter(writerSpi, monitor); RenderedImage renderedImage = gridCoverage2D.getRenderedImage(); writer.setOutput(output, region); writer.write(renderedImage); writer.dispose(); } catch (Exception e) { java.util.logging.Logger.getGlobal().log(java.util.logging.Level.INFO, "", e); } }
private RenderedImage performShadedRelief( ImageWorker intensityWorker, GridCoverage2D source, Hints hints) { RenderedImage ri = source.getRenderedImage(); MathTransform g2w = source.getGridGeometry().getGridToCRS(); AffineTransform af = (AffineTransform) g2w; CoordinateReferenceSystem crs = source.getCoordinateReferenceSystem();