svn-gvsig-desktop / trunk / extensions / extCatalogYNomenclator / src / es / gva / cit / gvsig / catalogClient / loaders / WMSLoader.java @ 3789
History | View | Annotate | Download (5.55 KB)
1 | 2823 | jorpiell | /* 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 | 3752 | jorpiell | import java.util.Vector; |
48 | 2823 | jorpiell | |
49 | 3752 | jorpiell | import org.exolab.castor.xml.ValidationException; |
50 | 2842 | jorpiell | |
51 | 3752 | jorpiell | import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException; |
52 | 2823 | jorpiell | import com.iver.cit.gvsig.fmap.drivers.WMSException; |
53 | 3752 | jorpiell | import com.iver.cit.gvsig.fmap.drivers.wms.FMapWMSDriver; |
54 | 2823 | jorpiell | import com.iver.cit.gvsig.fmap.layers.FLayer; |
55 | import com.iver.cit.gvsig.fmap.layers.FLyrWMS; |
||
56 | 3752 | jorpiell | import com.iver.cit.gvsig.fmap.layers.WMSLayerNode; |
57 | 2823 | jorpiell | |
58 | 3789 | jorpiell | 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 | 2823 | jorpiell | import es.uji.lsi.wcs.client.ServerOutOfOrderException; |
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 WMSLoader extends AbstractLoader{ |
||
69 | 3789 | jorpiell | |
70 | public WMSLoader(Resource resource) {
|
||
71 | super(resource);
|
||
72 | // TODO Auto-generated constructor stub
|
||
73 | } |
||
74 | |||
75 | /**
|
||
76 | * It loads the resource to the gvSIG view
|
||
77 | */
|
||
78 | public boolean load() { |
||
79 | return load(getResource().getLinkage(),getResource().getName());
|
||
80 | } |
||
81 | |||
82 | 2823 | jorpiell | /**
|
83 | * This function loads a WMS resource
|
||
84 | * @param host
|
||
85 | * URL where the server is located
|
||
86 | 3399 | jorpiell | * @param sLayer
|
87 | 2823 | jorpiell | * Layer name
|
88 | * @param crs
|
||
89 | * Coordenates System
|
||
90 | */
|
||
91 | |||
92 | 3789 | jorpiell | private boolean load(String host, String sLayer) { |
93 | try {
|
||
94 | 3399 | jorpiell | FLayer flayer = createWMSLayer(host, sLayer); |
95 | 2842 | jorpiell | if (flayer == null){ |
96 | return false; |
||
97 | }else{
|
||
98 | addLayerToView(flayer); |
||
99 | return true; |
||
100 | 3789 | jorpiell | } |
101 | 3399 | jorpiell | |
102 | 2823 | jorpiell | } catch (IOException e) { |
103 | // TODO Auto-generated catch block
|
||
104 | e.printStackTrace(); |
||
105 | } catch (ServerOutOfOrderException e) {
|
||
106 | // TODO Auto-generated catch block
|
||
107 | e.printStackTrace(); |
||
108 | } catch (WMSException e) {
|
||
109 | // TODO Auto-generated catch block
|
||
110 | e.printStackTrace(); |
||
111 | 3752 | jorpiell | } catch (IllegalStateException e) { |
112 | // TODO Auto-generated catch block
|
||
113 | e.printStackTrace(); |
||
114 | } catch (ValidationException e) {
|
||
115 | // TODO Auto-generated catch block
|
||
116 | e.printStackTrace(); |
||
117 | } catch (UnsupportedVersionException e) {
|
||
118 | // TODO Auto-generated catch block
|
||
119 | e.printStackTrace(); |
||
120 | 2823 | jorpiell | } |
121 | 2842 | jorpiell | return false; |
122 | 2823 | jorpiell | } |
123 | |||
124 | 2851 | jorpiell | /**
|
125 | * It retrieves a getCapabilities file and loads the layer in gvSIG
|
||
126 | * @param host
|
||
127 | * WMS URL
|
||
128 | * @param sLayer
|
||
129 | * Layer Name
|
||
130 | * @return
|
||
131 | 3399 | jorpiell | * FLayer
|
132 | 2851 | jorpiell | * @throws ServerOutOfOrderException
|
133 | * @throws ProtocolException
|
||
134 | * @throws MalformedURLException
|
||
135 | * @throws WMSException
|
||
136 | * @throws IOException
|
||
137 | 3752 | jorpiell | * @throws UnsupportedVersionException
|
138 | * @throws ValidationException
|
||
139 | * @throws IllegalStateException
|
||
140 | 2823 | jorpiell | */
|
141 | 3752 | jorpiell | private FLayer createWMSLayer(String host, String sLayer) throws ServerOutOfOrderException, ProtocolException, MalformedURLException, WMSException, IOException, IllegalStateException, ValidationException, UnsupportedVersionException { |
142 | FMapWMSDriver drv = new FMapWMSDriver();
|
||
143 | drv.createClient(new URL(host)); |
||
144 | 2842 | jorpiell | |
145 | 3752 | jorpiell | if (!drv.connect())
|
146 | return null; |
||
147 | 2853 | jorpiell | |
148 | 3752 | jorpiell | WMSLayerNode wmsNode = drv.getLayer(sLayer); |
149 | 2851 | jorpiell | |
150 | 3752 | jorpiell | if (wmsNode == null){ |
151 | return null; |
||
152 | } |
||
153 | |||
154 | String[] sLayers = sLayer.split(","); |
||
155 | |||
156 | FLyrWMS flyr = new FLyrWMS();
|
||
157 | flyr.setHost(new URL(host)); |
||
158 | flyr.setFullExtent(drv.getLayersExtent(sLayers,(String)wmsNode.getAllSrs().get(0))); |
||
159 | flyr.setFormat(getGreatFormat(drv.getFormats())); |
||
160 | flyr.setLayerQuery(sLayer); |
||
161 | flyr.setInfoLayerQuery("");
|
||
162 | flyr.setSRS((String)wmsNode.getAllSrs().get(0)); |
||
163 | flyr.setName(sLayer); |
||
164 | |||
165 | return flyr;
|
||
166 | 2851 | jorpiell | |
167 | } |
||
168 | 2826 | jorpiell | |
169 | 2851 | jorpiell | /**
|
170 | 3399 | jorpiell | * It choose the best format to load different maps if the server
|
171 | * supports it. This format could be png, because it supports
|
||
172 | * transparency.
|
||
173 | 2851 | jorpiell | * @param formats
|
174 | * Array with all the formats supported by the server
|
||
175 | * @return
|
||
176 | 3399 | jorpiell | */
|
177 | 3752 | jorpiell | private String getGreatFormat(Vector formats){ |
178 | for (int i=0 ; i<formats.size() ; i++){ |
||
179 | String format = (String) formats.get(i); |
||
180 | if (format.equals("image/png")){ |
||
181 | return format;
|
||
182 | } |
||
183 | } |
||
184 | 3399 | jorpiell | |
185 | 3752 | jorpiell | return (String)formats.get(0); |
186 | 2823 | jorpiell | } |
187 | 3789 | jorpiell | |
188 | public String getErrorMessage(ITranslator translator) { |
||
189 | return Translator.getText(translator,"wmsError") + ".\n" + |
||
190 | Translator.getText(translator,"server") + ": " + |
||
191 | getResource().getLinkage() + "\n" +
|
||
192 | Translator.getText(translator,"layer") + ": " + |
||
193 | getResource().getName(); |
||
194 | |||
195 | } |
||
196 | |||
197 | public String getLoaderMessage(ITranslator translator) { |
||
198 | return Translator.getText(translator,"loadWMS"); |
||
199 | } |
||
200 | 2823 | jorpiell | |
201 | |||
202 | |||
203 | } |