Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / WMSClient.java @ 5708

History | View | Annotate | Download (7.54 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
            BoundaryBox bbox;
172
            WMSLayer layer = getLayer(layerNames[0]);
173
            
174
            bbox = layer.getBbox(srs);
175
            if (bbox == null) return null;
176
            double xmin = bbox.getXmin();
177
            double xmax = bbox.getXmax();
178
            double ymin = bbox.getYmin();
179
            double ymax = bbox.getYmax();
180
            
181
            for(int i=1; i<layerNames.length; i++)
182
            {
183
                layer = getLayer(layerNames[i]);
184
                bbox = layer.getBbox(srs);
185
                if (bbox == null) return null;
186
                if (bbox.getXmin() < xmin)
187
                {
188
                    xmin = bbox.getXmin();
189
                }
190
                if (bbox.getYmin() < ymin)
191
                {
192
                    ymin = bbox.getYmin();
193
                }
194
                if (bbox.getXmax() > xmax)
195
                {
196
                    xmax = bbox.getXmax();
197
                }
198
                if (bbox.getYmax() > ymax)
199
                {
200
                    ymax = bbox.getYmax();
201
                }
202
            }        
203
            
204
            Rectangle2D extent = new Rectangle2D.Double(xmin,ymin,Math.abs(xmax-xmin),Math.abs(ymax-ymin));
205
            return extent;
206
        }
207
        catch(Exception e)
208
        {
209
            e.printStackTrace();
210
            return null;
211
        }
212
    }
213
    
214
    
215
    /**
216
     * Gets the Service information included in the Capabilities
217
     * */
218
    
219
    public WMSProtocolHandler.ServiceInformation getServiceInformation()
220
    {
221
        return handler.getServiceInformation();  
222
    }
223
    
224
    
225
    /**
226
     * <p>Checks the connection to de remote WMS and requests its capabilities.</p>
227
     * @param override, if true the previous downloaded data will be overridden
228
     */
229
    public boolean connect(boolean override, ICancellable cancel) 
230
    {
231
        try {            
232
            if (handler == null)
233
            {
234
                if (getHost().trim().length() > 0)
235
                {                                        
236
                    //TODO: Implement correctly the negotiate algorithm
237
                    handler = WMSProtocolHandlerFactory.negotiate(getHost());
238
                    //handler = new WMSProtocolHandler1_1_1();
239
                    handler.setHost(getHost());
240
                }
241
                else
242
                {
243
                    //must to specify host first!!!!
244
                    return false;
245
                }                
246
            }
247
            getCapabilities(null, override, cancel);
248
            return true;
249
            
250
        } catch (Exception e) {
251
            e.printStackTrace();
252
            return false;
253
        }
254
    }
255
    
256
    //TODO Check this out: Always 1 layer at first level...
257
    public WMSLayer getLayersRoot() {
258
        return handler.rootLayer;
259
    }
260

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