⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wcsutils.java

📁 电子地图服务器,搭建自己的地图服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
 * This code is licensed under the GPL 2.0 license, availible at the root
 * application directory.
 */
package org.vfny.geoserver.util;

import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridGeometry2D;
import org.geotools.coverage.processing.DefaultProcessor;
import org.geotools.coverage.processing.operation.Crop;
import org.geotools.coverage.processing.operation.FilteredSubsample;
import org.geotools.coverage.processing.operation.Interpolate;
import org.geotools.coverage.processing.operation.Resample;
import org.geotools.coverage.processing.operation.Scale;
import org.geotools.coverage.processing.operation.SelectSampleDimension;
import org.geotools.factory.Hints;
import org.geotools.geometry.GeneralEnvelope;
import org.geotools.resources.CRSUtilities;
import org.opengis.coverage.Coverage;
import org.opengis.coverage.grid.GridCoverage;
import org.opengis.coverage.grid.GridRange;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.vfny.geoserver.wcs.WcsException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Logger;
import javax.media.jai.BorderExtender;
import javax.media.jai.Interpolation;
import javax.media.jai.InterpolationNearest;


/**
 *
 * @author Simone Giannecchini, GeoSolutions
 * @author Alessio Fabiani, GeoSolutions
 *
 */
public class WCSUtils {
    private final static Hints LENIENT_HINT = new Hints(Hints.LENIENT_DATUM_SHIFT, Boolean.TRUE);
    private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.vfny.geoserver.util");
    private final static SelectSampleDimension bandSelectFactory = new SelectSampleDimension();
    private final static Crop cropFactory = new Crop();
    private final static Interpolate interpolateFactory = new Interpolate();
    private final static Scale scaleFactory = new Scale();
    private final static FilteredSubsample filteredSubsampleFactory = new FilteredSubsample();
    private final static Resample resampleFactory = new Resample();

    static {
        // ///////////////////////////////////////////////////////////////////
        //
        // Static Processors
        //
        // ///////////////////////////////////////////////////////////////////
        final DefaultProcessor processor = new DefaultProcessor(LENIENT_HINT);
        bandSelectParams = processor.getOperation("SelectSampleDimension").getParameters();
        cropParams = processor.getOperation("CoverageCrop").getParameters();
        interpolateParams = processor.getOperation("Interpolate").getParameters();
        scaleParams = processor.getOperation("Scale").getParameters();
        resampleParams = processor.getOperation("Resample").getParameters();
        filteredSubsampleParams = processor.getOperation("FilteredSubsample").getParameters();
    }

    private final static ParameterValueGroup bandSelectParams;
    private final static ParameterValueGroup cropParams;
    private final static ParameterValueGroup interpolateParams;
    private final static ParameterValueGroup resampleParams;
    private final static ParameterValueGroup scaleParams;
    private final static ParameterValueGroup filteredSubsampleParams;
    private final static Hints hints = new Hints(new HashMap(5));

    static {
        hints.add(LENIENT_HINT);
    }

    /**
     * <strong>Reprojecting</strong><br>
     * The new grid geometry can have a different coordinate reference system
     * than the underlying grid geometry. For example, a grid coverage can be
     * reprojected from a geodetic coordinate reference system to Universal
     * Transverse Mercator CRS.
     *
     * @param coverage
     *            GridCoverage2D
     * @param sourceCRS
     *            CoordinateReferenceSystem
     * @param targetCRS
     *            CoordinateReferenceSystem
     * @return GridCoverage2D
     * @throws WcsException
     */
    public static GridCoverage2D reproject(GridCoverage2D coverage,
        final CoordinateReferenceSystem sourceCRS, final CoordinateReferenceSystem targetCRS,
        final Interpolation interpolation) throws WcsException {
        // ///////////////////////////////////////////////////////////////////
        //
        // REPROJECT
        //
        //
        // ///////////////////////////////////////////////////////////////////
        if (!CRSUtilities.equalsIgnoreMetadata(sourceCRS, targetCRS)) {
            /*
             * Operations.DEFAULT.resample( coverage, targetCRS, null,
             * Interpolation.getInstance(Interpolation.INTERP_NEAREST))
             */
            final ParameterValueGroup param = (ParameterValueGroup) resampleParams.clone();
            param.parameter("Source").setValue(coverage);
            param.parameter("CoordinateReferenceSystem").setValue(targetCRS);
            param.parameter("GridGeometry").setValue(null);
            param.parameter("InterpolationType").setValue(interpolation);

            coverage = (GridCoverage2D) resampleFactory.doOperation(param, hints);
        }

        return coverage;
    }

    /**
     * <strong>Interpolating</strong><br>
     * Specifies the interpolation type to be used to interpolate values for
     * points which fall between grid cells. The default value is nearest
     * neighbor. The new interpolation type operates on all sample dimensions.
     * Possible values for type are: {@code "NearestNeighbor"},
     * {@code "Bilinear"} and {@code "Bicubic"} (the {@code "Optimal"}
     * interpolation type is currently not supported).
     *
     * @param coverage
     *            GridCoverage2D
     * @param interpolation
     *            Interpolation
     * @return GridCoverage2D
     * @throws WcsException
     */
    public static GridCoverage2D interpolate(GridCoverage2D coverage,
        final Interpolation interpolation) throws WcsException {
        // ///////////////////////////////////////////////////////////////////
        //
        // INTERPOLATE
        //
        //
        // ///////////////////////////////////////////////////////////////////
        if (interpolation != null) {
            /* Operations.DEFAULT.interpolate(coverage, interpolation) */
            final ParameterValueGroup param = (ParameterValueGroup) interpolateParams.clone();
            param.parameter("Source").setValue(coverage);
            param.parameter("Type").setValue(interpolation);

            coverage = (GridCoverage2D) interpolateFactory.doOperation(param, hints);
        }

        return coverage;
    }

    /**
     * <strong>Scaling</strong><br>
     * Let user to scale down to the EXACT needed resolution. This step does not
     * prevent from having loaded an overview of the original image based on the
     * requested scale.
     *
     * @param coverage
     *            GridCoverage2D
     * @param newGridRange
     *            GridRange
     * @param sourceCoverage
     *            GridCoverage
     * @param sourceCRS
     *            CoordinateReferenceSystem
     * @param destinationEnvelopeInSourceCRS
     * @return GridCoverage2D
     */
    public static GridCoverage2D scale(final GridCoverage2D coverage, final GridRange newGridRange,
        final GridCoverage sourceCoverage, final CoordinateReferenceSystem sourceCRS,
        final GeneralEnvelope destinationEnvelopeInSourceCRS) {
        // ///////////////////////////////////////////////////////////////////
        //
        // SCALE to the needed resolution
        // Let me now scale down to the EXACT needed resolution. This step does
        // not prevent from having loaded an overview of the original image
        // based on the requested scale.
        //
        // ///////////////////////////////////////////////////////////////////
        GridGeometry2D scaledGridGeometry = new GridGeometry2D(newGridRange,
                (destinationEnvelopeInSourceCRS != null) ? destinationEnvelopeInSourceCRS
                                                         : sourceCoverage.getEnvelope());

        /*
         * Operations.DEFAULT.resample( coverage, sourceCRS, scaledGridGeometry,
         * Interpolation.getInstance(Interpolation.INTERP_NEAREST));
         */
        final ParameterValueGroup param = (ParameterValueGroup) resampleParams.clone();
        param.parameter("Source").setValue(coverage);
        param.parameter("CoordinateReferenceSystem").setValue(sourceCRS);
        param.parameter("GridGeometry").setValue(scaledGridGeometry);
        param.parameter("InterpolationType")
             .setValue(Interpolation.getInstance(Interpolation.INTERP_NEAREST));

        final GridCoverage2D scaledGridCoverage = (GridCoverage2D) resampleFactory.doOperation(param,
                hints);

        return scaledGridCoverage;
    }

    /**
     * Scaling the input coverage using the provided parameters.
     *
     * @param scaleX
     * @param scaleY
     * @param xTrans
     * @param yTrans
     * @param interpolation
     * @param be
     * @param gc
     * @return
     */
    public static GridCoverage2D scale(final double scaleX, final double scaleY, float xTrans,
        float yTrans, final Interpolation interpolation, final BorderExtender be,
        final GridCoverage2D gc) {
        final ParameterValueGroup param = (ParameterValueGroup) scaleParams.clone();
        param.parameter("source").setValue(gc);
        param.parameter("xScale").setValue(new Float(scaleX));
        param.parameter("yScale").setValue(new Float(scaleY));
        param.parameter("xTrans").setValue(new Float(xTrans));
        param.parameter("yTrans").setValue(new Float(yTrans));
        param.parameter("Interpolation").setValue(interpolation);
        param.parameter("BorderExtender").setValue(be);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -