Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / WMSClient.java @ 7485

History | View | Annotate | Download (7.58 KB)

1

    
2
package org.gvsig.remoteClient.wms;
3

    
4
import java.awt.geom.Rectangle2D;
5
import java.io.File;
6
import java.io.IOException;
7
import java.net.ConnectException;
8
import java.util.TreeMap;
9
import java.util.Vector;
10

    
11
import org.gvsig.remoteClient.exceptions.ServerErrorException;
12
import org.gvsig.remoteClient.exceptions.WMSException;
13
import org.gvsig.remoteClient.utils.BoundaryBox;
14

    
15

    
16
/**
17
 * <p>Represents the class the with the necessary logic to connect to a OGCWMS and interpretate the data </p>
18
 * 
19
 */
20
public class WMSClient extends org.gvsig.remoteClient.RasterClient {
21
    
22
    private org.gvsig.remoteClient.wms.WMSProtocolHandler handler;
23
//    private TreeMap layers = new TreeMap();
24
//    private WMSLayer rootLayer;
25
    
26
    /**
27
     * @return Returns the rootLayer.
28
     */
29
    public WMSLayer getRootLayer() {
30
        return handler.rootLayer;
31
    }
32

    
33
    /**
34
     * Constructor.
35
     * the parameter host, indicates the WMS host to connect.
36
     * */
37
    public WMSClient(String host) throws ConnectException, IOException 
38
    {
39
            setHost(host);
40
        try {                
41
                handler = WMSProtocolHandlerFactory.negotiate(host);
42
                handler.setHost(host);        
43
        } catch(ConnectException conE) {
44
                conE.printStackTrace();
45
                throw conE; 
46
        } catch(IOException ioE) {
47
                ioE.printStackTrace();
48
                throw ioE; 
49
        } catch(Exception e) {
50
                e.printStackTrace();               
51
        }
52
    }
53
    
54
    public String getVersion()
55
    {
56
        return handler.getVersion();
57
    }
58
    /**
59
     * <p>One of the three interfaces that OGC WMS defines. Request a map.</p> 
60
     * @throws ServerErrorException 
61
     */
62
    public File getMap(WMSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{   
63
        return handler.getMap(status, cancel);
64
    } 
65
    
66
    /**
67
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
68
     * @param override, if true the previous downloaded data will be overridden
69
     */
70
    public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel) {        
71
        handler.getCapabilities(status, override, cancel);
72
    } 
73
    
74
    /**
75
     * <p>One of the three interfaces defined by the OGC WMS, it gets the information about a feature requested</p>
76
     * @return 
77
     */
78
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount) throws WMSException{        
79
        return handler.getFeatureInfo(status, x, y, featureCount);
80
    } 
81
    
82
    /**
83
     * <p> Reads from the WMS Capabilities, the layers available in the service</p>
84
     * @return a TreeMap with the available layers in the WMS 
85
     */
86
    public TreeMap getLayers() {        
87
        return handler.layers;
88
    } 
89
    
90
    /**
91
     * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
92
     * @return, number of layers available
93
     */
94
    public int getNumberOfLayers() {        
95
        if (handler.layers != null)
96
        {
97
            return handler.layers.size();
98
        }
99
        return 0;
100
    } 
101
    
102
    /**
103
     * <p>Gets the WMSLayer with this name</p>
104
     * 
105
     * @param _name, layer name
106
     * @return the layer with this name
107
     */
108
    public WMSLayer getLayer(String _name) {        
109
        if (handler.layers.get(_name) != null)
110
        {
111
            return (WMSLayer)handler.layers.get(_name);
112
        }
113
        
114
        return null;
115
    } 
116
    
117
    public String[] getLayerNames()
118
    {            
119
        WMSLayer[] lyrs;
120
        
121
        lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]);
122
        
123
        String[] names = new String[lyrs.length];
124
        
125
        for(int i = 0; i<lyrs.length; i++)
126
        {
127
            names[i] = ((WMSLayer)lyrs[i]).getName();
128
        }
129
        return names;
130
    }
131
    
132
    public String[] getLayerTitles()
133
    {            
134
        WMSLayer[] lyrs;
135
        
136
        lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]);
137
        
138
        String[] titles = new String[lyrs.length];
139
        
140
        for(int i = 0; i<lyrs.length; i++)
141
        {
142
            titles[i] = ((WMSLayer)lyrs[i]).getTitle();
143
        }
144
        return titles;
145
    }
146
    /**
147
     * <p>Gets the image formats available in the Service to retrieve the maps</p>
148
     * @return a vector with all the available formats
149
     */
150
    public Vector getFormats() {        
151
        return handler.getServiceInformation().formats;         
152
    } 
153
    
154
    public boolean isQueryable()
155
    {
156
            return handler.getServiceInformation().isQueryable();  
157
    }
158
    
159
    public void close() {        
160
        // your code here
161
    } 
162
    
163
    
164
    /**
165
     * Returns the max extent that envolves the requested layers
166
     * */
167
    public Rectangle2D getLayersExtent(String[]layerNames, String srs)
168
    {
169
        try
170
        {
171
                if (layerNames == null) return null;
172
            BoundaryBox bbox;
173
            WMSLayer layer = getLayer(layerNames[0]);
174
            
175
            bbox = layer.getBbox(srs);
176
            if (bbox == null) return null;
177
            double xmin = bbox.getXmin();
178
            double xmax = bbox.getXmax();
179
            double ymin = bbox.getYmin();
180
            double ymax = bbox.getYmax();
181
            
182
            for(int i=1; i<layerNames.length; i++)
183
            {
184
                layer = getLayer(layerNames[i]);
185
                bbox = layer.getBbox(srs);
186
                if (bbox == null) return null;
187
                if (bbox.getXmin() < xmin)
188
                {
189
                    xmin = bbox.getXmin();
190
                }
191
                if (bbox.getYmin() < ymin)
192
                {
193
                    ymin = bbox.getYmin();
194
                }
195
                if (bbox.getXmax() > xmax)
196
                {
197
                    xmax = bbox.getXmax();
198
                }
199
                if (bbox.getYmax() > ymax)
200
                {
201
                    ymax = bbox.getYmax();
202
                }
203
            }        
204
            
205
            Rectangle2D extent = new Rectangle2D.Double(xmin,ymin,Math.abs(xmax-xmin),Math.abs(ymax-ymin));
206
            return extent;
207
        }
208
        catch(Exception e)
209
        {
210
            e.printStackTrace();
211
            return null;
212
        }
213
    }
214
    
215
    
216
    /**
217
     * Gets the Service information included in the Capabilities
218
     * */
219
    
220
    public WMSProtocolHandler.ServiceInformation getServiceInformation()
221
    {
222
        return handler.getServiceInformation();  
223
    }
224
    
225
    
226
    /**
227
     * <p>Checks the connection to de remote WMS and requests its capabilities.</p>
228
     * @param override, if true the previous downloaded data will be overridden
229
     */
230
    public boolean connect(boolean override, ICancellable cancel) 
231
    {
232
        try {            
233
            if (handler == null)
234
            {
235
                if (getHost().trim().length() > 0)
236
                {                                        
237
                    //TODO: Implement correctly the negotiate algorithm
238
                    handler = WMSProtocolHandlerFactory.negotiate(getHost());
239
                    //handler = new WMSProtocolHandler1_1_1();
240
                    handler.setHost(getHost());
241
                }
242
                else
243
                {
244
                    //must to specify host first!!!!
245
                    return false;
246
                }                
247
            }
248
            getCapabilities(null, override, cancel);
249
            return true;
250
            
251
        } catch (Exception e) {
252
            e.printStackTrace();
253
            return false;
254
        }
255
    }
256
    
257
    //TODO Check this out: Always 1 layer at first level...
258
    public WMSLayer getLayersRoot() {
259
        return handler.rootLayer;
260
    }
261

    
262
        public boolean connect(ICancellable cancel) {
263
                return connect(false, cancel);
264
        }
265
}