Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.ogc / org.gvsig.raster.wmts.ogc.impl / src / main / java / org / gvsig / raster / wmts / ogc / impl / struct / WMTSLayerImpl.java @ 2614

History | View | Annotate | Download (9.61 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22
package org.gvsig.raster.wmts.ogc.impl.struct;
23

    
24
import java.awt.geom.Rectangle2D;
25
import java.util.ArrayList;
26
import java.util.HashMap;
27
import java.util.List;
28

    
29
import org.cresques.cts.ICoordTrans;
30
import org.cresques.cts.IProjection;
31
import org.gvsig.fmap.crs.CRSFactory;
32
import org.gvsig.raster.wmts.ogc.WMTSStatus;
33
import org.gvsig.raster.wmts.ogc.impl.base.WMTSServerDescription;
34
import org.gvsig.raster.wmts.ogc.struct.WMTSBoundingBox;
35
import org.gvsig.raster.wmts.ogc.struct.WMTSDimension;
36
import org.gvsig.raster.wmts.ogc.struct.WMTSLayer;
37
import org.gvsig.raster.wmts.ogc.struct.WMTSResourceURL;
38
import org.gvsig.raster.wmts.ogc.struct.WMTSStyle;
39
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrix;
40
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixLimits;
41
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSet;
42
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSetLink;
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

    
46
/**
47
 * Describes the attributes of a layer in a WMTS server
48
 *
49
 * @author Nacho Brodin (nachobrodin@gmail.com)
50
 */
51
public abstract class WMTSLayerImpl extends WMTSBaseStruct implements WMTSLayer {
52
        protected WMTSBoundingBox             wgs84bbox                    = null;
53
        protected WMTSBoundingBox             bbox                         = null;
54
        private List<WMTSStyle>               style                        = null;
55
        private List<String>                  imageFormat                  = null;
56
        private List<String>                  infoFormat                   = null;
57
        private List<String>                  keywords                     = null;
58
        private List<WMTSDimension>           dimension                    = null;
59
        private String                        metadata                     = null;
60
        private List<WMTSTileMatrixSetLink>   tileMatrixSetLinkList        = null;
61
        private List<WMTSResourceURL>         resourceURL                  = null;
62
        protected WMTSServerDescription       status                       = null;
63
        private Logger                        log                          = LoggerFactory.getLogger(WMTSLayerImpl.class);
64
        protected boolean                     forceChangeAxisOrder         = false;
65
        private HashMap<String, Rectangle2D>  transformations = new HashMap<String, Rectangle2D>();
66
        
67
        /**
68
         * Sets longitude first in the axis order read from the capabilities file
69
         * @param force
70
         */
71
        public void setForceChangeAxisOrder(boolean force) {
72
                this.forceChangeAxisOrder = force;
73
        }
74
        
75
        public WMTSLayerImpl(WMTSServerDescription status) {
76
                this.status = status;
77
        }
78
        
79
        public String getMetadata() {
80
                return metadata;
81
        }
82
        
83
        public void setMetadata(String metadata) {
84
                this.metadata = metadata;
85
        }
86
        
87
        public List<String> getFormat() {
88
                if(imageFormat == null)
89
                        imageFormat = new ArrayList<String>();
90
                return imageFormat;
91
        }
92
        
93
        public List<String> getInfoFormat() {
94
                if(infoFormat == null)
95
                        infoFormat = new ArrayList<String>();
96
                return infoFormat;
97
        }
98
        
99
        public List<String> getKeywords() {
100
                return keywords;
101
        }
102
        
103
        public void setKeywords(List<String> k) {
104
                this.keywords = k;
105
        }
106
        
107
        public WMTSBoundingBox getWGS84BBox() {
108
                return wgs84bbox;
109
        }
110
        
111
        public Rectangle2D getWGS84BBoxTransformed(String epsg) {
112
                Rectangle2D r = transformations.get(epsg);
113
                if(r != null) {
114
                        return r;
115
                }
116
                
117
                IProjection projSrc = CRSFactory.getCRS("EPSG:4326");
118
                try {
119
                        IProjection projDst = CRSFactory.getCRS(epsg);
120
                        if(projDst != null) {
121
                                try {
122
                                        ICoordTrans t = projSrc.getCT(projDst);
123
                                        r = t.convert(wgs84bbox.toRectangle2D());
124
                                        if(isBoundingBoxValid(r)) {
125
                                                transformations.put(epsg, r);
126
                                                return r;
127
                                        }
128
                                } catch (Exception e) {
129
                                        log.info("I cannot get the transformation between EPSG:4326 and " + epsg, e);
130
                                }
131
                        }
132
                } catch (Exception e1) {
133
                        log.info("I cannot get " + epsg, e1);
134
                }
135
                return null;
136
        }
137
        
138
        private boolean isBoundingBoxValid(Rectangle2D bbox) {
139
                if(Double.isInfinite(bbox.getX()) || 
140
                        Double.isNaN(bbox.getX()) || 
141
                        Double.isInfinite(bbox.getY()) || 
142
                        Double.isNaN(bbox.getY())) {
143
                        return false;
144
                }
145
                return true;
146
        }
147
        
148
        public WMTSBoundingBox getBBox() {
149
                return bbox;
150
        }
151
        
152
        public void setWGS84BBox(WMTSBoundingBox wgs84bbox) {
153
                this.wgs84bbox = wgs84bbox;
154
        }
155
        
156
        public void setBBox(WMTSBoundingBox bbox) {
157
                this.bbox = bbox;
158
        }
159
        
160
        public List<WMTSResourceURL> getResourceURL() {
161
                if(resourceURL == null)
162
                        resourceURL = new ArrayList<WMTSResourceURL>();//resourceURL = (WMTSResourceURL)status.createVersionObject("WMTSResourceURL");
163
                return resourceURL;
164
        }
165

    
166
        public List<WMTSDimension> getDimensions() {
167
                if(dimension == null)
168
                        dimension = new ArrayList<WMTSDimension>(); //(WMTSDimension)status.createVersionObject("WMTSDimension");
169
                return dimension;
170
        }
171

    
172
        public List<WMTSStyle> getStyle() {
173
                if(style == null)
174
                        style = new ArrayList<WMTSStyle>();//(WMTSStyle)status.createVersionObject("WMTSStyle");
175
                return style;
176
        }
177

    
178
        public List<WMTSTileMatrixSetLink> getTileMatrixSetLink() {
179
                if(tileMatrixSetLinkList == null)
180
                        tileMatrixSetLinkList = new ArrayList<WMTSTileMatrixSetLink>();
181
                return tileMatrixSetLinkList;
182
        }
183
        
184
        /**
185
         * Returns the initial level of this layer inside the matrix set. 
186
         * @return
187
         */
188
        public int getInitialLevel(String tileMatrixSetId) {
189
                int initialLevel = 0;
190
                for (int i = 0; i < tileMatrixSetLinkList.size(); i++) {
191
                        WMTSTileMatrixSetLinkImpl tileMatrixSetLink = (WMTSTileMatrixSetLinkImpl)tileMatrixSetLinkList.get(i);
192
                        String id = tileMatrixSetLink.getTileMatrixSetId();
193
                        if(tileMatrixSetId.compareTo(id) == 0) {
194
                                List<WMTSTileMatrixLimits> tileMatrixLimits = tileMatrixSetLink.getTileMatrixLimits();
195
                                if(tileMatrixLimits != null && tileMatrixLimits.size() > 0) {
196
                                        String ref = ((WMTSTileMatrixLimitsImpl)tileMatrixLimits.get(0)).getRefToTileMatrix();
197
                                        List<WMTSTileMatrix> tileMatrixList = ((WMTSTileMatrixSetImpl)tileMatrixSetLink.getTileMatrixSet()).getTileMatrix();
198
                                        for (int j = 0; j < tileMatrixList.size(); j++) {
199
                                                String idTileMatrix = ((WMTSTileMatrixImpl)tileMatrixList.get(j)).getIdentifier();
200
                                                if(idTileMatrix.compareTo(ref) == 0)
201
                                                        return initialLevel;
202
                                                initialLevel ++;
203
                                        }
204
                                }
205
                        }
206
                }
207
                return initialLevel;
208
        }
209
        
210
        /**
211
         * Links the TileMatrixSet with the limits inside the layer structure
212
         * @param tileMatrixSetList
213
         */
214
        public void linkTileMatrixSets(List<WMTSTileMatrixSet> tileMatrixSetList) {
215
                //Recorre la lista de TileMatrixSetLink y a cada uno le asigna su TileMatrixSet
216
                //Para cada TileMatrixLimits contenido en el TileMatrixSetLimits asociado le asigna
217
                //el objeto TileMatrix a partir de su referencia.
218
                if(tileMatrixSetLinkList == null)
219
                        return;
220
                for (int i = 0; i < tileMatrixSetLinkList.size(); i++) {
221
                        WMTSTileMatrixSetLinkImpl tileMatrixSetLink = (WMTSTileMatrixSetLinkImpl)tileMatrixSetLinkList.get(i);
222
                        String id = tileMatrixSetLink.getTileMatrixSetId();
223
                        for (int j = 0; j < tileMatrixSetList.size(); j++) {
224
                                WMTSTileMatrixSetImpl tileMatrixSet = (WMTSTileMatrixSetImpl)tileMatrixSetList.get(j);
225
                                if(tileMatrixSet.getIdentifier().compareTo(id) == 0) {
226
                                        tileMatrixSetLink.setTileMatrixSet(tileMatrixSet);
227
                                        tileMatrixSetLink.linkTileMatrix(tileMatrixSet);
228
                                        break;
229
                                }
230
                        }
231
                }
232
        }
233
        
234
        /**
235
         * Gets the list of srs's supported
236
         * @return
237
         */
238
        public List<String> getSrsList() {
239
                List<String> list = new ArrayList<String>();
240
                for (int i = 0; i < tileMatrixSetLinkList.size(); i++) {
241
                        WMTSTileMatrixSetLinkImpl link = (WMTSTileMatrixSetLinkImpl)tileMatrixSetLinkList.get(i);
242
                        WMTSTileMatrixSetImpl tileMatrixSet = (WMTSTileMatrixSetImpl)link.getTileMatrixSet();
243
                        list.add(tileMatrixSet.getSupportedCRS());
244
                }
245
                return list;
246
        }
247
        
248
        /**
249
         * Builds the list of URL whether the capabilities file uses templates.
250
         */
251
        public void buildResourceURLListFromTemplate(WMTSStatus status) {
252
                TemplateSupport templateSupport = new TemplateSupport(getDimensions(), getResourceURL());
253
                templateSupport.buildResourceURLListFromTemplate(status);
254
        }
255
        
256
        public void print() {
257
                System.out.println("*****WMTSLayer******");
258
                System.out.println("Abstract:" + getAbstract());
259
                System.out.println("Identifier:" + getIdentifier());
260
                System.out.println("Keywords:");
261
                for (int i = 0; i < keywords.size(); i++) {
262
                        System.out.println("Keyword:" + keywords.get(i));
263
                }
264
                System.out.println("Title:" + getTitle());
265
                for (int i = 0; i < getFormat().size(); i++) {
266
                        System.out.println("Format:" + getFormat().get(i));
267
                }
268
                for (int i = 0; i < getInfoFormat().size(); i++) {
269
                        System.out.println("InfoFormat:" + getInfoFormat().get(i));
270
                }
271
                ((WMTSBoundingBoxImpl)getBBox()).print();
272
                ((WMTSBoundingBoxImpl)getWGS84BBox()).print();
273
                for (int i = 0; i < getDimensions().size(); i++) {
274
                        ((WMTSDimensionImpl)getDimensions().get(i)).print();                        
275
                }
276
                System.out.println("Metadata:" + getMetadata());
277
                for (int i = 0; i < getTileMatrixSetLink().size(); i++) {
278
                        ((WMTSTileMatrixSetLinkImpl)getTileMatrixSetLink().get(i)).print();
279
                }
280
        }
281

    
282
}