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 / WMTSRasterProvider.java @ 8801
History | View | Annotate | Download (35.5 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2018 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 |
import java.awt.Image; |
26 |
import java.awt.geom.Rectangle2D; |
27 |
import java.awt.image.BufferedImage; |
28 |
import java.io.File; |
29 |
import java.io.IOException; |
30 |
import java.net.URL; |
31 |
import java.util.ArrayList; |
32 |
import java.util.Collections; |
33 |
import java.util.HashMap; |
34 |
import java.util.Iterator; |
35 |
import java.util.List; |
36 |
import java.util.Map; |
37 |
import java.util.SortedSet; |
38 |
import java.util.TreeSet; |
39 |
import java.util.Map.Entry; |
40 |
|
41 |
import javax.imageio.ImageIO; |
42 |
|
43 |
import org.cresques.cts.ICoordTrans; |
44 |
import org.cresques.cts.IProjection; |
45 |
import org.slf4j.Logger; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
import org.gvsig.fmap.crs.CRSFactory; |
49 |
import org.gvsig.fmap.dal.DALLocator; |
50 |
import org.gvsig.fmap.dal.DataManager; |
51 |
import org.gvsig.fmap.dal.DataServerExplorer; |
52 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
53 |
import org.gvsig.fmap.dal.DataStore; |
54 |
import org.gvsig.fmap.dal.DataStoreParameters; |
55 |
import org.gvsig.fmap.dal.FileHelper; |
56 |
import org.gvsig.fmap.dal.exception.DataException; |
57 |
import org.gvsig.fmap.dal.exception.InitializeException; |
58 |
import org.gvsig.fmap.dal.exception.OpenException; |
59 |
import org.gvsig.fmap.dal.exception.ReadException; |
60 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
61 |
import org.gvsig.fmap.dal.raster.api.BandQuery; |
62 |
import org.gvsig.fmap.dal.raster.api.RasterQuery; |
63 |
import org.gvsig.fmap.dal.raster.spi.AbstractRasterStoreProvider; |
64 |
import org.gvsig.fmap.dal.raster.spi.BandDescriptorServices; |
65 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
66 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
67 |
import org.gvsig.fmap.geom.Geometry; |
68 |
import org.gvsig.fmap.geom.Geometry.DIMENSIONS; |
69 |
import org.gvsig.fmap.geom.GeometryLocator; |
70 |
import org.gvsig.fmap.geom.GeometryManager; |
71 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
72 |
import org.gvsig.fmap.geom.primitive.Envelope; |
73 |
import org.gvsig.metadata.MetadataLocator; |
74 |
import org.gvsig.metadata.MetadataManager; |
75 |
import org.gvsig.metadata.exceptions.MetadataException; |
76 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
77 |
import org.gvsig.raster.lib.buffer.api.BufferDimensions; |
78 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
79 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
80 |
import org.gvsig.raster.lib.buffer.api.NoData; |
81 |
import org.gvsig.raster.lib.buffer.api.PageManager; |
82 |
import org.gvsig.raster.lib.buffer.api.TileStruct; |
83 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
84 |
import org.gvsig.raster.lib.buffer.spi.DefaultTileStruct; |
85 |
import org.gvsig.raster.wmts.ogc.WMTSClient; |
86 |
import org.gvsig.raster.wmts.ogc.WMTSOGCLocator; |
87 |
import org.gvsig.raster.wmts.ogc.WMTSStatus; |
88 |
import org.gvsig.raster.wmts.ogc.exception.WMTSException; |
89 |
import org.gvsig.raster.wmts.ogc.struct.WMTSLayer; |
90 |
import org.gvsig.raster.wmts.ogc.struct.WMTSStyle; |
91 |
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrix; |
92 |
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSet; |
93 |
import org.gvsig.tools.dataTypes.DataTypes; |
94 |
import org.gvsig.tools.dispose.DisposeUtils; |
95 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
96 |
import org.gvsig.tools.locator.LocatorException; |
97 |
|
98 |
/**
|
99 |
* @author fdiaz
|
100 |
*
|
101 |
*/
|
102 |
public class WMTSRasterProvider extends AbstractRasterStoreProvider { |
103 |
|
104 |
private static final Logger logger = LoggerFactory.getLogger(WMTSRasterProvider.class); |
105 |
|
106 |
public static String NAME = "WMTS"; |
107 |
public static String DESCRIPTION = "WMTS Raster Provider"; |
108 |
public static final String METADATA_DEFINITION_NAME = "WMTS"; |
109 |
|
110 |
public static final int DEFAULT_BUFFER_SIZE = 256; |
111 |
public static final int DEFAULT_MAX_ZOOM_LEVEL = 22; |
112 |
|
113 |
// WMTS Parameters
|
114 |
private WMTSClient wmtsClient;
|
115 |
private WMTSStatus wmtsStatus;
|
116 |
private String layerName; |
117 |
|
118 |
private Envelope envelope = null; |
119 |
|
120 |
private IProjection projection = null; |
121 |
|
122 |
private WMTSImage image;
|
123 |
private TileStruct tileStruct = null; |
124 |
private Map<Integer,List<PageManager>> pageManagersPerZoomLevel = null; |
125 |
private Map<Integer, WMTSTileMatrix> tileMatrixPerZoomLevel = null; |
126 |
private int columnsPerTile; |
127 |
private int rowsPerTile; |
128 |
|
129 |
private int maxZoomLevel; |
130 |
|
131 |
protected WMTSRasterProvider(DataStoreParameters params, DataStoreProviderServices storeServices) throws InitializeException { |
132 |
super(params, storeServices, FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME));
|
133 |
initParams(); |
134 |
} |
135 |
|
136 |
private void initParams() throws InitializeException { |
137 |
WMTSRasterProviderParameters parameters = (WMTSRasterProviderParameters) getParameters(); |
138 |
pageManagersPerZoomLevel = new HashMap<Integer, List<PageManager>>(); |
139 |
// this.projection = getProjection();
|
140 |
// wmtsClient = getWMTSClient();
|
141 |
// wmtsStatus = getWMTSStatus();
|
142 |
envelope = getEnvelope(); |
143 |
maxZoomLevel = DEFAULT_MAX_ZOOM_LEVEL; |
144 |
|
145 |
//PRUEBA
|
146 |
// tileStruct = getTileStruct();
|
147 |
|
148 |
} |
149 |
|
150 |
protected WMTSStatus getWMTSStatus() throws InitializeException { |
151 |
if( this.wmtsStatus == null ){ |
152 |
wmtsStatus = getWMTSClient().createStatus(); |
153 |
|
154 |
WMTSLayer wmtsLayer = getWMTSClient().getLayer(getLayerName()); |
155 |
wmtsStatus.setLayer(wmtsLayer.getIdentifier()); |
156 |
|
157 |
wmtsStatus.setStyle((String)getParameters().getDynValue(WMTSRasterProviderParameters.WMTS_STYLE_PARAMETER_NAME));
|
158 |
wmtsStatus.setFormat((String) getParameters().getDynValue(WMTSRasterProviderParameters.WMTS_FORMAT_PARAMETER_NAME));
|
159 |
wmtsStatus.setInfoFormat((String) getParameters().getDynValue(WMTSRasterProviderParameters.WMTS_INFO_FORMAT_PARAMETER_NAME));
|
160 |
wmtsStatus.setSrs(getProjection().getAbrev()); |
161 |
|
162 |
wmtsStatus.setTileMatrixSet(getTileMatrixSet().getIdentifier()); |
163 |
|
164 |
|
165 |
} |
166 |
// TODO Auto-generated method stub
|
167 |
return this.wmtsStatus; |
168 |
} |
169 |
|
170 |
private String getLayerName() { |
171 |
if( this.layerName == null){ |
172 |
this.layerName = (String) getParameters().getDynValue(WMTSRasterProviderParameters.WMTS_LAYER_PARAMETER_NAME); |
173 |
} |
174 |
return this.layerName; |
175 |
} |
176 |
|
177 |
protected static void registerMetadataDefinition() throws MetadataException { |
178 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
179 |
if (manager.getDefinition(METADATA_DEFINITION_NAME) == null) { |
180 |
manager.addDefinition(METADATA_DEFINITION_NAME, |
181 |
WMTSRasterProviderParameters.class.getResourceAsStream("WMTSRasterMetadata.xml"),
|
182 |
WMTSRasterProviderParameters.class.getClassLoader()); |
183 |
} |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public Buffer createBuffer(RasterQuery rasterQuery) throws BufferException { |
188 |
|
189 |
try {
|
190 |
BufferManager bufferManager = BufferLocator.getBufferManager(); |
191 |
int[] bandDataTypes; |
192 |
List<PageManager> pageManagers = new ArrayList<PageManager>(); |
193 |
NoData[] bandNoData;
|
194 |
Envelope envelope = getTileStruct().getEnvelope(); |
195 |
double pixelSize = rasterQuery.getPixelSize();
|
196 |
int zoomLevel;
|
197 |
if (pixelSize == 0) { |
198 |
zoomLevel = 0;
|
199 |
} else {
|
200 |
zoomLevel = getZoomLvlForPixelSize(pixelSize); |
201 |
} |
202 |
|
203 |
if (!rasterQuery.getBands().isEmpty()) {
|
204 |
List<BandQuery> bands;
|
205 |
bands = rasterQuery.getBands(); |
206 |
bandDataTypes = new int[bands.size()]; |
207 |
bandNoData = new NoData[bands.size()];
|
208 |
|
209 |
for (BandQuery bandQuery : bands) {
|
210 |
int band = bandQuery.getBand();
|
211 |
bandDataTypes[band] = this.getBandDescriptor(band).getDataType();
|
212 |
bandNoData[band] = this.getNoData(rasterQuery, band);
|
213 |
try {
|
214 |
pageManagers.add(getPageManagersInZoom(zoomLevel).get(band)); |
215 |
} catch (ValidateDataParametersException | CreateEnvelopeException | DataException
|
216 |
| CloneNotSupportedException e) {
|
217 |
// TODO Auto-generated catch block
|
218 |
throw new BufferException(e); |
219 |
} |
220 |
} |
221 |
} else {
|
222 |
int bands = getBands();
|
223 |
bandDataTypes = new int[bands]; |
224 |
bandNoData = new NoData[bands];
|
225 |
for (int i = 0; i < bands; i++) { |
226 |
bandDataTypes[i] = this.getBandDescriptor(i).getDataType();
|
227 |
bandNoData[i] = this.getNoData(rasterQuery, i);
|
228 |
try {
|
229 |
pageManagers.add(getPageManagersInZoom(zoomLevel).get(i)); |
230 |
} catch (ValidateDataParametersException | CreateEnvelopeException | DataException
|
231 |
| CloneNotSupportedException e) {
|
232 |
throw new BufferException(e); |
233 |
} |
234 |
} |
235 |
} |
236 |
|
237 |
Buffer buffer =
|
238 |
bufferManager.createBuffer(getRows(zoomLevel), getColumns(zoomLevel), bandDataTypes, bandNoData, |
239 |
projection, envelope, pageManagers); |
240 |
|
241 |
if(rasterQuery.getClip()!=null){ |
242 |
Buffer completeBuffer = buffer;
|
243 |
envelope = rasterQuery.getClip(); |
244 |
buffer = bufferManager.createClippedBuffer(completeBuffer, envelope); |
245 |
DisposeUtils.dispose(completeBuffer); |
246 |
} |
247 |
|
248 |
return buffer;
|
249 |
} catch (Exception e) { |
250 |
throw new BufferException(e); |
251 |
} |
252 |
|
253 |
} |
254 |
|
255 |
@Override
|
256 |
public BandDescriptorServices getBandDescriptor(int band) { |
257 |
BandDescriptorServices descriptor = super.getBandDescriptor(band);
|
258 |
switch (band) {
|
259 |
case 0: |
260 |
descriptor.setName("RED");
|
261 |
descriptor.setDescription("RED");
|
262 |
descriptor.setDataType(BufferManager.TYPE_BYTE); |
263 |
case 1: |
264 |
descriptor.setName("GREEN");
|
265 |
descriptor.setDescription("GREEN");
|
266 |
descriptor.setDataType(BufferManager.TYPE_BYTE); |
267 |
case 2: |
268 |
descriptor.setName("BLUE");
|
269 |
descriptor.setDescription("BLUE");
|
270 |
descriptor.setDataType(BufferManager.TYPE_BYTE); |
271 |
case 3: |
272 |
descriptor.setName("ALPHA");
|
273 |
descriptor.setDescription("ALPHA");
|
274 |
descriptor.setDataType(BufferManager.TYPE_BYTE); |
275 |
} |
276 |
return descriptor;
|
277 |
|
278 |
} |
279 |
|
280 |
|
281 |
@Override
|
282 |
public int getBands() { |
283 |
return 4; |
284 |
} |
285 |
|
286 |
@Override
|
287 |
public DataServerExplorer getExplorer() throws ReadException, ValidateDataParametersException { |
288 |
DataManager manager = DALLocator.getDataManager(); |
289 |
WMTSRasterServerExplorerParameters params; |
290 |
try {
|
291 |
params = |
292 |
(WMTSRasterServerExplorerParameters) manager |
293 |
.createServerExplorerParameters(WMTSRasterServerExplorer.NAME); |
294 |
params.setService(((WMTSRasterProviderParameters) getParameters()).getService()); |
295 |
// params.setXYAxisOrder(((WMTSRasterProviderParameters) getParameters()).getXYAxisOrder());
|
296 |
return manager.openServerExplorer(WMTSRasterServerExplorer.NAME, (DataServerExplorerParameters) params);
|
297 |
} catch (Exception e) { |
298 |
throw new ReadException(this.getName(), e); |
299 |
} |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public void open() throws OpenException { |
304 |
for(int i=0; i<getBands(); i++){ |
305 |
this.getBandDescriptor(i).setDataType(DataTypes.BYTE);
|
306 |
} |
307 |
} |
308 |
|
309 |
@Override
|
310 |
public ResourceProvider getResource() {
|
311 |
return null; |
312 |
} |
313 |
|
314 |
@Override
|
315 |
public Object getSourceId() { |
316 |
return getParameters().getDynValue(WMTSRasterProviderParameters.WMTS_SERVICE_PARAMETER_NAME);
|
317 |
} |
318 |
|
319 |
@Override
|
320 |
public String getProviderName() { |
321 |
return NAME;
|
322 |
} |
323 |
|
324 |
@Override
|
325 |
public String getName() { //lista de capas |
326 |
@SuppressWarnings("unchecked") |
327 |
String layerName = (String) getParameters().getDynValue("layer"); |
328 |
return layerName;
|
329 |
} |
330 |
|
331 |
@Override
|
332 |
public String getFullName() { // host:port - lista de capas |
333 |
|
334 |
StringBuilder builder = new StringBuilder(); |
335 |
URL service = (URL)this.getParameters().getDynValue(WMTSRasterProviderParameters.WMTS_SERVICE_PARAMETER_NAME); |
336 |
builder.append(service.getHost()); |
337 |
int port = service.getPort();
|
338 |
if(port < 0){ |
339 |
int defaultPort = service.getDefaultPort();
|
340 |
if(defaultPort>=0){ |
341 |
builder.append(":");
|
342 |
builder.append(service.getDefaultPort()); |
343 |
} |
344 |
} else {
|
345 |
builder.append(":");
|
346 |
builder.append(port); |
347 |
} |
348 |
builder.append(" - ");
|
349 |
builder.append(getName()); |
350 |
return builder.toString();
|
351 |
} |
352 |
|
353 |
@Override
|
354 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
355 |
if (DataStore.METADATA_CRS.equalsIgnoreCase(name)) {
|
356 |
return this.getParameters().getDynValue(name); |
357 |
} |
358 |
if (DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name)) {
|
359 |
try {
|
360 |
return this.getEnvelope(); |
361 |
} catch (InitializeException e) {
|
362 |
throw new DynFieldNotFoundException(name, this.getClass().getName()); |
363 |
} |
364 |
} |
365 |
return super.getDynValue(name); |
366 |
} |
367 |
|
368 |
private Envelope getEnvelope() throws InitializeException { |
369 |
if(this.envelope == null){ |
370 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
371 |
try {
|
372 |
WMTSLayer wmtsLayer = getWMTSClient().getLayer(getLayerName()); |
373 |
Rectangle2D rectangle2d = wmtsLayer.getWGS84BBox().toRectangle2D();
|
374 |
Envelope envelopeWGS84 = geomManager.createEnvelope(rectangle2d.getMinX(), rectangle2d.getMinY(), rectangle2d.getMaxX(), rectangle2d.getMaxY(), Geometry.SUBTYPES.GEOM2D); |
375 |
ICoordTrans ct = CRSFactory.getCRS("EPSG:4326").getCT(getProjection());
|
376 |
this.envelope = envelopeWGS84.convert(ct);
|
377 |
// Rectangle2D extent = wmtsLayer.getWGS84BBoxTransformed(getProjection().getAbrev());
|
378 |
//
|
379 |
// this.envelope =
|
380 |
// geomManager.createEnvelope(extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY(),
|
381 |
// SUBTYPES.GEOM2D);
|
382 |
} catch (CreateEnvelopeException e) {
|
383 |
throw new InitializeException("Can't create envelope.", e); |
384 |
} |
385 |
} |
386 |
return this.envelope; |
387 |
} |
388 |
|
389 |
@Override
|
390 |
public TileStruct getTileStruct() {
|
391 |
if (tileStruct == null) { |
392 |
/*
|
393 |
StringBuilder builder = new StringBuilder();
|
394 |
builder.append("\n");
|
395 |
builder.append("****************** LAYER ************************");
|
396 |
builder.append("\n");
|
397 |
builder.append("projection = ");
|
398 |
builder.append(projection.getAbrev());
|
399 |
builder.append("\n");
|
400 |
WMTSLayer layer = wmtsClient.getLayer(wmtsStatus.getLayer());
|
401 |
String layerAbstract = layer.getAbstract();
|
402 |
builder.append("layerAbstract = ");
|
403 |
builder.append(layerAbstract);
|
404 |
builder.append("\n");
|
405 |
String layerIdentifier = layer.getIdentifier();
|
406 |
builder.append("layerIdentifier = ");
|
407 |
builder.append(layerIdentifier);
|
408 |
builder.append("\n");
|
409 |
String layerTitle = layer.getTitle();
|
410 |
builder.append("layerTitle = ");
|
411 |
builder.append(layerTitle);
|
412 |
builder.append("\n");
|
413 |
WMTSBoundingBox layerBbox = layer.getBBox();
|
414 |
builder.append("layerBbox = ");
|
415 |
if (layerBbox != null) {
|
416 |
builder.append(layerBbox.getLowerCorner()[0]);
|
417 |
builder.append(", ");
|
418 |
builder.append(layerBbox.getLowerCorner()[1]);
|
419 |
builder.append(" - ");
|
420 |
builder.append(layerBbox.getUpperCorner()[0]);
|
421 |
builder.append(", ");
|
422 |
builder.append(layerBbox.getUpperCorner()[1]);
|
423 |
} else {
|
424 |
builder.append("null");
|
425 |
}
|
426 |
builder.append("\n");
|
427 |
|
428 |
WMTSBoundingBox layerWGS84BBox = layer.getWGS84BBox();
|
429 |
builder.append("layerWGS84BBox = ");
|
430 |
if (layerWGS84BBox != null) {
|
431 |
builder.append(layerWGS84BBox.getLowerCorner()[0]);
|
432 |
builder.append(", ");
|
433 |
builder.append(layerWGS84BBox.getLowerCorner()[1]);
|
434 |
builder.append(" - ");
|
435 |
builder.append(layerWGS84BBox.getUpperCorner()[0]);
|
436 |
builder.append(", ");
|
437 |
builder.append(layerWGS84BBox.getUpperCorner()[1]);
|
438 |
} else {
|
439 |
builder.append("null");
|
440 |
}
|
441 |
builder.append("\n");
|
442 |
Rectangle2D layerWGS84BBoxProjected = layer.getWGS84BBoxTransformed(projection.getAbrev());
|
443 |
builder.append("layerWGS84BBoxProjected = ");
|
444 |
if (layerWGS84BBoxProjected != null) {
|
445 |
builder.append(layerWGS84BBoxProjected.getMinX());
|
446 |
builder.append(", ");
|
447 |
builder.append(layerWGS84BBoxProjected.getMinY());
|
448 |
builder.append(" - ");
|
449 |
builder.append(layerWGS84BBoxProjected.getMaxX());
|
450 |
builder.append(", ");
|
451 |
builder.append(layerWGS84BBoxProjected.getMaxY());
|
452 |
} else {
|
453 |
builder.append("null");
|
454 |
}
|
455 |
builder.append("\n");
|
456 |
|
457 |
List<WMTSTileMatrixSet> matrixSetsList = wmtsClient.getTileMatrixSet();
|
458 |
builder.append("\n");
|
459 |
builder.append("############ TILE MATRIX SET ITERATOR ###########");
|
460 |
builder.append("\n");
|
461 |
for (Iterator iterator = matrixSetsList.iterator(); iterator.hasNext();) {
|
462 |
builder.append("================ MATRIX SET =====================");
|
463 |
builder.append("\n");
|
464 |
WMTSTileMatrixSet wmtsTileMatrixSet = (WMTSTileMatrixSet) iterator.next();
|
465 |
String identifier = wmtsTileMatrixSet.getIdentifier();
|
466 |
builder.append("wmtsTileMatrixSet.getIdentifier() = ");
|
467 |
builder.append(identifier);
|
468 |
builder.append("\n");
|
469 |
int layerInitialLevel = layer.getInitialLevel(identifier);
|
470 |
builder.append("*** layerInitialLevel = ");
|
471 |
builder.append(layerInitialLevel);
|
472 |
builder.append("\n");
|
473 |
String wellKnownScaleSet = wmtsTileMatrixSet.getWellKnownScaleSet();
|
474 |
builder.append("wmtsTileMatrixSet.getWellKnownScaleSet() = ");
|
475 |
builder.append(wellKnownScaleSet);
|
476 |
builder.append("\n");
|
477 |
WMTSBoundingBox bounding = wmtsTileMatrixSet.getBoundingBox();
|
478 |
builder.append("wmtsTileMatrixSet.getBoundingBox() = ");
|
479 |
builder.append(bounding.getLowerCorner()[0]);
|
480 |
builder.append(", ");
|
481 |
builder.append(bounding.getLowerCorner()[1]);
|
482 |
builder.append(" - ");
|
483 |
builder.append(bounding.getUpperCorner()[0]);
|
484 |
builder.append(", ");
|
485 |
builder.append(bounding.getUpperCorner()[1]);
|
486 |
builder.append("\n");
|
487 |
String supportedCRS = wmtsTileMatrixSet.getSupportedCRS();
|
488 |
builder.append("wmtsTileMatrixSet.getSupportedCRS() = ");
|
489 |
builder.append(supportedCRS);
|
490 |
builder.append("\n");
|
491 |
List<WMTSTileMatrix> tileMatrix = wmtsTileMatrixSet.getTileMatrix();
|
492 |
builder.append("???????????????? TILE MATRIX ????????????????????");
|
493 |
builder.append("\n");
|
494 |
for (Iterator iterator2 = tileMatrix.iterator(); iterator2.hasNext();) {
|
495 |
WMTSTileMatrix wmtsTileMatrix = (WMTSTileMatrix) iterator2.next();
|
496 |
String ident = wmtsTileMatrix.getIdentifier(); // 0 ?tal vez el zoom level? parece que s?
|
497 |
builder.append("ident = ");
|
498 |
builder.append(ident);
|
499 |
builder.append("\n");
|
500 |
Rectangle2D bboxTileMatrixSet = wmtsTileMatrix.getBBoxTileMatrixSet(); // null
|
501 |
builder.append("bboxTileMatrixSet = ");
|
502 |
if (bboxTileMatrixSet != null) {
|
503 |
builder.append(bboxTileMatrixSet.getMinX());
|
504 |
builder.append(", ");
|
505 |
builder.append(bboxTileMatrixSet.getMinY());
|
506 |
builder.append(" - ");
|
507 |
builder.append(bboxTileMatrixSet.getMaxX());
|
508 |
builder.append(", ");
|
509 |
builder.append(bboxTileMatrixSet.getMaxY());
|
510 |
} else {
|
511 |
builder.append("null");
|
512 |
}
|
513 |
builder.append("\n");
|
514 |
double heightWCTile = wmtsTileMatrix.getHeightWCTile(false);
|
515 |
builder.append("heightWCTile = ");
|
516 |
builder.append(heightWCTile);
|
517 |
builder.append("\n");
|
518 |
double widthWCTile = wmtsTileMatrix.getWidthWCTile(false);
|
519 |
builder.append("widthWCTile = ");
|
520 |
builder.append(widthWCTile);
|
521 |
builder.append("\n");
|
522 |
double heightWCProjected = wmtsTileMatrix.getHeightWCTile(true);
|
523 |
builder.append("heightWCTileProjected [CHUNGO] = ");
|
524 |
builder.append(heightWCProjected);
|
525 |
builder.append("\n");
|
526 |
double widthWCProjected = wmtsTileMatrix.getWidthWCTile(true);
|
527 |
builder.append("widthWCTileProjected [CHUNGO] = ");
|
528 |
builder.append(widthWCProjected);
|
529 |
builder.append("\n");
|
530 |
long matrixHeight = wmtsTileMatrix.getMatrixHeight();
|
531 |
builder.append("matrixHeight = ");
|
532 |
builder.append(matrixHeight);
|
533 |
builder.append("\n");
|
534 |
long matrixWidth = wmtsTileMatrix.getMatrixWidth();
|
535 |
builder.append("matrixWidth = ");
|
536 |
builder.append(matrixWidth);
|
537 |
builder.append("\n");
|
538 |
double scaleDenominator = wmtsTileMatrix.getScaleDenominator(); // 2.79541132014358E8
|
539 |
// puede ser el pixelSize
|
540 |
builder.append("scaleDenominator = ");
|
541 |
builder.append(scaleDenominator);
|
542 |
builder.append("\n");
|
543 |
int tileHeight = wmtsTileMatrix.getTileHeight(); // ??? rows in tile
|
544 |
builder.append("tileHeight = ");
|
545 |
builder.append(tileHeight);
|
546 |
builder.append("\n");
|
547 |
int tileWidth = wmtsTileMatrix.getTileWidth(); // ??? columns in tile
|
548 |
builder.append("tileWidth = ");
|
549 |
builder.append(tileWidth);
|
550 |
builder.append("\n");
|
551 |
double[] topLeftCorner = wmtsTileMatrix.getTopLeftCorner();
|
552 |
builder.append("topLeftCorner = ");
|
553 |
builder.append(topLeftCorner[0]);
|
554 |
builder.append(", ");
|
555 |
builder.append(topLeftCorner[1]);
|
556 |
builder.append("\n");
|
557 |
|
558 |
builder.append("-------------------------------------------------");
|
559 |
builder.append("\n");
|
560 |
|
561 |
}
|
562 |
builder.append("=================================================");
|
563 |
builder.append("\n");
|
564 |
}
|
565 |
builder.append("#################################################");
|
566 |
builder.append("\n");
|
567 |
|
568 |
logger.info(builder.toString());
|
569 |
*/
|
570 |
|
571 |
autoOpen(); |
572 |
try {
|
573 |
this.tileStruct = new DefaultTileStruct(); |
574 |
this.columnsPerTile = DEFAULT_BUFFER_SIZE;
|
575 |
this.rowsPerTile = DEFAULT_BUFFER_SIZE;
|
576 |
this.tileStruct.setEnvelope(getEnvelope());
|
577 |
tileMatrixPerZoomLevel = new HashMap<Integer, WMTSTileMatrix>(); |
578 |
|
579 |
WMTSTileMatrixSet matrixSet = getTileMatrixSet(); |
580 |
|
581 |
HashMap<Integer, Integer> tileWidthsCounter = new HashMap<Integer, Integer>(); |
582 |
HashMap<Integer, Integer> tileHeightsCounter = new HashMap<Integer, Integer>(); |
583 |
Map<Integer, Double> zoomLevels = new HashMap<Integer, Double>(); |
584 |
int zoomLevel = 0; |
585 |
if(matrixSet!=null){ |
586 |
List<WMTSTileMatrix> tileMatrixList = matrixSet.getTileMatrix();
|
587 |
for (Iterator iterator = tileMatrixList.iterator(); iterator.hasNext();) { |
588 |
WMTSTileMatrix wmtsTileMatrix = (WMTSTileMatrix) iterator.next(); |
589 |
long levelColumns = wmtsTileMatrix.getMatrixWidth()*wmtsTileMatrix.getTileWidth();
|
590 |
|
591 |
double levelWidth = wmtsTileMatrix.getMatrixWidth()*wmtsTileMatrix.getWidthWCTile(getProjection().isProjected());
|
592 |
double pixelSize = levelWidth/levelColumns;
|
593 |
zoomLevels.put(zoomLevel, pixelSize); |
594 |
tileMatrixPerZoomLevel.put(zoomLevel, wmtsTileMatrix); |
595 |
// Aprovechamos el bucle para contar los distintos
|
596 |
// valores que el servidor tiene para el tama?o de los
|
597 |
// tiles en cada nivel de zoom
|
598 |
int tileWidth = wmtsTileMatrix.getTileWidth();
|
599 |
if(!tileWidthsCounter.containsKey(tileWidth)){
|
600 |
tileWidthsCounter.put(tileWidth, 1);
|
601 |
} else {
|
602 |
tileWidthsCounter.put(tileWidth, tileWidthsCounter.get(tileWidth)+1);
|
603 |
} |
604 |
int tileHeight = wmtsTileMatrix.getTileHeight();
|
605 |
if(!tileHeightsCounter.containsKey(tileHeight)){
|
606 |
tileHeightsCounter.put(tileHeight, 1);
|
607 |
} else {
|
608 |
tileHeightsCounter.put(tileHeight, tileHeightsCounter.get(tileHeight)+1);
|
609 |
} |
610 |
maxZoomLevel = zoomLevel; |
611 |
zoomLevel++; |
612 |
} |
613 |
} |
614 |
//seleccionamos los m?s usados
|
615 |
Integer aux = tileWidthsCounter.get(columnsPerTile);
|
616 |
if(aux==null){ |
617 |
aux = 0;
|
618 |
} |
619 |
Iterator<Entry<Integer, Integer>> it = tileWidthsCounter.entrySet().iterator(); |
620 |
while (it.hasNext()) {
|
621 |
Entry<Integer, Integer> entry = it.next(); |
622 |
if(entry.getValue()>=aux){
|
623 |
aux = entry.getValue(); |
624 |
columnsPerTile = entry.getKey(); |
625 |
} |
626 |
} |
627 |
aux = tileHeightsCounter.get(rowsPerTile); |
628 |
if(aux==null){ |
629 |
aux = 0;
|
630 |
} |
631 |
Iterator<Entry<Integer, Integer>> it2 = tileWidthsCounter.entrySet().iterator(); |
632 |
while (it2.hasNext()) {
|
633 |
Entry<Integer, Integer> entry = it2.next(); |
634 |
if(entry.getValue()>=aux){
|
635 |
aux = entry.getValue(); |
636 |
rowsPerTile = entry.getKey(); |
637 |
} |
638 |
} |
639 |
|
640 |
//y los asignamos
|
641 |
this.tileStruct.setColumnsPerTile(columnsPerTile);
|
642 |
this.tileStruct.setRowsPerTile(rowsPerTile);
|
643 |
|
644 |
//
|
645 |
this.tileStruct.setPixelSizePerZoomLevel(zoomLevels);
|
646 |
} catch (LocatorException | InitializeException e) {
|
647 |
logger.warn("Can't create world extent of GoogleMaps", e);
|
648 |
} |
649 |
} |
650 |
return this.tileStruct; |
651 |
} |
652 |
|
653 |
/**
|
654 |
* @return
|
655 |
* @throws InitializeException
|
656 |
*/
|
657 |
private WMTSTileMatrixSet getTileMatrixSet() throws InitializeException { |
658 |
WMTSTileMatrixSet matrixSet = null;
|
659 |
List<WMTSTileMatrixSet> matrixSets = wmtsClient.getTileMatrixSet();
|
660 |
for (Iterator<WMTSTileMatrixSet> iterator = matrixSets.iterator(); iterator.hasNext();) { |
661 |
WMTSTileMatrixSet wmtsTileMatrixSet = (WMTSTileMatrixSet) iterator.next(); |
662 |
String supportedCrs = wmtsTileMatrixSet.getSupportedCRS();
|
663 |
IProjection supportedProjection = null;
|
664 |
try {
|
665 |
supportedProjection = CRSFactory.getCRS(supportedCrs); |
666 |
if(supportedProjection==null){ |
667 |
continue;
|
668 |
} |
669 |
|
670 |
} catch (Exception e) { |
671 |
continue;
|
672 |
} |
673 |
if ( supportedProjection.equals(getProjection()) ){
|
674 |
matrixSet = wmtsTileMatrixSet; |
675 |
break;
|
676 |
} |
677 |
} |
678 |
return matrixSet;
|
679 |
} |
680 |
|
681 |
private List<PageManager> getPageManagersInZoom(int zoomLevel) throws ValidateDataParametersException, CreateEnvelopeException, BufferException, DataException, CloneNotSupportedException { |
682 |
List<PageManager> pageManagersInZoom = this.pageManagersPerZoomLevel.get(zoomLevel); |
683 |
if (pageManagersInZoom == null) { |
684 |
pageManagersInZoom = new ArrayList<PageManager>(); |
685 |
for (int i = 0; i < getBands(); i++) { |
686 |
//FIXME el getTileStruct no sirve para este bandTileManager
|
687 |
pageManagersInZoom.add(new WMTSBandTileManager(getTileStruct(), this.getWMTSImage(), zoomLevel, i, this)); |
688 |
} |
689 |
this.pageManagersPerZoomLevel.put(zoomLevel, pageManagersInZoom);
|
690 |
} |
691 |
return pageManagersInZoom;
|
692 |
} |
693 |
|
694 |
/**
|
695 |
* @param zoomLevel
|
696 |
* @return rows per zoom level
|
697 |
*/
|
698 |
public int getRows(int zoomLevel) { |
699 |
return (int) (Math.round(getTileStruct().getEnvelope().getLength(DIMENSIONS.Y) / this.getTileStruct().getPixelSizePerZoomLevel().get(zoomLevel))); |
700 |
} |
701 |
|
702 |
/**
|
703 |
* @param zoomLevel
|
704 |
* @return columns per zoom level
|
705 |
*/
|
706 |
public int getColumns(int zoomLevel) { |
707 |
return (int) (Math.round(getTileStruct().getEnvelope().getLength(DIMENSIONS.X) / this.getTileStruct().getPixelSizePerZoomLevel().get(zoomLevel))); |
708 |
} |
709 |
|
710 |
/**
|
711 |
* @param pixelSize
|
712 |
* @return The appropriate zoom level for the pizelSize
|
713 |
*/
|
714 |
private int getZoomLvlForPixelSize(double pixelSize) { |
715 |
//FIXME: Elegir el c?digo id?neo
|
716 |
// Este trozo de c?digo selecciona un zoom superior con lo que,
|
717 |
// al hacer la interpolaci?n se generar?n p?xeles m?s gordos
|
718 |
int zoom = 0; |
719 |
Map<Integer, Double> pixelSizePerZoomLevel = tileStruct.getPixelSizePerZoomLevel(); |
720 |
|
721 |
SortedSet<Integer> keySet = new TreeSet<>(Collections.reverseOrder()); |
722 |
keySet.addAll(pixelSizePerZoomLevel.keySet()); |
723 |
Iterator<Integer> iterator = keySet.iterator(); |
724 |
while(iterator.hasNext()){
|
725 |
zoom = iterator.next(); |
726 |
double levelPixelSize = pixelSizePerZoomLevel.get(zoom);
|
727 |
if (pixelSize <= levelPixelSize) {
|
728 |
return zoom;
|
729 |
} |
730 |
} |
731 |
|
732 |
return zoom;
|
733 |
|
734 |
// Este trozo de c?digo selecciona un zoom inferior con lo que,
|
735 |
// al hacer la interpolaci?n se perder?n p?xeles
|
736 |
// repercutiendo en la legibilidad de las leyendas
|
737 |
/*
|
738 |
int zoom = 0;
|
739 |
Map<Integer, Double> pixelSizePerZoomLevel = getTileStruct().getPixelSizePerZoomLevel();
|
740 |
for (int i : pixelSizePerZoomLevel.keySet()) {
|
741 |
zoom = i;
|
742 |
double levelPixelSize = pixelSizePerZoomLevel.get(zoom);
|
743 |
if (pixelSize >= levelPixelSize) {
|
744 |
return zoom;
|
745 |
}
|
746 |
}
|
747 |
return zoom;
|
748 |
*/
|
749 |
} |
750 |
|
751 |
private WMTSImage getWMTSImage() {
|
752 |
if(this.image == null){ |
753 |
this.image = new WMTSImage(getTileStruct(), this.tileMatrixPerZoomLevel, this); |
754 |
} |
755 |
return this.image; |
756 |
} |
757 |
|
758 |
protected IProjection getProjection(){
|
759 |
if(this.projection==null){ |
760 |
projection = (IProjection) (getParameters().getDynValue(WMTSRasterProviderParameters.WMTS_CRS_PARAMETER_NAME)); |
761 |
} |
762 |
return this.projection; |
763 |
} |
764 |
|
765 |
protected WMTSClient getWMTSClient() throws InitializeException { |
766 |
if (wmtsClient == null) { |
767 |
WMTSRasterProviderParameters parameters = (WMTSRasterProviderParameters) getParameters(); |
768 |
URL service = parameters.getService();
|
769 |
try {
|
770 |
wmtsClient = WMTSOGCLocator.getManager().createWMTSClient(service.toString()); |
771 |
Boolean forceChangeAxisOrder = (Boolean) getParameters().getDynValue(WMTSRasterProviderParameters.WMTS_FORCECHANGEAXISORDER_PARAMETER_NAME); |
772 |
if(forceChangeAxisOrder!=null){ |
773 |
wmtsClient.setForceChangeAxisOrder(forceChangeAxisOrder.booleanValue()); |
774 |
} else {
|
775 |
wmtsClient.setForceChangeAxisOrder(false);
|
776 |
} |
777 |
wmtsClient.connect(true, null); |
778 |
} catch (IOException e) { |
779 |
throw new InitializeException("Can't create WMTSClient with from service '" + service.toString() + "'", |
780 |
e); |
781 |
} |
782 |
} |
783 |
return wmtsClient;
|
784 |
} |
785 |
|
786 |
protected int getRowsPerTile() { |
787 |
return this.rowsPerTile; |
788 |
}; |
789 |
|
790 |
protected int getColumnsPerTile() { |
791 |
return this.columnsPerTile; |
792 |
}; |
793 |
|
794 |
@Override
|
795 |
public Image getImageLegend() { |
796 |
File file;
|
797 |
BufferedImage img = null; |
798 |
try {
|
799 |
WMTSLayer wmtsLayer = getWMTSClient().getLayer(getLayerName()); |
800 |
WMTSStyle wmtsStyle = null;
|
801 |
List<WMTSStyle> wmtsStyles = wmtsLayer.getStyle();
|
802 |
for (Iterator iterator = wmtsStyles.iterator(); iterator.hasNext();) { |
803 |
wmtsStyle = (WMTSStyle) iterator.next(); |
804 |
if (wmtsStyle.getIdentifier().equalsIgnoreCase(wmtsStatus.getStyle())) {
|
805 |
break;
|
806 |
} |
807 |
wmtsStyle = null;
|
808 |
} |
809 |
|
810 |
file = getWMTSClient().getLegendGraphic(wmtsLayer, wmtsStyle, null);
|
811 |
img = ImageIO.read(file);
|
812 |
} catch (InitializeException | WMTSException | IOException e) { |
813 |
logger.warn("Can't read image legend of WMTS Provider", e);
|
814 |
} |
815 |
return img;
|
816 |
} |
817 |
|
818 |
@Override
|
819 |
public BufferDimensions getDimensions() throws InitializeException { |
820 |
if(this.dimensions==null){; |
821 |
Double pixelSize = getTileStruct().getPixelSizePerZoomLevel().get(getMaxZoomLevel());
|
822 |
Envelope envelope = this.getEnvelope();
|
823 |
int rows = (int)Math.round(envelope.getLength(DIMENSIONS.Y)/pixelSize); |
824 |
int columns = (int)Math.round(envelope.getLength(DIMENSIONS.X)/pixelSize); |
825 |
this.dimensions = BufferLocator.getBufferManager().createBufferDimensions(rows, columns, envelope);
|
826 |
} |
827 |
return this.dimensions; |
828 |
} |
829 |
|
830 |
/**
|
831 |
* @return the maxZoomLevel
|
832 |
*/
|
833 |
public int getMaxZoomLevel() { |
834 |
return maxZoomLevel;
|
835 |
} |
836 |
|
837 |
// /**
|
838 |
// * @param maxZoomLevel the maxZoomLevel to set
|
839 |
// */
|
840 |
// public void setMaxZoomLevel(int maxZoomLevel) {
|
841 |
// this.maxZoomLevel = maxZoomLevel;
|
842 |
// }
|
843 |
|
844 |
} |