Statistics
| Revision:

root / branches / gvSIG_WMSv2 / extensions / extWMS / src / com / iver / cit / gvsig / fmap / drivers / wms / FMapWMSDriver.java @ 3655

History | View | Annotate | Download (8.37 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.drivers.wms;
42

    
43
import java.awt.Image;
44
import java.awt.geom.Rectangle2D;
45
import java.awt.image.BufferedImage;
46
import java.io.ByteArrayInputStream;
47
import java.io.IOException;
48
import java.net.ProtocolException;
49
import java.net.URL;
50
import java.util.ArrayList;
51
import java.util.TreeMap;
52
import java.util.Vector;
53

    
54
import javax.imageio.ImageIO;
55

    
56
import org.gvsig.remoteClient.exceptions.ServerErrorException;
57
import org.gvsig.remoteClient.wms.WMSClient;
58
import org.gvsig.remoteClient.wms.WMSDimension;
59
import org.gvsig.remoteClient.wms.WMSLayer;
60
import org.gvsig.remoteClient.wms.WMSStatus;
61
import org.gvsig.remoteClient.wms.WMSStyle;
62

    
63
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
64
import com.iver.cit.gvsig.fmap.drivers.WMSDriver;
65
import com.iver.cit.gvsig.fmap.drivers.WMSException;
66
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
67
//import com.iver.wmsclient.Capabilities;
68
//import com.iver.wmsclient.FeatureInfoQuery;
69
//import com.iver.wmsclient.MapQuery;
70
//import com.iver.wmsclient.UnsupportedVersionException;
71
//import com.iver.wmsclient.WMSClient;
72
//import com.iver.wmsclient.WMSClientFactory;
73

    
74

    
75
/**
76
 * Driver WMS.
77
 *
78
 * @author Vicente Caballero Navarro
79
 */
80
public class FMapWMSDriver implements WMSDriver {
81
        private WMSClient client;
82
    private WMSLayerNode fmapRootLayer;
83
    private TreeMap layers = new TreeMap();
84
    
85
    
86
        /**
87
         * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getCapabilities(java.net.URL)
88
         */
89
        public void getCapabilities(URL servidor)
90
                throws WMSException {
91
                try {
92
                        getClient(servidor).connect();
93
                } catch (Exception e) {                        
94
                        throw new WMSException(e);
95
                }
96
        }
97

    
98
        /**
99
     * @deprecated
100
         * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getMap(com.iver.cit.gvsig.fmap.drivers.wms.MapQuery)
101
         */
102
        public Image _getMap(WMSStatus wmsstatus)
103
                throws WMSException, IOException, ProtocolException {
104
                BufferedImage image = new BufferedImage(wmsstatus.getWidth(),
105
                                wmsstatus.getHeight(), BufferedImage.TYPE_INT_ARGB);
106

    
107
                try {
108
                        byte[] res = client.getMap(wmsstatus);                        
109
                        ByteArrayInputStream inbytes = new ByteArrayInputStream(res);
110
                        image = ImageIO.read(inbytes);
111

    
112
                        return image;
113
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
114
            throw new WMSException(e);
115
        } catch (ServerErrorException e) {
116
            throw new WMSException(e);
117
        }
118
    }
119

    
120
    public byte[] getMap(WMSStatus status) throws WMSException {
121
        try {
122
            return client.getMap(status);
123
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
124
            throw new WMSException(e.getMessage());
125
        } catch (ServerErrorException e) {
126
            throw new WMSException("WMS Unexpected server error.");      
127
        }
128
    }
129
    
130
        /**
131
         * Devuelve WMSClient a partir de su URL.
132
         *
133
         * @param url URL.
134
         *
135
         * @return WMSClient.
136
         *
137
         * @throws UnsupportedVersionException
138
         * @throws IOException
139
         */
140
        private WMSClient getClient(URL url) {
141
                if (client == null) {
142
                        client = new WMSClient(url.toString());
143
                }
144

    
145
                return client;
146
        }
147
    
148
    /**
149
     * Creates a new instance of a WMSClient.
150
     * @param host
151
     */
152
    public void createClient(URL host){
153
        client = new WMSClient(host.toString());
154
    }
155

    
156
    /**
157
     * Establishes the connection.
158
     * @return <b>true</b> if the connection was successful, or <b>false</b> if it was no possible
159
     * to establish the connection for any reason such as version negotiation.
160
     */
161
    public boolean connect() {
162
        return client.connect();
163
    }
164

    
165
    /**
166
     * @return the version of this client.
167
     */
168
    public String getVersion() {
169
        return client.getVersion();
170
    }
171

    
172
    /**
173
     * @return The title of the service offered by the WMS server.
174
     */
175
    public String getServiceTitle() {
176
        return client.getServiceInformation().title;
177
    }
178

    
179
    /**
180
     * @return <b>Vector</b> containing strings with the formats supported by this server.
181
     */
182
    public Vector getFormats() {
183
        return client.getFormats();
184
    }
185

    
186
    /**
187
     * @return A tree containing the info of all layers available on this server. 
188
     */
189
    public WMSLayerNode getLayersTree() {
190
        if (fmapRootLayer == null){
191
            WMSLayer clientRoot = client.getLayersRoot();
192
            
193
            fmapRootLayer = parseTree(clientRoot, null);
194
        }
195
        return fmapRootLayer;
196
    }
197

    
198
    /**
199
     * Parses the client's layer node and translates to a fmap's layer node
200
     * @param WMSLayer
201
     * @return WMSLayerNode
202
     */
203
    private WMSLayerNode parseTree(WMSLayer node, WMSLayerNode parentNode) {
204
        
205
        WMSLayerNode myNode = new WMSLayerNode();
206
        
207
        myNode.setName(node.getName());
208
        myNode.setTitle(node.getTitle());
209
        myNode.setTransparency(node.hasTransparency());
210
        myNode.setSrs(node.getAllSrs());
211
        myNode.setQueryable(node.isQueryable());
212
        myNode.setParent(parentNode);
213
        myNode.setAbstract(node.getAbstract());
214
        if (node.getLatLonBox()!=null)
215
            myNode.setLatLonBox(node.getLatLonBox().toString());
216
        
217
        ArrayList styles = node.getStyles();
218
        for (int i = 0; i < styles.size(); i++) {
219
            WMSStyle style = (WMSStyle) styles.get(i);
220
            myNode.addStyle(style.getName(), style.getTitle(), style.getAbstract());
221
        }
222
        
223
        ArrayList dimensions = node.getDimensions();
224
        for (int i = 0; i < dimensions.size(); i++) {
225
            WMSDimension d = (WMSDimension) dimensions.get(i);
226
            myNode.addDimension(d.getName(), d.getUnits(), d.getUnitSymbol(), d.getDimensionExpression());
227
        }
228
        
229
        int children = node.getChildren().size();
230
        myNode.setChildren(new ArrayList());
231
        for (int i = 0; i < children; i++) {
232
            myNode.getChildren().add(parseTree((WMSLayer)node.getChildren().get(i), myNode));
233
        }
234
        
235
        if (myNode.getName()!=null)
236
            layers.put(myNode.getName(), myNode);
237
        
238
        return myNode;
239
    }
240

    
241
    /**
242
     * @return
243
     */
244
    public String getAbstract() {
245
        return client.getServiceInformation().abstr;
246
    }
247

    
248
    /**
249
     * @param layerName
250
     * @param srs
251
     * @return
252
     */
253
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
254
        return client.getLayersExtent(layerName, srs);
255
    }
256

    
257
    /**
258
     * @param string
259
     * @return
260
     */
261
    public WMSLayerNode getLayer(String layerName) {
262
        if (getLayers().get(layerName) != null)
263
        {
264
            return (WMSLayerNode)layers.get(layerName);
265
        }
266
        return null;
267
    }
268

    
269
    /**
270
     * @return
271
     */
272
    private TreeMap getLayers() {
273
        if (fmapRootLayer == null){
274
            fmapRootLayer = getLayersTree();
275
        }
276
        return layers;
277
    }
278

    
279
    /**
280
     * @param wmsStatus
281
     * @param i
282
     * @param j
283
     * @param max_value
284
     * @return
285
     * @throws WMSException 
286
     */
287
    public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, int max_value) throws WMSException {
288
        try {
289
            return client.getFeatureInfo(_wmsStatus, i, j, max_value);
290
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
291
            throw new WMSException();
292
        }
293
    }
294
    
295
    public String getHost(){
296
        return client.getHost();
297
    }
298
}