Search in sources :

Example 1 with BandedCoverageResource

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();
}
Also used : DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) DataStoreException(org.apache.sis.storage.DataStoreException) GridCoverage2D(org.apache.sis.coverage.grid.GridCoverage2D) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) BandedCoverageResource(org.geotoolkit.storage.coverage.BandedCoverageResource) RasterPresentation(org.geotoolkit.display2d.presentation.RasterPresentation) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) PortrayalException(org.geotoolkit.display.PortrayalException) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) FactoryException(org.opengis.util.FactoryException) CannotEvaluateException(org.opengis.coverage.CannotEvaluateException) IOException(java.io.IOException) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) TransformException(org.opengis.referencing.operation.TransformException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) RasterSymbolizer(org.opengis.style.RasterSymbolizer) CachedRasterSymbolizer(org.geotoolkit.display2d.style.CachedRasterSymbolizer) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource)

Example 2 with BandedCoverageResource

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);
    }
}
Also used : StorageConnector(org.apache.sis.storage.StorageConnector) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageIO.createImageOutputStream(javax.imageio.ImageIO.createImageOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FramedLZ4CompressorOutputStream(org.apache.commons.compress.compressors.lz4.FramedLZ4CompressorOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ImageWriter(javax.imageio.ImageWriter) URI(java.net.URI) IIOImage(javax.imageio.IIOImage) ImageTile(org.geotoolkit.storage.coverage.ImageTile) BufferedInputStream(java.io.BufferedInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Assets(org.geotoolkit.storage.Assets) DataStore(org.apache.sis.storage.DataStore) BufferedOutputStream(java.io.BufferedOutputStream) ImageIO.createImageOutputStream(javax.imageio.ImageIO.createImageOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) Path(java.nio.file.Path) DataStoreException(org.apache.sis.storage.DataStoreException) WritableBandedCoverageResource(org.geotoolkit.storage.coverage.WritableBandedCoverageResource) DataStoreProvider(org.apache.sis.storage.DataStoreProvider) BandedCoverageResource(org.geotoolkit.storage.coverage.BandedCoverageResource) Resource(org.apache.sis.storage.Resource) WritableBandedCoverageResource(org.geotoolkit.storage.coverage.WritableBandedCoverageResource) BandedCoverageResource(org.geotoolkit.storage.coverage.BandedCoverageResource) WritableBandedCoverageResource(org.geotoolkit.storage.coverage.WritableBandedCoverageResource) Data(org.geotoolkit.storage.Assets.Data) BandedCoverage(org.apache.sis.coverage.BandedCoverage) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) Point(java.awt.Point) DeferredTile(org.geotoolkit.storage.multires.DeferredTile) RenderedImage(java.awt.image.RenderedImage) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) File(java.io.File) FramedLZ4CompressorOutputStream(org.apache.commons.compress.compressors.lz4.FramedLZ4CompressorOutputStream)

Example 3 with BandedCoverageResource

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;
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.apache.sis.storage.Query) RenderingRules(org.geotoolkit.display2d.container.RenderingRules) Feature(org.opengis.feature.Feature) Function(java.util.function.Function) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) FeatureTypeStyle(org.opengis.style.FeatureTypeStyle) Style(org.opengis.style.Style) CachedRule(org.geotoolkit.display2d.style.CachedRule) Stream(java.util.stream.Stream) OutputStream(java.io.OutputStream) DataStoreException(org.apache.sis.storage.DataStoreException) BandedCoverageResource(org.geotoolkit.storage.coverage.BandedCoverageResource) Resource(org.apache.sis.storage.Resource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) BandedCoverageResource(org.geotoolkit.storage.coverage.BandedCoverageResource) Presentation(org.apache.sis.internal.map.Presentation) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) CachedSymbolizer(org.geotoolkit.display2d.style.CachedSymbolizer) SymbolizerRenderer(org.geotoolkit.display2d.style.renderer.SymbolizerRenderer) Filter(org.opengis.filter.Filter) FeatureSet(org.apache.sis.storage.FeatureSet) FeatureTypeStyle(org.opengis.style.FeatureTypeStyle) Rule(org.opengis.style.Rule) CachedRule(org.geotoolkit.display2d.style.CachedRule) Aggregate(org.apache.sis.storage.Aggregate) PortrayalException(org.geotoolkit.display.PortrayalException)

Aggregations

DataStoreException (org.apache.sis.storage.DataStoreException)3 BandedCoverageResource (org.geotoolkit.storage.coverage.BandedCoverageResource)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ExceptionPresentation (org.apache.sis.internal.map.ExceptionPresentation)2 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)2 Resource (org.apache.sis.storage.Resource)2 PortrayalException (org.geotoolkit.display.PortrayalException)2 Point (java.awt.Point)1 RenderedImage (java.awt.image.RenderedImage)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 Function (java.util.function.Function)1 Stream (java.util.stream.Stream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 IIOImage (javax.imageio.IIOImage)1