Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wcs / WCSClient.java @ 41125

History | View | Annotate | Download (8.3 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.wcs;
25

    
26
import java.awt.geom.Rectangle2D;
27
import java.io.File;
28
import java.io.IOException;
29
import java.net.ConnectException;
30
import java.net.URL;
31
import java.util.ArrayList;
32
import java.util.Hashtable;
33
import java.util.Iterator;
34

    
35
import org.gvsig.compat.net.ICancellable;
36
import org.gvsig.remoteclient.exceptions.ServerErrorException;
37
import org.gvsig.remoteclient.exceptions.WCSException;
38
import org.gvsig.remoteclient.exceptions.WMSException;
39
import org.gvsig.remoteclient.utils.BoundaryBox;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

    
43
/**
44
 * WCSClient managing the low-level comunication to the server. It is used
45
 * as a bridge between a standard WCS server and other high-level clients.
46
 * @author jaume dominguez faus - jaume.dominguez@iver.es
47
 *
48
 */
49
public class WCSClient extends org.gvsig.remoteclient.RasterClient{
50

    
51
    private WCSProtocolHandler handler   = null;
52
    private Hashtable          layerPool = null;
53
    private Logger             log       = LoggerFactory.getLogger(WCSClient.class);
54

    
55
    /**
56
     * Constructor.
57
     * the parameter host, indicates the WCS host to connect.
58
     * @throws IOException
59
     *
60
     */
61
    public WCSClient(String host) throws ConnectException, IOException {
62
        setHost(host);
63
        try {
64
                handler = WCSProtocolHandlerFactory.negotiate(hostName);
65
                handler.setHost(hostName);
66
        } catch(ConnectException conE) {
67
                throw conE;
68
        } catch(IOException ioE) {
69
                throw ioE;
70
        } catch(Exception e) {
71
                log.info("Error getting handler for " + hostName, e);
72
        }
73
    }
74

    
75
    /**
76
     * <p>Checks the connection to de remote WMS and requests its capabilities.</p>
77
     * @param override
78
     *
79
     */
80
    public boolean connect(boolean override, ICancellable cancel)
81
    {
82
        try {
83
            if (handler == null) {
84
                if (getHost().trim().length() > 0) {
85
                        handler = WCSProtocolHandlerFactory.negotiate(getHost());
86
                    handler.setHost(getHost());
87
                } else {
88
                    // must to specify host first!!!!
89
                    return false;
90
                }
91
            }
92
            getCapabilities(null, override, cancel);
93

    
94
            describeCoverage(null, override, cancel); //TODO falta posar el onlineresource del describe coverage
95
            return true;
96

    
97
        } catch (Exception e) {
98
            e.printStackTrace();
99
            return false;
100
        }
101
    }
102

    
103
    /**
104
     * Sends a GetCapabilities request using the properties contained by
105
     * the status. If status is null, then it uses the default configuration.
106
     * @param override
107
     * @param WCSStatus, containing the status properties
108
     */
109
    private void getCapabilities(WCSStatus status, boolean override, ICancellable cancel) {
110
        handler.getCapabilities(status, override, cancel);
111
    }
112

    
113
    /**
114
     * Sends a DescribeCoverage request using the properties contained by
115
     * the status.
116
     * @param override, if true the cache is ignored
117
     * @param WCSStatus, containing the status properties
118
     */
119
    private void describeCoverage(WCSStatus status, boolean override, ICancellable cancel) {
120
        handler.describeCoverage(status, override, cancel);
121
        // check it was response or if we need to perform a specific DescribeCoverage for each coverage
122
        Hashtable layers = handler.getLayers();
123
        Iterator it = layers.keySet().iterator();
124
        while (it.hasNext()) {
125
                Object obj = layers.get(it.next());
126
                if (obj instanceof CoverageOfferingBrief) {
127
                        if (status == null)
128
                                status = new WCSStatus();
129
                        status.setCoveraName( ((CoverageOfferingBrief) obj).getName());
130
                        handler.describeCoverage(status, override, cancel);
131
                }
132
        }
133

    
134
        layerPool = handler.getLayers();
135
    }
136
    
137
    private Hashtable getLayerPool() {
138
            if(layerPool == null || layerPool.isEmpty()) {
139
                    layerPool = getHandLer().getLayers();
140
                    if(layerPool.isEmpty()) {
141
                            describeCoverage(null, true, null);
142
                            layerPool = getHandLer().getLayers();
143
                    }
144
            }
145
            return layerPool;
146
    }
147
    
148
    private WCSProtocolHandler getHandLer() {
149
            if(handler == null) {
150
                    try {
151
                    handler = WCSProtocolHandlerFactory.negotiate(hostName);
152
                    handler.setHost(hostName);
153
            } catch(ConnectException conE) {
154
                    log.debug("Error getting handler for " + hostName, conE);
155
            } catch(IOException ioE) {
156
                    log.debug("Error getting handler for " + hostName, ioE);
157
            } catch(Exception e) {
158
                    log.debug("Error getting handler for " + hostName, e);
159
            }
160
            }
161
            return handler;
162
    }
163
    
164
    public void close() {
165
    }
166

    
167
    /**
168
     * Returns the title of the service. The title is a human-readable string format
169
     * used to label the service connection.
170
     * @return String
171
     */
172
        public String getServiceTitle() {
173
                return getHandLer().serviceInfo.title;
174
        }
175

    
176
        /**
177
         * Returns the service version (1.0.0, 1.1.0, ...).
178
         * @return String
179
         */
180
        public String getVersion() {
181
                return getHandLer().getVersion();
182
        }
183

    
184
        /**
185
         * Returns a brief description of the service, it is a human-readable string.
186
         * @return String
187
         */
188
        public String getDescription() {
189
                return getHandLer().serviceInfo.abstr;
190
        }
191

    
192
        /**
193
         *
194
         * @return
195
         */
196
        public ArrayList getFormats() {
197
                return getHandLer().getFormats();
198
        }
199

    
200
        /**
201
         * Returns a hash table containing the WCSCoverage's produced at parse time using
202
         * the coverage names as the Hashtable's keys
203
         * @return Hashtable.
204
         */
205
        public Hashtable getCoverageList() {
206
                return getLayerPool();
207
        }
208

    
209
        /**
210
         * Given a coverage name, it returns the coverage's title.
211
         * @param coverageName
212
         * @return String
213
         */
214
        public String getLabel(String coverageName) {
215
                return ((WCSCoverage) getLayerPool().get(coverageName)).getTitle();
216
        }
217

    
218
        /**
219
         * Given a coverage name and the CRS name, it returns the extent defined by the
220
         * server in the DescribeCoverage document.
221
         *
222
         * @param coverageName
223
         * @param crs
224
         * @return Rectangle2D
225
         */
226
        public Rectangle2D getExtent(String coverageName, String crs) {
227
                BoundaryBox bbox = (BoundaryBox) ((WCSCoverage) getLayerPool().get(coverageName)).getBbox(crs);
228
        if (bbox == null) return null;
229
        double xmin = bbox.getXmin();
230
        double xmax = bbox.getXmax();
231
        double ymin = bbox.getYmin();
232
        double ymax = bbox.getYmax();
233
                return new Rectangle2D.Double(xmin, ymin, xmax-xmin, ymax-ymin);
234
        }
235

    
236
        /**
237
         * Sends the GetCoverage request according to the settings passed in the status
238
         * argument.
239
         * @param status
240
         * @return File
241
         * @throws ServerErrorException
242
         * @throws WCSException
243
         */
244
        public File getCoverage(WCSStatus status, ICancellable cancel) throws ServerErrorException, WCSException {
245
                return getHandLer().getCoverage(status, cancel);
246
        }
247
        
248
        /**
249
     * <p>Gets the GetMap URL. The final client should download the file</p> 
250
     * @throws ServerErrorException 
251
     */
252
    public URL getCoverageURL(WCSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{   
253
       return getHandLer().getCoverageURL(status, cancel);
254
    } 
255
    
256
    /**
257
     * Returns the exception message if the file is a XML instead of a image.
258
     * @param file
259
     * @return
260
     * @throws IOException 
261
     */
262
    public String getExceptionMessage(File file) throws IOException {
263
            return getHandLer().getExceptionMessage(file);
264
    }
265
}
266