Statistics
| Revision:

root / branches / v05 / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / WMSClient.java @ 4036

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 rootLayer;
31
    }
32

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

    
40
    /**
41
     * Constructor.
42
     * the parameter host, indicates the WMS host to connect.
43
     * */
44
    public WMSClient(String host) throws ConnectException, IOException 
45
    {
46
        //m_log = LogManager.getLogManager().getLogger(getClass().getName());
47
        setHost(host);
48
        /*
49
         
50
          The negotiation is performed in the connect method.
51
          
52
         try
53
        {
54
            //TODO: implement a correct negotiation algorithm
55
            //handler = WMSProtocolHandlerFactory.negotiate(new URL(host));
56
            handler = new WMSProtocolHandler1_1_1();
57
            handler.setHost(host);
58
        }
59
        catch(Exception e)
60
        {
61
            e.printStackTrace();
62
        }
63
        */
64
        //handler = new WMSProtocolHandler1_1_1();
65
        try
66
        {                
67
                handler = WMSProtocolHandlerFactory.negotiate(host);
68
                handler.setHost(host);        
69
        }
70
        catch(ConnectException conE)
71
        {
72
                conE.printStackTrace();
73
                throw conE; 
74
        }catch(IOException ioE)
75
        {
76
                ioE.printStackTrace();
77
                throw ioE; 
78
        }catch(Exception e)
79
        {
80
                e.printStackTrace();               
81
        }
82
    }
83
    
84
    public String getVersion()
85
    {
86
        return handler.getVersion();
87
    }
88
    /**
89
     * <p>One of the three interfaces that OGC WMS defines. Request a map.</p> 
90
     * @throws ServerErrorException 
91
     */
92
    public File getMap(WMSStatus status) throws WMSException, ServerErrorException{   
93
        return handler.getMap(status);
94
    } 
95
    
96
    /**
97
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
98
     * 
99
     */
100
    public void getCapabilities(WMSStatus status) {        
101
        handler.getCapabilities(status);
102
        layers = handler.layers;
103
        rootLayer = handler.rootLayer;
104
    } 
105
    
106
    /**
107
     * <p>One of the three interfaces defined by the OGC WMS, it gets the information about a feature requested</p>
108
     * @return 
109
     */
110
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount) throws WMSException{        
111
        return handler.getFeatureInfo(status, x, y, featureCount);
112
    } 
113
    
114
    /**
115
     * <p> Reads from the WMS Capabilities, the layers available in the service</p>
116
     * @return a TreeMap with the available layers in the WMS 
117
     */
118
    public TreeMap getLayers() {        
119
        return layers;
120
    } 
121
    
122
    /**
123
     * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
124
     * @return, number of layers available
125
     */
126
    public int getNumberOfLayers() {        
127
        if (layers != null)
128
        {
129
            return layers.size();
130
        }
131
        return 0;
132
    } 
133
    
134
    /**
135
     * <p>Gets the WMSLayer with this name</p>
136
     * 
137
     * @param _name, layer name
138
     * @return the layer with this name
139
     */
140
    public WMSLayer getLayer(String _name) {        
141
        if (layers.get(_name) != null)
142
        {
143
            return (WMSLayer)layers.get(_name);
144
        }
145
        
146
        return null;
147
    } 
148
    
149
    public String[] getLayerNames()
150
    {            
151
        WMSLayer[] lyrs;
152
        
153
        lyrs = (WMSLayer[])layers.values().toArray(new WMSLayer[0]);
154
        
155
        String[] names = new String[lyrs.length];
156
        
157
        for(int i = 0; i<lyrs.length; i++)
158
        {
159
            names[i] = ((WMSLayer)lyrs[i]).getName();
160
        }
161
        return names;
162
    }
163
    
164
    /**
165
     * <p>Gets the image formats available in the Service to retrieve the maps</p>
166
     * @return a vector with all the available formats
167
     */
168
    public Vector getFormats() {        
169
        return handler.getServiceInformation().formats;         
170
    } 
171
    
172
    public boolean isQueryable()
173
    {
174
            return handler.getServiceInformation().isQueryable();  
175
    }
176
    
177
    public void close() {        
178
        // your code here
179
    } 
180
    
181
    
182
    /**
183
     * Returns the max extent that envolves the requested layers
184
     * */
185
    public Rectangle2D getLayersExtent(String[]layerNames, String srs)
186
    {
187
        try
188
        {
189
            BoundaryBox bbox;
190
            WMSLayer layer = getLayer(layerNames[0]);
191
            
192
            bbox = layer.getBbox(srs);
193
            if (bbox == null) return null;
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
                if (bbox == null) return null;
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(null);
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
}