Statistics
| Revision:

root / tags / v2_0_0_Build_2020 / extensions / extWCS / src / org / gvsig / wcs / fmap / drivers / wcs / FMapWCSDriver.java @ 33833

History | View | Annotate | Download (9.48 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 org.gvsig.wcs.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.compat.net.ICancellable;
55
import org.gvsig.fmap.dal.exception.ReadException;
56
import org.gvsig.remoteclient.exceptions.ServerErrorException;
57
import org.gvsig.remoteclient.exceptions.WCSException;
58
import org.gvsig.remoteclient.utils.BoundaryBox;
59
import org.gvsig.remoteclient.wcs.WCSClient;
60
import org.gvsig.remoteclient.wcs.WCSCoverage;
61
import org.gvsig.remoteclient.wcs.WCSStatus;
62
import org.gvsig.remoteclient.wcs.WCSCoverage.AxisDescription;
63
import org.gvsig.wcs.fmap.layers.FMapWCSParameter;
64
import org.gvsig.wcs.fmap.layers.WCSLayer;
65

    
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 {
78
        private WCSClient client = null;
79
        private Hashtable coverages;
80
        private WCSLayer[] layerList;
81

    
82

    
83

    
84
        /**
85
         * Obtains the host name.
86
         *
87
         * Devuelve el nombre del host.
88
         */
89
        public String getHost() {
90
                return client.getHost();
91
        }
92

    
93
        private FMapWCSDriver() {}
94

    
95
        protected FMapWCSDriver(URL url) throws ConnectException, IOException {
96
                client = new WCSClient(url.toString());
97
        }
98

    
99
        /**
100
         * Returns the string "WCSDriver", which is the driver's name.
101
         *
102
         * Devuelve "WCSDriver", el nombre del driver.
103
         * @return String
104
         */
105
        public String getName() { return "WCSDriver"; }
106

    
107
        /**
108
         * Sets the server that we want to connect to.
109
         *
110
         * Establece el servidor al que se quiere conectar.
111
         *
112
         * @param host
113
         * @throws IOException
114
         */
115
        public void setHost(String host) throws IOException{
116
                client = new WCSClient(host);
117
        }
118

    
119

    
120
        /**
121
         * Returns a human-readable string containing the server's name.
122
         *
123
         * Devuelve el nombre legible del servidor devuelto por ?ste.
124
         *
125
         * @return String
126
         */
127
        public String getLabel() {
128
                return client.getServiceTitle();
129
        }
130

    
131
        /**
132
         * Returns a string containing the server's WCS version number.
133
         *
134
         * Devuelve el n?mero de versi?n WCS del servidor
135
         *
136
         * @return String
137
         */
138
        public String getVersion(){
139
                return client.getVersion();
140
        }
141

    
142
        /**
143
         * <p>
144
         * Returns name and description of the server. It is supposed to be used
145
         * as the source of the abstract field in your application's interface.
146
         * </p>
147
         * <p>
148
         * Devuelve nombre y descripci?n (abstract) del servidor.
149
         * </p>
150
         * @return String
151
         */
152
        public String getDescription(){
153
                return client.getDescription();
154
        }
155

    
156
        /**
157
         * Returns the layer descriptor for a given coverage name.
158
         * @param layerName
159
         * @return WCSLayer
160
         */
161
        public WCSLayer getLayer(String layerName) {
162
                getLayerList();
163
                return (WCSLayer) coverages.get(layerName);
164
        }
165

    
166
        /**
167
         * Returns an array of WCSLayer's with the descriptors of all coverages
168
         * @return WCSLayer[]
169
         */
170
        public WCSLayer[] getLayerList(){
171
                if (coverages == null || coverages.isEmpty()) {
172
                        // the WCSLayer collection will be built
173
                        coverages = new Hashtable();
174
                        Hashtable wcsCoverages  = client.getCoverageList();
175
                        int sz = wcsCoverages.size();
176

    
177
                        // Create an array with the WCSCoverages
178
                        WCSCoverage[] coverageList = new WCSCoverage[sz];
179
                        Iterator it = wcsCoverages.keySet().iterator();
180
                        int i = 0;
181
                        while (it.hasNext()) {
182
                                coverageList[i] = (WCSCoverage) wcsCoverages.get(it.next());
183
                                i++;
184
                        }
185

    
186
                        // Create a WCSLayer array from the previous WCSCoverage array
187
                        layerList = new WCSLayer[sz];
188
                        for (int j = 0; j < layerList.length; j++) {
189
                                WCSLayer lyr = new WCSLayer();
190
                                WCSCoverage cov = coverageList[j];
191
                                // name
192
                                lyr.setName(cov.getName());
193

    
194
                                // title
195
                                lyr.setTitle(cov.getTitle());
196

    
197
                                // description
198
                                lyr.setDescription(cov.getAbstract());
199

    
200
                                // srs
201
                                lyr.addAllSrs(cov.getAllSrs());
202

    
203
                                // native srs
204
                                lyr.setNativeSRS(cov.getNativeSRS());
205

    
206
                                // extents
207
                                Set k = cov.getBBoxes().keySet();
208
                                if (!k.isEmpty()) {
209
                                        it = k.iterator();
210
                                        while (it.hasNext()) {
211
                                                String srs = (String) it.next();
212
                                                BoundaryBox bBox = cov.getBbox(srs);
213
                                                Rectangle2D r = new Rectangle2D.Double(
214
                                                                                                bBox.getXmin(),
215
                                                                                                bBox.getYmin(),
216
                                                                                                bBox.getXmax()-bBox.getXmin(),
217
                                                                                                bBox.getYmax()-bBox.getYmin()
218
                                                                                                );
219
                                                lyr.addExtent(srs, r);
220
                                                }
221
                                }
222

    
223
                                // formats
224
                                lyr.setFormats(cov.getFormats());
225

    
226
                                // time positions
227
                                lyr.setTimePositions(cov.getTimePositions());
228

    
229
                                // max res
230
                                lyr.setMaxRes(new Point2D.Double(cov.getResX(), cov.getResY()));
231

    
232
                                // interpolations
233
                                lyr.setInterpolationMethods(cov.getInterpolationMethods());
234

    
235
                                // parameters
236
                                k = cov.axisPool.keySet();
237
                                if (!k.isEmpty()) {
238
                                        it = k.iterator();
239
                                        while (it.hasNext()) {
240
                                                AxisDescription ad = (AxisDescription) cov.axisPool.get(it.next());
241
                                                FMapWCSParameter p = new FMapWCSParameter();
242
                                                p.setName(ad.getName());
243
                                                p.setLabel(ad.getLabel());
244
                                                p.setType(ad.getInterval()==null? FMapWCSParameter.VALUE_LIST : FMapWCSParameter.INTERVAL);
245
                                                if (p.getType()==FMapWCSParameter.VALUE_LIST) {
246
                                                        p.setValueList(ad.getSingleValues());
247
                                                } else {
248
                                                        p.setInterval(ad.getInterval());
249
                                                }
250
                                                lyr.addParameter(p);
251
                                        }
252
                                }
253
                                layerList[j] = lyr;
254
                                coverages.put(lyr.getName(), lyr);
255
                        }
256
                }
257
                return layerList;
258
        }
259

    
260
        /**
261
         * Establishes the connection to the WCS server. Connecting to a WCS is
262
         * an abstraction.<br>
263
         * <p>
264
         * Actually, it sends a GetCapabilities and a general DescribeCoverage
265
         * request (not a coverage-specific DescribeCoverage request) to read the
266
         * necessary data for building further GetCoverage requests.
267
         * </p>
268
         * @param override
269
         * @throws IOException.
270
         */
271
        public boolean connect(boolean override, ICancellable cancel)
272
                        throws IOException, ReadException {
273
                coverages = null;
274
                setHost(client.getHost());
275
                return client.connect(override, cancel);
276
        }
277

    
278
        /**
279
         * No close operation is needed since WCS service it is a non-session based
280
         * protocol. So, this does nothing and you can omit it.<br>
281
         */
282
        public void close() {
283
//                connected = false;
284
        }
285

    
286
        /**
287
         * Returns the label of an specific coverage given by the coverage name
288
         * @param coverage name (string)
289
         * @return string
290
         */
291
        public String getLabel(String coverageName) {
292
                return client.getLabel(coverageName);
293
        }
294

    
295
        /**
296
         * Returns the coverage's MAX extent from the server.
297
         * @return Rectangle2D
298
         * @throws IOException
299
         */
300
        public Rectangle2D getFullExtent(String coverageName, String srs)
301
                        throws IOException, ReadException {
302
                return client.getExtent(coverageName, srs);
303
        }
304

    
305
        /**
306
         * Returns the max resolution of a specific coverage given by the coverage's name.
307
         * @param coverage name (string)
308
         * @return double
309
         */
310
        public Point2D getMaxResolution(String coverageName) {
311
                if (coverages.containsKey(coverageName)) {
312
                        return ((WCSLayer) coverages.get(coverageName)).getMaxRes();
313
                }
314
                return null;
315
        }
316

    
317

    
318
        /**
319
         * Returns an ArrayList containing a set of Strings with the coverage's SRSs.
320
         * @param coverage name (string)
321
         * @return ArrayList
322
         */
323
        public ArrayList getSRSs(String coverageName) {
324
                if (coverages.containsKey(coverageName)) {
325
                        return ((WCSLayer) coverages.get(coverageName)).getSRSs();
326
                }
327
                return null;
328
        }
329

    
330
        /**
331
         * Returns a String containing a description of an specific coverage.
332
         * @param coverage name (string)
333
         * @return string
334
         */
335
        public String getCoverageDescription(String coverageName) {
336
                if (coverages.containsKey(coverageName)) {
337
                        return ((WCSLayer) coverages.get(coverageName)).getDescription();
338
                }
339
                return null;
340
        }
341

    
342
        /**
343
         * Returns an ArrayList containing strings for the time positions of an
344
         * specific coverage given by the coverage's name.
345
         * @param coverage name (string)
346
         * @return ArrayList
347
         */
348
        public ArrayList getTimes(String coverageName) {
349
                if (coverages.containsKey(coverageName)) {
350
                        return ((WCSLayer) coverages.get(coverageName)).getTimePositions();
351
                }
352
                return null;
353
        }
354

    
355
        /**
356
         * Sends a GetCoverage request to the client.
357
         * @param status
358
         * @return
359
         * @throws WCSException
360
         */
361
        public File getCoverage(WCSStatus status, ICancellable cancel) throws WCSDriverException {
362
                try {
363
                        return client.getCoverage(status, cancel);
364
                } catch (ServerErrorException e) {
365
                        throw new WCSDriverException(getName(),e);
366
                } catch (org.gvsig.remoteclient.exceptions.WCSException e) {
367
                        throw new WCSDriverException(getName(),e);
368
                }
369
        }
370

    
371
}