Search in sources :

Example 1 with ProcessListener

use of org.geotoolkit.process.ProcessListener in project geotoolkit by Geomatys.

the class AbstractTileGenerator method generate.

@Override
public void generate(TileMatrixSet pyramid, Envelope env, NumberRange resolutions, ProcessListener listener) throws DataStoreException, InterruptedException {
    if (env != null) {
        try {
            env = Envelopes.transform(env, pyramid.getCoordinateReferenceSystem());
        } catch (TransformException ex) {
            throw new DataStoreException(ex.getMessage(), ex);
        }
    }
    final long total = TileMatrices.countTiles(pyramid, env, resolutions);
    final AtomicLong al = new AtomicLong();
    // generate mosaic in resolution order
    // this order allows the pyramid to be used at high scales until she is not completed.
    final List<TileMatrix> mosaics = new ArrayList<>(pyramid.getTileMatrices());
    mosaics.sort((TileMatrix o1, TileMatrix o2) -> Double.compare(o2.getScale(), o1.getScale()));
    for (final TileMatrix mosaic : mosaics) {
        if (resolutions == null || resolutions.containsAny(mosaic.getScale())) {
            final Rectangle rect;
            try {
                rect = TileMatrices.getTilesInEnvelope(mosaic, env);
            } catch (NoSuchDataException ex) {
                continue;
            }
            final long nbTile = ((long) rect.width) * ((long) rect.height);
            final long eventstep = Math.min(1000, Math.max(1, nbTile / 100l));
            Stream<Tile> stream = LongStream.range(0, nbTile).parallel().mapToObj(new LongFunction<Tile>() {

                @Override
                public Tile apply(long value) {
                    final long x = rect.x + (value % rect.width);
                    final long y = rect.y + (value / rect.width);
                    Tile data = null;
                    try {
                        // do not regenerate existing tiles
                        // if (!mosaic.isMissing((int)x, (int)y)) return;
                        final Point coord = new Point((int) x, (int) y);
                        try {
                            data = generateTile(pyramid, mosaic, coord);
                        } catch (Exception ex) {
                            data = TileInError.create(coord, null, ex);
                        }
                    } finally {
                        long v = al.incrementAndGet();
                        if (listener != null & (v % eventstep == 0)) {
                            listener.progressing(new ProcessEvent(DUMMY, v + "/" + total + " mosaic=" + mosaic.getIdentifier() + " scale=" + mosaic.getScale(), (float) ((((double) v) / ((double) total)) * 100.0)));
                        }
                    }
                    return data;
                }
            }).filter(this::emptyFilter);
            batchWrite(stream, mosaic, listener == null ? null : err -> listener.progressing(new ProcessEvent(DUMMY, "Error while writing tile batch", (float) ((((double) al.get()) / ((double) total)) * 100.0))), 200);
            long v = al.get();
            if (listener != null) {
                listener.progressing(new ProcessEvent(DUMMY, v + "/" + total + " mosaic=" + mosaic.getIdentifier() + " scale=" + mosaic.getScale(), (float) ((((double) v) / ((double) total)) * 100.0)));
            }
        }
    }
}
Also used : Rectangle(java.awt.Rectangle) NumberRange(org.apache.sis.measure.NumberRange) ProcessEvent(org.geotoolkit.process.ProcessEvent) Point(java.awt.Point) Envelope(org.opengis.geometry.Envelope) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ProcessException(org.geotoolkit.process.ProcessException) IllegalGridGeometryException(org.apache.sis.coverage.grid.IllegalGridGeometryException) DataStoreException(org.apache.sis.storage.DataStoreException) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) TransformException(org.opengis.referencing.operation.TransformException) LongStream(java.util.stream.LongStream) LongFunction(java.util.function.LongFunction) Collection(java.util.Collection) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor) Logger(java.util.logging.Logger) Consumer(java.util.function.Consumer) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) Process(org.geotoolkit.process.Process) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProcessStep(org.opengis.metadata.lineage.ProcessStep) List(java.util.List) Stream(java.util.stream.Stream) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) Envelopes(org.apache.sis.geometry.Envelopes) ProcessListener(org.geotoolkit.process.ProcessListener) Streams(org.geotoolkit.util.Streams) DataStoreException(org.apache.sis.storage.DataStoreException) ProcessEvent(org.geotoolkit.process.ProcessEvent) TransformException(org.opengis.referencing.operation.TransformException) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) Point(java.awt.Point) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) ProcessException(org.geotoolkit.process.ProcessException) IllegalGridGeometryException(org.apache.sis.coverage.grid.IllegalGridGeometryException) DataStoreException(org.apache.sis.storage.DataStoreException) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) TransformException(org.opengis.referencing.operation.TransformException) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) AtomicLong(java.util.concurrent.atomic.AtomicLong) LongFunction(java.util.function.LongFunction)

Example 2 with ProcessListener

use of org.geotoolkit.process.ProcessListener in project geotoolkit by Geomatys.

the class CoverageTileGenerator method generate.

@Override
public void generate(TileMatrixSet pyramid, Envelope env, NumberRange resolutions, ProcessListener listener) throws DataStoreException, InterruptedException {
    if (!coverageIsHomogeneous) {
        super.generate(pyramid, env, resolutions, listener);
        return;
    }
    /*
        We can generate the pyramid starting from the lowest level then going up
        using the previously generated level.
        */
    if (env != null) {
        try {
            CoordinateReferenceSystem targetCrs = pyramid.getCoordinateReferenceSystem();
            Envelope baseEnv = env;
            env = Envelopes.transform(env, targetCrs);
            if (resolutions != null) {
                double[] minres = new double[] { resolutions.getMinDouble(), resolutions.getMinDouble() };
                double[] maxres = new double[] { resolutions.getMaxDouble(), resolutions.getMaxDouble() };
                minres = ReferencingUtilities.convertResolution(baseEnv, minres, targetCrs, null);
                maxres = ReferencingUtilities.convertResolution(baseEnv, maxres, targetCrs, null);
                resolutions = NumberRange.create(minres[0], true, maxres[0], true);
            }
        } catch (TransformException ex) {
            throw new DataStoreException(ex.getMessage(), ex);
        }
    }
    // generate lower level from data
    final TileMatrix[] mosaics = pyramid.getTileMatrices().toArray(new TileMatrix[0]);
    Arrays.sort(mosaics, (TileMatrix o1, TileMatrix o2) -> Double.compare(o1.getScale(), o2.getScale()));
    final long total = TileMatrices.countTiles(pyramid, env, resolutions);
    final double totalAsDouble = total;
    final AtomicLong al = new AtomicLong();
    final Supplier<Float> progress = () -> (float) (al.get() / totalAsDouble * 100.0);
    GridCoverageResource resourceCenter = this.resource;
    GridCoverageResource resourceBorder = this.resource;
    for (final TileMatrix mosaic : mosaics) {
        if (resolutions == null || resolutions.contains(mosaic.getScale())) {
            final Rectangle rect;
            try {
                rect = TileMatrices.getTilesInEnvelope(mosaic, env);
            } catch (NoSuchDataException ex) {
                continue;
            }
            final GridCoverageResource sourceCenter = resourceCenter;
            final GridCoverageResource sourceBorder = resourceBorder;
            final long nbTile = ((long) rect.width) * ((long) rect.height);
            final long eventstep = Math.min(1000, Math.max(1, nbTile / 100l));
            Stream<Tile> stream = LongStream.range(0, nbTile).parallel().mapToObj(new LongFunction<Tile>() {

                @Override
                public Tile apply(long value) {
                    final long x = rect.x + (value % rect.width);
                    final long y = rect.y + (value / rect.width);
                    final boolean isBorderTile = (x == rect.x || x == (rect.x + rect.width - 1)) || (y == rect.y || y == (rect.y + rect.height - 1));
                    Tile data = null;
                    try {
                        if (skipExistingTiles && !mosaic.isMissing((int) x, (int) y)) {
                            // tile already exist
                            return null;
                        }
                        final Point coord = new Point((int) x, (int) y);
                        try {
                            data = generateTile(pyramid, mosaic, coord, isBorderTile ? sourceBorder : sourceCenter);
                        } catch (Exception ex) {
                            data = TileInError.create(coord, null, ex);
                        }
                    } finally {
                        long v = al.incrementAndGet();
                        if (listener != null & (v % eventstep == 0)) {
                            listener.progressing(new ProcessEvent(DUMMY, v + "/" + total + " mosaic=" + mosaic.getIdentifier() + " scale=" + mosaic.getScale(), progress.get()));
                        }
                    }
                    return data;
                }
            }).filter(this::emptyFilter);
            batchWrite(stream, mosaic, listener == null ? null : err -> listener.progressing(new ProcessEvent(DUMMY, "Error while writing tile batch", progress.get(), err)), 200);
            long v = al.get();
            if (listener != null) {
                listener.progressing(new ProcessEvent(DUMMY, v + "/" + total + " mosaic=" + mosaic.getIdentifier() + " scale=" + mosaic.getScale(), progress.get()));
            }
            if (!generateFromSource) {
                // modify context
                final DefaultTileMatrixSet pm = new DefaultTileMatrixSet(pyramid.getCoordinateReferenceSystem());
                pm.getMosaicsInternal().add(mosaic);
                final InMemoryTiledGridCoverageResource r = new InMemoryTiledGridCoverageResource(NamesExt.create("test"));
                r.setSampleDimensions(resourceCenter.getSampleDimensions());
                r.getTileMatrixSets().add(pm);
                // we must still use the original resource for generation because
                // lower level tiles may not be sufficient to generate border tiles
                final AggregatedCoverageResource aggregated = new AggregatedCoverageResource();
                aggregated.setMode(AggregatedCoverageResource.Mode.ORDER);
                aggregated.add(r);
                aggregated.add(this.resource);
                aggregated.setInterpolation(interpolation.toSis());
                resourceCenter = r;
                resourceBorder = aggregated;
            }
        }
    }
}
Also used : Tile(org.geotoolkit.storage.multires.Tile) Arrays(java.util.Arrays) GridOrientation(org.apache.sis.coverage.grid.GridOrientation) CRS(org.apache.sis.referencing.CRS) PixelInCell(org.opengis.referencing.datum.PixelInCell) TileMatrixSet(org.geotoolkit.storage.multires.TileMatrixSet) NumberRange(org.apache.sis.measure.NumberRange) GridCoverageProcessor(org.apache.sis.coverage.grid.GridCoverageProcessor) ProcessEvent(org.geotoolkit.process.ProcessEvent) Point(java.awt.Point) TileMatrix(org.geotoolkit.storage.multires.TileMatrix) Envelope(org.opengis.geometry.Envelope) Future(java.util.concurrent.Future) EmptyTile(org.geotoolkit.storage.multires.EmptyTile) LinearTransform(org.apache.sis.referencing.operation.transform.LinearTransform) ReferencingUtilities(org.geotoolkit.referencing.ReferencingUtilities) TransformException(org.opengis.referencing.operation.TransformException) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) Dimension(java.awt.Dimension) List(java.util.List) Stream(java.util.stream.Stream) NamesExt(org.geotoolkit.util.NamesExt) TileInError(org.geotoolkit.storage.multires.TileInError) Interpolation(org.apache.sis.image.Interpolation) Envelopes(org.apache.sis.geometry.Envelopes) InterpolationCase(org.geotoolkit.image.interpolation.InterpolationCase) ProcessListener(org.geotoolkit.process.ProcessListener) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Rectangle(java.awt.Rectangle) SampleDimension(org.apache.sis.coverage.SampleDimension) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) AggregatedCoverageResource(org.geotoolkit.storage.coverage.mosaic.AggregatedCoverageResource) BufferedImages(org.geotoolkit.image.BufferedImages) ArgumentChecks(org.apache.sis.util.ArgumentChecks) SampleDimensionUtils(org.geotoolkit.coverage.SampleDimensionUtils) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) IllegalGridGeometryException(org.apache.sis.coverage.grid.IllegalGridGeometryException) CommonCRS(org.apache.sis.referencing.CommonCRS) DataStoreException(org.apache.sis.storage.DataStoreException) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) ImageProcessor(org.apache.sis.image.ImageProcessor) TileMatrices(org.geotoolkit.storage.multires.TileMatrices) LongStream(java.util.stream.LongStream) LongFunction(java.util.function.LongFunction) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) DefaultTileMatrixSet(org.geotoolkit.storage.multires.DefaultTileMatrixSet) StringUtilities(org.geotoolkit.util.StringUtilities) AtomicLong(java.util.concurrent.atomic.AtomicLong) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) ForkJoinPool(java.util.concurrent.ForkJoinPool) InMemoryTiledGridCoverageResource(org.geotoolkit.storage.memory.InMemoryTiledGridCoverageResource) GridExtent(org.apache.sis.coverage.grid.GridExtent) AbstractTileGenerator(org.geotoolkit.storage.multires.AbstractTileGenerator) InMemoryTiledGridCoverageResource(org.geotoolkit.storage.memory.InMemoryTiledGridCoverageResource) Rectangle(java.awt.Rectangle) Envelope(org.opengis.geometry.Envelope) DefaultTileMatrixSet(org.geotoolkit.storage.multires.DefaultTileMatrixSet) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) InMemoryTiledGridCoverageResource(org.geotoolkit.storage.memory.InMemoryTiledGridCoverageResource) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) TileMatrix(org.geotoolkit.storage.multires.TileMatrix) DataStoreException(org.apache.sis.storage.DataStoreException) ProcessEvent(org.geotoolkit.process.ProcessEvent) TransformException(org.opengis.referencing.operation.TransformException) Tile(org.geotoolkit.storage.multires.Tile) EmptyTile(org.geotoolkit.storage.multires.EmptyTile) Point(java.awt.Point) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) TransformException(org.opengis.referencing.operation.TransformException) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) IllegalGridGeometryException(org.apache.sis.coverage.grid.IllegalGridGeometryException) DataStoreException(org.apache.sis.storage.DataStoreException) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) LongFunction(java.util.function.LongFunction) AggregatedCoverageResource(org.geotoolkit.storage.coverage.mosaic.AggregatedCoverageResource)

Example 3 with ProcessListener

use of org.geotoolkit.process.ProcessListener in project geotoolkit by Geomatys.

the class ProcessJob method execute.

@Override
public void execute(final JobExecutionContext jec) throws JobExecutionException {
    final JobDataMap parameters = jec.getJobDetail().getJobDataMap();
    final Object objFactoryId = parameters.get(KEY_FACTORY_ID);
    final Object objProcessId = parameters.get(KEY_PROCESS_ID);
    final Object objProcessParams = parameters.get(KEY_PARAMETERS);
    final Object objProcess = parameters.get(KEY_PROCESS);
    if (!(objFactoryId instanceof String)) {
        throw new JobExecutionException("Factory id is not String, value found : " + objFactoryId);
    }
    if (!(objProcessId instanceof String)) {
        throw new JobExecutionException("Process id is not String, value found : " + objProcessId);
    }
    if (!(objProcessParams instanceof ParameterValueGroup)) {
        throw new JobExecutionException("Parameters is not an ISO parameter, value found : " + objProcessParams);
    }
    if (objProcess != null && !(objProcess instanceof Process)) {
        throw new JobExecutionException("Process object is invalid, value found : " + objProcess);
    }
    final String factoryId = (String) objFactoryId;
    final String processId = (String) objProcessId;
    final Parameters params = Parameters.castOrWrap((ParameterValueGroup) objProcessParams);
    process = (Process) objProcess;
    if (process == null) {
        final ProcessDescriptor desc = getProcessDescriptor(factoryId, processId);
        process = desc.createProcess(params);
    }
    final StoreExceptionMonitor monitor = new StoreExceptionMonitor();
    process.addListener(monitor);
    for (ProcessListener pl : listeners) {
        process.addListener(pl);
    }
    // set the result int he context, for listener that might want it.
    final ParameterValueGroup result;
    try {
        result = process.call();
    } catch (ProcessException ex) {
        if (monitor.failed != null) {
            throw monitor.failed;
        } else {
            throw new JobExecutionException(ex);
        }
    }
    jec.setResult(result);
}
Also used : JobDataMap(org.quartz.JobDataMap) ProcessException(org.geotoolkit.process.ProcessException) JobExecutionException(org.quartz.JobExecutionException) Parameters(org.apache.sis.parameter.Parameters) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ProcessListener(org.geotoolkit.process.ProcessListener) AbstractProcess(org.geotoolkit.processing.AbstractProcess) Process(org.geotoolkit.process.Process) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor)

Example 4 with ProcessListener

use of org.geotoolkit.process.ProcessListener in project geotoolkit by Geomatys.

the class ProcessJobDetail method getListeners.

public List<ProcessListener> getListeners() {
    final List<ProcessListener> listeners = new ArrayList<>();
    if (getJobDataMap().get(KEY_PROCESS) != null) {
        final AbstractProcess process = (AbstractProcess) getJobDataMap().get(KEY_PROCESS);
        Collections.addAll(listeners, process.getListeners());
    }
    return listeners;
}
Also used : AbstractProcess(org.geotoolkit.processing.AbstractProcess) ProcessListener(org.geotoolkit.process.ProcessListener) ArrayList(java.util.ArrayList)

Example 5 with ProcessListener

use of org.geotoolkit.process.ProcessListener in project geotoolkit by Geomatys.

the class ForwardProcessListener method fireProgressing.

private void fireProgressing(String message, float progress, Exception ex) {
    final ProcessEvent event = new ProcessEvent(parentProcess, message, progress, ex);
    final ProcessListener[] listeners = parentProcess.getListeners();
    for (ProcessListener listener : listeners) {
        listener.progressing(event);
    }
}
Also used : ProcessEvent(org.geotoolkit.process.ProcessEvent) ProcessListener(org.geotoolkit.process.ProcessListener)

Aggregations

ProcessListener (org.geotoolkit.process.ProcessListener)5 ArrayList (java.util.ArrayList)3 ProcessEvent (org.geotoolkit.process.ProcessEvent)3 Point (java.awt.Point)2 Rectangle (java.awt.Rectangle)2 List (java.util.List)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 LongFunction (java.util.function.LongFunction)2 Level (java.util.logging.Level)2 LongStream (java.util.stream.LongStream)2 Stream (java.util.stream.Stream)2 IllegalGridGeometryException (org.apache.sis.coverage.grid.IllegalGridGeometryException)2 Envelopes (org.apache.sis.geometry.Envelopes)2 NumberRange (org.apache.sis.measure.NumberRange)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 NoSuchDataException (org.apache.sis.storage.NoSuchDataException)2 AbstractProcess (org.geotoolkit.processing.AbstractProcess)2 Dimension (java.awt.Dimension)1 BufferedImage (java.awt.image.BufferedImage)1 RenderedImage (java.awt.image.RenderedImage)1