All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.vfny.geoserver.config.CoverageConfig Maven / Gradle / Ivy

There is a newer version: 1.7.0
Show newest version
/* 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.config;

import org.geotools.coverage.Category;
import org.geotools.coverage.GridSampleDimension;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridGeometry2D;
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
import org.geotools.coverage.grid.io.AbstractGridFormat;
import org.geotools.geometry.GeneralEnvelope;
import org.opengis.coverage.grid.Format;
import org.opengis.coverage.grid.GridGeometry;
import org.opengis.metadata.Identifier;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.TransformException;
import org.opengis.util.InternationalString;
import org.vfny.geoserver.global.ConfigurationException;
import org.vfny.geoserver.global.CoverageDimension;
import org.vfny.geoserver.global.MetaDataLink;
import org.vfny.geoserver.global.dto.CoverageInfoDTO;
import org.vfny.geoserver.util.CoverageStoreUtils;
import org.vfny.geoserver.util.CoverageUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.units.Unit;


/**
 * User interface Coverage staging area.
 *
 * @author dzwiers, Refractions Research, Inc.
 * @author $Author: Alessio Fabiani ([email protected]) $ (last
 *         modification)
 * @author $Author: Simone Giannecchini ([email protected]) $ (last
 *         modification)
 * @version $Id: CoverageConfig.java 7937 2007-12-05 15:06:07Z aaime $
 */
public class CoverageConfig {
    /**
     *
     */
    private String formatId;

    /**
     *
     */
    private String name;

    /**
     *
     */
    private String wmsPath;

    /**
     *
     */
    private String label;

    /**
     *
     */
    private String description;

    /**
     *
     */
    private MetaDataLink metadataLink;

    /**
     *
     */
    private String dirName;

    /**
     *
     */
    private List keywords;

    /**
     *
     */
    private GeneralEnvelope envelope;

    /**
     *
     */
    private GeneralEnvelope lonLatWGS84Envelope;

    /**
     *
     */
    private GridGeometry grid;

    /**
     *
     */
    private CoverageDimension[] dimensions;

    /**
     *
     */
    private InternationalString[] dimentionNames;

    /**
     *
     */
    private List requestCRSs;

    /**
     *
     */
    private List responseCRSs;

    /**
     *
     */
    private String nativeFormat;

    /**
     *
     */
    private List supportedFormats;

    /**
     *
     */
    private String defaultInterpolationMethod;

    /**
     *
     */
    private List interpolationMethods;

    /**
     *
     */
    private String srsName;

    /**
     *
     */
    private String srsWKT;

    /**
     *
     */
    private CoordinateReferenceSystem crs;

    /**
     * The default style name.
     */
    private String defaultStyle;

    /**
    * Other WMS Styles
    */
    private ArrayList styles;

    /**
     * String representation of connection parameter values
     */
    private Map parameters;

    /**
     * Package visible constructor for test cases
     */
    CoverageConfig() {
    }

    /**
     * Creating a coverage config from gridcoverages information
     *
     * @param formatId
     * @param format
     * @param gc
     * @throws ConfigurationException
     */
    public CoverageConfig(String formatId, Format format, AbstractGridCoverage2DReader reader,
        HttpServletRequest request) throws ConfigurationException {
        if ((formatId == null) || (formatId.length() == 0)) {
            throw new IllegalArgumentException("formatId is required for CoverageConfig");
        }

        if (format == null) {
            throw new ConfigurationException(new StringBuffer("Cannot handle format: ").append(
                    formatId).toString());
        }

        this.formatId = formatId;

        final DataConfig dataConfig = getDataConfig(request);
        final CoverageStoreConfig cvConfig = dataConfig.getDataFormat(formatId);

        if (cvConfig == null) {
            // something is horribly wrong no FormatID selected!
            // The JSP needs to not include us if there is no
            // selected Format
            //
            throw new RuntimeException("selectedCoverageSetId required in Session");
        }

        crs = reader.getCrs();
        srsName = (((crs != null) && !crs.getIdentifiers().isEmpty())
            ? crs.getIdentifiers().toArray()[0].toString() : "UNKNOWN");
        srsWKT = ((crs != null) ? crs.toWKT() : "UNKNOWN");
        envelope = reader.getOriginalEnvelope();

        try {
            lonLatWGS84Envelope = CoverageStoreUtils.getWGS84LonLatEnvelope(envelope);
        } catch (IndexOutOfBoundsException e) {
            final ConfigurationException newEx = new ConfigurationException(new StringBuffer(
                        "Converting Envelope to Lat-Lon WGS84: ").append(e.toString()).toString());
            newEx.initCause(e);
            throw newEx;
        } catch (FactoryException e) {
            final ConfigurationException newEx = new ConfigurationException(new StringBuffer(
                        "Converting Envelope to Lat-Lon WGS84: ").append(e.toString()).toString());
            newEx.initCause(e);
            throw newEx;
        } catch (TransformException e) {
            final ConfigurationException newEx = new ConfigurationException(new StringBuffer(
                        "Converting Envelope to Lat-Lon WGS84: ").append(e.toString()).toString());
            newEx.initCause(e);
            throw newEx;
        }

        /**
         * Now reading a fake small GridCoverage just to retrieve meta information:
         * - calculating a new envelope which is 1/20 of the original one
         * - reading the GridCoverage subset
         */
        final GridCoverage2D gc;

        try {
            final ParameterValueGroup readParams = format.getReadParameters();
            final Map parameters = CoverageUtils.getParametersKVP(readParams);

            double[] minCP = envelope.getLowerCorner().getCoordinates();
            double[] maxCP = new double[] {
                    minCP[0] + (envelope.getLength(0) / 20.0),
                    minCP[1] + (envelope.getLength(1) / 20.0)
                };
            final GeneralEnvelope subEnvelope = new GeneralEnvelope(minCP, maxCP);
            subEnvelope.setCoordinateReferenceSystem(reader.getCrs());

            parameters.put(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName().toString(),
                new GridGeometry2D(reader.getOriginalGridRange(), subEnvelope));
            gc = (GridCoverage2D) reader.read(CoverageUtils.getParameters(readParams, parameters,
                        true));
            // //
            // AlFa: Getting the original coverage GridGeometry
            // //
            grid = gc.getGridGeometry();
            dimensions = parseCoverageDimesions(gc.getSampleDimensions());
        } catch (UnsupportedEncodingException e) {
            final ConfigurationException newEx = new ConfigurationException(new StringBuffer(
                        "Coverage dimensions: ").append(e.toString()).toString());
            newEx.initCause(e);
            throw newEx;
        } catch (IllegalArgumentException e) {
            final ConfigurationException newEx = new ConfigurationException(new StringBuffer(
                        "Coverage dimensions: ").append(e.toString()).toString());
            newEx.initCause(e);
            throw newEx;
        } catch (IOException e) {
            final ConfigurationException newEx = new ConfigurationException(new StringBuffer(
                        "Coverage dimensions: ").append(e.toString()).toString());
            newEx.initCause(e);
            throw newEx;
        }

        dimentionNames = gc.getDimensionNames();

        final DataConfig config = ConfigRequests.getDataConfig(request);
        StringBuffer cvName = new StringBuffer(gc.getName().toString());
        int count = 0;
        StringBuffer key;
        Map coverages;
        Set cvKeySet;
        boolean key_exists;
        String cvKey;
        Iterator it;

        while (true) {
            key = new StringBuffer(gc.getName().toString());

            if (count > 0) {
                key.append("_").append(count) /*.append("]")*/;
            }

            coverages = config.getCoverages();
            cvKeySet = coverages.keySet();
            key_exists = false;

            for (it = cvKeySet.iterator(); it.hasNext();) {
                cvKey = ((String) it.next()).toLowerCase();

                if (cvKey.endsWith(key.toString().toLowerCase())) {
                    key_exists = true;
                }
            }

            if (!key_exists) {
                cvName = key;

                break;
            } else {
                count++;
            }
        }

        name = cvName.toString();
        wmsPath = "/";
        label = new StringBuffer(name).append(" is a ").append(format.getDescription()).toString();
        description = new StringBuffer("Generated from ").append(formatId).toString();
        metadataLink = new MetaDataLink();
        metadataLink.setAbout(format.getDocURL());
        metadataLink.setMetadataType("other");
        keywords = new ArrayList(10);
        keywords.add("WCS");
        keywords.add(formatId);
        keywords.add(name);
        nativeFormat = format.getName();
        dirName = new StringBuffer(formatId).append("_").append(name).toString();
        requestCRSs = new ArrayList(10);

        if ((gc.getCoordinateReferenceSystem2D().getIdentifiers() != null)
                && !gc.getCoordinateReferenceSystem2D().getIdentifiers().isEmpty()) {
            requestCRSs.add(((Identifier) gc.getCoordinateReferenceSystem2D().getIdentifiers()
                                            .toArray()[0]).toString());
        }

        responseCRSs = new ArrayList(10);

        if ((gc.getCoordinateReferenceSystem2D().getIdentifiers() != null)
                && !gc.getCoordinateReferenceSystem2D().getIdentifiers().isEmpty()) {
            responseCRSs.add(((Identifier) gc.getCoordinateReferenceSystem2D().getIdentifiers()
                                             .toArray()[0]).toString());
        }

        supportedFormats = new ArrayList(10);

        final List formats = CoverageStoreUtils.listDataFormats();
        String fName;
        Format fTmp;

        for (Iterator i = formats.iterator(); i.hasNext();) {
            fTmp = (Format) i.next();
            fName = fTmp.getName();

            if (fName.equalsIgnoreCase("WorldImage")) {
                /*
                 * final String[] formatNames = ImageIO.getReaderFormatNames();
                 * final int length = formatNames.length; for (int f=0; f




© 2015 - 2024 Weber Informatics LLC | Privacy Policy