Statistics
| Revision:

svn-gvsig-desktop / branches / v05 / extensions / extWMS / src / com / iver / cit / gvsig / fmap / drivers / wms / FMapWMSDriver.java @ 3855

History | View | Annotate | Download (8.66 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.ConnectException;
49
import java.net.ProtocolException;
50
import java.net.URL;
51
import java.util.ArrayList;
52
import java.util.TreeMap;
53
import java.util.Vector;
54

    
55
import javax.imageio.ImageIO;
56

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

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

    
75

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

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

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

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

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

    
148
                return client;
149
        }
150
    
151
    
152
    /**
153
     * Creates a new instance of a WMSClient.
154
     * @param host
155
     * @throws IOException 
156
     * @throws ConnectException 
157
     */
158
    public void createClient(URL host) throws ConnectException, IOException{
159
        client = new WMSClient(host.toString());
160
    }
161

    
162
    /**
163
     * Establishes the connection.
164
     * @return <b>true</b> if the connection was successful, or <b>false</b> if it was no possible
165
     * to establish the connection for any reason such as version negotiation.
166
     */
167
    public boolean connect() {
168
        return client.connect();
169
    }
170

    
171
    /**
172
     * @return the version of this client.
173
     */
174
    public String getVersion() {
175
        return client.getVersion();
176
    }
177

    
178
    /**
179
     * @return The title of the service offered by the WMS server.
180
     */
181
    public String getServiceTitle() {
182
        return client.getServiceInformation().title;
183
    }
184

    
185
    /**
186
     * @return <b>Vector</b> containing strings with the formats supported by this server.
187
     */
188
    public Vector getFormats() {
189
        return client.getFormats();
190
    }
191

    
192
    /**
193
     * @return A tree containing the info of all layers available on this server. 
194
     */
195
    public WMSLayerNode getLayersTree() {
196
        if (fmapRootLayer == null){
197
            WMSLayer clientRoot = client.getLayersRoot();
198
            
199
            fmapRootLayer = parseTree(clientRoot, null);
200
        }
201
        return fmapRootLayer;
202
    }
203

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

    
248
    /**
249
     * @return
250
     */
251
    public String getAbstract() {
252
        return client.getServiceInformation().abstr;
253
    }
254

    
255
    /**
256
     * @param layerName
257
     * @param srs
258
     * @return
259
     */
260
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
261
        return client.getLayersExtent(layerName, srs);
262
    }
263

    
264
    /**
265
     * @param string
266
     * @return
267
     */
268
    public WMSLayerNode getLayer(String layerName) {
269
        if (getLayers().get(layerName) != null)
270
        {
271
            return (WMSLayerNode)layers.get(layerName);
272
        }
273
        return null;
274
    }
275

    
276
    /**
277
     * @return
278
     */
279
    private TreeMap getLayers() {
280
        if (fmapRootLayer == null){
281
            fmapRootLayer = getLayersTree();
282
        }
283
        return layers;
284
    }
285

    
286
    /**
287
     * @param wmsStatus
288
     * @param i
289
     * @param j
290
     * @param max_value
291
     * @return
292
     * @throws WMSException 
293
     */
294
    public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, int max_value) throws WMSException {
295
        try {
296
            return client.getFeatureInfo(_wmsStatus, i, j, max_value);
297
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
298
            throw new WMSException();
299
        }
300
    }
301
    
302
    public String getHost(){
303
        return client.getHost();
304
    }
305
}