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 @ 1964

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

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

    
265
    public void setHost(String _hostName) {
266
        hostName = _hostName;
267
    }
268

    
269
    public int getPort() {
270
        return port;
271
    }
272

    
273
    public void setPort(int _port) {
274
        port = _port;
275
    }
276

    
277
    public String getServiceName() {
278
        return serviceName;
279
    }
280

    
281
    public void setServiceName(String _serviceName) {
282
        serviceName = _serviceName;
283
    }
284

    
285
    public String getType() {
286
        return type;
287
    }
288

    
289
    public void setType(String _type) {
290
        type = _type;
291
    }
292

    
293
    public String getSubtype() {
294
        return subtype;
295
    }
296
    
297
    public void setSubtype(String _subtype) {
298
        subtype = _subtype;
299
    }
300

    
301
        public WMTSStatus createStatus() {
302
                return new WMTSStatusImpl();
303
        }
304
}