Statistics
| Revision:

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

History | View | Annotate | Download (7.24 KB)

1

    
2
package org.gvsig.remoteClient.wms;
3

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

    
11

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

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

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