Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wcs / WCSClient.java @ 7010

History | View | Annotate | Download (6.75 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
     * <p>Checks the connection to de remote WMS and requests its capabilities.</p>
90
     *
91
     */
92
    public boolean connect(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, cancel);
105

    
106
            describeCoverage(null, 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 WCSStatus, containing the status properties
119
     */
120
    private void getCapabilities(WCSStatus status, ICancellable cancel) {
121
        handler.getCapabilities(status, cancel);
122
    }
123

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

    
144
        layerPool = handler.getLayers();
145
    }
146

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

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

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

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

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

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

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

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

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