Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wmts / WMTSClient.java @ 35471

History | View | Annotate | Download (7.71 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.remoteclient.wmts;
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.ArrayList;
30

    
31
import org.gvsig.compat.net.ICancellable;
32
import org.gvsig.remoteclient.exceptions.ServerErrorException;
33
import org.gvsig.remoteclient.wmts.exception.DownloadException;
34
import org.gvsig.remoteclient.wmts.exception.WMTSException;
35
import org.gvsig.remoteclient.wmts.struct.WMTSLayer;
36
import org.gvsig.remoteclient.wmts.struct.WMTSServiceIdentification;
37
import org.gvsig.remoteclient.wmts.struct.WMTSServiceProvider;
38
import org.gvsig.remoteclient.wmts.struct.WMTSThemes;
39

    
40

    
41
/**
42
 * <p>Represents the class the with the necessary logic to connect to a OGCWMS and interpretate the data </p>
43
 * @author Nacho Brodin (nachobrodin@gmail.com)
44
 */
45
public class WMTSClient extends org.gvsig.remoteclient.RasterClient {
46
        private org.gvsig.remoteclient.wmts.WMTSProtocolHandler handler                      = null;
47
        private WMTSServerDescription                           serverDescription            = null;
48
        protected boolean                                       forceLongitudeFirstAxisOrder = false;
49
        
50
        /**
51
         * Constructor.
52
         * the parameter host, indicates the WMS host to connect.
53
         * */
54
        public WMTSClient(String host) throws ConnectException, IOException {
55
                setHost(host);
56
                handler = WMTSProtocolHandlerFactory.negotiate(host);
57
                handler.setHost(host);        
58
        }
59
        
60
         /**
61
         * Sets longitude first in the axis order
62
         * @param force
63
         */
64
        public void setForceLongitudeFirstAxisOrder(boolean force) {
65
                this.forceLongitudeFirstAxisOrder = force;
66
                if(handler != null) {
67
                        handler.setForceLongitudeFirstAxisOrder(force);
68
                }
69
        }
70
    
71
    public String getVersion() {
72
        return handler.getVersion();
73
    }
74
    
75
    /**
76
     * <p> Reads from the WMS Capabilities, the layers available in the service</p>
77
     * @return a TreeMap with the available layers in the WMS 
78
     */
79
        public ArrayList getLayers() {        
80
            if(serverDescription != null)
81
                        return serverDescription.getLayerList();
82
                return null;
83
    } 
84
    
85
    /**
86
     * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
87
     * @return, number of layers available
88
     */
89
    public int getNumberOfLayers() {        
90
            if(serverDescription != null)
91
                        return serverDescription.getLayerList().size();
92
                return 0;
93
    }
94

    
95
        public void close() {
96
                serverDescription = null;
97
                handler = null;
98
        }
99
        
100
        /**
101
     * <p>Gets a tile downloading using a specific path and file.</p> 
102
     * @throws ServerErrorException 
103
     */
104
    public synchronized File getTile(WMTSStatus status, ICancellable cancel, File file) throws WMTSException, ServerErrorException {   
105
        return handler.getTile(status, cancel, file);
106
    }
107
        
108
         /**
109
     * <p>One of the three interfaces that OGC WMS defines. Request a map.</p> 
110
     * @throws ServerErrorException 
111
     */
112
    public synchronized File getTile(WMTSStatus status, ICancellable cancel) throws WMTSException, ServerErrorException {   
113
        return handler.getTile(status, cancel);
114
    }
115
    
116
    /**
117
     * Builds the URL to get a tile using a WMTSStatus object 
118
     * @throws ServerErrorException 
119
     */
120
    public synchronized URL getTileURL(WMTSStatus status) throws MalformedURLException {   
121
        return handler.getTileURL(status);
122
    }
123
    
124
    /**
125
     * Downloads a file
126
     * @throws DownloadException 
127
     * @throws ServerErrorException 
128
     */
129
    public synchronized File downloadFile(URL url, ICancellable cancel) throws DownloadException {   
130
        return handler.downloadFile(url, cancel);
131
    }
132
        
133
        /**
134
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
135
     * @param override, if true the previous downloaded data will be overridden
136
     */
137
    public void getCapabilities(WMTSServerDescription serverDescription, boolean override, ICancellable cancel) {        
138
        handler.getCapabilities(serverDescription, override, cancel);
139
    } 
140
    
141
    /**
142
     * <p>It will send a GetFeatureInfo request to the WMTS
143
     * Parsing the response and redirecting the info to the WMTS client</p>
144
     */
145
    public String getFeatureInfo(WMTSStatus status, int x, int y, ICancellable cancel) {
146
            return handler.getFeatureInfo(status, x, y, cancel);
147
    }
148

    
149
        /*
150
         * (non-Javadoc)
151
         * @see org.gvsig.remoteclient.RemoteClient#connect(boolean, org.gvsig.compat.net.ICancellable)
152
         */
153
        public boolean connect(boolean override, ICancellable cancel) {
154
                try {            
155
            if (handler == null) {
156
                if (getHost().trim().length() > 0) {                                        
157
                    handler = WMTSProtocolHandlerFactory.negotiate(getHost());
158
                    handler.setHost(getHost());
159
                } else {
160
                    return false;
161
                }                
162
            }
163
            if(serverDescription == null) {
164
                    serverDescription = new WMTSServerDescription(handler.getVersion());
165
                    handler.setServerDescription(serverDescription);
166
                    handler.setForceLongitudeFirstAxisOrder(forceLongitudeFirstAxisOrder);
167
            }
168
            getCapabilities(serverDescription, override, cancel);
169
            return true;
170
            
171
        } catch (Exception e) {
172
            e.printStackTrace();
173
            return false;
174
        }
175
        } 
176
        
177
        public boolean connect(ICancellable cancel) {
178
                return connect(false, cancel);
179
        }
180
        
181
        /**
182
         * Gets the list of formats supported by a layer
183
         * @param layerTitle
184
         * @return
185
         */
186
        public ArrayList getFormats(String layerTitle) {
187
                if(serverDescription != null)
188
                        return serverDescription.getLayerByTitle(layerTitle).getFormat();
189
                return null;
190
    }
191
        
192
        /**
193
         * Gets the host description
194
         * @return
195
         */
196
        public WMTSServiceIdentification getServiceIdentification() {
197
                if(serverDescription != null)
198
                        return serverDescription.getServiceIdentification();
199
                return null;
200
        }
201
        
202
        /**
203
         * Gets the host description
204
         * @return
205
         */
206
        public WMTSServiceProvider getServiceProvider() {
207
                if(serverDescription != null)
208
                        return serverDescription.getServiceProvider();
209
                return null;
210
        }
211

    
212
        /**
213
         * Gets the list of themes
214
         * @return
215
         */
216
        public WMTSThemes getThemes() {
217
                if(serverDescription != null)
218
                        return serverDescription.getThemes();
219
                return null;
220
        }
221
        
222
    /**
223
     * Gets the set of tiles definition
224
     * @return
225
     */
226
    public ArrayList getTileMatrixSet() {
227
            if(serverDescription != null)
228
                    return serverDescription.getTileMatrixSet();
229
            return null;
230
    }
231
    
232
    /**
233
     * Gets a layer 
234
     * @param layerName
235
     * @return
236
     */
237
    public WMTSLayer getLayer(String layerName) {
238
            if(serverDescription != null) {
239
                        ArrayList list = serverDescription.getLayerList();
240
                        for (int i = 0; i < list.size(); i++) {
241
                                WMTSLayer layer = (WMTSLayer)list.get(i);
242
                                if(layer.getTitle().compareTo(layerName) == 0) {
243
                                        return layer;
244
                                }
245
                        }
246
            }
247
                return null;
248
    }
249
}