Search in sources :

Example 1 with Resampling

use of org.esa.snap.core.dataop.resamp.Resampling in project s1tbx by senbox-org.

the class GeolocationGridGeocodingOp method getPixelValue.

/**
 * Compute orthorectified pixel value for given pixel.
 *
 * @param azimuthIndex The azimuth index for pixel in source image.
 * @param rangeIndex   The range index for pixel in source image.
 * @return The pixel value.
 */
private double getPixelValue(final double azimuthIndex, final double rangeIndex, final Band sourceBand1, final Band sourceBand2) {
    try {
        final int x0 = (int) (rangeIndex + 0.5);
        final int y0 = (int) (azimuthIndex + 0.5);
        Rectangle srcRect = null;
        Tile sourceTileI, sourceTileQ = null;
        if (imgResampling.equals(Resampling.NEAREST_NEIGHBOUR)) {
            srcRect = new Rectangle(x0, y0, 1, 1);
        } else if (imgResampling.equals(Resampling.BILINEAR_INTERPOLATION)) {
            srcRect = new Rectangle(Math.max(0, x0 - 1), Math.max(0, y0 - 1), 3, 3);
        } else if (imgResampling.equals(Resampling.CUBIC_CONVOLUTION)) {
            srcRect = new Rectangle(Math.max(0, x0 - 2), Math.max(0, y0 - 2), 5, 5);
        } else if (imgResampling.equals(Resampling.BISINC_5_POINT_INTERPOLATION)) {
            srcRect = new Rectangle(Math.max(0, x0 - 3), Math.max(0, y0 - 3), 6, 6);
        } else if (imgResampling == Resampling.BISINC_11_POINT_INTERPOLATION) {
            srcRect = new Rectangle(Math.max(0, x0 - 6), Math.max(0, y0 - 6), 12, 12);
        } else if (imgResampling == Resampling.BISINC_21_POINT_INTERPOLATION) {
            srcRect = new Rectangle(Math.max(0, x0 - 11), Math.max(0, y0 - 11), 22, 22);
        } else if (imgResampling.equals(Resampling.BICUBIC_INTERPOLATION)) {
            srcRect = new Rectangle(Math.max(0, x0 - 2), Math.max(0, y0 - 2), 5, 5);
        } else {
            throw new OperatorException("Unhandled interpolation method");
        }
        sourceTileI = getSourceTile(sourceBand1, srcRect);
        if (sourceBand2 != null) {
            sourceTileQ = getSourceTile(sourceBand2, srcRect);
        }
        final ResamplingRaster imgResamplingRaster = new ResamplingRaster(sourceTileI, sourceTileQ);
        final Resampling resampling = imgResampling;
        final Resampling.Index imgResamplingIndex = resampling.createIndex();
        resampling.computeCornerBasedIndex(rangeIndex, azimuthIndex, sourceImageWidth, sourceImageHeight, imgResamplingIndex);
        return resampling.resample(imgResamplingRaster, imgResamplingIndex);
    } catch (Throwable e) {
        OperatorUtils.catchOperatorException(getId(), e);
    }
    return 0;
}
Also used : Tile(org.esa.snap.core.gpf.Tile) Resampling(org.esa.snap.core.dataop.resamp.Resampling) OperatorException(org.esa.snap.core.gpf.OperatorException)

Example 2 with Resampling

use of org.esa.snap.core.dataop.resamp.Resampling in project s1tbx by senbox-org.

the class MosaicOp method computeTileStack.

/**
 * Called by the framework in order to compute the stack of tiles for the given target bands.
 * <p>The default implementation throws a runtime exception with the message "not implemented".</p>
 *
 * @param targetTiles     The current tiles to be computed for each target band.
 * @param targetRectangle The area in pixel coordinates to be computed (same for all rasters in <code>targetRasters</code>).
 * @param pm              A progress monitor which should be used to determine computation cancelation requests.
 * @throws OperatorException if an error occurs during computation of the target rasters.
 */
@Override
public void computeTileStack(Map<Band, Tile> targetTiles, Rectangle targetRectangle, ProgressMonitor pm) throws OperatorException {
    try {
        final List<Product> validProducts = new ArrayList<>(sourceProduct.length);
        for (final Product srcProduct : selectedProducts) {
            final Rectangle srcRect = srcRectMap.get(srcProduct);
            if (srcRect == null || !srcRect.intersects(targetRectangle)) {
                continue;
            }
            validProducts.add(srcProduct);
        }
        if (validProducts.isEmpty()) {
            return;
        }
        final GeoPos geoPos = new GeoPos();
        final PixelPos pixelPos = new PixelPos();
        final int minX = targetRectangle.x;
        final int minY = targetRectangle.y;
        final int maxX = targetRectangle.x + targetRectangle.width - 1;
        final int maxY = targetRectangle.y + targetRectangle.height - 1;
        final TileGeoreferencing tileGeoRef = new TileGeoreferencing(targetProduct, minX, minY, maxX - minX + 1, maxY - minY + 1);
        final List<PixelPos[]> srcPixelCoords = new ArrayList<>(validProducts.size());
        final int numPixelPos = targetRectangle.width * targetRectangle.height;
        for (Product validProduct : validProducts) {
            srcPixelCoords.add(new PixelPos[numPixelPos]);
        }
        int coordIndex = 0;
        int prodIndex;
        for (int y = minY; y <= maxY; ++y) {
            for (int x = minX; x <= maxX; ++x) {
                tileGeoRef.getGeoPos(x, y, geoPos);
                prodIndex = 0;
                for (final Product srcProduct : validProducts) {
                    srcProduct.getSceneGeoCoding().getPixelPos(geoPos, pixelPos);
                    if (pixelPos.x >= feather && pixelPos.y >= feather && pixelPos.x < srcProduct.getSceneRasterWidth() - feather && pixelPos.y < srcProduct.getSceneRasterHeight() - feather) {
                        srcPixelCoords.get(prodIndex)[coordIndex] = new PixelPos(pixelPos.x, pixelPos.y);
                    } else {
                        srcPixelCoords.get(prodIndex)[coordIndex] = null;
                    }
                    ++prodIndex;
                }
                ++coordIndex;
            }
        }
        final Resampling resampling = ResamplingFactory.createResampling(resamplingMethod);
        if (resampling == null) {
            throw new OperatorException("Resampling method " + resamplingMethod + " is invalid");
        }
        if (gradientDomainMosaic) {
            performGradientDomainMosaic(targetTiles, targetRectangle, srcPixelCoords, validProducts, resampling, pm);
            return;
        }
        final List<SourceData> validSourceData = new ArrayList<>(validProducts.size());
        for (final Map.Entry<Band, Tile> bandTileEntry : targetTiles.entrySet()) {
            final String trgBandName = bandTileEntry.getKey().getName();
            validSourceData.clear();
            prodIndex = 0;
            for (final Product srcProduct : validProducts) {
                final Band srcBand = srcProduct.getBand(trgBandName);
                if (srcBand == null) {
                    continue;
                }
                final PixelPos[] pixPos = srcPixelCoords.get(prodIndex);
                final Rectangle sourceRectangle = getBoundingBox(pixPos, feather, feather, srcProduct.getSceneRasterWidth() - feather, srcProduct.getSceneRasterHeight() - feather, 4);
                if (sourceRectangle != null) {
                    double min = 0, max = 0, mean = 0, std = 0;
                    if (normalizeByMean) {
                        // get stat values
                        try {
                            final Stx stats = srcBand.getStx(true, ProgressMonitor.NULL);
                            mean = stats.getMean();
                            min = stats.getMinimum();
                            max = stats.getMaximum();
                            std = stats.getStandardDeviation();
                        } catch (Throwable e) {
                            // OperatorUtils.catchOperatorException(getId(), e);
                            // temporary disable
                            normalizeByMean = false;
                        }
                    }
                    try {
                        final Tile srcTile = getSourceTile(srcBand, sourceRectangle);
                        if (srcTile != null) {
                            validSourceData.add(new SourceData(srcTile, pixPos, resampling, min, max, mean, std));
                        }
                    } catch (Exception e) {
                        SystemUtils.LOG.severe("Mosaic getSourceTile failed " + e.getMessage());
                    // continue
                    }
                }
                ++prodIndex;
            }
            if (!validSourceData.isEmpty()) {
                collocateSourceBand(validSourceData, resampling, bandTileEntry.getValue());
            }
        }
    } catch (Throwable e) {
        OperatorUtils.catchOperatorException(getId(), e);
    } finally {
        pm.done();
    }
}
Also used : TargetProduct(org.esa.snap.core.gpf.annotations.TargetProduct) Tile(org.esa.snap.core.gpf.Tile) TileGeoreferencing(org.esa.snap.engine_utilities.gpf.TileGeoreferencing) UnformattableObjectException(org.geotools.referencing.wkt.UnformattableObjectException) OperatorException(org.esa.snap.core.gpf.OperatorException) Resampling(org.esa.snap.core.dataop.resamp.Resampling) OperatorException(org.esa.snap.core.gpf.OperatorException)

Example 3 with Resampling

use of org.esa.snap.core.dataop.resamp.Resampling in project s1tbx by senbox-org.

the class Collocator method collocateSourceBand.

public void collocateSourceBand(final RasterDataNode sourceBand, final Tile targetTile, final Resampling selectedResampling) throws OperatorException {
    final RasterDataNode targetBand = targetTile.getRasterDataNode();
    final Rectangle targetRectangle = targetTile.getRectangle();
    final ProductData trgBuffer = targetTile.getDataBuffer();
    final float noDataValue = (float) targetBand.getGeophysicalNoDataValue();
    final int maxX = targetRectangle.x + targetRectangle.width;
    final int maxY = targetRectangle.y + targetRectangle.height;
    Tile sourceTile = null;
    if (sourceRectangle != null)
        sourceTile = operator.getSourceTile(sourceBand, sourceRectangle);
    if (sourceTile != null) {
        final Product srcProduct = sourceBand.getProduct();
        final int sourceRasterHeight = srcProduct.getSceneRasterHeight();
        final int sourceRasterWidth = srcProduct.getSceneRasterWidth();
        final Resampling resampling;
        if (isFlagBand(sourceBand) || isValidPixelExpressionUsed(sourceBand)) {
            resampling = Resampling.NEAREST_NEIGHBOUR;
        } else {
            resampling = selectedResampling;
        }
        final Resampling.Index resamplingIndex = resampling.createIndex();
        final ResamplingRaster resamplingRaster = new ResamplingRaster(sourceTile);
        for (int y = targetRectangle.y, index = 0; y < maxY; ++y) {
            for (int x = targetRectangle.x; x < maxX; ++x, ++index) {
                final PixelPos sourcePixelPos = sourcePixelPositions[index];
                final int trgIndex = targetTile.getDataBufferIndex(x, y);
                if (sourcePixelPos != null) {
                    resampling.computeIndex(sourcePixelPos.x, sourcePixelPos.y, sourceRasterWidth, sourceRasterHeight, resamplingIndex);
                    try {
                        double sample = resampling.resample(resamplingRaster, resamplingIndex);
                        if (Double.isNaN(sample)) {
                            sample = noDataValue;
                        }
                        trgBuffer.setElemDoubleAt(trgIndex, sample);
                    } catch (Exception e) {
                        throw new OperatorException(e.getMessage());
                    }
                } else {
                    trgBuffer.setElemDoubleAt(trgIndex, noDataValue);
                }
            }
        }
        sourceTile.getDataBuffer().dispose();
    } else {
        final TileIndex trgIndex = new TileIndex(targetTile);
        for (int y = targetRectangle.y, index = 0; y < maxY; ++y) {
            trgIndex.calculateStride(y);
            for (int x = targetRectangle.x; x < maxX; ++x, ++index) {
                trgBuffer.setElemDoubleAt(trgIndex.getIndex(x), noDataValue);
            }
        }
    }
}
Also used : TileIndex(org.esa.snap.engine_utilities.gpf.TileIndex) Tile(org.esa.snap.core.gpf.Tile) OperatorException(org.esa.snap.core.gpf.OperatorException) Resampling(org.esa.snap.core.dataop.resamp.Resampling) OperatorException(org.esa.snap.core.gpf.OperatorException)

Example 4 with Resampling

use of org.esa.snap.core.dataop.resamp.Resampling in project s1tbx by senbox-org.

the class WarpOp method createDEM.

private synchronized void createDEM() {
    final Resampling resampling = ResamplingFactory.createResampling(ResamplingFactory.BILINEAR_INTERPOLATION_NAME);
    if (dem != null)
        return;
    final ElevationModelRegistry elevationModelRegistry = ElevationModelRegistry.getInstance();
    final ElevationModelDescriptor demDescriptor = elevationModelRegistry.getDescriptor(demName);
    if (demDescriptor == null) {
        throw new OperatorException("The DEM '" + demName + "' is not supported.");
    }
    dem = demDescriptor.createDem(resampling);
    if (dem == null) {
        throw new OperatorException("The DEM '" + demName + "' has not been installed.");
    }
    demNoDataValue = demDescriptor.getNoDataValue();
}
Also used : ElevationModelRegistry(org.esa.snap.core.dataop.dem.ElevationModelRegistry) ElevationModelDescriptor(org.esa.snap.core.dataop.dem.ElevationModelDescriptor) Resampling(org.esa.snap.core.dataop.resamp.Resampling) OperatorException(org.esa.snap.core.gpf.OperatorException)

Example 5 with Resampling

use of org.esa.snap.core.dataop.resamp.Resampling in project s1tbx by senbox-org.

the class SimulateAmplitudeOp method defineDEM.

private void defineDEM() throws IOException {
    final ElevationModelRegistry elevationModelRegistry = ElevationModelRegistry.getInstance();
    final ElevationModelDescriptor demDescriptor = elevationModelRegistry.getDescriptor(demName);
    if (demDescriptor == null) {
        throw new OperatorException("The DEM '" + demName + "' is not supported.");
    }
    Resampling resampling = Resampling.BILINEAR_INTERPOLATION;
    if (externalDEMFile != null) {
        // if external DEM file is specified by user
        dem = new FileElevationModel(externalDEMFile, resampling.getName(), externalDEMNoDataValue);
        demNoDataValue = externalDEMNoDataValue;
        demName = externalDEMFile.getPath();
    } else {
        dem = demDescriptor.createDem(resampling);
        if (dem == null)
            throw new OperatorException("The DEM '" + demName + "' has not been installed.");
        demNoDataValue = demDescriptor.getNoDataValue();
        demSampling = demDescriptor.getTileWidthInDegrees() * (1.0f / demDescriptor.getTileWidth()) * Constants.DTOR;
    }
}
Also used : ElevationModelRegistry(org.esa.snap.core.dataop.dem.ElevationModelRegistry) ElevationModelDescriptor(org.esa.snap.core.dataop.dem.ElevationModelDescriptor) Resampling(org.esa.snap.core.dataop.resamp.Resampling) FileElevationModel(org.esa.snap.dem.dataio.FileElevationModel) OperatorException(org.esa.snap.core.gpf.OperatorException)

Aggregations

Resampling (org.esa.snap.core.dataop.resamp.Resampling)6 OperatorException (org.esa.snap.core.gpf.OperatorException)6 ElevationModelDescriptor (org.esa.snap.core.dataop.dem.ElevationModelDescriptor)3 ElevationModelRegistry (org.esa.snap.core.dataop.dem.ElevationModelRegistry)3 Tile (org.esa.snap.core.gpf.Tile)3 FileElevationModel (org.esa.snap.dem.dataio.FileElevationModel)2 IOException (java.io.IOException)1 TargetProduct (org.esa.snap.core.gpf.annotations.TargetProduct)1 TileGeoreferencing (org.esa.snap.engine_utilities.gpf.TileGeoreferencing)1 TileIndex (org.esa.snap.engine_utilities.gpf.TileIndex)1 UnformattableObjectException (org.geotools.referencing.wkt.UnformattableObjectException)1