summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/wcs/server/core/executeDescribeCoverage.java
blob: 1a7f8822dd0e59529f414636d6b4961d840db350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
 rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 */
package petascope.wcs.server.core;

//~--- non-JDK imports --------------------------------------------------------
import petascope.wcs.server.exceptions.WCSException;
import net.opengis.ows.v_1_0_0.BoundingBoxType;
import net.opengis.wcs.ows.v_1_1_0.DomainMetadataType;
import net.opengis.wcs.ows.v_1_1_0.InterpolationMethodType;
import net.opengis.wcs.ows.v_1_1_0.InterpolationMethods;
import net.opengis.wcs.ows.v_1_1_0.UnNamedDomainType;
import net.opengis.wcs.v_1_1_0.CoverageDescriptionType;
import net.opengis.wcs.v_1_1_0.CoverageDescriptions;
import net.opengis.wcs.v_1_1_0.CoverageDomainType;
import net.opengis.wcs.v_1_1_0.DescribeCoverage;
import net.opengis.wcs.v_1_1_0.FieldType;
import net.opengis.wcs.v_1_1_0.RangeType;
import net.opengis.wcs.v_1_1_0.SpatialDomainType;

import petascope.wcps.server.core.*;

//~--- JDK imports ------------------------------------------------------------

import java.util.Iterator;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import net.opengis.ows.v_1_0_0.KeywordsType;
import net.opengis.wcs.ows.v_1_1_0.AnyValue;
import net.opengis.wcs.v_1_1_0.TimeSequenceType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import petascope.wcs.server.exceptions.InvalidParameterValueException;
import petascope.wcs.server.exceptions.NoApplicableCodeException;

/**
 * This class takes a WCS DescribeCoverage XML request and executes request,
 * building the corresponding XML respose.
 *
 * @author Andrei Aiordachioaie
 */
public class executeDescribeCoverage {

    private static Logger LOG = LoggerFactory.getLogger(executeDescribeCoverage.class);
    private boolean finished;
    private DescribeCoverage input;
    private DbMetadataSource meta;
    private CoverageDescriptions output;

    /**
     * Default constructor
     * @param cap DescribeCoverage object, a WCS (or WCPS) request
     * @param metadataDbPath Path to the "dbparams.properties" file
     */
    public executeDescribeCoverage(DescribeCoverage cap, DbMetadataSource source)
            throws WCSException {
        input = cap;
        output = new CoverageDescriptions();
        finished = false;
        meta = source;
    }

    /**
     * Main method of this class. Retrieves the response to the DescribeCoverage
     * request given to the constructor. If needed, it also calls <b>process()</b>
     * @return a CoverageDescriptions object.
     * @throws wcs_web_service.WCSException
     */
    public CoverageDescriptions get() throws WCSException {
        if (finished == false) {
            process();
        }
        if (finished == false) {
            throw new NoApplicableCodeException("Could not execute the GetCapabilities request! "
                    + "Please see the other errors...");
        }

        return output;
    }

    /**
     * Computes the response to the DescribeCoverage request given to the constructor.
     */
    public void process() throws WCSException {
        String name;

        for (int i = 0; i < input.getIdentifier().size(); i++) {
            name = input.getIdentifier().get(i);
            output.getCoverageDescription().add(getCoverageDescription(name));
        }
        finished = true;
    }

    /**
     * Retrieve details for one coverage.
     * @param name Name of the coverage
     * @return CoverageDescriptionType object, that can just be plugged in the respose object
     */
    private CoverageDescriptionType getCoverageDescription(String name) throws WCSException {
        LOG.trace("Building coverage description for coverage '" + name + "' ...");
        CoverageDescriptionType desc = new CoverageDescriptionType();

        // Error checking: is the coverage available?
        if (meta.existsCoverageName(name) == false) {
            throw new InvalidParameterValueException("Identifier. Explanation: Coverage "
                    + name + " is not served by this server !");
        }

        // Read all coverage metadata
        Metadata cov = null;

        try {
            cov = meta.read(name);
        } catch (Exception e) {
            throw new NoApplicableCodeException("Metadata for coverage " + name + " is not valid.");
        }

        desc.setIdentifier(name);
        desc.setTitle(cov.getTitle());
        desc.setAbstract(cov.getAbstract());

        KeywordsType keyword = new KeywordsType();
        Iterator<String> keys = SDU.str2string(cov.getKeywords()).iterator();
        while (keys.hasNext()) {
            String k = keys.next();
            keyword.getKeyword().add(k);
        }
        desc.getKeywords().add(keyword);

        // Coverage Domain
        CoverageDomainType domain = null;
        Double lo1 = 0.0, lo2 = 0.0, hi1 = 0.0, hi2 = 0.0;

        /* Default Bounding Box (uses IMAGE_CRS): use image size */
        BoundingBoxType bbox = new BoundingBoxType();
        CellDomainElement X = cov.getXCellDomain();
        CellDomainElement Y = cov.getYCellDomain();
        if (X != null && Y != null) {
            lo1 = X.getLo().doubleValue();
            hi1 = X.getHi().doubleValue();
            lo2 = Y.getLo().doubleValue();
            hi2 = Y.getHi().doubleValue();

            bbox.setCrs(DomainElement.IMAGE_CRS);

            bbox.getLowerCorner().add(lo1);
            bbox.getLowerCorner().add(lo2);
            bbox.getUpperCorner().add(hi1);
            bbox.getUpperCorner().add(hi2);
        } else {
            throw new NoApplicableCodeException("Internal error: Could "
                    + "not find X and Y cell domain extents.");
        }

        /* Try to use WGS84 bounding box, if available */
        Wgs84Crs crs = cov.getCrs();
        BoundingBoxType bbox84 = new BoundingBoxType();
        bbox84.setCrs(DomainElement.WGS84_CRS);
        if (crs != null) {
            lo1 = crs.getLow1().doubleValue();
            hi1 = crs.getHigh1().doubleValue();
            lo2 = crs.getLow2().doubleValue();
            hi2 = crs.getHigh2().doubleValue();

            bbox84.getLowerCorner().add(lo1);
            bbox84.getLowerCorner().add(lo2);
            bbox84.getUpperCorner().add(hi1);
            bbox84.getUpperCorner().add(hi2);

            bbox = bbox84;
        }

        domain = new CoverageDomainType();
        SpatialDomainType spatial = new SpatialDomainType();
        spatial.getBoundingBox().add(new JAXBElement<BoundingBoxType>(
                new QName("http://www.opengis.net/ows", "BoundingBox", XMLConstants.DEFAULT_NS_PREFIX),
                BoundingBoxType.class, bbox));
        domain.setSpatialDomain(spatial);


        /* Find a time-axis if exists */
        CellDomainElement T = cov.getTCellDomain();
        if (T != null) {
            LOG.trace("Found time-axis for coverage: [" + T.getLo() + ", " + T.getHi() + "]");
            TimeSequenceType temporal = new TimeSequenceType();
            temporal.getTimePositionOrTimePeriod().add(T.getLo().intValue());
            temporal.getTimePositionOrTimePeriod().add(T.getHi().intValue());
            domain.setTemporalDomain(temporal);
        }

        desc.setDomain(domain);

        // The coverage Range
        RangeType wcsRange = new RangeType();
        Iterator<RangeElement> rangeIt = cov.getRangeIterator();

        while (rangeIt.hasNext()) {
            RangeElement range = rangeIt.next();
            FieldType field = new FieldType();

            field.setIdentifier(range.getName());
            UnNamedDomainType domtype = new UnNamedDomainType();
            AnyValue anyVal = new AnyValue();
            domtype.setAnyValue(anyVal);
            DomainMetadataType dommeta = new DomainMetadataType();

            dommeta.setValue(range.getType());
            domtype.setDataType(dommeta);
            field.setDefinition(domtype);

            InterpolationMethods interp = new InterpolationMethods();

            InterpolationMethodType meth = new InterpolationMethodType();

            meth.setValue(cov.getInterpolationDefault());
            meth.setNullResistance(cov.getNullResistanceDefault());
            interp.setDefaultMethod(meth);

            Iterator<InterpolationMethod> interpIt = cov.getInterpolationMethodIterator();

            while (interpIt.hasNext()) {
                InterpolationMethod wcpsInterp = interpIt.next();

                meth = new InterpolationMethodType();
                meth.setValue(wcpsInterp.getInterpolationType());
                meth.setNullResistance(wcpsInterp.getNullResistance());
                if ((wcpsInterp.getInterpolationType().equals(interp.getDefaultMethod().getValue()) == false) || (wcpsInterp.getNullResistance().equals(interp.getDefaultMethod().getNullResistance()) == false)) {
                    interp.getOtherMethod().add(meth);
                }
            }

            field.setInterpolationMethods(interp);
            wcsRange.getField().add(field);
        }
        desc.setRange(wcsRange);

        // Supported formats for GetCoverage: known rasdaman encoders
        String[] mimetypes = meta.getMimetypesList();

        for (int i = 0; i < mimetypes.length; i++) {
            String format = mimetypes[i];
            desc.getSupportedFormat().add(format);
        }

        // Available CRSs for current coverage
        desc.getSupportedCRS().add(DomainElement.IMAGE_CRS);
        if (cov.getCrs() != null) {
            desc.getSupportedCRS().add(DomainElement.WGS84_CRS);
        }

        LOG.trace("Done building the Coverage Description for coverage '" + name + "'.");

        return desc;
    }
}