gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.wmts / org.gvsig.wmts.provider / src / main / java / org / gvsig / wmts / provider / DefaultWMTSRasterServerExplorer.java @ 8700
History | View | Annotate | Download (11 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.wmts.provider; |
24 |
|
25 |
|
26 |
import java.io.IOException; |
27 |
import java.net.ConnectException; |
28 |
import java.net.URL; |
29 |
import java.util.ArrayList; |
30 |
import java.util.HashSet; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import java.util.Set; |
34 |
|
35 |
import org.cresques.cts.IProjection; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
import org.gvsig.fmap.crs.CRSFactory; |
40 |
import org.gvsig.fmap.dal.DataServerExplorer; |
41 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
42 |
import org.gvsig.fmap.dal.DataStoreParameters; |
43 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
44 |
import org.gvsig.fmap.dal.exception.DataException; |
45 |
import org.gvsig.fmap.dal.exception.InitializeException; |
46 |
import org.gvsig.fmap.dal.spi.AbstractDataServerExplorer; |
47 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
48 |
import org.gvsig.raster.wmts.ogc.WMTSClient; |
49 |
import org.gvsig.raster.wmts.ogc.WMTSOGCLocator; |
50 |
import org.gvsig.raster.wmts.ogc.struct.WMTSLayer; |
51 |
import org.gvsig.raster.wmts.ogc.struct.WMTSStyle; |
52 |
import org.gvsig.raster.wmts.ogc.struct.WMTSTheme; |
53 |
import org.gvsig.raster.wmts.ogc.struct.WMTSThemes; |
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 |
import org.gvsig.wmts.provider.tree.DefaultWMTSRootNode; |
59 |
import org.gvsig.wmts.provider.tree.WMTSNode; |
60 |
import org.gvsig.wmts.provider.tree.WMTSRootNode; |
61 |
|
62 |
|
63 |
/**
|
64 |
* @author fdiaz
|
65 |
*
|
66 |
*/
|
67 |
public class DefaultWMTSRasterServerExplorer extends AbstractDataServerExplorer implements WMTSRasterServerExplorer { |
68 |
|
69 |
private static final Logger logger = |
70 |
LoggerFactory.getLogger(DefaultWMTSRasterServerExplorer.class); |
71 |
|
72 |
private URL service = null; |
73 |
|
74 |
//WMTS Parameters
|
75 |
private WMTSClient wmtsClient = null; |
76 |
|
77 |
private WMTSRootNode rootNode = null; |
78 |
|
79 |
protected DefaultWMTSRasterServerExplorer(DataServerExplorerParameters parameters,
|
80 |
DataServerExplorerProviderServices providerServices) throws InitializeException {
|
81 |
super(parameters, providerServices);
|
82 |
// TODO Auto-generated constructor stub
|
83 |
boolean override = false; |
84 |
|
85 |
this.service = (URL)(parameters.getDynValue(WMTSRasterServerExplorerParameters.WMTS_SERVICE_PARAMETER_NAME)); |
86 |
if (wmtsClient == null){ |
87 |
try {
|
88 |
WMTSOGCLocator.getManager().createWMTSClient(service.toString()); |
89 |
wmtsClient = WMTSOGCLocator.getManager().createWMTSClient(service.toString()); |
90 |
|
91 |
// wmtsClient.setForceChangeAxisOrder(((WMTSRasterProviderParameters)getParameters()).getForceChangeAxisOrder());
|
92 |
|
93 |
if (!wmtsClient.connect(override, null)) { |
94 |
throw new ConnectException("Error connecting"); |
95 |
} |
96 |
|
97 |
// if (status == null){
|
98 |
// status = wmtsClient.createStatus();
|
99 |
// }
|
100 |
//
|
101 |
// wmtsClient.getCapabilities(status, true, null);
|
102 |
} catch (ConnectException e) { |
103 |
throw new InitializeException("Not possible to connect with " + service, e); |
104 |
} catch (IOException e) { |
105 |
throw new InitializeException("Not possible to connect with " + service, e); |
106 |
} |
107 |
} |
108 |
|
109 |
} |
110 |
|
111 |
@Override
|
112 |
public String getProviderName() { |
113 |
return WMTSRasterProvider.NAME;
|
114 |
} |
115 |
|
116 |
@Override
|
117 |
public boolean canAdd() { |
118 |
return false; |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public boolean canAdd(String storeName) throws DataException { |
123 |
return false; |
124 |
} |
125 |
|
126 |
private List<WMTSLayer> getLayers(WMTSTheme theme){ |
127 |
List<WMTSLayer> wmtsLayers = new ArrayList<WMTSLayer>(); |
128 |
if(theme.getLayer()!=null){ |
129 |
wmtsLayers.add(theme.getLayer()); |
130 |
} |
131 |
for (int i = 0; i < theme.getChildCount(); i++) { |
132 |
wmtsLayers.addAll(getLayers(theme.getChildren(i))); |
133 |
} |
134 |
return wmtsLayers;
|
135 |
} |
136 |
|
137 |
@Override
|
138 |
public List<WMTSRasterProviderParameters> list() throws DataException { |
139 |
List<WMTSRasterProviderParameters> result = new ArrayList<WMTSRasterProviderParameters>(); |
140 |
@SuppressWarnings("unchecked") |
141 |
WMTSThemes themes = wmtsClient.getLayerListAsThemes(); |
142 |
|
143 |
List<WMTSLayer> wmtsLayers = new ArrayList<WMTSLayer>(); |
144 |
for (int i = 0; i < themes.getChildCount(); i++) { |
145 |
WMTSTheme theme = themes.getChildren(i); |
146 |
wmtsLayers.addAll(getLayers(theme)); |
147 |
} |
148 |
|
149 |
for (Iterator<WMTSLayer> iterator = wmtsLayers.iterator(); iterator.hasNext();) { |
150 |
WMTSLayer wmtsLayer = (WMTSLayer) iterator.next(); |
151 |
|
152 |
WMTSRasterProviderParameters params = new WMTSRasterProviderParameters();
|
153 |
params.setService(this.service);
|
154 |
params.setLayer(wmtsLayer.getTitle()); |
155 |
|
156 |
@SuppressWarnings("unchecked") |
157 |
List<WMTSStyle> styles = wmtsLayer.getStyle();
|
158 |
if (styles != null && styles.size() > 0) { |
159 |
params.setStyle(((WMTSStyle) (styles.get(0))).getIdentifier());
|
160 |
} |
161 |
|
162 |
|
163 |
@SuppressWarnings("unchecked") |
164 |
List<String> formats = wmtsLayer.getFormat(); |
165 |
if(formats!=null && formats.size()>0){ |
166 |
params.setFormat((String)(formats.get(0))); |
167 |
} |
168 |
|
169 |
@SuppressWarnings("unchecked") |
170 |
List<String> infoFormats = wmtsLayer.getInfoFormat(); |
171 |
if(infoFormats!=null && infoFormats.size()>0){ |
172 |
params.setInfoFormat((String)infoFormats.get(0)); |
173 |
} |
174 |
|
175 |
@SuppressWarnings("unchecked") |
176 |
List<String> allSrs = wmtsLayer.getSrsList(); |
177 |
if(allSrs!=null && allSrs.size()>0){ |
178 |
int i=0; |
179 |
while(i<allSrs.size()){
|
180 |
String srs = allSrs.get(i);
|
181 |
if(isValidSrs(srs)){
|
182 |
params.setCRS(srs); |
183 |
IProjection crs=CRSFactory.getCRS(srs); |
184 |
//FIXME: ?Qu? condiciones se deben cumplir para que este par?metro se deba poner a true?
|
185 |
// params.setForceChangeAxisOrder(false);
|
186 |
break;
|
187 |
} |
188 |
i++; |
189 |
} |
190 |
if(i>=allSrs.size()){
|
191 |
throw new IllegalArgumentException(this.getProviderName() |
192 |
+ ": The server does not provide any valid srs.'");
|
193 |
} |
194 |
} |
195 |
|
196 |
result.add(params); |
197 |
} |
198 |
|
199 |
return result;
|
200 |
} |
201 |
|
202 |
@Override
|
203 |
public List<WMTSRasterProviderParameters> list(int mode) throws DataException { |
204 |
if(mode!=DataServerExplorer.MODE_RASTER){
|
205 |
return null; |
206 |
} |
207 |
return list();
|
208 |
} |
209 |
|
210 |
@Override
|
211 |
public boolean add(String provider, NewDataStoreParameters parameters, boolean overwrite) throws DataException { |
212 |
return false; |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public void remove(DataStoreParameters parameters) throws DataException { |
217 |
throw new UnsupportedOperationException("Can't remove a WMTS provider."); |
218 |
|
219 |
} |
220 |
|
221 |
@Override
|
222 |
public NewDataStoreParameters getAddParameters(String storeName) throws DataException { |
223 |
return null; |
224 |
} |
225 |
|
226 |
@Override
|
227 |
public List<String> getDataStoreProviderNames() { |
228 |
List<String> x = new ArrayList<String>(); |
229 |
x.add(WMTSRasterProvider.NAME); |
230 |
return x;
|
231 |
} |
232 |
|
233 |
/* (non-Javadoc)
|
234 |
* @see org.gvsig.wmts.provider.WMTSRasterServerExplorer#getSrss(org.gvsig.wmts.provider.WMTSRasterProviderParameters)
|
235 |
*/
|
236 |
@Override
|
237 |
@SuppressWarnings("unchecked") |
238 |
public List<String> getSrss(WMTSRasterProviderParameters params) { |
239 |
String layer = params.getLayer();
|
240 |
Set<String> setSrs = new HashSet<String>(); |
241 |
WMTSLayer wmtsLayer = wmtsClient.getLayer(layer); |
242 |
List<String> allSrs = wmtsLayer.getSrsList(); |
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 |
return new ArrayList<String>(setSrs); |
250 |
} |
251 |
|
252 |
private boolean isValidSrs(String srs){ |
253 |
try{
|
254 |
IProjection crs=CRSFactory.getCRS(srs); |
255 |
if(crs!=null){ |
256 |
return true; |
257 |
} |
258 |
}catch(Exception e){ |
259 |
logger.info("Can't get crs from: '"+srs+"' code", e); |
260 |
return false; |
261 |
} |
262 |
return false; |
263 |
} |
264 |
|
265 |
/* (non-Javadoc)
|
266 |
* @see org.gvsig.wmts.provider.WMTSRasterServerExplorer#getInfoString()
|
267 |
*/
|
268 |
@Override
|
269 |
public String getInfoString() { |
270 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
271 |
|
272 |
InformationBuilder builder = ToolsUtilLocator.getToolsUtilManager().createInformationBuilder(); |
273 |
|
274 |
builder.title().labelkey(i18nManager.getTranslation("_service_information"));
|
275 |
|
276 |
builder.property().labelkey("_server").value(service.toString());
|
277 |
|
278 |
builder.property().labelkey("_server_type").value("WMTS "+ wmtsClient.getServiceIdentification().getServiceType()); |
279 |
builder.property().labelkey("_server_title").value(wmtsClient.getServiceIdentification().getTitle());
|
280 |
builder.property().labelkey("_server_sumary").value(wmtsClient.getServiceIdentification().getAbstract());
|
281 |
|
282 |
return builder.toString();
|
283 |
} |
284 |
|
285 |
@Override
|
286 |
public boolean hasThemes() { |
287 |
return wmtsClient.getThemes().hasThemes();
|
288 |
} |
289 |
|
290 |
@Override
|
291 |
public WMTSNode getListAsTree() {
|
292 |
return getListAsTree(false); |
293 |
} |
294 |
|
295 |
@Override
|
296 |
public WMTSNode getListAsTree(boolean clone) { |
297 |
if (clone) {
|
298 |
WMTSThemes wmtsThemes = wmtsClient.getThemes(); |
299 |
if(!wmtsThemes.hasThemes()){
|
300 |
wmtsThemes = wmtsClient.getLayerListAsThemes(); |
301 |
} |
302 |
DefaultWMTSRootNode cloned = new DefaultWMTSRootNode(wmtsThemes, this.service); |
303 |
return cloned;
|
304 |
} else {
|
305 |
return getRootNode();
|
306 |
} |
307 |
} |
308 |
|
309 |
private WMTSNode getRootNode() {
|
310 |
if(rootNode == null){ |
311 |
WMTSThemes wmtsThemes = wmtsClient.getThemes(); |
312 |
if(!wmtsThemes.hasThemes()){
|
313 |
wmtsThemes = wmtsClient.getLayerListAsThemes(); |
314 |
} |
315 |
rootNode = new DefaultWMTSRootNode(wmtsThemes, this.service); |
316 |
} |
317 |
return rootNode;
|
318 |
} |
319 |
|
320 |
} |