summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/wcps/server/core/ConstructCoverageExpr.java
blob: 89343e85d5b13a779cd3d83875c2601997b399cb (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
/*

 * This file is part of PetaScope.

 *

 * PetaScope is free software: you can redistribute it and/or modify

 * it under the terms of the GNU Lesser General Public License as

 * published by the Free Software Foundation, either version 3 of

 * the License, or (at your option) any later version.

 *

 * PetaScope 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 Lesser General Public License for more details.

 *

 * You should have received a copy of the GNU Lesser General Public

 * License along with PetaScope. If not, see <http://www.gnu.org/licenses/>.

 *

 * For more information please see <http://www.PetaScope.org>

 * or contact Peter Baumann via <baumann@rasdaman.com>.

 *

 * Copyright 2009 Jacobs University Bremen, Peter Baumann.

 */
package petascope.wcps.server.core;

import petascope.wcps.server.exceptions.InvalidCrsException;
import petascope.wcps.server.exceptions.WCPSException;
import petascope.wcps.server.exceptions.InvalidMetadataException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import org.w3c.dom.*;

public class ConstructCoverageExpr implements IRasNode, ICoverageInfo {

    private String covName;
    private Vector<AxisIterator> iterators;
    private IRasNode values;
    private CoverageInfo info;
    private String axisIteratorString;
    private String newIteratorName;

    public ConstructCoverageExpr(Node node, XmlQuery xq)
            throws WCPSException, InvalidCrsException {
        while ((node != null) && node.getNodeName().equals("#text")) {
            node = node.getNextSibling();
        }

        iterators = new Vector();
        System.err.println("Parsing Construct Coverage Expr: " + node.getNodeName());
        newIteratorName = xq.registerNewExpressionWithVariables();

        while (node != null) {
            String name = node.getNodeName();
            if (name.equals("name")) {
                covName = node.getTextContent();
            } else if (name.equals("axisIterator")) {
                AxisIterator it = new AxisIterator(node.getFirstChild(), xq, newIteratorName);
                iterators.add(it);
                // Top level structures need to know about these iterators

                CoverageIterator dyn = new CoverageIterator(it.getVar(), covName);
                xq.addDynamicCoverageIterator(dyn);
            } else {
                /* The iterator is probably used in the "values" section,

                 * so send the iterator to the top-level query */
                if (covName != null && iterators.size() > 0) {
                    buildMetadata(xq);
                } else {
                    throw new WCPSException("Cannot build coverage metadata !!!");
                }
                // And only then start parsing the "values" section 

                values = new ScalarExpr(node, xq);
            }

            node = node.getNextSibling();
            while ((node != null) && node.getNodeName().equals("#text")) {
                node = node.getNextSibling();
            }
        }

        buildAxisIteratorDomain();

        // Sanity check: metadata should have already been build

        if (info == null) {
            throw new WCPSException("Could not build coverage metadata !!!");
        }
    }

    public String toRasQL() {
        String result = "marray ";
        result += axisIteratorString;
        result += " values " + values.toRasQL();

        return result;
    }

    public CoverageInfo getCoverageInfo() {
        return info;
    }

    /* Concatenates all the AxisIterators into one large multi-dimensional object,

     * that will be used to build to RasQL query */
    private void buildAxisIteratorDomain() {
        axisIteratorString = "";
        axisIteratorString += newIteratorName + " in [";

        for (int i = 0; i < iterators.size(); i++) {
            if (i > 0) {
                axisIteratorString += ", ";
            }
            AxisIterator ai = iterators.elementAt(i);
            axisIteratorString += ai.getInterval();
        }

        axisIteratorString += "]";
    }

    /** Builds full metadata for the newly constructed coverage **/
    private void buildMetadata(XmlQuery xq) throws WCPSException {
        List<CellDomainElement> cellDomainList = new LinkedList<CellDomainElement>();
        List<RangeElement> rangeList = new LinkedList<RangeElement>();
        HashSet<String> nullSet = new HashSet<String>();
        String nullDefault = "0";
        nullSet.add(nullDefault);
        HashSet<InterpolationMethod> interpolationSet = new HashSet<InterpolationMethod>();
        InterpolationMethod interpolationDefault = new InterpolationMethod("none", "none");
        interpolationSet.add(interpolationDefault);
        String coverageName = covName;
        List<DomainElement> domainList = new LinkedList<DomainElement>();

        Iterator<AxisIterator> i = iterators.iterator();
        while (i.hasNext()) {
            // Build domain metadata

            AxisIterator ai = i.next();
            cellDomainList.add(new CellDomainElement(ai.getLow(), ai.getHigh()));
            String axisName = ai.getVar();
            String axisType = ai.getAxisType();
            String crs = DomainElement.WGS84_CRS;
            HashSet<String> crsset = new HashSet<String>();
            crsset.add(crs);
            DomainElement domain = new DomainElement(axisName, axisType,
                    ai.getLow().doubleValue(), ai.getHigh().doubleValue(),
                    null, null, crsset, xq.getMetadataSource().getAxisNames());
            domainList.add(domain);
        }
        // "unsigned int" is default datatype

        rangeList.add(new RangeElement("dynamic_type", "unsigned int"));

        try {
            Metadata metadata = new Metadata(cellDomainList, rangeList, nullSet,
                    nullDefault, interpolationSet, interpolationDefault,
                    coverageName, domainList, null);
            // Let the top-level query know the full metadata about us

            xq.getMetadataSource().addDynamicMetadata(covName, metadata);
            info = new CoverageInfo(metadata);
        } catch (InvalidMetadataException e) {
            throw new WCPSException("Could not build coverage '" + covName
                    + "' metadata !", e);
        }
    }
}