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 / base / WMTSClientImpl.java @ 2613

History | View | Annotate | Download (9.35 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.base;
23

    
24
import java.io.File;
25
import java.io.IOException;
26
import java.net.ConnectException;
27
import java.net.MalformedURLException;
28
import java.net.URL;
29
import java.util.List;
30

    
31
import org.gvsig.compat.net.ICancellable;
32
import org.gvsig.raster.wmts.ogc.WMTSClient;
33
import org.gvsig.raster.wmts.ogc.WMTSStatus;
34
import org.gvsig.raster.wmts.ogc.exception.DownloadException;
35
import org.gvsig.raster.wmts.ogc.exception.ServerErrorException;
36
import org.gvsig.raster.wmts.ogc.exception.WMTSException;
37
import org.gvsig.raster.wmts.ogc.impl.struct.WMTSLayerImpl;
38
import org.gvsig.raster.wmts.ogc.impl.struct.WMTSServiceProviderImpl;
39
import org.gvsig.raster.wmts.ogc.struct.WMTSLayer;
40
import org.gvsig.raster.wmts.ogc.struct.WMTSServiceIdentification;
41
import org.gvsig.raster.wmts.ogc.struct.WMTSStyle;
42
import org.gvsig.raster.wmts.ogc.struct.WMTSThemes;
43
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSet;
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46

    
47

    
48
/**
49
 * <p>Represents the class with the necessary logic to connect to a OGCWMTS and interpret the data </p>
50
 * @author Nacho Brodin (nachobrodin@gmail.com)
51
 */
52
public class WMTSClientImpl implements WMTSClient {
53
        protected String                      hostName;
54
        protected int                         port;
55
        protected String                      serviceName;
56
        private String                        type;
57
        private String                        subtype;
58
        private WMTSProtocolHandler           handler                      = null;
59
        private WMTSServerDescription         serverDescription            = null;
60
        protected boolean                     forceChangeAxisOrder         = false;
61
        private Logger                        logger                       = LoggerFactory.getLogger(WMTSClientImpl.class.toString());
62
        
63
        /**
64
         * Constructor.
65
         * the parameter host, indicates the WMS host to connect.
66
         * */
67
        public WMTSClientImpl(String host) throws ConnectException, IOException {
68
                setHost(host);
69
                getHandler();       
70
        }
71
        
72
        private WMTSProtocolHandler getHandler() throws ConnectException, IOException {
73
            if (handler == null) {
74
                        if (getHost().trim().length() > 0) {                                        
75
                                handler = WMTSProtocolHandlerFactory.negotiate(getHost());
76
                                handler.setHost(getHost());
77
                        }          
78
                }
79
            return handler;
80
    }
81
        
82
         /**
83
         * Sets longitude first in the axis order
84
         * @param force
85
         */
86
        public void setForceChangeAxisOrder(boolean force) {
87
                this.forceChangeAxisOrder = force;
88
                if(handler != null) {
89
                        handler.setForceChangeAxisOrder(force);
90
                }
91
        }
92
    
93
    public String getVersion() {
94
        return handler.getVersion();
95
    }
96
    
97
    /**
98
     * <p> Reads from the WMS Capabilities, the layers available in the service</p>
99
     * @return a TreeMap with the available layers in the WMS 
100
     */
101
        public List<WMTSLayer> getLayers() {        
102
            if(serverDescription != null)
103
                        return serverDescription.getLayerList();
104
                return null;
105
    } 
106
    
107
    /**
108
     * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
109
     * @return, number of layers available
110
     */
111
    public int getNumberOfLayers() {        
112
            if(serverDescription != null)
113
                        return serverDescription.getLayerList().size();
114
                return 0;
115
    }
116

    
117
        public void close() {
118
                serverDescription = null;
119
                handler = null;
120
        }
121
        
122
        public File getTile(String url, ICancellable cancel, File file) throws WMTSException, ServerErrorException {
123
                return handler.getTile(url, cancel, file);
124
        }
125
        
126
        /**
127
     * <p>Gets a tile downloading using a specific path and file.</p> 
128
     * @throws ServerErrorException 
129
     */
130
    public synchronized File getTile(WMTSStatus status, ICancellable cancel, File file) throws WMTSException, ServerErrorException {   
131
        return handler.getTile((WMTSStatusImpl)status, cancel, file);
132
    }
133
        
134
         /**
135
     * <p>One of the three interfaces that OGC WMS defines. Request a map.</p> 
136
     * @throws ServerErrorException 
137
     */
138
    public synchronized File getTile(WMTSStatus status, ICancellable cancel) throws WMTSException, ServerErrorException {   
139
        return handler.getTile((WMTSStatusImpl)status, cancel);
140
    }
141
    
142
    /**
143
     * Builds the URL to get a tile using a WMTSStatus object 
144
     * @throws ServerErrorException 
145
     */
146
    public synchronized URL getTileURL(WMTSStatus status) throws MalformedURLException {   
147
        return handler.getTileURL((WMTSStatusImpl)status);
148
    }
149
    
150
    /**
151
     * Downloads a file
152
     * @throws DownloadException 
153
     * @throws ServerErrorException 
154
     */
155
    public synchronized File downloadFile(URL url, ICancellable cancel) throws DownloadException {   
156
        return handler.downloadFile(url, cancel, null);
157
    }
158
        
159
        /**
160
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
161
     * @param override, if true the previous downloaded data will be overridden
162
     */
163
    public void getCapabilities(WMTSServerDescription serverDescription, boolean override, ICancellable cancel) {        
164
        handler.getCapabilities(serverDescription, override, cancel);
165
    } 
166
    
167
    /**
168
     * <p>It will send a GetFeatureInfo request to the WMTS
169
     * Parsing the response and redirecting the info to the WMTS client</p>
170
     */
171
    public String getFeatureInfo(WMTSStatus status, int x, int y, ICancellable cancel) {
172
            return handler.getFeatureInfo((WMTSStatusImpl)status, x, y, cancel);
173
    }
174
    
175
    /**
176
     * Gets the legend graphic of one layer
177
     */
178
    public File getLegendGraphic(WMTSLayer layer, WMTSStyle style, ICancellable cancel) throws WMTSException {
179
            try {
180
                        return handler.getLegendGraphic(layer, style, cancel);
181
                } catch (ServerErrorException e) {
182
                        throw new WMTSException("Error getting the legend", e);
183
                } catch (DownloadException e) {
184
                        throw new WMTSException("Error downloading the legend", e);
185
                }
186
    }
187
    
188
    public boolean connect(boolean override, ICancellable cancel) {
189
            try {            
190
                    if(getHandler() == null)
191
                            return false;
192
                    
193
                           serverDescription = getHandler().getServerDescription();
194
                    //getHandler().setForceLongitudeFirstAxisOrder(forceLongitudeFirstAxisOrder);
195
                    getCapabilities(serverDescription, override, cancel);
196
                    return true;
197
            } catch (Exception e) {
198
                    logger.debug("Error connecting to the server", e);
199
                    return false;
200
            }
201
    } 
202
        
203
        public boolean connect(ICancellable cancel) {
204
                return connect(false, cancel);
205
        }
206
        
207
        /**
208
         * Gets the list of formats supported by a layer
209
         * @param layerTitle
210
         * @return
211
         */
212
        public List<String> getFormats(String layerTitle) {
213
                if(serverDescription != null)
214
                        return serverDescription.getLayerByTitle(layerTitle).getFormat();
215
                return null;
216
    }
217
        
218
        public WMTSServiceIdentification getServiceIdentification() {
219
                if(serverDescription != null)
220
                        return serverDescription.getServiceIdentification();
221
                return null;
222
        }
223
        
224
        public WMTSServiceProviderImpl getServiceProvider() {
225
                if(serverDescription != null)
226
                        return serverDescription.getServiceProvider();
227
                return null;
228
        }
229

    
230
        /**
231
         * Gets the list of themes
232
         * @return
233
         */
234
        public WMTSThemes getThemes() {
235
                if(serverDescription != null)
236
                        return serverDescription.getThemes();
237
                return null;
238
        }
239
        
240
        public WMTSThemes getLayerListAsThemes() {
241
                if(serverDescription != null)
242
                        return serverDescription.getLayerListAsThemes();
243
                return null;
244
        }
245
        
246
    public List<WMTSTileMatrixSet> getTileMatrixSet() {
247
            if(serverDescription != null)
248
                    return serverDescription.getTileMatrixSet();
249
            return null;
250
    }
251
    
252
    public WMTSLayer getLayer(String layerName) {
253
            if(serverDescription != null) {
254
                        List<WMTSLayer> list = serverDescription.getLayerList();
255
                        for (int i = 0; i < list.size(); i++) {
256
                                WMTSLayerImpl layer = (WMTSLayerImpl)list.get(i);
257
                                if(layer.getTitle().compareTo(layerName) == 0) {
258
                                        return layer;
259
                                }
260
                        }
261
            }
262
                return null;
263
    }
264
    
265
    public String getHost() {
266
        return hostName;
267
    }
268

    
269
    public void setHost(String _hostName) {
270
        hostName = _hostName;
271
    }
272

    
273
    public int getPort() {
274
        return port;
275
    }
276

    
277
    public void setPort(int _port) {
278
        port = _port;
279
    }
280

    
281
    public String getServiceName() {
282
        return serviceName;
283
    }
284

    
285
    public void setServiceName(String _serviceName) {
286
        serviceName = _serviceName;
287
    }
288

    
289
    public String getType() {
290
        return type;
291
    }
292

    
293
    public void setType(String _type) {
294
        type = _type;
295
    }
296

    
297
    public String getSubtype() {
298
        return subtype;
299
    }
300
    
301
    public void setSubtype(String _subtype) {
302
        subtype = _subtype;
303
    }
304

    
305
        public WMTSStatus createStatus() {
306
                return new WMTSStatusImpl();
307
        }
308
}