gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster.wms2 / org.gvsig.raster.wms2.provider / src / main / java / org / gvsig / raster / wms / provider / WMSRasterServerExplorer.java @ 8686
History | View | Annotate | Download (9.8 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.raster.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 javax.swing.tree.TreeNode; |
38 |
|
39 |
import org.cresques.cts.IProjection; |
40 |
import org.slf4j.Logger; |
41 |
import org.slf4j.LoggerFactory; |
42 |
|
43 |
import org.gvsig.fmap.crs.CRSFactory; |
44 |
import org.gvsig.fmap.dal.DataServerExplorer; |
45 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
46 |
import org.gvsig.fmap.dal.DataStoreParameters; |
47 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
48 |
import org.gvsig.fmap.dal.exception.DataException; |
49 |
import org.gvsig.fmap.dal.exception.InitializeException; |
50 |
import org.gvsig.fmap.dal.spi.AbstractDataServerExplorer; |
51 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProvider; |
52 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
53 |
import org.gvsig.raster.lib.buffer.api.statistics.StatisticsBand; |
54 |
import org.gvsig.remoteclient.wms.WMSClient; |
55 |
import org.gvsig.remoteclient.wms.WMSLayer; |
56 |
import org.gvsig.remoteclient.wms.WMSStatus; |
57 |
import org.gvsig.remoteclient.wms.WMSStyle; |
58 |
import org.gvsig.tools.ToolsLocator; |
59 |
import org.gvsig.tools.i18n.I18nManager; |
60 |
import org.gvsig.tools.util.InformationBuilder; |
61 |
import org.gvsig.tools.util.ToolsUtilLocator; |
62 |
|
63 |
|
64 |
/**
|
65 |
* @author fdiaz
|
66 |
*
|
67 |
*/
|
68 |
public class WMSRasterServerExplorer extends AbstractDataServerExplorer implements DataServerExplorerProvider { |
69 |
|
70 |
private static final Logger logger = |
71 |
LoggerFactory.getLogger(WMSRasterServerExplorer.class); |
72 |
|
73 |
public static final String NAME = "WMS"; |
74 |
public static String DESCRIPTION = "WMS Server"; |
75 |
|
76 |
private URL service = null; |
77 |
private boolean override = false; |
78 |
|
79 |
//WMS Parameters
|
80 |
private WMSStatus status = null; |
81 |
private WMSClient wmsClient = null; |
82 |
|
83 |
protected WMSRasterServerExplorer(DataServerExplorerParameters parameters,
|
84 |
DataServerExplorerProviderServices providerServices) throws InitializeException {
|
85 |
super(parameters, providerServices);
|
86 |
// TODO Auto-generated constructor stub
|
87 |
|
88 |
this.service = (URL)(parameters.getDynValue(WMSRasterServerExplorerParameters.WMS_SERVICE_PARAMETER_NAME)); |
89 |
this.override = (Boolean)(parameters.getDynValue(WMSRasterServerExplorerParameters.WMS_OVERRIDE_CAPABILITIES_PARAMETER_NAME)); |
90 |
if (wmsClient == null){ |
91 |
try {
|
92 |
wmsClient = new WMSClient(service.toString());
|
93 |
if (status == null){ |
94 |
status = new WMSStatus();
|
95 |
} |
96 |
|
97 |
wmsClient.getCapabilities(status, this.override, null); |
98 |
} catch (ConnectException e) { |
99 |
throw new InitializeException("Not possible to connect with " + service, e); |
100 |
} catch (IOException e) { |
101 |
throw new InitializeException("Not possible to connect with " + service, e); |
102 |
} |
103 |
} |
104 |
|
105 |
} |
106 |
|
107 |
@Override
|
108 |
public String getProviderName() { |
109 |
return WMSRasterProvider.NAME;
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public boolean canAdd() { |
114 |
return false; |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public boolean canAdd(String storeName) throws DataException { |
119 |
return false; |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public List<WMSRasterProviderParameters> list() throws DataException { |
124 |
List<WMSRasterProviderParameters> result = new ArrayList<WMSRasterProviderParameters>(); |
125 |
@SuppressWarnings("unchecked") |
126 |
TreeMap<String, WMSLayer> wmsLayersMap = wmsClient.getLayers(); |
127 |
|
128 |
Collection<WMSLayer> values = wmsLayersMap.values();
|
129 |
|
130 |
for (Iterator<WMSLayer> iterator = values.iterator(); iterator.hasNext();) { |
131 |
WMSLayer wmsLayer = (WMSLayer) iterator.next(); |
132 |
|
133 |
WMSRasterProviderParameters params = new WMSRasterProviderParameters();
|
134 |
params.setService(this.service);
|
135 |
List<String> paramLayers = new ArrayList<String>(); |
136 |
paramLayers.add(wmsLayer.getName()); |
137 |
params.setLayers(paramLayers); |
138 |
|
139 |
@SuppressWarnings("unchecked") |
140 |
ArrayList<WMSStyle> styles = wmsLayer.getStyles();
|
141 |
if (styles != null && styles.size() > 0) { |
142 |
List<String> paramStyles = new ArrayList<String>(); |
143 |
paramStyles.add(((WMSStyle) (styles.get(0))).getName());
|
144 |
params.setStyles(paramStyles); |
145 |
} |
146 |
|
147 |
@SuppressWarnings("unchecked") |
148 |
Vector<String> formats = wmsClient.getFormats(); |
149 |
if(formats!=null && formats.size()>0){ |
150 |
params.setFormat((String)(formats.get(0))); |
151 |
} |
152 |
|
153 |
@SuppressWarnings("unchecked") |
154 |
Vector<String> infoFormats = wmsClient.getInfoFormats(); |
155 |
if(infoFormats!=null && infoFormats.size()>0){ |
156 |
params.setInfoFormat((String)infoFormats.get(0)); |
157 |
} |
158 |
|
159 |
@SuppressWarnings("unchecked") |
160 |
Vector<String> allSrs = wmsLayer.getAllSrs(); |
161 |
if(allSrs!=null && allSrs.size()>0){ |
162 |
int i=0; |
163 |
while(i<allSrs.size()){
|
164 |
String srs = allSrs.get(i);
|
165 |
if(isValidSrs(srs)){
|
166 |
params.setCRS(srs); |
167 |
break;
|
168 |
} |
169 |
i++; |
170 |
} |
171 |
if(i>=allSrs.size()){
|
172 |
throw new IllegalArgumentException(this.getProviderName() |
173 |
+ ": The server does not provide any valid srs.'");
|
174 |
} |
175 |
} |
176 |
|
177 |
result.add(params); |
178 |
} |
179 |
|
180 |
return result;
|
181 |
} |
182 |
|
183 |
@Override
|
184 |
public List<WMSRasterProviderParameters> list(int mode) throws DataException { |
185 |
if(mode!=DataServerExplorer.MODE_RASTER){
|
186 |
return null; |
187 |
} |
188 |
return list();
|
189 |
} |
190 |
|
191 |
@Override
|
192 |
public boolean add(String provider, NewDataStoreParameters parameters, boolean overwrite) throws DataException { |
193 |
return false; |
194 |
} |
195 |
|
196 |
@Override
|
197 |
public void remove(DataStoreParameters parameters) throws DataException { |
198 |
throw new UnsupportedOperationException("Can't remove a WMS provider."); |
199 |
|
200 |
} |
201 |
|
202 |
@Override
|
203 |
public NewDataStoreParameters getAddParameters(String storeName) throws DataException { |
204 |
return null; |
205 |
} |
206 |
|
207 |
@Override
|
208 |
public List<String> getDataStoreProviderNames() { |
209 |
List<String> x = new ArrayList<String>(); |
210 |
x.add(WMSRasterProvider.NAME); |
211 |
return x;
|
212 |
} |
213 |
|
214 |
/**
|
215 |
* @param params
|
216 |
* @return formats
|
217 |
*/
|
218 |
@SuppressWarnings("unchecked") |
219 |
public List<String> getFormats(){ |
220 |
return new ArrayList<String>(wmsClient.getFormats()); |
221 |
} |
222 |
|
223 |
/**
|
224 |
* @return info formats
|
225 |
*/
|
226 |
@SuppressWarnings("unchecked") |
227 |
public List<String> getInfoFormats(){ |
228 |
return new ArrayList<String>(wmsClient.getInfoFormats()); |
229 |
} |
230 |
|
231 |
/**
|
232 |
* @param params
|
233 |
* @return list of available srs for the params
|
234 |
*/
|
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 |
// public WMSLayer getRootLayer(){
|
267 |
// return this.wmsClient.getRootLayer();
|
268 |
// }
|
269 |
|
270 |
public TreeNode getTreeNode(){ |
271 |
LayerNode node = new LayerNode(this.wmsClient.getRootLayer(), null); |
272 |
return node;
|
273 |
} |
274 |
|
275 |
public String getInfoString(){ |
276 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
277 |
|
278 |
InformationBuilder builder = ToolsUtilLocator.getToolsUtilManager().createInformationBuilder(); |
279 |
|
280 |
builder.title().labelkey(i18nManager.getTranslation("_service_information"));
|
281 |
|
282 |
builder.property().labelkey("_server").value(service.toString());
|
283 |
|
284 |
builder.property().labelkey("_server_type").value("WMS "+ wmsClient.getVersion()); |
285 |
builder.property().labelkey("_server_title").value(wmsClient.getServiceInformation().title);
|
286 |
builder.property().labelkey("_server_sumary").value(wmsClient.getServiceInformation().abstr);
|
287 |
|
288 |
return builder.toString();
|
289 |
} |
290 |
|
291 |
|
292 |
} |