Statistics
| Revision:

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

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