Statistics
| Revision:

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

History | View | Annotate | Download (6.18 KB)

1 34235 nbrodin
/* 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 34305 nbrodin
import java.io.File;
25 34235 nbrodin
import java.io.IOException;
26
import java.net.ConnectException;
27
import java.util.ArrayList;
28
29
import org.gvsig.compat.net.ICancellable;
30 34305 nbrodin
import org.gvsig.remoteclient.exceptions.ServerErrorException;
31
import org.gvsig.remoteclient.wmts.exception.WMTSException;
32 34235 nbrodin
import org.gvsig.remoteclient.wmts.struct.WMTSLayer;
33 34269 nbrodin
import org.gvsig.remoteclient.wmts.struct.WMTSServiceIdentification;
34
import org.gvsig.remoteclient.wmts.struct.WMTSServiceProvider;
35 34235 nbrodin
import org.gvsig.remoteclient.wmts.struct.WMTSThemes;
36
37
38
/**
39
 * <p>Represents the class the with the necessary logic to connect to a OGCWMS and interpretate the data </p>
40
 * @author Nacho Brodin (nachobrodin@gmail.com)
41
 */
42
@SuppressWarnings("unchecked")
43
public class WMTSClient extends org.gvsig.remoteclient.RasterClient {
44 34305 nbrodin
        private org.gvsig.remoteclient.wmts.WMTSProtocolHandler handler           = null;
45
        private WMTSServerDescription                           serverDescription = null;
46 34235 nbrodin
47
        /**
48
         * Constructor.
49
         * the parameter host, indicates the WMS host to connect.
50
         * */
51
        public WMTSClient(String host) throws ConnectException, IOException {
52
                setHost(host);
53
                try {
54
                        handler = WMTSProtocolHandlerFactory.negotiate(host);
55
                        handler.setHost(host);
56
                } catch(ConnectException conE) {
57
                        conE.printStackTrace();
58
                        throw conE;
59
                } catch(IOException ioE) {
60
                        ioE.printStackTrace();
61
                        throw ioE;
62
                } catch(Exception e) {
63
                        e.printStackTrace();
64
                }
65
        }
66
67
    public String getVersion() {
68
        return handler.getVersion();
69
    }
70
71
    /**
72
     * <p> Reads from the WMS Capabilities, the layers available in the service</p>
73
     * @return a TreeMap with the available layers in the WMS
74
     */
75
        public ArrayList getLayers() {
76 34305 nbrodin
            if(serverDescription != null)
77
                        return serverDescription.getLayerList();
78 34235 nbrodin
                return null;
79
    }
80
81
    /**
82
     * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
83
     * @return, number of layers available
84
     */
85
    public int getNumberOfLayers() {
86 34305 nbrodin
            if(serverDescription != null)
87
                        return serverDescription.getLayerList().size();
88 34235 nbrodin
                return 0;
89
    }
90
91
        @Override
92
        public void close() {
93 34305 nbrodin
                serverDescription = null;
94 34235 nbrodin
                handler = null;
95
        }
96
97 34305 nbrodin
         /**
98
     * <p>One of the three interfaces that OGC WMS defines. Request a map.</p>
99
     * @throws ServerErrorException
100
     */
101
    public File getTile(WMTSStatus status, ICancellable cancel) throws WMTSException, ServerErrorException {
102
        return handler.getTile(status, cancel);
103
    }
104
105 34235 nbrodin
        /**
106
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
107
     * @param override, if true the previous downloaded data will be overridden
108
     */
109 34305 nbrodin
    public void getCapabilities(WMTSServerDescription serverDescription, boolean override, ICancellable cancel) {
110
        handler.getCapabilities(serverDescription, override, cancel);
111 34235 nbrodin
    }
112
113
        /*
114
         * (non-Javadoc)
115
         * @see org.gvsig.remoteclient.RemoteClient#connect(boolean, org.gvsig.compat.net.ICancellable)
116
         */
117
        @Override
118
        public boolean connect(boolean override, ICancellable cancel) {
119
                try {
120
            if (handler == null) {
121
                if (getHost().trim().length() > 0) {
122
                    handler = WMTSProtocolHandlerFactory.negotiate(getHost());
123
                    handler.setHost(getHost());
124
                } else {
125
                    return false;
126
                }
127
            }
128 34305 nbrodin
            if(serverDescription == null)
129
                    serverDescription = new WMTSServerDescription(handler.getVersion());
130
            getCapabilities(serverDescription, override, cancel);
131 34235 nbrodin
            return true;
132
133
        } catch (Exception e) {
134
            e.printStackTrace();
135
            return false;
136
        }
137
        }
138
139
        public boolean connect(ICancellable cancel) {
140
                return connect(false, cancel);
141
        }
142
143
        /**
144
         * Gets the list of formats supported by a layer
145
         * @param layerTitle
146
         * @return
147
         */
148
        public ArrayList getFormats(String layerTitle) {
149 34305 nbrodin
                if(serverDescription != null)
150
                        return serverDescription.getLayerByTitle(layerTitle).getFormat();
151 34235 nbrodin
                return null;
152
    }
153
154 34269 nbrodin
        /**
155
         * Gets the host description
156
         * @return
157
         */
158
        public WMTSServiceIdentification getServiceIdentification() {
159 34305 nbrodin
                if(serverDescription != null)
160
                        return serverDescription.getServiceIdentification();
161 34235 nbrodin
                return null;
162
        }
163 34269 nbrodin
164
        /**
165
         * Gets the host description
166
         * @return
167
         */
168
        public WMTSServiceProvider getServiceProvider() {
169 34305 nbrodin
                if(serverDescription != null)
170
                        return serverDescription.getServiceProvider();
171 34269 nbrodin
                return null;
172
        }
173 34235 nbrodin
174
        /**
175
         * Gets the list of themes
176
         * @return
177
         */
178
        public WMTSThemes getThemes() {
179 34305 nbrodin
                if(serverDescription != null)
180
                        return serverDescription.getThemes();
181 34235 nbrodin
                return null;
182
        }
183 34257 nbrodin
184
    /**
185
     * Gets the set of tiles definition
186
     * @return
187
     */
188
    public ArrayList getTileMatrixSet() {
189 34305 nbrodin
            if(serverDescription != null)
190
                    return serverDescription.getTileMatrixSet();
191 34257 nbrodin
            return null;
192
    }
193 34235 nbrodin
194
    /**
195
     * Gets a layer
196
     * @param layerName
197
     * @return
198
     */
199
    public WMTSLayer getLayer(String layerName) {
200 34305 nbrodin
            if(serverDescription != null) {
201
                        ArrayList list = serverDescription.getLayerList();
202 34235 nbrodin
                        for (int i = 0; i < list.size(); i++) {
203
                                WMTSLayer layer = (WMTSLayer)list.get(i);
204
                                if(layer.getTitle().compareTo(layerName) == 0) {
205
                                        return layer;
206
                                }
207
                        }
208
            }
209
                return null;
210
    }
211
}