Statistics
| Revision:

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

History | View | Annotate | Download (7.51 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

    
8
import org.gvsig.remoteClient.exceptions.WMSException;
9
import org.gvsig.remoteClient.utils.BoundaryBox;
10
import org.gvsig.remoteClient.wms.wms_1_1_1.WMSProtocolHandler1_1_1;
11

    
12

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

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

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