use of org.geotoolkit.storage.coverage.BandedCoverageResource 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.storage.coverage.BandedCoverageResource in project geotoolkit by Geomatys.
the class URITileMatrix method writeTile.
/**
* Note : we don't start by deleting the previous tile, the end replace operation will do it.
* @param tile
* @throws DataStoreException
*/
@Override
protected void writeTile(Tile tile) throws DataStoreException {
ArgumentChecks.ensureNonNull("tile", tile);
final int tileX = tile.getPosition().x;
final int tileY = tile.getPosition().y;
final URI tileUri = getTilePath(tileX, tileY);
final Path tilePath = toPath(tileUri);
if (tilePath == null) {
throw new DataStoreException("Tile " + tileUri + " is not on the file system, writing is not supported.");
}
final Path tempTilePath = tilePath.resolveSibling(tilePath.getFileName().toString() + "_" + Thread.currentThread().getId());
try {
Files.createDirectories(tilePath.getParent());
} catch (IOException ex) {
throw new DataStoreException(ex.getMessage(), ex);
}
if (format.isImage()) {
if (tile instanceof ImageTile) {
try {
final RenderedImage image = ((ImageTile) tile).getImage();
final ImageWriterSpi writerSpi = XImageIO.getImageWriterSpi(format.getImageSpi());
final ImageWriter writer = writerSpi.createWriterInstance();
final File output = tempTilePath.toFile();
output.delete();
final ImageOutputStream stream = createImageOutputStream(output);
try {
ImageWriteParam writeParam = writer.getDefaultWriteParam();
if (format.getImageCompression() != null) {
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType(format.getImageCompression());
}
writer.setOutput(stream);
writer.write(null, new IIOImage(image, null, null), writeParam);
writer.dispose();
} finally {
stream.close();
}
} catch (IOException ex) {
throw new DataStoreException(ex.getMessage(), ex);
}
} else {
throw new DataStoreException("Invalid tile class " + tile.getClass() + ". " + "TileMatrixSet format is declared as image, an ImageTile instance is expected.");
}
} else {
final DataStoreProvider provider = format.getStoreProvider();
final StorageConnector connector = new StorageConnector(tempTilePath);
try (final DataStore store = provider.open(connector)) {
Resource tileData = tile;
if (tile instanceof DeferredTile) {
tileData = ((DeferredTile) tile).open();
}
if (tileData instanceof Assets) {
final Assets source = (Assets) tile;
if (store instanceof Assets) {
final Assets target = (Assets) store;
for (Data data : source.getDatas()) {
target.addData(data);
}
} else {
throw new DataStoreException("No procedure found to copy from " + tile.getClass() + " to " + store.getClass());
}
} else if (tileData instanceof BandedCoverageResource && store instanceof WritableBandedCoverageResource) {
final BandedCoverage bandedCoverage = ((BandedCoverageResource) tileData).read(null);
((WritableBandedCoverageResource) store).write(bandedCoverage);
} else {
throw new DataStoreException("No procedure found to copy from " + tile.getClass() + " to " + store.getClass());
}
} finally {
connector.closeAllExcept(null);
}
}
// compress produced tile
final URITileFormat.Compression compression = format.getCompression();
switch(compression.name()) {
case "GZ":
{
// read and compress datas
byte[] datas;
try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(tempTilePath))) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final GZIPOutputStream gout = new GZIPOutputStream(out);
IOUtilities.copy(in, gout);
gout.finish();
gout.close();
datas = out.toByteArray();
} catch (IOException ex) {
throw new DataStoreException(ex.getMessage(), ex);
}
// write result
try (OutputStream fout = new BufferedOutputStream(Files.newOutputStream(tempTilePath))) {
fout.write(datas);
} catch (IOException ex) {
throw new DataStoreException(ex.getMessage(), ex);
}
}
break;
case "LZ4":
{
// read and compress datas
byte[] datas;
try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(tempTilePath))) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final FramedLZ4CompressorOutputStream gout = new FramedLZ4CompressorOutputStream(out);
IOUtilities.copy(in, gout);
gout.finish();
gout.close();
datas = out.toByteArray();
} catch (IOException ex) {
throw new DataStoreException(ex.getMessage(), ex);
}
// write result
try (OutputStream fout = new BufferedOutputStream(Files.newOutputStream(tempTilePath))) {
fout.write(datas);
} catch (IOException ex) {
throw new DataStoreException(ex.getMessage(), ex);
}
}
break;
}
try {
// replace tile file by new tile.
Files.move(tempTilePath, tilePath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException ex) {
throw new DataStoreException(ex.getMessage(), ex);
}
}
use of org.geotoolkit.storage.coverage.BandedCoverageResource in project geotoolkit by Geomatys.
the class DefaultPortrayalService method present.
public static Stream<Presentation> present(MapLayer layer, Resource resource, RenderingContext2D renderContext) {
final Style style = layer.getStyle();
Stream<Presentation> stream = Stream.empty();
FeatureType type = null;
if (resource instanceof FeatureSet) {
try {
type = ((FeatureSet) resource).getType();
} catch (DataStoreException ex) {
ExceptionPresentation ep = new ExceptionPresentation(ex);
ep.setLayer(layer);
ep.setResource(resource);
return Stream.of(ep);
}
} else if (resource instanceof GridCoverageResource || resource instanceof BandedCoverageResource) {
type = null;
} else if (resource instanceof Aggregate) {
try {
// combine each component resource in the stream
for (Resource r : ((Aggregate) resource).components()) {
stream = Stream.concat(stream, present(layer, r, renderContext));
}
} catch (DataStoreException ex) {
ExceptionPresentation ep = new ExceptionPresentation(ex);
ep.setLayer(layer);
ep.setResource(resource);
return Stream.of(ep);
}
return stream;
} else {
// unknown type
return Stream.empty();
}
final boolean keepAllProperties = GO2Utilities.mustPreserveAllProperties(renderContext);
for (FeatureTypeStyle fts : style.featureTypeStyles()) {
final List<Rule> rules = GO2Utilities.getValidRules(fts, renderContext.getSEScale(), type);
if (rules.isEmpty())
continue;
// prepare the renderers
final CachedRule[] cachedRules = toCachedRules(rules, type);
final RenderingRules renderers = new RenderingRules(cachedRules, renderContext);
{
// special case for group symbolizers
// group symbolizers must be alone in a FTS
SymbolizerRenderer groupRenderer = null;
int count = 0;
for (int i = 0; i < renderers.renderers.length; i++) {
for (int k = 0; k < renderers.renderers[i].length; k++) {
count++;
if (renderers.renderers[i][k].getService().isGroupSymbolizer()) {
groupRenderer = renderers.renderers[i][k];
}
}
}
if (groupRenderer != null) {
if (count > 1) {
ExceptionPresentation ep = new ExceptionPresentation(new PortrayalException("Group symbolizer (" + groupRenderer.getService().getSymbolizerClass().getSimpleName() + ") must be alone in a FeatureTypeStyle element."));
ep.setLayer(layer);
ep.setResource(resource);
stream = Stream.concat(stream, Stream.of(ep));
} else {
stream = Stream.concat(stream, groupRenderer.presentations(layer, resource));
}
continue;
}
}
// extract the used names
Set<String> names;
if (keepAllProperties) {
names = null;
} else {
names = GO2Utilities.propertiesNames(rules);
if (names.contains("*")) {
// we need all properties
names = null;
}
}
// performance routine, only one symbol to render
if (renderers.rules.length == 1 && (renderers.rules[0].getFilter() == null || renderers.rules[0].getFilter() == Filter.include()) && renderers.rules[0].symbolizers().length == 1) {
stream = Stream.concat(stream, renderers.renderers[0][0].presentations(layer, resource));
continue;
}
if (resource instanceof GridCoverageResource) {
boolean painted = false;
for (int i = 0; i < renderers.elseRuleIndex; i++) {
final CachedRule rule = renderers.rules[i];
final Filter ruleFilter = rule.getFilter();
// test if the rule is valid for this feature
if (ruleFilter == null || ruleFilter.test(resource)) {
painted = true;
for (final SymbolizerRenderer renderer : renderers.renderers[i]) {
stream = Stream.concat(stream, renderer.presentations(layer, resource));
}
}
}
// the data hasn't been painted, paint it with the 'else' rules
if (!painted) {
for (int i = renderers.elseRuleIndex; i < renderers.rules.length; i++) {
final CachedRule rule = renderers.rules[i];
final Filter ruleFilter = rule.getFilter();
// test if the rule is valid for this feature
if (ruleFilter == null || ruleFilter.test(resource)) {
for (final SymbolizerRenderer renderer : renderers.renderers[i]) {
stream = Stream.concat(stream, renderer.presentations(layer, resource));
}
}
}
}
} else if (resource instanceof FeatureSet) {
final FeatureSet fs = (FeatureSet) resource;
// calculate max symbol size, to expand search envelope.
double symbolsMargin = 0.0;
for (CachedRule rule : cachedRules) {
for (CachedSymbolizer cs : rule.symbolizers()) {
symbolsMargin = Math.max(symbolsMargin, cs.getMargin(null, renderContext));
}
}
if (Double.isNaN(symbolsMargin) || Double.isInfinite(symbolsMargin)) {
// symbol margin can not be pre calculated, expect a max of 300pixels
symbolsMargin = 300f;
}
if (symbolsMargin > 0) {
final double scale = AffineTransforms2D.getScale(renderContext.getDisplayToObjective());
symbolsMargin = scale * symbolsMargin;
}
// optimize
final Query query;
try {
query = RenderingRoutines.prepareQuery(renderContext, fs, layer, names, rules, symbolsMargin);
final Stream<Presentation> s = fs.subset(query).features(false).flatMap(new Function<Feature, Stream<Presentation>>() {
@Override
public Stream<Presentation> apply(Feature feature) {
Stream<Presentation> stream = Stream.empty();
boolean painted = false;
for (int i = 0; i < renderers.elseRuleIndex; i++) {
final CachedRule rule = renderers.rules[i];
final Filter ruleFilter = rule.getFilter();
// test if the rule is valid for this feature
if (ruleFilter == null || ruleFilter.test(feature)) {
painted = true;
for (final SymbolizerRenderer renderer : renderers.renderers[i]) {
stream = Stream.concat(stream, renderer.presentations(layer, feature));
}
}
}
// the feature hasn't been painted, paint it with the 'else' rules
if (!painted) {
for (int i = renderers.elseRuleIndex; i < renderers.rules.length; i++) {
final CachedRule rule = renderers.rules[i];
final Filter ruleFilter = rule.getFilter();
// test if the rule is valid for this feature
if (ruleFilter == null || ruleFilter.test(feature)) {
for (final SymbolizerRenderer renderer : renderers.renderers[i]) {
stream = Stream.concat(stream, renderer.presentations(layer, feature));
}
}
}
}
return stream;
}
});
stream = Stream.concat(stream, s);
} catch (PortrayalException | DataStoreException ex) {
ExceptionPresentation ep = new ExceptionPresentation(ex);
ep.setLayer(layer);
ep.setResource(resource);
return Stream.of(ep);
}
}
}
return stream;
}
Aggregations