Statistics
| Revision:

root / trunk / extensions / extWCS / src / com / iver / cit / gvsig / fmap / drivers / wcs / FMapWCSDriver.java @ 4718

History | View | Annotate | Download (9.53 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*
19
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib??ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   IVER T.I. S.A
34
*   Salamanca 50
35
*   46005 Valencia
36
*   Spain
37
*
38
*   +34 963163400
39
*   dac@iver.es
40
*/
41
package com.iver.cit.gvsig.fmap.drivers.wcs;
42

    
43
import java.awt.geom.Point2D;
44
import java.awt.geom.Rectangle2D;
45
import java.io.File;
46
import java.io.IOException;
47
import java.net.ConnectException;
48
import java.net.URL;
49
import java.util.ArrayList;
50
import java.util.Hashtable;
51
import java.util.Iterator;
52
import java.util.Set;
53

    
54
import org.gvsig.remoteClient.exceptions.ServerErrorException;
55
import org.gvsig.remoteClient.utils.BoundaryBox;
56
import org.gvsig.remoteClient.wcs.WCSClient;
57
import org.gvsig.remoteClient.wcs.WCSCoverage;
58
import org.gvsig.remoteClient.wcs.WCSStatus;
59
import org.gvsig.remoteClient.wcs.WCSCoverage.AxisDescription;
60

    
61
import com.iver.cit.gvsig.fmap.DriverException;
62
import com.iver.cit.gvsig.fmap.drivers.RemoteServiceDriver;
63
import com.iver.cit.gvsig.fmap.drivers.WCSException;
64
import com.iver.cit.gvsig.fmap.layers.FMapWCSParameter;
65
import com.iver.cit.gvsig.fmap.layers.WCSLayer;
66

    
67

    
68

    
69
/**
70
 * Driver between the FMap and WCSClient
71
 * 
72
 * Controlador entre FMap y WCSClient
73
 * 
74
 * @author jaume
75
 *
76
 */
77
public class FMapWCSDriver extends RemoteServiceDriver {
78
        private WCSClient client = null;
79
        private Hashtable coverages;
80
        private WCSLayer[] layerList;
81
        
82
        
83
        /**
84
         * Returns the string "WCSDriver", which is the driver's name.
85
         * 
86
         * Devuelve "WCSDriver", el nombre del driver.
87
         * @return String 
88
         */
89
        public String getName() { return "WCSDriver"; } 
90
        
91
        /**
92
         * Sets the server that we want to connect to.
93
         * 
94
         * Establece el servidor al que se quiere conectar.
95
         * 
96
         * @param host
97
         * @throws IOException 
98
         */
99
        public void setHost(String host) throws IOException{
100
                super.setHost(host);
101
                setServiceName("WCS"); 
102
                client = new WCSClient(host);
103
        }
104
        
105
        
106
        /**
107
         * Returns a human-readable string containing the server's name.
108
         * 
109
         * Devuelve el nombre legible del servidor devuelto por ?ste.
110
         * 
111
         * @return String
112
         */ 
113
        public String getLabel() {
114
                return client.getServiceTitle();
115
        }
116
        
117
        /**
118
         * Returns a string containing the server's WCS version number.
119
         * 
120
         * Devuelve el n?mero de versi?n WCS del servidor
121
         * 
122
         * @return String
123
         */
124
        public String getVersion(){
125
                return client.getVersion();
126
        }
127
        
128
        /**
129
         * <p>
130
         * Returns name and description of the server. It is supposed to be used
131
         * as the source of the abstract field in your application's interface.
132
         * </p>
133
         * <p>
134
         * Devuelve nombre y descripci?n (abstract) del servidor.
135
         * </p>
136
         * @return String
137
         */
138
        public String getDescription(){
139
                return client.getDescription();
140
        }
141
        
142
        /**
143
         * Returns the layer descriptor for a given coverage name.
144
         * @param layerName
145
         * @return WCSLayer
146
         */
147
        public WCSLayer getLayer(String layerName) {
148
                return (WCSLayer) coverages.get(layerName);
149
        }
150
        
151
        /**
152
         * Returns an array of WCSLayer's with the descriptors of all coverages
153
         * @return WCSLayer[]
154
         */
155
        public WCSLayer[] getLayerList(){
156
                if (coverages == null) {
157
                        // the WCSLayer collection will be built
158
                        coverages = new Hashtable();
159
                        Hashtable wcsCoverages  = client.getCoverageList();
160
                        int sz = wcsCoverages.size();
161
                        
162
                        // Create an array with the WCSCoverages
163
                        WCSCoverage[] coverageList = new WCSCoverage[sz];
164
                        Iterator it = wcsCoverages.keySet().iterator();
165
                        int i = 0;
166
                        while (it.hasNext()) {
167
                                coverageList[i] = (WCSCoverage) wcsCoverages.get(it.next());
168
                                i++;
169
                        }
170
                        
171
                        // Create a WCSLayer array from the previous WCSCoverage array
172
                        layerList = new WCSLayer[sz];
173
                        for (int j = 0; j < layerList.length; j++) {
174
                                WCSLayer lyr = new WCSLayer();
175
                                WCSCoverage cov = coverageList[j];
176
                                // name
177
                                lyr.setName(cov.getName());
178
                                
179
                                // title
180
                                lyr.setTitle(cov.getTitle());
181
                                
182
                                // description
183
                                lyr.setDescription(cov.getAbstract());
184
                                
185
                                // srs
186
                                lyr.addAllSrs(cov.getAllSrs());
187
                                
188
                                // native srs
189
                                lyr.setNativeSRS(cov.getNativeSRS());
190
                                
191
                                // extents
192
                                Set k = cov.getBBoxes().keySet();
193
                                if (!k.isEmpty()) {
194
                                        it = k.iterator();
195
                                        while (it.hasNext()) {
196
                                                String srs = (String) it.next();
197
                                                BoundaryBox bBox = cov.getBbox(srs);
198
                                                Rectangle2D r = new Rectangle2D.Double(
199
                                                                                                bBox.getXmin(),
200
                                                                                                bBox.getYmin(), 
201
                                                                                                bBox.getXmax()-bBox.getXmin(), 
202
                                                                                                bBox.getYmax()-bBox.getYmin()
203
                                                                                                );
204
                                                lyr.addExtent(srs, r);
205
                                                }
206
                                }
207
                                
208
                                // formats
209
                                lyr.setFormats(cov.getFormats());
210
                                
211
                                // time positions
212
                                lyr.setTimePositions(cov.getTimePositions());
213
                                
214
                                // max res
215
                                lyr.setMaxRes(new Point2D.Double(cov.getResX(), cov.getResY()));
216
                                
217
                                // interpolations
218
                                lyr.setInterpolationMethods(cov.getInterpolationMethods());
219
                                
220
                                // parameters
221
                                k = cov.axisPool.keySet();
222
                                if (!k.isEmpty()) {
223
                                        it = k.iterator();
224
                                        while (it.hasNext()) {
225
                                                AxisDescription ad = (AxisDescription) cov.axisPool.get(it.next());
226
                                                FMapWCSParameter p = new FMapWCSParameter();
227
                                                p.setName(ad.getName());
228
                                                p.setLabel(ad.getLabel());
229
                                                p.setType(ad.getInterval()==null? FMapWCSParameter.VALUE_LIST : FMapWCSParameter.INTERVAL);
230
                                                if (p.getType()==FMapWCSParameter.VALUE_LIST) 
231
                                                        p.setValueList(ad.getSingleValues());
232
                                                else
233
                                                        p.setInterval(ad.getInterval());
234
                                                lyr.addParameter(p);
235
                                        }
236
                                }
237
                                layerList[j] = lyr;
238
                                coverages.put(lyr.getName(), lyr);
239
                        }
240
                }
241
                
242
                return layerList;
243
        }
244
        
245
        /**
246
         * Establishes the connection to the WCS server. Connecting to a WCS is
247
         * an abstraction.<br>
248
         * <p>
249
         * Actually, it sends a GetCapabilities and a general DescribeCoverage
250
         * request (not a coverage-specific DescribeCoverage request) to read the
251
         * necessary data for building further GetCoverage requests.
252
         * </p>
253
         * @throws IOException, DriverException.
254
         */
255
        public boolean connect() throws IOException, DriverException {
256
                setHost(client.getHost());
257
                return client.connect();
258
        }
259

    
260
        /**
261
         * No close operation is needed since WCS service it is a non-session based
262
         * protocol. So, this does nothing and you can omit it.<br>
263
         */
264
        public void close() {
265
                setConnected(false);
266
        }
267

    
268
        /**
269
         * Returns the label of an specific coverage given by the coverage name
270
         * @param coverage name (string)
271
         * @return string
272
         */
273
        public String getLabel(String coverageName) {
274
                return client.getLabel(coverageName);
275
        }
276

    
277
        /**
278
         * Returns the coverage's MAX extent from the server.
279
         * @return Rectangle2D
280
         * @throws DriverException
281
         * @throws IOException
282
         */
283
        public Rectangle2D getFullExtent(String coverageName, String srs) throws IOException, DriverException {
284
                return client.getExtent(coverageName, srs);
285
        }
286
        
287
        /**
288
         * Returns the max resolution of a specific coverage given by the coverage's name.
289
         * @param coverage name (string)
290
         * @return double
291
         */
292
        public Point2D getMaxResolution(String coverageName) {
293
                if (coverages.containsKey(coverageName)) {
294
                        return ((WCSLayer) coverages.get(coverageName)).getMaxRes();
295
                }
296
                return null;
297
        }
298

    
299

    
300
        /**
301
         * Returns an ArrayList containing a set of Strings with the coverage's SRSs.
302
         * @param coverage name (string)
303
         * @return ArrayList
304
         */
305
        public ArrayList getSRSs(String coverageName) {        
306
                if (coverages.containsKey(coverageName)) {
307
                        return ((WCSLayer) coverages.get(coverageName)).getSRSs();
308
                }
309
                return null;
310
        }
311

    
312
        /**
313
         * Returns a String containing a description of an specific coverage.
314
         * @param coverage name (string)
315
         * @return string
316
         */
317
        public String getCoverageDescription(String coverageName) {
318
                if (coverages.containsKey(coverageName)) {
319
                        return ((WCSLayer) coverages.get(coverageName)).getDescription();
320
                }
321
                return null;
322
        }
323

    
324
        /**
325
         * Returns an ArrayList containing strings for the time positions of an
326
         * specific coverage given by the coverage's name.
327
         * @param coverage name (string)
328
         * @return ArrayList
329
         */
330
        public ArrayList getTimes(String coverageName) {
331
                if (coverages.containsKey(coverageName)) {
332
                        return ((WCSLayer) coverages.get(coverageName)).getTimePositions();
333
                }
334
                return null;
335
        }
336

    
337
        /**
338
         * Sends a GetCoverage request to the client.
339
         * @param status
340
         * @return
341
         * @throws WCSException
342
         */
343
        public File getCoverage(WCSStatus status) throws WCSException {
344
                try {
345
                        return client.getCoverage(status);
346
                } catch (ServerErrorException e) {
347
                        throw new WCSException("WCS Unexpected server error."+e.getMessage());
348
                } catch (org.gvsig.remoteClient.exceptions.WCSException e) {
349
                        throw new WCSException(e.getMessage());
350
                }
351
        }
352

    
353
        /**
354
     * Creates a new instance of a WCSClient.
355
     * @param host
356
     * @throws IOException 
357
     * @throws ConnectException 
358
     */
359
        public void createClient(URL host) throws ConnectException, IOException{
360
                client = new WCSClient(host.toString());
361
        } 
362
}