Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_1_RELEASE / libraries / libRemoteServices / src / org / gvsig / remoteClient / wcs / WCSClient.java @ 9531

History | View | Annotate | Download (6.86 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.remoteClient.wcs;
42

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

    
51
import org.gvsig.remoteClient.exceptions.ServerErrorException;
52
import org.gvsig.remoteClient.exceptions.WCSException;
53
import org.gvsig.remoteClient.utils.BoundaryBox;
54
import org.gvsig.remoteClient.wcs.wcs_1_0_0.WCSProtocolHandler1_0_0;
55
import org.gvsig.remoteClient.wms.ICancellable;
56

    
57
/**
58
 * WCSClient managing the low-level comunication to the server. It is used
59
 * as a bridge between a standard WCS server and other high-level clients.
60
 * @author jaume dominguez faus - jaume.dominguez@iver.es
61
 *
62
 */
63
public class WCSClient extends org.gvsig.remoteClient.RasterClient{
64

    
65
    private WCSProtocolHandler handler;
66
    private Hashtable layerPool;
67

    
68
    /**
69
     * Constructor.
70
     * the parameter host, indicates the WCS host to connect.
71
     * @throws IOException
72
     *
73
     */
74
    public WCSClient(String host) throws ConnectException, IOException {
75
        setHost(host);
76
        try {
77
                handler = WCSProtocolHandlerFactory.negotiate(host);
78
                handler.setHost(host);
79
        } catch(ConnectException conE) {
80
                throw conE;
81
        } catch(IOException ioE) {
82
                throw ioE;
83
        } catch(Exception e) {
84
                e.printStackTrace();
85
        }
86
    }
87

    
88
    /**
89
     * @param override
90
     *
91
     */
92
    public boolean connect(boolean override, ICancellable cancel)
93
    {
94
        try {
95
            if (handler == null) {
96
                if (getHost().trim().length() > 0) {
97
                        handler = WCSProtocolHandlerFactory.negotiate(getHost());
98
                    handler.setHost(getHost());
99
                } else {
100
                    // must to specify host first!!!!
101
                    return false;
102
                }
103
            }
104
            getCapabilities(null, override, cancel);
105

    
106
            describeCoverage(null, override, cancel); //TODO falta posar el onlineresource del describe coverage
107
            return true;
108

    
109
        } catch (Exception e) {
110
            e.printStackTrace();
111
            return false;
112
        }
113
    }
114

    
115
    /**
116
     * Sends a GetCapabilities request using the properties contained by
117
     * the status. If status is null, then it uses the default configuration.
118
     * @param override
119
     * @param WCSStatus, containing the status properties
120
     */
121
    private void getCapabilities(WCSStatus status, boolean override, ICancellable cancel) {
122
        handler.getCapabilities(status, override, cancel);
123
    }
124

    
125
    /**
126
     * Sends a DescribeCoverage request using the properties contained by
127
     * the status.
128
     * @param override, if true the cache is ignored
129
     * @param WCSStatus, containing the status properties
130
     */
131
    private void describeCoverage(WCSStatus status, boolean override, ICancellable cancel) {
132
        handler.describeCoverage(status, override, cancel);
133
        // check it was response or if we need to perform a specific DescribeCoverage for each coverage
134
        Hashtable layers = handler.getLayers();
135
        Iterator it = layers.keySet().iterator();
136
        while (it.hasNext()) {
137
                Object obj = layers.get(it.next());
138
                if (obj instanceof CoverageOfferingBrief) {
139
                        if (status == null)
140
                                status = new WCSStatus();
141
                        status.setCoveraName( ((CoverageOfferingBrief) obj).getName());
142
                        handler.describeCoverage(status, override, cancel);
143
                }
144
        }
145

    
146
        layerPool = handler.getLayers();
147
    }
148

    
149
    /* (non-Javadoc)
150
     * @see org.gvsig.remoteClient.RemoteClient#close()
151
     */
152
    public void close() {
153
    }
154

    
155
    /**
156
     * Returns the title of the service. The title is a human-readable string format
157
     * used to label the service connection.
158
     * @return String
159
     */
160
        public String getServiceTitle() {
161
                return handler.serviceInfo.title;
162
        }
163

    
164
        /**
165
         * Returns the service version (1.0.0, 1.1.0, ...).
166
         * @return String
167
         */
168
        public String getVersion() {
169
                return handler.getVersion();
170
        }
171

    
172
        /**
173
         * Returns a brief description of the service, it is a human-readable string.
174
         * @return String
175
         */
176
        public String getDescription() {
177
                return handler.serviceInfo.abstr;
178
        }
179

    
180
        /**
181
         *
182
         * @return
183
         */
184
        public ArrayList getFormats() {
185
                return handler.getFormats();
186
        }
187

    
188
        /**
189
         * Returns a hash table containing the WCSCoverage's produced at parse time using
190
         * the coverage names as the Hashtable's keys
191
         * @return Hashtable.
192
         */
193
        public Hashtable getCoverageList() {
194
                return layerPool;
195
        }
196

    
197
        /**
198
         * Given a coverage name, it returns the coverage's title.
199
         * @param coverageName
200
         * @return String
201
         */
202
        public String getLabel(String coverageName) {
203
                return ((WCSCoverage) layerPool.get(coverageName)).getTitle();
204
        }
205

    
206
        /**
207
         * Given a coverage name and the CRS name, it returns the extent defined by the
208
         * server in the DescribeCoverage document.
209
         *
210
         * @param coverageName
211
         * @param crs
212
         * @return Rectangle2D
213
         */
214
        public Rectangle2D getExtent(String coverageName, String crs) {
215
                BoundaryBox bbox = (BoundaryBox) ((WCSCoverage) layerPool.get(coverageName)).getBbox(crs);;
216
        if (bbox == null) return null;
217
        double xmin = bbox.getXmin();
218
        double xmax = bbox.getXmax();
219
        double ymin = bbox.getYmin();
220
        double ymax = bbox.getYmax();
221
                return new Rectangle2D.Double(xmin, ymin, xmax-xmin, ymax-ymin);
222
        }
223

    
224
        /**
225
         * Sends the GetCoverage request according to the settings passed in the status
226
         * argument.
227
         * @param status
228
         * @return File
229
         * @throws ServerErrorException
230
         * @throws WCSException
231
         */
232
        public File getCoverage(WCSStatus status, ICancellable cancel) throws ServerErrorException, WCSException {
233
                return handler.getCoverage(status, cancel);
234
        }
235
}
236