summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/wcps/server/core/DimensionIntervalElement.java
blob: 7453e9b4a847a603999f534b5f57b5d68bc9aa19 (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
/*
 * 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.wcps.server.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import petascope.wcps.server.exceptions.InvalidCrsException;
import petascope.wcps.server.exceptions.WCPSException;
import org.w3c.dom.*;
import petascope.wcs.server.exceptions.NoApplicableCodeException;

public class DimensionIntervalElement implements IRasNode, ICoverageInfo {

    Logger LOG = LoggerFactory.getLogger(DimensionIntervalElement.class);
    private IRasNode child;
    private CoverageInfo info = null;
    private AxisName axis;
    private Crs crs;
    private ScalarExpr domain1, domain2;  // lower and upper bound, or "DomainMetadataExprType" and null

    private long coord1, coord2;
    private int counter = 0;            // counter for the domain vars

    private Metadata meta = null;       // metadata about the current coverage

    private boolean finished = false;
    private Node nextNode;
    private boolean transformedCoordinates = false;

    /**

     * Constructs an element of a dimension interval.

     * @param node XML Node

     * @param xq WCPS Xml Query object

     * @param covInfo CoverageInfo object about the Trim parent object

     * @throws WCPSException

     */
    public DimensionIntervalElement(Node node, XmlQuery xq, CoverageInfo covInfo)
            throws WCPSException, InvalidCrsException {

        if (covInfo.getCoverageName() != null) {
            // Add WGS84 CRS information from coverage metadata, may be useful

            // for converting geo-coordinates to pixel-coordinates

            String coverageName = covInfo.getCoverageName();
            meta = xq.getMetadataSource().read(coverageName);
        }

        System.err.println("Trying to parse DimensionIntervalElement expression...");
        String name;

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

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

            name = node.getNodeName();
            System.err.println("Current node is " + name);

            // Try Axis

            try {
                axis = new AxisName(node, xq);
                node = node.getNextSibling();
                continue;
            } catch (WCPSException e) {
                System.err.println("Failed to parse an axis!");
            }

            // Try CRS name

            try {
                crs = new Crs(node, xq);
                node = node.getNextSibling();
                if (axis == null) {
                    throw new WCPSException("Expected Axis node before CRS !");
                }
                continue;
            } catch (WCPSException e) {
                System.err.println("Failed to parse a crs!");
            }

            // TODO: how to implement DomainMetadataExpr ?


//            // Try last thing

//            try

//            {

//                domain1 = new DomainMetadataExprType(node, xq);

//                counter = 1;

//                continue;

//            }

//            catch (WCPSException e)

//            {

//                System.err.println("Failed to parse domain metadata!");

//            }


            // Then it must be a pair of nodes "lowerBound" + "upperBound"

            if (node.getNodeName().equals("lowerBound")) {
                counter = 2;
                domain1 = new ScalarExpr(node.getFirstChild(), xq);
                if (axis == null) {
                    throw new WCPSException("Expected <axis> node before <lowerBound> !");
                }
            } else if (node.getNodeName().equals("upperBound")) {
                counter = 2;
                domain2 = new ScalarExpr(node.getFirstChild(), xq);
                if (axis == null) {
                    throw new WCPSException("Expected <lowerBound> node before <upperBound> !");
                }
            } else {
                throw new WCPSException("Unexpected node: " + node.getFirstChild().getNodeName());
            }

            if (axis != null && counter == 1 && domain1 != null) {
                finished = true;
            }
            if (axis != null && counter == 2 && domain1 != null && domain2 != null) {
                finished = true;
            }

            if (finished == true) {
                nextNode = node.getNextSibling();
            }

            node = node.getNextSibling();
        }

        if (finished == true) {
            convertToPixelCoordinates();
        }
    }


    /* If input coordinates are geo-, convert them to pixel coordinates. */
    private void convertToPixelCoordinates() {
        if (meta.getCrs() == null && crs != null && crs.getName().equals(DomainElement.WGS84_CRS)) {
            throw new RuntimeException("Coverage '" + meta.getCoverageName()
                    + "' is not georeferenced with 'EPSG:4326' coordinate system.");
        }
        if (counter == 2 && crs != null && domain1.isSingleValue() && domain2.isSingleValue()) {
            if (crs.getName().equals(DomainElement.WGS84_CRS)) {
                LOG.debug("CRS is '{}' and should be equal to '{}'", crs.getName(), DomainElement.WGS84_CRS);
                try {
                    this.transformedCoordinates = true;
                    // Convert to pixel coordinates

                    Double val1 = domain1.getSingleValue();
                    Double val2 = domain2.getSingleValue();
                    String axisName = axis.toRasQL().toUpperCase();
                    if (axisName.equals("X")) {
                        long[] pCoord = crs.convertToPixelCoordinates(meta, "X", val1, val2, null, null);
                        coord1 = pCoord[0];
                        coord2 = pCoord[1];
                    }
                    if (axisName.equals("Y")) {
                        long[] pCoord = crs.convertToPixelCoordinates(meta, "Y", null, null, val1, val2);
                        coord1 = pCoord[2];
                        coord2 = pCoord[3];
                    }
                } catch (NoApplicableCodeException e) {
                    this.transformedCoordinates = false;
                    LOG.error("Error while transforming geo-coordinates to pixel coordinates."
                            + "The metadata is probably not valid.");
                }
            }
        }
    }

    /* Not used */
    public String toRasQL() {
        return "<DimensionIntervalElement Not Converted to RasQL>";
    }

    public CoverageInfo getCoverageInfo() {
        return info;
    }

    public Node getNextNode() {
        return nextNode;
    }

    public String getAxisName() {
        return this.axis.toRasQL();
    }

    public String getAxisCoords() {
        return this.domain1.toRasQL() + " : " + this.domain2.toRasQL();
    }

    public String getLowCoord() {
        if (transformedCoordinates) {
            return String.valueOf(coord1);
        } else {
            return this.domain1.toRasQL();
        }
    }

    public String getHighCoord() {
        if (transformedCoordinates) {
            return String.valueOf(coord2);
        } else {
            return this.domain2.toRasQL();
        }
    }
}