Statistics
| Revision:

svn-gvsig-desktop / branches / v05 / extensions / extCatalogYNomenclator / src / es / gva / cit / gvsig / catalogClient / loaders / WMSLayerLoader.java @ 4488

History | View | Annotate | Download (5.79 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 es.gva.cit.gvsig.catalogClient.loaders;
42

    
43
import java.io.IOException;
44
import java.net.MalformedURLException;
45
import java.net.ProtocolException;
46
import java.net.URL;
47
import java.util.Vector;
48

    
49
import org.exolab.castor.xml.ValidationException;
50

    
51
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
52
import com.iver.cit.gvsig.fmap.drivers.WMSException;
53
import com.iver.cit.gvsig.fmap.drivers.wms.FMapWMSDriver;
54
import com.iver.cit.gvsig.fmap.layers.FLayer;
55
import com.iver.cit.gvsig.fmap.layers.FLyrWMS;
56
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
57

    
58
import es.gva.cit.catalogClient.schemas.discoverer.Resource;
59
import es.gva.cit.catalogClient.traductor.ITranslator;
60
import es.gva.cit.catalogClient.traductor.Translator;
61

    
62

    
63
/**
64
 * This class is used to load a WMS layer in gvSIG
65
 * 
66
 * @author Jorge Piera Llodra (piera_jor@gva.es)
67
 */
68
public class WMSLayerLoader extends LayerLoader{
69
        
70
                
71
    public WMSLayerLoader(Resource resource, ITranslator translator) {
72
                super(resource, translator);
73
                //load(getResource().getLinkage(),getResource().getName());
74
        }
75

    
76
        /**
77
         * This function loads a WMS resource 
78
         * @param host
79
         * URL where the server is located
80
         * @param sLayer
81
         * Layer name
82
         * @param crs
83
         * Coordenates System
84
         * @throws LayerLoaderException 
85
         */
86
        
87
        public void loadLayer() throws LayerLoaderException {
88
                String host = getResource().getLinkage();
89
                String sLayer = getResource().getName();
90
                
91
                    FLayer flayer;
92
                    
93
                        try {
94
                                flayer = createWMSLayer(host, sLayer);
95
                                addLayerToView(flayer);
96
                        } catch (ProtocolException e) {
97
                                throw new LayerLoaderException(e.getMessage(),getWindowMessage());
98
                        } catch (MalformedURLException e) {
99
                                throw new LayerLoaderException(e.getMessage(),getWindowMessage());
100
                        } catch (IllegalStateException e) {
101
                                throw new LayerLoaderException(e.getMessage(),getWindowMessage());
102
                        } catch (ValidationException e) {
103
                                throw new LayerLoaderException(e.getMessage(),getWindowMessage());
104
                        } catch (WMSException e) {
105
                                throw new LayerLoaderException(e.getMessage(),getWindowMessage());
106
                        } catch (IOException e) {
107
                                throw new LayerLoaderException(e.getMessage(),getWindowMessage());
108
                        } catch (UnsupportedVersionException e) {
109
                                throw new LayerLoaderException(e.getMessage(),getWindowMessage());
110
                        }                                        
111

    
112
        }
113
        
114
        /**
115
         * It retrieves a getCapabilities file and loads the layer in gvSIG
116
         * @param host
117
         * WMS URL
118
         * @param sLayer
119
         * Layer Name
120
         * @return
121
         * FLayer
122
         * @throws ServerOutOfOrderException
123
         * @throws ProtocolException
124
         * @throws MalformedURLException
125
         * @throws WMSException
126
         * @throws IOException
127
         * @throws UnsupportedVersionException 
128
         * @throws ValidationException 
129
         * @throws IllegalStateException 
130
         * @throws LayerLoaderException 
131
         */
132
        private FLayer createWMSLayer(String host, String sLayer) throws ProtocolException, MalformedURLException, WMSException, IOException, IllegalStateException, ValidationException, UnsupportedVersionException, LayerLoaderException {
133
                FMapWMSDriver drv = new FMapWMSDriver();
134
                drv.createClient(new URL(host));
135
                
136
                if (!drv.connect())
137
                        throw new LayerLoaderException(getErrorMessage(),getWindowMessage()); 
138
                
139
                WMSLayerNode wmsNode = drv.getLayer(sLayer);
140
                                                
141
                if (wmsNode == null){
142
                        throw new LayerLoaderException(getErrorMessage(),getWindowMessage());
143
                }                
144
                
145
                String[] sLayers = sLayer.split(",");
146
                                
147
                FLyrWMS flyr = new FLyrWMS();
148
                flyr.setHost(new URL(host));
149
                flyr.setFullExtent(drv.getLayersExtent(sLayers,(String)wmsNode.getAllSrs().get(0)));
150
                flyr.setFormat(getGreatFormat(drv.getFormats()));
151
                flyr.setLayerQuery(sLayer);
152
                flyr.setInfoLayerQuery("");
153
                flyr.setSRS((String)wmsNode.getAllSrs().get(0));
154
                flyr.setName(sLayer);
155
                
156
                return flyr;
157
           
158
        }
159
        
160
        /**
161
         * It choose the best format to load different maps if the server 
162
         * supports it. This format could be png, because it supports 
163
         * transparency.
164
         * @param formats
165
         * Array with all the formats supported by the server
166
         * @return
167
         */        
168
        private String getGreatFormat(Vector formats){
169
            for (int i=0 ; i<formats.size() ; i++){
170
                String format = (String) formats.get(i);
171
                    if (format.equals("image/jpg")){
172
                    return format;
173
                    }
174
                    if (format.equals("image/jpeg")){
175
                    return format;
176
                    }
177
            }
178
                    
179
            return (String)formats.get(0);
180
        }
181

    
182
        private String getErrorMessage() {
183
                return Translator.getText(getTranslator(),"wmsError") + ".\n" +
184
                                Translator.getText(getTranslator(),"server") + ": " + 
185
                                getResource().getLinkage() + "\n" +
186
                                Translator.getText(getTranslator(),"layer") + ": " +
187
                                getResource().getName();                
188
        }
189

    
190
        private String getWindowMessage() {
191
                return Translator.getText(getTranslator(),"loadWMS");
192
        }
193
        
194
        
195
        
196
}