Statistics
| Revision:

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

History | View | Annotate | Download (6 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
/**
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
 
23
    
24
    /**
25
     * Constructor.
26
     * the parameter host, indicates the WMS host to connect.
27
     * */
28
        public WMSClient(String host)
29
        {
30
                //m_log = LogManager.getLogManager().getLogger(getClass().getName());
31
                setHost(host);
32
                try
33
                {
34
                        //TODO: implement a correct negotiation algorithm
35
                        //handler = WMSProtocolHandlerFactory.negotiate(new URL(host));
36
                        handler = new WMSProtocolHandler1_1_1();
37
                        handler.setHost(host);
38
                }
39
                catch(Exception e)
40
                {
41
                        e.printStackTrace();
42
                }                
43
        }
44
   
45
        public String getVersion()
46
        {
47
                return handler.getVersion();
48
        }
49
/**
50
 * <p>One of the three interfaces that OGC WMS defines. Request a map.</p> 
51
 */
52
    public byte[] getMap(WMSStatus status) throws WMSException{       
53
        return handler.getMap(status);
54
    } 
55

    
56
/**
57
 * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
58
 * 
59
 */
60
    public void getCapabilities() {        
61
        handler.getCapabilities();
62
        layers = handler.layers;
63
    } 
64

    
65
/**
66
 * <p>One of the three interfaces defined by the OGC WMS, it gets the information about a feature requested</p>
67
 * @return 
68
 */
69
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount) throws WMSException{        
70
        return handler.getFeatureInfo(status, x, y, featureCount);
71
    } 
72

    
73
/**
74
 * <p> Reads from the WMS Capabilities, the layers available in the service</p>
75
 * @return a TreeMap with the available layers in the WMS 
76
 */
77
    public TreeMap getLayers() {        
78
        return layers;
79
    } 
80

    
81
/**
82
 * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
83
 * @return, number of layers available
84
 */
85
    public int getNumberOfLayers() {        
86
            if (layers != null)
87
            {
88
                    return layers.size();
89
            }
90
            else
91
            {
92
                    return 0;
93
            }
94
    } 
95

    
96
/**
97
 * <p>Gets the WMSLayer with this name</p>
98
 * 
99
 * @param _name, layer name
100
 * @return the layer with this name
101
 */
102
    public WMSLayer getLayer(String _name) {        
103
              if (layers.get(_name) != null)
104
              {
105
          return (WMSLayer)layers.get(_name);
106
              }
107

    
108
        return null;
109
    } 
110

    
111
    public String[] getLayerNames()
112
    {            
113
            WMSLayer[] lyrs;
114
            
115
            lyrs = (WMSLayer[])layers.values().toArray(new WMSLayer[0]);
116
            
117
            String[] names = new String[lyrs.length];
118
            
119
            for(int i = 0; i<lyrs.length; i++)
120
            {
121
                    names[i] = ((WMSLayer)lyrs[i]).getName();
122
            }
123
            return names;
124
    }
125
    
126
//    /**
127
//     * Adds a Layer object to the vector of layers of this LayerSet
128
//     */
129
//    public void addLayer(WMSLayer layer)
130
//    {
131
//      String layerName = layer.getName();
132
//
133
//      if (layerName == null)
134
//      {
135
//        throw new RuntimeException("Error: attempt to add a nameless layer");
136
//      }
137
//
138
//      // We use the lower case version of the name as a key for the hash map.
139
//      layerName = layerName.toLowerCase();    
140
//      if (layers == null)
141
//      {
142
//              layers = new TreeMap();
143
//      }
144
//      layers.put(layerName, layer);
145
//    }
146
    
147
/**
148
 * <p>Gets the image formats available in the Service to retrieve the maps</p>
149
 * @return a vector with all the available formats
150
 */
151
    public Vector getFormats() {        
152
        return handler.getServiceInformation().formats;         
153
    } 
154

    
155
/**
156
 * <p>Gets the SRS availables in the Service, all the SRS the Service can reproject to.</p>
157
 * @return vector with all the available SRS.
158
 */
159
    public Vector getSrs() {        
160
       return handler.srs;
161
    } 
162

    
163
    public void close() {        
164
        // your code here
165
    } 
166
    
167
    
168
/**
169
 * Returns the max extent that envolves the requested layers
170
 * */
171
public Rectangle2D getLayersExtent(String[]layerNames, String srs)
172
{
173
        try
174
        {
175
                BoundaryBox bbox;
176
                WMSLayer layer = getLayer(layerNames[0]);
177
                
178
                bbox = layer.getBbox(srs);
179
                double xmin = bbox.getXmin();
180
                double xmax = bbox.getXmax();
181
                double ymin = bbox.getYmin();
182
                double ymax = bbox.getYmax();
183
                
184
                for(int i=1; i<layerNames.length; i++)
185
                {
186
                        layer = getLayer(layerNames[i]);
187
                        bbox = layer.getBbox(srs);
188
                        
189
                        if (bbox.getXmin() < xmin)
190
                        {
191
                                xmin = bbox.getXmin();
192
                        }
193
                        if (bbox.getYmin() < ymin)
194
                        {
195
                                ymin = bbox.getYmin();
196
                        }
197
                        if (bbox.getXmax() > xmax)
198
                        {
199
                                xmax = bbox.getXmax();
200
                        }
201
                        if (bbox.getYmax() > ymax)
202
                        {
203
                                ymax = bbox.getYmax();
204
                        }
205
                }        
206
                Rectangle2D extent = new Rectangle2D.Double(xmin,ymin,Math.abs(xmax-xmin),Math.abs(ymax-ymin));
207
                return extent;
208
        }
209
        catch(Exception e)
210
        {
211
                //TODO:
212
                //Implement logic to handle all the exceptions
213
                return null;
214
        }
215
}
216

    
217

    
218
/**
219
 * Gets the Service information included in the Capabilities
220
 * */
221

    
222
public WMSProtocolHandler.ServiceInformation getServiceInformation()
223
{
224
        return handler.getServiceInformation();  
225
}
226
/**
227
 * <p>Checks the connection to de remote WMS and requests its capabilities.</p>
228
 * 
229
 */
230
    public boolean connect() 
231
        {
232
                try {
233
                        getCapabilities();
234
                        if (handler == null)
235
                        {
236
                                if (getHost().trim().length() > 0)
237
                                {                                        
238
                                        //TODO: Implement correctly the negotiate algorithm
239
                                        //handler = WMSProtocolHandlerFactory.negotiate(new URL(getHost()));
240
                                        handler = new WMSProtocolHandler1_1_1();
241
                                        handler.setHost(getHost());
242
                                }
243
                                else
244
                                {
245
                                        //must to specify host first!!!!
246
                                        return false;
247
                                }
248
                        }
249
                        return true;
250
                        
251
                } catch (Exception e) {
252
                        e.printStackTrace();
253
                        return false;
254
                }
255
        }
256

    
257
    //TODO Check this out: Always 1 layer at first level...
258
    public WMSLayer getLayersRoot() {
259
            return (WMSLayer)layers.values().toArray()[0];
260
    }
261
}