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));
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations