Statistics
| Revision:

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

History | View | Annotate | Download (7.55 KB)

1

    
2
package org.gvsig.remoteClient.wms;
3

    
4
import java.awt.geom.Rectangle2D;
5
import java.io.IOException;
6
import java.net.ConnectException;
7
import java.util.TreeMap;
8
import java.util.Vector;
9
import org.gvsig.remoteClient.exceptions.ServerErrorException;
10
import org.gvsig.remoteClient.exceptions.WMSException;
11
import org.gvsig.remoteClient.utils.BoundaryBox;
12

    
13

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

    
31
    /**
32
     * @param rootLayer The rootLayer to set.
33
     */
34
    public void setRootLayer(WMSLayer rootLayer) {
35
        this.rootLayer = rootLayer;
36
    }
37

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