Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.remoteclient / src / main / java / org / gvsig / remoteclient / wms / wms_1_3_0 / WMSLayer1_3_0.java @ 3083

History | View | Annotate | Download (10.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.remoteclient.wms.wms_1_3_0;
26

    
27
import java.io.IOException;
28
import java.util.ArrayList;
29
import java.util.TreeMap;
30

    
31
import org.cresques.cts.IProjection;
32
import org.gvsig.compat.CompatLocator;
33
import org.gvsig.compat.lang.StringUtils;
34
import org.gvsig.fmap.crs.CRSFactory;
35
import org.gvsig.remoteclient.utils.BoundaryBox;
36
import org.gvsig.remoteclient.utils.CapabilitiesTags;
37
import org.gvsig.remoteclient.utils.Utilities;
38
import org.gvsig.remoteclient.wms.WMSDimension;
39
import org.kxml2.io.KXmlParser;
40
import org.xmlpull.v1.XmlPullParserException;
41

    
42

    
43
/**
44
 * <p>WMS Layer for WMS 1.3.0</p>
45
 * 
46
 */
47
public class WMSLayer1_3_0 extends org.gvsig.remoteclient.wms.WMSLayer {
48
    
49
    private static final StringUtils stringUtils = CompatLocator.getStringUtils();
50
    
51
    
52
    public ArrayList getDimensions() {   
53
        WMSDimension pDimension;
54
        WMSDimension myDimension;    
55
        ArrayList myDimensions = (ArrayList) this.dimensions.clone();        
56
        ArrayList pDimensions;
57
        
58
        if (parent != null) {
59
                pDimensions = parent.getDimensions();
60
                for (int i= 0; i < pDimensions.size(); i++) {
61
                        pDimension = (WMSDimension)pDimensions.get(i);
62
                        myDimension = getDimension(pDimension.getName());
63
                        if (myDimension != null){
64
                                pDimensions.remove(pDimension);
65
                        }
66
                }
67
                myDimensions.addAll(pDimensions);
68
        }
69
        return myDimensions;
70
    }
71
    
72
    public WMSLayer1_3_0() {
73
        children = new ArrayList();
74
    }
75
    /**
76
     * <p>Parses the contents of the parser(WMSCapabilities)
77
     * to extract the information about an WMSLayer</p>
78
     * 
79
     */
80
    public void parse(KXmlParser parser, TreeMap layerTreeMap) throws IOException, XmlPullParserException {
81
        int currentTag;
82
        boolean end = false;
83
        String value;
84
        BoundaryBox bbox;
85
        parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.LAYER);
86
        
87
        readLayerAttributes( parser );
88
        
89
        currentTag = parser.nextTag();
90
        
91
        while (!end) {
92
                switch(currentTag) {
93
                case KXmlParser.START_TAG:
94
                        if (parser.getName().compareTo(CapabilitiesTags.LAYER) == 0) {        
95
                                WMSLayer1_3_0 lyr = new WMSLayer1_3_0();                                                
96
                                //parser.next(); 
97
                                lyr.parse(parser, layerTreeMap);
98
                                lyr.setParent(this);
99
                                this.children.add(lyr);
100
                                // Jaume
101
                                if (lyr.getName()!=null)
102
                                        layerTreeMap.put(lyr.getName(), lyr);
103
                        } else if (parser.getName().compareTo(CapabilitiesTags.ATTRIBUTION) == 0) {
104
                                // TODO comprobar que esto se necesite o se deseche
105
                                parser.skipSubTree();
106
                        } else if (parser.getName().compareTo(CapabilitiesTags.NAME) == 0) {                
107
                                value = parser.nextText();
108
                                if (value != null) setName(value);                                                
109
                        } else if (parser.getName().compareTo(CapabilitiesTags.TITLE) == 0) {
110
                                value = parser.nextText();
111
                                if (value != null) setTitle(value);
112
                        } else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT) == 0) {
113
                                value = parser.nextText();
114
                                if (value != null) setAbstract(value);
115
                        } else if (parser.getName().compareTo(CapabilitiesTags.CRS) == 0) {
116
                                value = parser.nextText();
117
                                if (value != null) {
118
                                        String[] mySRSs = stringUtils.split(value, " ");
119
                                        for (int i = 0; i < mySRSs.length; i++) {
120
                                                addSrs(mySRSs[i]);    
121
                                        }                        
122
                                }
123
                        } else if (parser.getName().compareTo(CapabilitiesTags.BOUNDINGBOX) == 0) {
124
                                bbox = new BoundaryBox();
125
                                value = parser.getAttributeValue("",CapabilitiesTags.CRS);
126
                                if (value != null)
127
                                        bbox.setSrs(value);
128
                                value = parser.getAttributeValue("",CapabilitiesTags.MINX);
129
                                if ((value != null) && (Utilities.isNumber(value)))
130
                                        bbox.setXmin(Double.parseDouble(value));        
131
                                value = parser.getAttributeValue("",CapabilitiesTags.MINY);
132
                                if ((value != null) && (Utilities.isNumber(value)))
133
                                        bbox.setYmin(Double.parseDouble(value));        
134
                                value = parser.getAttributeValue("",CapabilitiesTags.MAXX);
135
                                if ((value != null) && (Utilities.isNumber(value)))
136
                                        bbox.setXmax(Double.parseDouble(value));        
137
                                value = parser.getAttributeValue("",CapabilitiesTags.MAXY);
138
                                if ((value != null) && (Utilities.isNumber(value)))
139
                                        bbox.setYmax(Double.parseDouble(value));        
140

    
141
                                bbox = changeAxis(bbox.getSrs(), bbox);
142
                                addBBox(bbox);
143
                                addSrs(bbox.getSrs());
144
                        } else if (parser.getName().compareTo(CapabilitiesTags.EX_GEOGRAPHICBOUNDINGBOX) == 0) {
145
                                //minimum bounding rectangle in decimal degrees of the area covered by the layer.
146
                                bbox = parseEXGeographicBBTag(parser);        
147
                                addBBox(bbox);
148
                                setLatLonBox(bbox);
149
                                addSrs(bbox.getSrs());
150
                        } else if (parser.getName().compareTo(CapabilitiesTags.SCALEHINT) == 0) {
151
                                value = parser.getAttributeValue("",CapabilitiesTags.MIN);
152
                                if ((value != null) && (Utilities.isNumber(value)))
153
                                        setScaleMin(Double.parseDouble(value));
154
                                value = parser.getAttributeValue("",CapabilitiesTags.MAX);
155
                                if ((value != null) && (Utilities.isNumber(value)))
156
                                        setScaleMax(Double.parseDouble(value));                                                                                                                                        
157
                        } else if (parser.getName().compareTo(CapabilitiesTags.STYLE) == 0) {
158
                                WMSStyle1_3_0 style = new WMSStyle1_3_0();
159
                                style.parse(parser);
160
                                if ((style != null) && (style.getName() != null)) {
161
                                        styles.add(style);
162
                                }
163
                        } else if (parser.getName().compareTo(CapabilitiesTags.DIMENSION) == 0) {
164
                                WMSDimension dim = new WMSDimension();
165
                                dim.parse(parser);
166
                                if ((dim != null) && (dim.getName() != null)) {
167
                                        addDimension(dim);
168

    
169
                                }
170
                        } else if (parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST) == 0) {
171
                                parseKeywordList(parser);
172
                        }                     
173
                        break;
174
                case KXmlParser.END_TAG:
175
                        if (parser.getName().compareTo(CapabilitiesTags.LAYER) == 0)
176
                                end = true;
177
                        break;
178
                case KXmlParser.TEXT:                                        
179
                        break;
180
                }
181
                if (!end)
182
                        currentTag = parser.next();
183
        }
184
        parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.LAYER);
185
    }     
186
    
187
    /**
188
     * Protocol 1.3 needs change axis in some cases.
189
     * If coordinates are read from the tag BoundingBox (where the bounding box is
190
     * defined for each specific CRS) then the axis needs to change its axis order in 
191
     * geographic projections. If coordinates are read from EX_GeographicBoundingBox 
192
     * then the axis are in right position.
193
     * 
194
     * There is to take into account that the request will have to change the axis order again
195
     * in cases which those had been changed.
196
     */
197
    private BoundaryBox changeAxis(String epsg, BoundaryBox oldbbox) {
198
            if(epsg.contains("CRS:84") || epsg.contains("CRS84"))
199
                    return oldbbox;
200
            try {
201
                    IProjection projSrc = CRSFactory.getCRS(epsg);
202
                    if(projSrc != null && !projSrc.isProjected()) {
203
                            BoundaryBox bbox = new BoundaryBox();
204
                            bbox.setXmin(oldbbox.getYmin());        
205
                            bbox.setYmin(oldbbox.getXmin());        
206
                            bbox.setXmax(oldbbox.getYmax());        
207
                            bbox.setYmax(oldbbox.getXmax());
208
                            bbox.setSrs(oldbbox.getSrs());
209
                            return bbox;
210
                    }
211
            } catch (Exception e) {
212
            }
213
        return oldbbox;
214
    }
215
  
216
    
217
    /**
218
     * <p>Parses the EX_GeographicBoundingBox </p>
219
     */    
220
    private BoundaryBox parseEXGeographicBBTag(KXmlParser parser) throws IOException, XmlPullParserException {
221
            int currentTag;
222
            boolean end = false;
223
            BoundaryBox bbox = new BoundaryBox ();
224
            String value;
225
            
226
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.EX_GEOGRAPHICBOUNDINGBOX);
227
            currentTag = parser.next();
228
            
229
            while (!end) {
230
                         switch(currentTag) {
231
                                case KXmlParser.START_TAG:
232
                                        if (parser.getName().compareTo(CapabilitiesTags.WESTBOUNDLONGITUDE) == 0) {
233
                                                value = parser.nextText();                               
234
                            if ((value != null) && (Utilities.isNumber(value)))
235
                                    bbox.setXmin(Double.parseDouble(value));
236
                                        } else if (parser.getName().compareTo(CapabilitiesTags.EASTBOUNDLONGITUDE) == 0) {
237
                                                value = parser.nextText();                               
238
                            if ((value != null) && (Utilities.isNumber(value)))
239
                                    bbox.setXmax(Double.parseDouble(value));
240
                                        } else if (parser.getName().compareTo(CapabilitiesTags.NORTHBOUNDLATITUDE) == 0) {
241
                                                value = parser.nextText();                               
242
                            if ((value != null) && (Utilities.isNumber(value)))
243
                                    bbox.setYmax(Double.parseDouble(value));
244
                                        } else if (parser.getName().compareTo(CapabilitiesTags.SOUTHBOUNDLATITUDE) == 0) {
245
                                                value = parser.nextText();                               
246
                            if ((value != null) && (Utilities.isNumber(value)))
247
                                    bbox.setYmin(Double.parseDouble(value));
248
                                        }                                        
249
                                        break;
250
                                case KXmlParser.END_TAG:
251
                                        if (parser.getName().compareTo(CapabilitiesTags.EX_GEOGRAPHICBOUNDINGBOX) == 0)
252
                                                end = true;
253
                                        break;
254
                                case KXmlParser.TEXT:                                        
255
                                        break;
256
                         }
257
             if (!end)
258
                 currentTag = parser.next();
259
            }            
260
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.EX_GEOGRAPHICBOUNDINGBOX);
261
            
262
            //TODO: 
263
            bbox.setSrs("CRS:84");
264
            return bbox;
265
    }
266
    
267
    public String toString() {
268
        return super.toString();
269
    }
270
}