gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.wms / org.gvsig.wms.provider / src / main / java / org / gvsig / wms / provider / DefaultWMSRasterServerExplorer.java @ 8692
History | View | Annotate | Download (10.1 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2016 gvSIG Association
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.wms.provider; |
24 |
|
25 |
import java.io.IOException; |
26 |
import java.net.ConnectException; |
27 |
import java.net.URL; |
28 |
import java.util.ArrayList; |
29 |
import java.util.Collection; |
30 |
import java.util.HashSet; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import java.util.Set; |
34 |
import java.util.TreeMap; |
35 |
import java.util.Vector; |
36 |
|
37 |
import org.cresques.cts.IProjection; |
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
import org.gvsig.fmap.crs.CRSFactory; |
42 |
import org.gvsig.fmap.dal.DataServerExplorer; |
43 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
44 |
import org.gvsig.fmap.dal.DataStoreParameters; |
45 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
46 |
import org.gvsig.fmap.dal.exception.DataException; |
47 |
import org.gvsig.fmap.dal.exception.InitializeException; |
48 |
import org.gvsig.fmap.dal.spi.AbstractDataServerExplorer; |
49 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
50 |
import org.gvsig.remoteclient.wms.WMSClient; |
51 |
import org.gvsig.remoteclient.wms.WMSLayer; |
52 |
import org.gvsig.remoteclient.wms.WMSStatus; |
53 |
import org.gvsig.remoteclient.wms.WMSStyle; |
54 |
import org.gvsig.tools.ToolsLocator; |
55 |
import org.gvsig.tools.i18n.I18nManager; |
56 |
import org.gvsig.tools.util.InformationBuilder; |
57 |
import org.gvsig.tools.util.ToolsUtilLocator; |
58 |
|
59 |
|
60 |
/**
|
61 |
* @author fdiaz
|
62 |
*
|
63 |
*/
|
64 |
public class DefaultWMSRasterServerExplorer extends AbstractDataServerExplorer implements WMSRasterServerExplorer { |
65 |
|
66 |
private static final Logger logger = |
67 |
LoggerFactory.getLogger(DefaultWMSRasterServerExplorer.class); |
68 |
|
69 |
private URL service = null; |
70 |
private boolean override = false; |
71 |
|
72 |
//WMS Parameters
|
73 |
private WMSStatus status = null; |
74 |
private WMSClient wmsClient = null; |
75 |
|
76 |
private WMSLayerNode layerNode = null; |
77 |
|
78 |
private WMSLayer rootLayer;
|
79 |
|
80 |
protected DefaultWMSRasterServerExplorer(DataServerExplorerParameters parameters,
|
81 |
DataServerExplorerProviderServices providerServices) throws InitializeException {
|
82 |
super(parameters, providerServices);
|
83 |
// TODO Auto-generated constructor stub
|
84 |
|
85 |
this.service = (URL)(parameters.getDynValue(WMSRasterServerExplorerParameters.WMS_SERVICE_PARAMETER_NAME)); |
86 |
this.override = (Boolean)(parameters.getDynValue(WMSRasterServerExplorerParameters.WMS_OVERRIDE_CAPABILITIES_PARAMETER_NAME)); |
87 |
if (wmsClient == null){ |
88 |
try {
|
89 |
wmsClient = new WMSClient(service.toString());
|
90 |
if (status == null){ |
91 |
status = new WMSStatus();
|
92 |
} |
93 |
|
94 |
wmsClient.getCapabilities(status, this.override, null); |
95 |
} catch (ConnectException e) { |
96 |
throw new InitializeException("Not possible to connect with " + service, e); |
97 |
} catch (IOException e) { |
98 |
throw new InitializeException("Not possible to connect with " + service, e); |
99 |
} |
100 |
} |
101 |
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public String getProviderName() { |
106 |
return WMSRasterProvider.NAME;
|
107 |
} |
108 |
|
109 |
@Override
|
110 |
public boolean canAdd() { |
111 |
return false; |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public boolean canAdd(String storeName) throws DataException { |
116 |
return false; |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public List<WMSRasterProviderParameters> list() throws DataException { |
121 |
List<WMSRasterProviderParameters> result = new ArrayList<WMSRasterProviderParameters>(); |
122 |
@SuppressWarnings("unchecked") |
123 |
TreeMap<String, WMSLayer> wmsLayersMap = wmsClient.getLayers(); |
124 |
|
125 |
Collection<WMSLayer> values = wmsLayersMap.values();
|
126 |
|
127 |
for (Iterator<WMSLayer> iterator = values.iterator(); iterator.hasNext();) { |
128 |
WMSLayer wmsLayer = (WMSLayer) iterator.next(); |
129 |
|
130 |
WMSRasterProviderParameters params = new WMSRasterProviderParameters();
|
131 |
params.setService(this.service);
|
132 |
List<String> paramLayers = new ArrayList<String>(); |
133 |
paramLayers.add(wmsLayer.getName()); |
134 |
params.setLayers(paramLayers); |
135 |
|
136 |
@SuppressWarnings("unchecked") |
137 |
ArrayList<WMSStyle> styles = wmsLayer.getStyles();
|
138 |
if (styles != null && styles.size() > 0) { |
139 |
List<String> paramStyles = new ArrayList<String>(); |
140 |
paramStyles.add(((WMSStyle) (styles.get(0))).getName());
|
141 |
params.setStyles(paramStyles); |
142 |
} |
143 |
|
144 |
@SuppressWarnings("unchecked") |
145 |
Vector<String> formats = wmsClient.getFormats(); |
146 |
if(formats!=null && formats.size()>0){ |
147 |
params.setFormat((String)(formats.get(0))); |
148 |
} |
149 |
|
150 |
@SuppressWarnings("unchecked") |
151 |
Vector<String> infoFormats = wmsClient.getInfoFormats(); |
152 |
if(infoFormats!=null && infoFormats.size()>0){ |
153 |
params.setInfoFormat((String)infoFormats.get(0)); |
154 |
} |
155 |
|
156 |
@SuppressWarnings("unchecked") |
157 |
Vector<String> allSrs = wmsLayer.getAllSrs(); |
158 |
if(allSrs!=null && allSrs.size()>0){ |
159 |
int i=0; |
160 |
while(i<allSrs.size()){
|
161 |
String srs = allSrs.get(i);
|
162 |
if(isValidSrs(srs)){
|
163 |
params.setCRS(srs); |
164 |
break;
|
165 |
} |
166 |
i++; |
167 |
} |
168 |
if(i>=allSrs.size()){
|
169 |
throw new IllegalArgumentException(this.getProviderName() |
170 |
+ ": The server does not provide any valid srs.'");
|
171 |
} |
172 |
} |
173 |
|
174 |
result.add(params); |
175 |
} |
176 |
|
177 |
return result;
|
178 |
} |
179 |
|
180 |
@Override
|
181 |
public List<WMSRasterProviderParameters> list(int mode) throws DataException { |
182 |
if(mode!=DataServerExplorer.MODE_RASTER){
|
183 |
return null; |
184 |
} |
185 |
return list();
|
186 |
} |
187 |
|
188 |
@Override
|
189 |
public boolean add(String provider, NewDataStoreParameters parameters, boolean overwrite) throws DataException { |
190 |
return false; |
191 |
} |
192 |
|
193 |
@Override
|
194 |
public void remove(DataStoreParameters parameters) throws DataException { |
195 |
throw new UnsupportedOperationException("Can't remove a WMS provider."); |
196 |
|
197 |
} |
198 |
|
199 |
@Override
|
200 |
public NewDataStoreParameters getAddParameters(String storeName) throws DataException { |
201 |
return null; |
202 |
} |
203 |
|
204 |
@Override
|
205 |
public List<String> getDataStoreProviderNames() { |
206 |
List<String> x = new ArrayList<String>(); |
207 |
x.add(WMSRasterProvider.NAME); |
208 |
return x;
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* @param params
|
213 |
* @return formats
|
214 |
*/
|
215 |
@Override
|
216 |
@SuppressWarnings("unchecked") |
217 |
public List<String> getFormats(){ |
218 |
return new ArrayList<String>(wmsClient.getFormats()); |
219 |
} |
220 |
|
221 |
/**
|
222 |
* @return info formats
|
223 |
*/
|
224 |
@Override
|
225 |
@SuppressWarnings("unchecked") |
226 |
public List<String> getInfoFormats(){ |
227 |
return new ArrayList<String>(wmsClient.getInfoFormats()); |
228 |
} |
229 |
|
230 |
/**
|
231 |
* @param params
|
232 |
* @return list of available srs for the params
|
233 |
*/
|
234 |
@Override
|
235 |
@SuppressWarnings("unchecked") |
236 |
public List<String> getSrss(WMSRasterProviderParameters params){ |
237 |
List<String> layers = params.getLayers(); |
238 |
Set<String> setSrs = new HashSet<String>(); |
239 |
for (Iterator<String> iterator = layers.iterator(); iterator.hasNext();) { |
240 |
String name = (String) iterator.next(); |
241 |
WMSLayer wmsLayer = wmsClient.getLayer(name); |
242 |
Vector<String> allSrs = wmsLayer.getAllSrs(); |
243 |
for (Iterator<String> iterator2 = allSrs.iterator(); iterator2.hasNext();) { |
244 |
String srs = iterator2.next();
|
245 |
if(isValidSrs(srs)){
|
246 |
setSrs.add(srs); |
247 |
} |
248 |
} |
249 |
} |
250 |
return new ArrayList<String>(setSrs); |
251 |
} |
252 |
|
253 |
private boolean isValidSrs(String srs){ |
254 |
try{
|
255 |
IProjection crs=CRSFactory.getCRS(srs); |
256 |
if(crs!=null){ |
257 |
return true; |
258 |
} |
259 |
}catch(Exception e){ |
260 |
logger.info("Can't get crs from: '"+srs+"' code", e); |
261 |
return false; |
262 |
} |
263 |
return false; |
264 |
} |
265 |
|
266 |
@Override
|
267 |
public WMSLayerNode getListAsTree(){
|
268 |
return getListAsTree(false); |
269 |
} |
270 |
|
271 |
@Override
|
272 |
public WMSLayerNode getListAsTree(boolean clone) { |
273 |
if (clone) {
|
274 |
DefaultWMSLayerNode cloned = new DefaultWMSLayerNode(getRooLayer(), null, wmsClient, service); |
275 |
return cloned;
|
276 |
} else {
|
277 |
if (layerNode == null) { |
278 |
layerNode = new DefaultWMSLayerNode(getRooLayer(), null, wmsClient, service); |
279 |
} |
280 |
return this.layerNode; |
281 |
} |
282 |
} |
283 |
|
284 |
private WMSLayer getRooLayer() {
|
285 |
if(rootLayer == null) { |
286 |
rootLayer = this.wmsClient.getRootLayer();
|
287 |
} |
288 |
return rootLayer;
|
289 |
} |
290 |
|
291 |
@Override
|
292 |
public String getInfoString(){ |
293 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
294 |
|
295 |
InformationBuilder builder = ToolsUtilLocator.getToolsUtilManager().createInformationBuilder(); |
296 |
|
297 |
builder.title().labelkey(i18nManager.getTranslation("_service_information"));
|
298 |
|
299 |
builder.property().labelkey("_server").value(service.toString());
|
300 |
|
301 |
builder.property().labelkey("_server_type").value("WMS "+ wmsClient.getVersion()); |
302 |
builder.property().labelkey("_server_title").value(wmsClient.getServiceInformation().title);
|
303 |
builder.property().labelkey("_server_sumary").value(wmsClient.getServiceInformation().abstr);
|
304 |
|
305 |
return builder.toString();
|
306 |
} |
307 |
|
308 |
|
309 |
} |