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 @ 40559

History | View | Annotate | Download (7.31 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.gvsig.remoteclient.wms.WMSStatus;
41

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

    
50
    private WCSProtocolHandler handler;
51
    private Hashtable layerPool;
52

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

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

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

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

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

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

    
132
        layerPool = handler.getLayers();
133
    }
134

    
135
    /* (non-Javadoc)
136
     * @see org.gvsig.remoteClient.RemoteClient#close()
137
     */
138
    public void close() {
139
    }
140

    
141
    /**
142
     * Returns the title of the service. The title is a human-readable string format
143
     * used to label the service connection.
144
     * @return String
145
     */
146
        public String getServiceTitle() {
147
                return handler.serviceInfo.title;
148
        }
149

    
150
        /**
151
         * Returns the service version (1.0.0, 1.1.0, ...).
152
         * @return String
153
         */
154
        public String getVersion() {
155
                return handler.getVersion();
156
        }
157

    
158
        /**
159
         * Returns a brief description of the service, it is a human-readable string.
160
         * @return String
161
         */
162
        public String getDescription() {
163
                return handler.serviceInfo.abstr;
164
        }
165

    
166
        /**
167
         *
168
         * @return
169
         */
170
        public ArrayList getFormats() {
171
                return handler.getFormats();
172
        }
173

    
174
        /**
175
         * Returns a hash table containing the WCSCoverage's produced at parse time using
176
         * the coverage names as the Hashtable's keys
177
         * @return Hashtable.
178
         */
179
        public Hashtable getCoverageList() {
180
                return layerPool;
181
        }
182

    
183
        /**
184
         * Given a coverage name, it returns the coverage's title.
185
         * @param coverageName
186
         * @return String
187
         */
188
        public String getLabel(String coverageName) {
189
                return ((WCSCoverage) layerPool.get(coverageName)).getTitle();
190
        }
191

    
192
        /**
193
         * Given a coverage name and the CRS name, it returns the extent defined by the
194
         * server in the DescribeCoverage document.
195
         *
196
         * @param coverageName
197
         * @param crs
198
         * @return Rectangle2D
199
         */
200
        public Rectangle2D getExtent(String coverageName, String crs) {
201
                BoundaryBox bbox = (BoundaryBox) ((WCSCoverage) layerPool.get(coverageName)).getBbox(crs);;
202
        if (bbox == null) return null;
203
        double xmin = bbox.getXmin();
204
        double xmax = bbox.getXmax();
205
        double ymin = bbox.getYmin();
206
        double ymax = bbox.getYmax();
207
                return new Rectangle2D.Double(xmin, ymin, xmax-xmin, ymax-ymin);
208
        }
209

    
210
        /**
211
         * Sends the GetCoverage request according to the settings passed in the status
212
         * argument.
213
         * @param status
214
         * @return File
215
         * @throws ServerErrorException
216
         * @throws WCSException
217
         */
218
        public File getCoverage(WCSStatus status, ICancellable cancel) throws ServerErrorException, WCSException {
219
                return handler.getCoverage(status, cancel);
220
        }
221
        
222
        /**
223
     * <p>Gets the GetMap URL. The final client should download the file</p> 
224
     * @throws ServerErrorException 
225
     */
226
    public URL getCoverageURL(WCSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{   
227
       return handler.getCoverageURL(status, cancel);
228
    } 
229
    
230
    /**
231
     * Returns the exception message if the file is a XML instead of a image.
232
     * @param file
233
     * @return
234
     * @throws IOException 
235
     */
236
    public String getExceptionMessage(File file) throws IOException {
237
            return handler.getExceptionMessage(file);
238
    }
239
}
240