Search in sources :

Example 1 with GridCoverageFactory

use of org.geotools.coverage.grid.GridCoverageFactory in project mapfish-print by mapfish.

the class AbstractSingleImageLayer method getLayers.

@Override
protected final List<? extends Layer> getLayers(final MfClientHttpRequestFactory httpRequestFactory, final MapfishMapContext mapContext, final Processor.ExecutionContext context) {
    BufferedImage image;
    try {
        image = loadImage(httpRequestFactory, mapContext);
    } catch (Throwable t) {
        throw ExceptionUtils.getRuntimeException(t);
    }
    final MapBounds bounds = mapContext.getBounds();
    final ReferencedEnvelope mapEnvelope = bounds.toReferencedEnvelope(mapContext.getPaintArea());
    GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null);
    GeneralEnvelope gridEnvelope = new GeneralEnvelope(mapEnvelope.getCoordinateReferenceSystem());
    gridEnvelope.setEnvelope(mapEnvelope.getMinX(), mapEnvelope.getMinY(), mapEnvelope.getMaxX(), mapEnvelope.getMaxY());
    final String coverageName = getClass().getSimpleName();
    final GridCoverage2D gridCoverage2D = factory.create(coverageName, image, gridEnvelope, null, null, null);
    Style style = this.styleSupplier.load(httpRequestFactory, gridCoverage2D);
    return Collections.singletonList(new GridCoverageLayer(gridCoverage2D, style));
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridCoverageFactory(org.geotools.coverage.grid.GridCoverageFactory) MapBounds(org.mapfish.print.attribute.map.MapBounds) GridCoverageLayer(org.geotools.map.GridCoverageLayer) Style(org.geotools.styling.Style) GeneralEnvelope(org.geotools.geometry.GeneralEnvelope) BufferedImage(java.awt.image.BufferedImage)

Example 2 with GridCoverageFactory

use of org.geotools.coverage.grid.GridCoverageFactory in project mapfish-print by mapfish.

the class CoverageTask method call.

/**
 * Call the Coverage Task.
 */
public GridCoverage2D call() {
    try {
        BufferedImage coverageImage = this.tiledLayer.createBufferedImage(this.tilePreparationInfo.getImageWidth(), this.tilePreparationInfo.getImageHeight());
        Graphics2D graphics = coverageImage.createGraphics();
        try {
            for (SingleTilePreparationInfo tileInfo : this.tilePreparationInfo.getSingleTiles()) {
                final TileTask task;
                if (tileInfo.getTileRequest() != null) {
                    task = new SingleTileLoaderTask(tileInfo.getTileRequest(), this.errorImage, tileInfo.getTileIndexX(), tileInfo.getTileIndexY(), this.failOnError, this.registry, this.context);
                } else {
                    task = new PlaceHolderImageTask(this.tiledLayer.getMissingTileImage(), tileInfo.getTileIndexX(), tileInfo.getTileIndexY());
                }
                Tile tile = task.call();
                if (tile.getImage() != null) {
                    // crop the image here
                    BufferedImage noBufferTileImage;
                    if (this.tiledLayer.getTileBufferWidth() > 0 || this.tiledLayer.getTileBufferHeight() > 0) {
                        int noBufferWidth = Math.min(this.tiledLayer.getTileSize().width, tile.getImage().getWidth() - this.tiledLayer.getTileBufferWidth());
                        int noBufferHeight = Math.min(this.tiledLayer.getTileSize().height, tile.getImage().getHeight() - this.tiledLayer.getTileBufferHeight());
                        noBufferTileImage = tile.getImage().getSubimage(this.tiledLayer.getTileBufferWidth(), this.tiledLayer.getTileBufferHeight(), noBufferWidth, noBufferHeight);
                    } else {
                        noBufferTileImage = tile.getImage();
                    }
                    graphics.drawImage(noBufferTileImage, tile.getxIndex() * this.tiledLayer.getTileSize().width, tile.getyIndex() * this.tiledLayer.getTileSize().height, null);
                }
            }
        } finally {
            graphics.dispose();
        }
        GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null);
        GeneralEnvelope gridEnvelope = new GeneralEnvelope(this.tilePreparationInfo.getMapProjection());
        gridEnvelope.setEnvelope(this.tilePreparationInfo.getGridCoverageOrigin().x, this.tilePreparationInfo.getGridCoverageOrigin().y, this.tilePreparationInfo.getGridCoverageMaxX(), this.tilePreparationInfo.getGridCoverageMaxY());
        return factory.create(this.tiledLayer.createCommonUrl(), coverageImage, gridEnvelope, null, null, null);
    } catch (Exception e) {
        throw ExceptionUtils.getRuntimeException(e);
    }
}
Also used : GridCoverageFactory(org.geotools.coverage.grid.GridCoverageFactory) SingleTilePreparationInfo(org.mapfish.print.map.tiled.TilePreparationInfo.SingleTilePreparationInfo) GeneralEnvelope(org.geotools.geometry.GeneralEnvelope) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D)

Example 3 with GridCoverageFactory

use of org.geotools.coverage.grid.GridCoverageFactory in project hortonmachine by TheHortonMachine.

the class CoverageUtilities method clipCoverage.

/**
 * Clip a coverage to a given envelope.
 *
 * The region is cut to keep the resolution consistent.
 */
public static GridCoverage2D clipCoverage(GridCoverage2D coverage, ReferencedEnvelope envelope) throws Exception {
    RegionMap regionMap = getRegionParamsFromGridCoverage(coverage);
    double xRes = regionMap.getXres();
    double yRes = regionMap.getYres();
    envelope = envelope.transform(coverage.getCoordinateReferenceSystem(), true);
    // snap the envelope to the grid in order to properly crop the raster
    RegionMap subRegion = regionMap.toSubRegion(envelope);
    envelope = new ReferencedEnvelope(subRegion.toEnvelope(), coverage.getCoordinateReferenceSystem());
    double west = envelope.getMinX();
    double south = envelope.getMinY();
    double east = envelope.getMaxX();
    double north = envelope.getMaxY();
    int cols = (int) ((east - west) / xRes);
    int rows = (int) ((north - south) / yRes);
    ComponentSampleModel sampleModel = new ComponentSampleModel(DataBuffer.TYPE_DOUBLE, cols, rows, 1, cols, new int[] { 0 });
    WritableRaster writableRaster = RasterFactory.createWritableRaster(sampleModel, null);
    Envelope2D writeEnvelope = new Envelope2D(coverage.getCoordinateReferenceSystem(), west, south, east - west, north - south);
    GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null);
    GridCoverage2D clippedCoverage = factory.create("newraster", writableRaster, writeEnvelope);
    GridGeometry2D destGG = clippedCoverage.getGridGeometry();
    GridGeometry2D sourceGG = coverage.getGridGeometry();
    RandomIter iter = getRandomIterator(coverage);
    for (int y = 0; y < rows; y++) {
        for (int x = 0; x < cols; x++) {
            DirectPosition world = destGG.gridToWorld(new GridCoordinates2D(x, y));
            GridCoordinates2D sourceGrid = sourceGG.worldToGrid(world);
            double value = iter.getSampleDouble(sourceGrid.x, sourceGrid.y, 0);
            writableRaster.setSample(x, y, 0, value);
        }
    }
    iter.done();
    return clippedCoverage;
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) GridCoverageFactory(org.geotools.coverage.grid.GridCoverageFactory) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) ComponentSampleModel(java.awt.image.ComponentSampleModel) Envelope2D(org.geotools.geometry.Envelope2D) GridEnvelope2D(org.geotools.coverage.grid.GridEnvelope2D) Point(java.awt.Point) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) WritableRaster(java.awt.image.WritableRaster) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D)

Example 4 with GridCoverageFactory

use of org.geotools.coverage.grid.GridCoverageFactory in project hortonmachine by TheHortonMachine.

the class CoverageUtilities method buildCoverageWithNovalue.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static GridCoverage2D buildCoverageWithNovalue(String name, RenderedImage renderedImage, HashMap<String, Double> envelopeParams, CoordinateReferenceSystem crs, double novalue) {
    double west = envelopeParams.get(WEST);
    double south = envelopeParams.get(SOUTH);
    double east = envelopeParams.get(EAST);
    double north = envelopeParams.get(NORTH);
    Envelope2D writeEnvelope = new Envelope2D(crs, west, south, east - west, north - south);
    final GridSampleDimension[] bands = RenderedSampleDimension.create(name, renderedImage.getData(), null, null, null, null, null);
    GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null);
    Map properties = new HashMap<>();
    NoDataContainer ndc = new NoDataContainer(novalue);
    properties.put(NoDataContainer.GC_NODATA, ndc);
    GridCoverage2D coverage2D = factory.create(name, renderedImage, writeEnvelope, bands, null, properties);
    return coverage2D;
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridCoverageFactory(org.geotools.coverage.grid.GridCoverageFactory) HashMap(java.util.HashMap) GridSampleDimension(org.geotools.coverage.GridSampleDimension) NoDataContainer(it.geosolutions.jaiext.range.NoDataContainer) Envelope2D(org.geotools.geometry.Envelope2D) GridEnvelope2D(org.geotools.coverage.grid.GridEnvelope2D) Map(java.util.Map) HashMap(java.util.HashMap) RegionMap(org.hortonmachine.gears.utils.RegionMap)

Example 5 with GridCoverageFactory

use of org.geotools.coverage.grid.GridCoverageFactory in project hortonmachine by TheHortonMachine.

the class CoverageUtilities method createSubCoverageFromTemplate.

/**
 * Create a subcoverage given a template coverage and an envelope.
 *
 * @param template the template coverage used for the resolution.
 * @param subregion the envelope to extract to the new coverage. This should
 *                  be snapped on the resolution of the coverage, in order to avoid
 *                  shifts.
 * @param value the value to set the new raster to, if not <code>null</code>.
 * @param writableRasterHolder an array of length 1 to place the writable raster in, that
 *                  was can be used to populate the coverage. If <code>null</code>, it is ignored.
 * @return the new coverage.
 */
public static GridCoverage2D createSubCoverageFromTemplate(GridCoverage2D template, Envelope2D subregion, Double value, WritableRaster[] writableRasterHolder) {
    RegionMap regionMap = getRegionParamsFromGridCoverage(template);
    double xRes = regionMap.getXres();
    double yRes = regionMap.getYres();
    double west = subregion.getMinX();
    double south = subregion.getMinY();
    double east = subregion.getMaxX();
    double north = subregion.getMaxY();
    int cols = (int) ((east - west) / xRes);
    int rows = (int) ((north - south) / yRes);
    ComponentSampleModel sampleModel = new ComponentSampleModel(DataBuffer.TYPE_DOUBLE, cols, rows, 1, cols, new int[] { 0 });
    WritableRaster writableRaster = RasterFactory.createWritableRaster(sampleModel, null);
    if (value != null) {
        // autobox only once
        double v = value;
        for (int y = 0; y < rows; y++) {
            for (int x = 0; x < cols; x++) {
                writableRaster.setSample(x, y, 0, v);
            }
        }
    }
    if (writableRasterHolder != null)
        writableRasterHolder[0] = writableRaster;
    Envelope2D writeEnvelope = new Envelope2D(template.getCoordinateReferenceSystem(), west, south, east - west, north - south);
    GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null);
    GridCoverage2D coverage2D = factory.create("newraster", writableRaster, writeEnvelope);
    return coverage2D;
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridCoverageFactory(org.geotools.coverage.grid.GridCoverageFactory) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) ComponentSampleModel(java.awt.image.ComponentSampleModel) Envelope2D(org.geotools.geometry.Envelope2D) GridEnvelope2D(org.geotools.coverage.grid.GridEnvelope2D) Point(java.awt.Point)

Aggregations

GridCoverageFactory (org.geotools.coverage.grid.GridCoverageFactory)17 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)11 WritableRaster (java.awt.image.WritableRaster)7 Envelope2D (org.geotools.geometry.Envelope2D)7 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)7 BufferedImage (java.awt.image.BufferedImage)6 GridEnvelope2D (org.geotools.coverage.grid.GridEnvelope2D)6 GeneralEnvelope (org.geotools.geometry.GeneralEnvelope)6 IOException (java.io.IOException)5 RegionMap (org.hortonmachine.gears.utils.RegionMap)5 Point (java.awt.Point)4 HashMap (java.util.HashMap)4 GridSampleDimension (org.geotools.coverage.GridSampleDimension)4 ComponentSampleModel (java.awt.image.ComponentSampleModel)3 Map (java.util.Map)3 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)3 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)3 FactoryException (org.opengis.referencing.FactoryException)3 NoDataContainer (it.geosolutions.jaiext.range.NoDataContainer)2 Graphics2D (java.awt.Graphics2D)2