use of org.geotoolkit.display2d.presentation.RasterPresentation in project geotoolkit by Geomatys.
the class RasterSymbolizerRenderer method presentations.
@Override
public Stream<Presentation> presentations(MapLayer layer, Feature feature) {
final RasterSymbolizer sourceSymbol = symbol.getSource();
final Object candidate = GO2Utilities.evaluate(sourceSymbol.getGeometry(), feature, null, null);
GridCoverageResource ref = null;
if (candidate instanceof GridCoverageResource) {
ref = (GridCoverageResource) candidate;
} else if (candidate instanceof GridCoverage) {
ref = new InMemoryGridCoverageResource((GridCoverage) candidate);
} else {
return Stream.empty();
}
try {
final int[] channelSelection = channelSelection(sourceSymbol, ref);
final GridCoverage dataCoverage = getObjectiveCoverage(ref, renderingContext.getGridGeometry(), false, channelSelection);
if (dataCoverage == null) {
return Stream.empty();
}
final GridCoverage2D dataImage = applyStyle(ref, dataCoverage, sourceSymbol);
final RasterPresentation rasterPresentation = new RasterPresentation(layer, ref, dataImage);
rasterPresentation.forGrid(renderingContext);
return Stream.concat(Stream.of(rasterPresentation), outline(layer, dataImage.getGridGeometry()));
} catch (NoSuchDataException | DisjointExtentException e) {
LOGGER.log(Level.FINE, "Disjoint exception: " + e.getMessage(), e);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Portrayal exception: " + e.getMessage(), e);
}
return Stream.empty();
}
use of org.geotoolkit.display2d.presentation.RasterPresentation in project geotoolkit by Geomatys.
the class RasterSymbolizerRenderer method presentations.
@Override
public Stream<Presentation> presentations(MapLayer layer, Resource rs) {
if (rs instanceof BandedCoverageResource) {
BandedCoverageResource bcr = (BandedCoverageResource) rs;
try {
GridCoverage coverage = BandedCoverageResource.sample(bcr, renderingContext.getGridGeometry2D());
rs = new InMemoryGridCoverageResource(rs.getIdentifier().orElse(null), coverage);
} catch (DataStoreException ex) {
ExceptionPresentation ep = new ExceptionPresentation(ex);
ep.setLayer(layer);
ep.setResource(rs);
return Stream.of(ep);
}
}
if (rs instanceof GridCoverageResource) {
GridCoverageResource ref = (GridCoverageResource) rs;
try {
final RasterSymbolizer sourceSymbol = symbol.getSource();
final int[] channelSelection = channelSelection(sourceSymbol, ref);
final GridCoverage dataCoverage = getObjectiveCoverage(ref, renderingContext.getGridGeometry(), false, channelSelection);
if (dataCoverage == null) {
return Stream.empty();
}
final GridCoverage2D dataImage = applyStyle(ref, dataCoverage, sourceSymbol);
final RasterPresentation rasterPresentation = new RasterPresentation(layer, layer.getData(), dataImage);
rasterPresentation.forGrid(renderingContext);
return Stream.concat(Stream.of(rasterPresentation), outline(layer, dataImage.getGridGeometry()));
} catch (NoSuchDataException | DisjointExtentException e) {
LOGGER.log(Level.FINE, "Disjoint exception: " + e.getMessage(), e);
} catch (Exception e) {
ExceptionPresentation ep = new ExceptionPresentation(e);
ep.setLayer(layer);
ep.setResource(rs);
return Stream.of(ep);
}
} else {
return super.presentations(layer, rs);
}
return Stream.empty();
}
use of org.geotoolkit.display2d.presentation.RasterPresentation in project geotoolkit by Geomatys.
the class DynamicRangeSymbolizerRenderer method presentations.
@Override
public Stream<Presentation> presentations(MapLayer layer, Resource resource) {
if (resource instanceof GridCoverageResource) {
try {
final GridCoverageResource covref = (GridCoverageResource) resource;
final DynamicRangeSymbolizer symbolizer = symbol.getSource();
final int[] bands = new int[] { -1, -1, -1, -1 };
final double[][] ranges = new double[][] { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } };
final Map<String, Object> stats = new HashMap<>();
// we can avoid loading metadata if all ranges are literals,
// metadata can be expensive because of histogram informations
boolean allLiteral = true;
for (DynamicRangeSymbolizer.DRChannel channel : symbolizer.getChannels()) {
allLiteral &= channel.getLower().getValue() instanceof Literal;
allLiteral &= channel.getUpper().getValue() instanceof Literal;
}
CoverageDescription covdesc = null;
if (!allLiteral) {
if (covdesc == null) {
Metadata metadata = covref.getMetadata();
if (metadata != null) {
for (ContentInformation ci : metadata.getContentInfo()) {
if (ci instanceof CoverageDescription) {
covdesc = (CoverageDescription) ci;
break;
}
}
}
}
if (covdesc == null) {
// create an empty description
covdesc = new DefaultCoverageDescription();
}
}
for (DynamicRangeSymbolizer.DRChannel channel : symbolizer.getChannels()) {
final Integer bandIdx;
try {
bandIdx = Integer.valueOf(channel.getBand());
} catch (NumberFormatException ex) {
// not a number index
continue;
}
final String cs = channel.getColorSpaceComponent().trim();
final int idx;
if (DynamicRangeSymbolizer.DRChannel.BAND_RED.equalsIgnoreCase(cs))
idx = 0;
else if (DynamicRangeSymbolizer.DRChannel.BAND_GREEN.equalsIgnoreCase(cs))
idx = 1;
else if (DynamicRangeSymbolizer.DRChannel.BAND_BLUE.equalsIgnoreCase(cs))
idx = 2;
else if (DynamicRangeSymbolizer.DRChannel.BAND_ALPHA.equalsIgnoreCase(cs))
idx = 3;
else {
// no mapping
continue;
}
bands[idx] = bandIdx;
// search for band statistics
stats.clear();
if (!allLiteral) {
search: for (AttributeGroup attg : covdesc.getAttributeGroups()) {
for (RangeDimension rd : attg.getAttributes()) {
if (!(rd instanceof org.opengis.metadata.content.SampleDimension))
continue;
final int i = Integer.parseInt(rd.getSequenceIdentifier().tip().toString());
if (i == bandIdx) {
final org.opengis.metadata.content.SampleDimension sd = (org.opengis.metadata.content.SampleDimension) rd;
stats.put(DynamicRangeSymbolizer.PROPERTY_MIN, sd.getMinValue());
stats.put(DynamicRangeSymbolizer.PROPERTY_MAX, sd.getMaxValue());
stats.put(DynamicRangeSymbolizer.PROPERTY_MEAN, sd.getMeanValue());
stats.put(DynamicRangeSymbolizer.PROPERTY_STD, sd.getStandardDeviation());
if (sd instanceof DefaultSampleDimensionExt) {
final DefaultSampleDimensionExt dsd = (DefaultSampleDimensionExt) sd;
stats.put(DynamicRangeSymbolizer.PROPERTY_HISTO, dsd.getHistogram());
stats.put(DynamicRangeSymbolizer.PROPERTY_HISTO_MIN, dsd.getHistogramMin());
stats.put(DynamicRangeSymbolizer.PROPERTY_HISTO_MAX, dsd.getHistogramMax());
}
break search;
}
}
}
}
ranges[idx][0] = evaluate(channel.getLower(), stats);
ranges[idx][1] = evaluate(channel.getUpper(), stats);
}
// read only requested coverage bands
int[] toRead = new int[0];
final int[] mapping = new int[4];
for (int i = 0; i < 4; i++) {
if (bands[i] != -1) {
int index = 0;
boolean contained = false;
for (int k = 0; k < toRead.length; k++) {
if (toRead[k] == bands[i]) {
index = k;
contained = true;
break;
}
}
if (!contained) {
toRead = Arrays.copyOf(toRead, toRead.length + 1);
index = toRead.length - 1;
toRead[index] = bands[i];
}
mapping[i] = index;
}
}
GridCoverage dataCoverage;
try {
dataCoverage = covref.read(renderingContext.getGridGeometry(), toRead);
} catch (NoSuchDataException ex) {
return Stream.empty();
}
if (dataCoverage == null) {
return Stream.empty();
}
// check if the reader honored the band request
final List<SampleDimension> readDimensions = dataCoverage.getSampleDimensions();
final List<SampleDimension> sampleDimensions = covref.getSampleDimensions();
boolean bandReadHonored = (readDimensions.size() == toRead.length);
for (int i = 0; bandReadHonored && i < toRead.length; i++) {
bandReadHonored &= Objects.equals(readDimensions.get(i).getName(), (sampleDimensions == null) ? null : sampleDimensions.get(toRead[i]).getName());
}
// swap new band indexes
if (bandReadHonored) {
for (int i = 0; i < 4; i++) {
if (bands[i] != -1)
bands[i] = mapping[i];
}
}
dataCoverage = dataCoverage.forConvertedValues(true);
final RenderedImage ri = dataCoverage.render(null);
final DynamicRangeStretchProcess p = new DynamicRangeStretchProcess(ri, bands, ranges);
RenderedImage img = p.executeNow();
if (img instanceof WritableRenderedImage)
GO2Utilities.removeBlackBorder((WritableRenderedImage) img);
final GridCoverageBuilder gcb = new GridCoverageBuilder();
gcb.setDomain(dataCoverage.getGridGeometry());
gcb.setValues(img);
final GridCoverage recolored = gcb.build();
final RasterPresentation presentation = new RasterPresentation(layer, layer.getData(), recolored);
presentation.forGrid(renderingContext);
return Stream.of(presentation);
} catch (DataStoreException e) {
if (e.getCause() instanceof ProjectionException) {
// out of domain exception
monitor.exceptionOccured(e, Level.FINE);
} else {
monitor.exceptionOccured(e, Level.WARNING);
}
} catch (Exception e) {
monitor.exceptionOccured(e, Level.WARNING);
}
}
return Stream.empty();
}
use of org.geotoolkit.display2d.presentation.RasterPresentation in project geotoolkit by Geomatys.
the class MapLayerJ2D method paintLayer.
/**
* Render layer, will only be painted if an appropriate graphic builder is attached
* to it.
*/
public Stream<Presentation> paintLayer(final RenderingContext2D context) throws PortrayalException, DataStoreException {
// we abort painting if the layer is not visible.
if (!item.isVisible())
return Stream.empty();
// we abort if opacity is to low
final double opacity = item.getOpacity();
if (opacity < 1e-6)
return Stream.empty();
if (1 - opacity < 1e-6) {
// we are very close to opacity one, no need to create a intermediate image
return streamPresentations(context);
} else {
// create an intermediate layer which will be painted on the main context
// after with the given opacity
final Rectangle rect = context.getCanvasDisplayBounds();
final BufferedImage inter = createBufferedImage(ColorModel.getRGBdefault(), ColorModel.getRGBdefault().createCompatibleSampleModel(rect.width, rect.height));
final Graphics2D g2d = inter.createGraphics();
final RenderingContext2D interContext = context.create(g2d);
final J2DPainter painter = new J2DPainter();
try (Stream<Presentation> stream = streamPresentations(interContext)) {
painter.paint(interContext, stream, true);
}
final GridCoverageBuilder gcb = new GridCoverageBuilder();
gcb.setDomain(interContext.getGridGeometry());
gcb.setValues(inter);
final GridCoverage coverage = gcb.build();
final RasterPresentation rp = new RasterPresentation(layer, null, coverage);
rp.forGrid(context);
rp.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) opacity);
return Stream.of(rp);
}
}
Aggregations