Revision 1374

View differences:

org.gvsig.raster.wmts/tags/buildNumber_24/org.gvsig.raster.wmts/org.gvsig.raster.wmts.io/src/test/resources/TemplateRasterProvider.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.fmap.dal.coverage.dataset.io.netcdf;
23

  
24
import java.awt.geom.AffineTransform;
25

  
26
import org.gvsig.fmap.dal.DALFileLocator;
27
import org.gvsig.fmap.dal.DALLocator;
28
import org.gvsig.fmap.dal.DataStore;
29
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
30
import org.gvsig.fmap.dal.coverage.dataset.io.tile.downloader.FileTileServer;
31
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
32
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
33
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
34
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
35
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
36
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
37
import org.gvsig.fmap.dal.coverage.exception.ParsingException;
38
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
39
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
40
import org.gvsig.fmap.dal.coverage.store.RasterFileStoreParameters;
41
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
42
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
43
import org.gvsig.metadata.MetadataLocator;
44
import org.gvsig.raster.cache.tile.provider.TileListener;
45
import org.gvsig.raster.cache.tile.provider.TileServer;
46
import org.gvsig.raster.impl.datastruct.ExtentImpl;
47
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
48
import org.gvsig.raster.impl.provider.RasterProvider;
49
import org.gvsig.raster.impl.store.AbstractRasterDataParameters;
50
import org.gvsig.raster.impl.store.AbstractRasterDataStore;
51
import org.gvsig.raster.impl.store.DefaultStoreFactory;
52
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
53
import org.gvsig.raster.impl.store.properties.DataStoreMetadata;
54
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
55
import org.gvsig.tools.ToolsLocator;
56
import org.gvsig.tools.extensionpoint.ExtensionPoint;
57
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
58
/**
59
 * Data provider for ...
60
 *
61
 * @author Nacho Brodin (nachobrodin@gmail.com)
62
 */
63
public class NetCDFProvider extends DefaultRasterProvider {
64
	public static String        NAME                     = "Template Store";
65
	public static String        DESCRIPTION              = "Template Raster file";
66
	public final String         METADATA_DEFINITION_NAME = "TemplateStore";
67
	
68
	private static String[]     formatList               = new String[]{"*"};
69

  
70
	private Extent              viewRequest              = null;
71
	private TileServer          tileServer               = null;
72
	private boolean             open                     = false;
73

  
74
	public static void register() {
75
		DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
76
		if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
77
			dataman.registerStoreProvider(NAME,
78
					NetCDFProvider.class, NetCDFDataParameters.class);
79
		}
80
		
81
		if(DALFileLocator.getFilesystemServerExplorerManager() != null)
82
			DALFileLocator.getFilesystemServerExplorerManager().registerProvider(
83
					NAME, DESCRIPTION,
84
					NetCDFFilesystemServerExplorer.class);
85
		
86
		dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
87
	}
88

  
89
	/**
90
	 * Returns true if the extension is supported and false if doesn't
91
	 * @param ext
92
	 * @return
93
	 */
94
	public static boolean isExtensionSupported(String ext) {
95
		for (int i = 0; i < formatList.length; i++) {
96
			if(formatList[i].compareTo(ext) == 0)
97
				return true;
98
		}
99
		return false;
100
	}
101
	
102
	/**
103
	 * Constructor. Abre el dataset.
104
	 * @param proj Proyecci?n
105
	 * @param fName Nombre del fichero
106
	 * @throws NotSupportedExtensionException
107
	 */
108
	public NetCDFProvider(String params) throws NotSupportedExtensionException {
109
		super(params);
110
		if(params instanceof String) {
111
			NetCDFDataParameters p = new NetCDFDataParameters();
112
			p.setURI((String)params);
113
			super.init(p, null, ToolsLocator.getDynObjectManager()
114
					.createDynObject(
115
							MetadataLocator.getMetadataManager().getDefinition(
116
									DataStore.METADATA_DEFINITION_NAME)));
117
			init(p, null);
118
		}
119
	}
120
	
121
	public NetCDFProvider (NetCDFDataParameters params,
122
			AbstractRasterDataStore storeServices) throws NotSupportedExtensionException {
123
		super(params, storeServices, ToolsLocator.getDynObjectManager()
124
				.createDynObject(
125
						MetadataLocator.getMetadataManager().getDefinition(
126
								DataStore.METADATA_DEFINITION_NAME)));
127
		init(params, storeServices);
128
	}
129

  
130
	/**
131
	 * Crea las referencias al fichero y carga
132
	 * las estructuras con la informaci?n y los metadatos.
133
	 * @param proj Proyecci?n
134
	 * @param param Parametros de carga
135
	 * @throws NotSupportedExtensionException
136
	 */
137
	public void init (AbstractRasterDataParameters params,
138
			DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
139
		//TODO:
140
		
141
		if(((RasterFileStoreParameters)params).getFile().exists()) {
142
			setParam(params);
143
			colorTable = null;
144
			noData = 0;
145
			wktProjection = null;
146
			//CrsWkt crs = new CrsWkt(wktProjection);
147
			//IProjection proj = CRSFactory.getCRS("EPSG:23030");
148
			noDataEnabled = true;
149
			ownTransformation = null;
150
			externalTransformation = (AffineTransform)ownTransformation.clone();
151
			load();
152
		} else
153
			setParam(params);
154
		bandCount = 0;
155
		setDataType(null);
156
		super.init();
157

  
158
		try {
159
			loadFromRmf(getRmfBlocksManager());
160
		} catch (ParsingException e) {
161
			//No lee desde rmf
162
		}
163
	}
164
	
165
	/*
166
	 * (non-Javadoc)
167
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#load()
168
	 */
169
	public RasterProvider load() {
170
		return this;
171
	}
172
	
173
	/*
174
	 * (non-Javadoc)
175
	 * @see org.gvsig.raster.impl.provider.RasterProvider#isOpen()
176
	 */
177
	public boolean isOpen() {
178
		return open;
179
	}
180

  
181
	/*
182
	 * (non-Javadoc)
183
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
184
	 */
185
	public void close() {
186
		//TODO:
187
	}
188

  
189
	/*
190
	 * (non-Javadoc)
191
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#translateFileName(java.lang.String)
192
	 */
193
	public String translateFileName(String fileName) {
194
		return fileName;
195
	}
196

  
197
	/**
198
	 * Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci?n
199
	 * de la vista asignada ya que la petici?n viene en coordenadas del fichero .rmf y la vista (v)
200
	 * ha de estar en coordenadas del fichero.
201
	 */
202
	public void setView(Extent e) {
203
		viewRequest = new ExtentImpl(e);
204
	}
205

  
206
	/*
207
	 * (non-Javadoc)
208
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
209
	 */
210
	public Extent getView() {
211
		return viewRequest;
212
	}
213

  
214
	/*
215
	 * (non-Javadoc)
216
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
217
	 */
218
	public double getWidth() {
219
		//TODO:
220
		return 0;
221
	}
222

  
223
	/*
224
	 * (non-Javadoc)
225
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
226
	 */
227
	public double getHeight() {
228
		//TODO:
229
		return 0;
230
	}
231

  
232
	/**
233
	 * Read a line from the file
234
	 * @param line
235
	 * @param band
236
	 * @return
237
	 * @throws InvalidSetViewException
238
	 * @throws FileNotOpenException
239
	 * @throws RasterDriverException
240
	 * @Deprecated This operation is deprecated because is not useful and in the future
241
	 * it will not be maintained. The abstract operation has dissapear
242
	 */
243
	public Object readCompleteLine(int line, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
244
		if(line > this.getHeight() || band > this.getBandCount())
245
			throw new InvalidSetViewException("Request out of grid");
246

  
247
		//TODO:
248
		return null;
249
	}
250

  
251
	/*
252
	 *  (non-Javadoc)
253
	 * @see org.gvsig.raster.dataset.RasterDataset#readBlock(int, int)
254
	 */
255
	public Object readBlock(int pos, int blockHeight)
256
		throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
257
		if(pos < 0)
258
			throw new InvalidSetViewException("Request out of grid");
259

  
260
		if((pos + blockHeight) > getHeight())
261
			blockHeight = Math.abs(((int)getHeight()) - pos);
262
		
263
		//TODO:
264
		return null;
265
	}
266

  
267
	/*
268
	 * (non-Javadoc)
269
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getData(int, int, int)
270
	 */
271
	public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
272
		if(x < 0 || y < 0 || x >= getWidth() || y >= getHeight())
273
			throw new InvalidSetViewException("Request out of grid");
274
		
275
		//TODO
276
		return null;
277
	}
278

  
279
	/*
280
	 * (non-Javadoc)
281
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.raster.cache.tile.provider.TileListener)
282
	 */
283
	public void getWindow(Extent ex, int bufWidth, int bufHeight, 
284
			BandList bandList, TileListener listener) throws ProcessInterruptedException, RasterDriverException {
285
		//TODO
286
	}
287

  
288
	/*
289
	 * (non-Javadoc)
290
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(org.gvsig.fmap.dal.coverage.datastruct.Extent, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
291
	 */
292
	public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf) 
293
		throws ProcessInterruptedException, RasterDriverException {
294
		//TODO
295

  
296
		return rasterBuf;
297
	}
298

  
299
	/*
300
	 * (non-Javadoc)
301
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
302
	 */
303
	public Buffer getWindow(double ulx, double uly, double w, double h, 
304
			BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
305
		//TODO
306

  
307
		return rasterBuf;
308
	}
309

  
310
	/*
311
	 * (non-Javadoc)
312
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
313
	 */
314
	public Buffer getWindow(Extent extent, 
315
			int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
316
		//TODO
317
		
318
		return rasterBuf;
319
	}
320

  
321
	/*
322
	 * (non-Javadoc)
323
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
324
	 */
325
	public Buffer getWindow(int x, int y, int w, int h, 
326
			BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
327
		//TODO
328
		
329
		return rasterBuf;
330
	}
331

  
332
	/*
333
	 * (non-Javadoc)
334
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
335
	 */
336
	public Buffer getWindow(int x, int y, int w, int h, 
337
			int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
338
		//TODO
339
		
340
		return rasterBuf;
341
	}
342

  
343
	/*
344
	 * (non-Javadoc)
345
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
346
	 */
347
	public int getBlockSize(){
348
		//TODO
349
		
350
		return 0;
351
	}
352

  
353
	/*
354
	 * (non-Javadoc)
355
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getMetadata()
356
	 */
357
	public DataStoreMetadata getMetadata() {
358
		//TODO
359
		
360
		return null;
361
	}
362

  
363
	/*
364
	 * (non-Javadoc)
365
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getColorInterpretation()
366
	 */
367
	public DataStoreColorInterpretation getColorInterpretation(){
368
		//TODO
369
		
370
		return null;
371
	}
372

  
373
	/*
374
	 * (non-Javadoc)
375
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setColorInterpretation(org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation)
376
	 */
377
	public void setColorInterpretation(DataStoreColorInterpretation colorInterpretation){
378
		//TODO
379
	}
380

  
381
	/*
382
	 * (non-Javadoc)
383
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
384
	 */
385
	public DataStoreTransparency getTransparency() {
386
		//TODO
387
		
388
		return null;
389
	}
390

  
391
	/*
392
	 * (non-Javadoc)
393
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isGeoreferenced()
394
	 */
395
	public boolean isGeoreferenced() {
396
		//TODO
397
		
398
		return false;
399
	}
400

  
401
	/**
402
	 * Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
403
	 * el valor de esta variable cada vez que dibuja.
404
	 * @return true si se ha supersampleado y false si no se ha hecho.
405
	 */
406
	public boolean isSupersampling() {
407
		//TODO
408
		
409
		return false;
410
	}
411

  
412
	/*
413
	 * (non-Javadoc)
414
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setAffineTransform(java.awt.geom.AffineTransform)
415
	 */
416
	public void setAffineTransform(AffineTransform t){
417
		super.setAffineTransform(t);
418
	}
419

  
420
	/*
421
	 * (non-Javadoc)
422
	 * @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
423
	 */
424
	public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
425
		if(band >= getBandCount())
426
			throw new BandAccessException("Wrong band");
427
		//TODO
428
		
429
		return 0;
430
	}
431

  
432
	/*
433
	 * (non-Javadoc)
434
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewWidth(int, int)
435
	 */
436
	public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
437
		if (band >= getBandCount())
438
			throw new BandAccessException("Wrong band");
439
		//TODO
440
		
441
		return 0;
442
	}
443

  
444
	/*
445
	 * (non-Javadoc)
446
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewHeight(int, int)
447
	 */
448
	public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
449
		if (band >= getBandCount())
450
			throw new BandAccessException("Wrong band");
451
		//TODO
452
		
453
		return 0;
454
	}
455

  
456
	/*
457
	 * (non-Javadoc)
458
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#overviewsSupport()
459
	 */
460
	public boolean overviewsSupport() {
461
		return true;
462
	}
463

  
464
	/*
465
	 * (non-Javadoc)
466
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isReproyectable()
467
	 */
468
	public boolean isReproyectable() {
469
		return true;
470
	}
471

  
472
	/*
473
	 * (non-Javadoc)
474
	 * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getName()
475
	 */
476
	public String getName() {
477
		return NAME;
478
	}
479
	
480
	/*
481
	 * (non-Javadoc)
482
	 * @see org.gvsig.raster.impl.provider.RasterProvider#setStatus(org.gvsig.raster.impl.provider.RasterProvider)
483
	 */
484
	public void setStatus(RasterProvider provider) {
485
		if(provider instanceof NetCDFProvider) {
486
			//Not implemented yet
487
		}
488
	}
489
	
490
	/*
491
	 * (non-Javadoc)
492
	 * @see org.gvsig.raster.impl.provider.RasterProvider#getTileServer()
493
	 */
494
	public TileServer getTileServer() {
495
		if(tileServer == null)
496
			tileServer = new FileTileServer(this);
497
		return tileServer;
498
	}
499
}
0 500

  
org.gvsig.raster.wmts/tags/buildNumber_24/org.gvsig.raster.wmts/org.gvsig.raster.wmts.io/src/test/resources/README.txt
1
Put into this folder the resources needed by your test classes.
2

  
3
This folder is added to the Tests classpath, so you can load any resources 
4
through the ClassLoader.
5

  
6
By default, in this folder you can find an example of log4j configuration,
7
prepared to log messages through the console, so logging works when you
8
run your tests classes.
0 9

  
org.gvsig.raster.wmts/tags/buildNumber_24/org.gvsig.raster.wmts/org.gvsig.raster.wmts.io/src/test/resources/log4j.xml
1
<?xml version="1.0" encoding="ISO-8859-1" ?>
2
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3

  
4
<!-- 
5
Log4J configuration file for unit tests execution.
6
 -->
7
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
8

  
9
	<!-- Appender configuration to show logging messages through the console -->
10
	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
11
		<layout class="org.apache.log4j.PatternLayout">
12
			<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{2}.%M()]\n  %m%n" />
13
		</layout>
14
	</appender>
15

  
16
	<!-- 
17
	Activate logging messages of DEBUG level of higher only for the
18
	org.gvsig.tools packages.
19
	You can put full classes names or packages instead, to configure
20
	logging for all the classes and subpackages of the package.
21
	-->
22
	<category name="org.gvsig.tools">
23
		<priority value="DEBUG" />
24
	</category>
25
	<category name="org.gvsig.raster">
26
		<priority value="DEBUG" />
27
	</category>
28

  
29
	<!-- 
30
	By default, show only logging messages of INFO level or higher, 
31
	through the previously configured CONSOLE appender. 
32
	-->
33
	<root>
34
		<priority value="INFO" />
35
		<appender-ref ref="CONSOLE" />
36
	</root>
37
</log4j:configuration>
0 38

  
org.gvsig.raster.wmts/tags/buildNumber_24/org.gvsig.raster.wmts/org.gvsig.raster.wmts.io/src/main/java/org/gvsig/raster/wmts/io/WMTSConnector.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.wmts.io;
23

  
24
import java.io.File;
25
import java.io.IOException;
26
import java.net.ConnectException;
27
import java.net.MalformedURLException;
28
import java.net.URL;
29
import java.util.ArrayList;
30

  
31
import org.gvsig.compat.net.ICancellable;
32
import org.gvsig.remoteclient.exceptions.ServerErrorException;
33
import org.gvsig.remoteclient.wmts.WMTSClient;
34
import org.gvsig.remoteclient.wmts.WMTSStatus;
35
import org.gvsig.remoteclient.wmts.exception.DownloadException;
36
import org.gvsig.remoteclient.wmts.exception.WMTSException;
37
import org.gvsig.remoteclient.wmts.struct.WMTSLayer;
38
import org.gvsig.remoteclient.wmts.struct.WMTSServiceIdentification;
39
import org.gvsig.remoteclient.wmts.struct.WMTSServiceProvider;
40
import org.gvsig.remoteclient.wmts.struct.WMTSThemes;
41
import org.gvsig.remoteclient.wmts.struct.WMTSTileMatrixSet;
42

  
43
/**
44
 * This class offers an interface of services for a WMTS  
45
 *
46
 * @author Nacho Brodin (nachobrodin@gmail.com)
47
 */
48
public class WMTSConnector  {
49
	private WMTSClient                     client        = null;
50

  
51
    public WMTSConnector(URL url) throws ConnectException, IOException {
52
    	client = new WMTSClient(url.toString());
53
    }
54
    
55
    /**
56
	 * Sets longitude first in the axis order
57
	 * @param force
58
	 */
59
	public void setForceLongitudeFirstAxisOrder(boolean force) {
60
		if(client != null) {
61
			client.setForceLongitudeFirstAxisOrder(force);
62
		}
63
	}
64

  
65
    /**
66
     * Establishes the connection.
67
     * @param override, if true the previous downloaded data will be overridden
68
     * @return <b>true</b> if the connection was successful, or <b>false</b> if it was no possible
69
     * to establish the connection for any reason such as version negotiation.
70
     */
71
    public boolean connect(boolean override, ICancellable cancel) {
72
    	return client.connect(override, cancel);
73
    }
74

  
75
    public boolean connect(ICancellable cancel) {
76
    	return client.connect(false, cancel);
77
    }
78

  
79
	/**
80
	 * Gets the list of themes
81
	 * @return
82
	 */
83
	public WMTSThemes getThemes() {
84
		return client.getThemes();
85
	}
86
    
87
    /**
88
     * Gets a layer 
89
     * @param layerName
90
     * @return
91
     */
92
    public WMTSLayer getLayer(String layerName) {
93
    	return client.getLayer(layerName);
94
    }
95
    
96
    /**
97
     * Gets the set of tiles definition
98
     * @return
99
     */
100
    @SuppressWarnings("unchecked")
101
	public ArrayList<WMTSTileMatrixSet> getTileMatrixSet() {
102
    	return client.getTileMatrixSet();
103
    }
104
    
105
    /*
106
	 * (non-Javadoc)
107
	 * @see org.gvsig.fmap.dal.coverage.explorer.WMTSServerExplorer#getServiceIdentification()
108
	 */
109
	public WMTSServiceIdentification getServiceIdentification() {
110
		return client.getServiceIdentification();
111
	}
112
	
113
	public WMTSServiceProvider getServiceProvider() {
114
		return client.getServiceProvider();
115
	}
116

  
117
    /**
118
     * Gets the host
119
     * @return
120
     */
121
    public String getHost(){
122
    	return client.getHost();
123
    }
124
   
125
    /**
126
     * <p>Gets a tile downloading using a specific path and file.</p> 
127
     * @throws ServerErrorException 
128
     */
129
    public synchronized File getTile(WMTSStatus status, ICancellable cancel, File file) throws WMTSException, ServerErrorException {   
130
        return client.getTile(status, cancel, file);
131
    }
132
    
133
    /**
134
     * <p>One of the three interfaces that OGC WMTS defines. Request a map.</p> 
135
     * @throws ServerErrorException 
136
     */
137
    public synchronized File getTile(WMTSStatus status, ICancellable cancel) throws WMTSException, ServerErrorException {   
138
        return client.getTile(status, cancel);
139
    }
140
    
141
    /**
142
     * Builds the URL to get a tile using a WMTSStatus object 
143
     * @throws ServerErrorException 
144
     */
145
    public synchronized URL getTileURL(WMTSStatus status) throws MalformedURLException {   
146
        return client.getTileURL(status);
147
    }
148
    
149
    /**
150
     * Downloads a file
151
     * @throws DownloadException 
152
     * @throws ServerErrorException 
153
     */
154
    public synchronized File downloadFile(URL url, ICancellable cancel) throws DownloadException {   
155
        return client.downloadFile(url, cancel);
156
    }
157
    
158
    /**
159
     * <p>It will send a GetFeatureInfo request to the WMTS
160
     * Parsing the response and redirecting the info to the WMTS client</p>
161
     */
162
    public String getFeatureInfo(WMTSStatus status, int x, int y, ICancellable cancel) throws WMTSException, ServerErrorException {   
163
        return client.getFeatureInfo(status, x, y, cancel);
164
    }
165
}
0 166

  
org.gvsig.raster.wmts/tags/buildNumber_24/org.gvsig.raster.wmts/org.gvsig.raster.wmts.io/src/main/java/org/gvsig/raster/wmts/io/WMTSProvider.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.wmts.io;
23

  
24
import java.awt.geom.AffineTransform;
25
import java.awt.geom.NoninvertibleTransformException;
26
import java.awt.geom.Point2D;
27
import java.awt.geom.Rectangle2D;
28
import java.io.File;
29
import java.io.IOException;
30
import java.net.ConnectException;
31
import java.net.MalformedURLException;
32
import java.net.URL;
33
import java.util.ArrayList;
34

  
35
import org.gvsig.compat.net.ICancellable;
36
import org.gvsig.fmap.dal.DALLocator;
37
import org.gvsig.fmap.dal.DataStore;
38
import org.gvsig.fmap.dal.DataStoreParameters;
39
import org.gvsig.fmap.dal.coverage.RasterLocator;
40
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
41
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
42
import org.gvsig.fmap.dal.coverage.datastruct.DatasetBand;
43
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
44
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
45
import org.gvsig.fmap.dal.coverage.exception.BandNotFoundInListException;
46
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
47
import org.gvsig.fmap.dal.coverage.exception.InfoByPointException;
48
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
49
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
50
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
51
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
52
import org.gvsig.fmap.dal.coverage.exception.RemoteServiceException;
53
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
54
import org.gvsig.fmap.dal.coverage.store.props.HistogramComputer;
55
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
56
import org.gvsig.fmap.dal.coverage.util.MathUtils;
57
import org.gvsig.fmap.dal.exception.InitializeException;
58
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
59
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
60
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
61
import org.gvsig.metadata.MetadataLocator;
62
import org.gvsig.raster.cache.tile.TileCacheLocator;
63
import org.gvsig.raster.cache.tile.TileCacheManager;
64
import org.gvsig.raster.cache.tile.exception.TileGettingException;
65
import org.gvsig.raster.cache.tile.provider.CacheStruct;
66
import org.gvsig.raster.cache.tile.provider.TileListener;
67
import org.gvsig.raster.cache.tile.provider.TileServer;
68
import org.gvsig.raster.impl.DefaultRasterManager;
69
import org.gvsig.raster.impl.buffer.DefaultRasterQuery;
70
import org.gvsig.raster.impl.datastruct.BandListImpl;
71
import org.gvsig.raster.impl.datastruct.DatasetBandImpl;
72
import org.gvsig.raster.impl.datastruct.ExtentImpl;
73
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
74
import org.gvsig.raster.impl.provider.MemoryMatrixBuffer;
75
import org.gvsig.raster.impl.provider.RasterProvider;
76
import org.gvsig.raster.impl.provider.RemoteRasterProvider;
77
import org.gvsig.raster.impl.provider.TiledRasterProvider;
78
import org.gvsig.raster.impl.store.DefaultStoreFactory;
79
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
80
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
81
import org.gvsig.raster.impl.store.properties.RemoteStoreHistogram;
82
import org.gvsig.raster.util.DefaultProviderServices;
83
import org.gvsig.raster.wmts.io.downloader.WMTSTileServer;
84
import org.gvsig.remoteclient.exceptions.ServerErrorException;
85
import org.gvsig.remoteclient.wmts.WMTSStatus;
86
import org.gvsig.remoteclient.wmts.exception.DownloadException;
87
import org.gvsig.remoteclient.wmts.exception.WMTSException;
88
import org.gvsig.remoteclient.wmts.struct.WMTSBoundingBox;
89
import org.gvsig.remoteclient.wmts.struct.WMTSLayer;
90
import org.gvsig.remoteclient.wmts.struct.WMTSTileMatrix;
91
import org.gvsig.remoteclient.wmts.struct.WMTSTileMatrixLimits;
92
import org.gvsig.remoteclient.wmts.struct.WMTSTileMatrixSet;
93
import org.gvsig.remoteclient.wmts.struct.WMTSTileMatrixSetLink;
94
import org.gvsig.remoteclient.wmts.struct.WMTSTileMatrix.Tile;
95
import org.gvsig.tools.ToolsLocator;
96
import org.gvsig.tools.task.TaskStatus;
97
import org.slf4j.Logger;
98
import org.slf4j.LoggerFactory;
99
/**
100
 * Provider for WMTS service
101
 *
102
 * @author Nacho Brodin (nachobrodin@gmail.com)
103
 */
104
public class WMTSProvider extends DefaultRasterProvider implements RemoteRasterProvider, TiledRasterProvider {
105
	public static String                NAME                     = "Wmts Store";
106
	public static String                DESCRIPTION              = "Wmts Raster file";
107
	public static final String          METADATA_DEFINITION_NAME = "WmtsStore";
108
	private static Logger               logger                   = LoggerFactory.getLogger(WMTSProvider.class);
109
	public static boolean               TILED                    = true;
110
	
111
	//Los tiles se piden de forma secuencial y sin lanzar threads para ello (+Lento)
112
	public static int                   SEQUENTIAL               = 0;
113
	//Los tiles se piden en threads y hay un thread manager para gestionar que no se pidan m?s de cierto n?mero
114
	public static int                   LIMITED_THREADS          = 1;
115
	//Los tiles se piden en threads y se lanzan tantos threads como tiles haya
116
	public static int                   UNLIMITED_THREADS        = 2;
117
	private int                         requestType              = LIMITED_THREADS;
118
	
119
	private static final double         MTS_X_GRADO              = 111319.490793274;
120
	
121
	private Extent                      viewRequest              = null;
122
	private WMTSConnector               connector                = null;
123
	//private static Hashtable<URL, WMTSConnector>    
124
	//                                    drivers                  = new Hashtable<URL, WMTSConnector> ();
125
	private boolean                     open                     = false;
126
	private File                        lastRequest              = null;
127
	private DataStoreTransparency       lastFileTransparency     = null;
128
	private int                         lastWidthRequest         = 0;
129
	private int                         lastHeightRequest        = 0;
130
	private WMTSStatus                  lastStatus               = null;
131
	private boolean                     gridSubsets              = true;
132
	private Extent[]                    extentByLevel            = null; //Only for layers without gridSubsets
133
	private MathUtils                   math                     = RasterLocator.getManager().getMathUtils();
134
	
135
	/**
136
	 * This thread manages the number of tiles that have been thrown.
137
	 * This number is controlled by the NTHREADS_QUEUE variable.
138
	 * 
139
	 * @author Nacho Brodin (nachobrodin@gmail.com)
140
	 */
141
	public class RequestThreadManager extends Thread {
142
		private TilePipe           pipe           = null;
143
		private ArrayList<Tile>    tiles          = null;
144
		private WMTSStatus         status         = null;
145
		
146
		public RequestThreadManager(TilePipe pipe, ArrayList<Tile> tiles, WMTSStatus status) {
147
			this.pipe = pipe;
148
			this.tiles = tiles;
149
			this.status = status;
150
		}
151
		
152
		public void run() {
153
			for (int i = 0; i < tiles.size(); i++) {
154
				Tile tile = tiles.get(i);
155
				WMTSStatus statusCopy = status.cloneStatus();
156
				statusCopy.setTileRow(tile.row);
157
				statusCopy.setTileCol(tile.col);
158
				if (pipe.getSize() > TilePipe.NTHREADS_QUEUE) {
159
					try {
160
						synchronized (this) {
161
							wait();							
162
						}
163
					} catch( InterruptedException e ) {
164
					}
165
				}
166
				new RequestTileLauncher(pipe, statusCopy, tile).start();
167
			}
168
		}
169
	}
170
	
171
	/**
172
	 * Thread to download a tile
173
	 * @author Nacho Brodin (nachobrodin@gmail.com)
174
	 */
175
	class RequestTileLauncher extends Thread {
176
		private TilePipe      pipe    = null;
177
		private WMTSStatus    status  = null;
178
		private Tile          tile    = null;
179

  
180
		public RequestTileLauncher(TilePipe pipe, WMTSStatus status, Tile tile) {
181
			this.pipe = pipe;
182
			this.status = status;
183
			this.tile = tile;
184
		}
185

  
186
		public void run() {
187
			try {
188
				//File file = getConnector().getTile(status, null);
189
				URL url = getConnector().getTileURL(status);
190
				tile.file = getConnector().downloadFile(url, null);
191
				pipe.setTile(tile);
192
			} catch (DownloadException e) {
193
				logger.info("Error downloading files", e);
194
			} catch (MalformedURLException e) {
195
				logger.info("Malformed URL", e);
196
			} catch (WMTSException e) {
197
				logger.info("", e);
198
			}
199
		}
200
	}
201
	
202
	/**
203
	 * Point information 
204
	 * @author Nacho Brodin (nachobrodin@gmail.com)
205
	 */
206
	public class PointInfo {
207
		public Point2D worldCoord;
208
		public Point2D tile;
209
		public Point2D pixelInTile;
210
		public int     level;
211
		
212
		public PointInfo(Point2D worldCoord) {
213
			this.worldCoord = worldCoord;
214
		}
215
	}
216

  
217
	public static void register() {
218
		DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
219
		if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
220
			dataman.registerStoreProvider(NAME,
221
					WMTSProvider.class, WMTSDataParametersImpl.class);
222
		}
223

  
224
		if (!dataman.getExplorerProviders().contains(WMTSServerExplorer.NAME)) {
225
			dataman.registerExplorerProvider(WMTSServerExplorer.NAME, WMTSServerExplorer.class, WMTSServerExplorerParameters.class);
226
		}
227
		dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
228
	}
229
	
230
	public WMTSProvider() throws NotSupportedExtensionException {
231
		super();
232
	}
233
	
234
	/**
235
	 * Constructor. Abre el dataset.
236
	 * @param proj Proyecci?n
237
	 * @param fName Nombre del fichero
238
	 * @throws NotSupportedExtensionException
239
	 */
240
	public WMTSProvider(String params) throws NotSupportedExtensionException {
241
		super(params);
242
		if(params instanceof String) {
243
			WMTSDataParameters p = new WMTSDataParametersImpl();
244
			p.setURI((String)params);
245
			super.init(p, null, ToolsLocator.getDynObjectManager()
246
					.createDynObject(
247
							MetadataLocator.getMetadataManager().getDefinition(
248
									DataStore.METADATA_DEFINITION_NAME)));
249
			init(p, null);
250
		}
251
	}
252
	
253
	public WMTSProvider(WMTSDataParameters params,
254
			DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
255
		super(params, storeServices, ToolsLocator.getDynObjectManager()
256
				.createDynObject(
257
						MetadataLocator.getMetadataManager().getDefinition(
258
								DataStore.METADATA_DEFINITION_NAME)));
259
		init(params, storeServices);
260
	}
261
	
262
	/**
263
	 * Gets the connector from the URL
264
	 * @return
265
	 * @throws RemoteServiceException
266
	 */
267
	public WMTSConnector getConnector() throws WMTSException {
268
		if(connector == null) {
269
			WMTSDataParameters p = (WMTSDataParameters)parameters;
270
			URL url = null;
271
			try {
272
				url = new URL(p.getURI());
273
			} catch (Exception e) {
274
				throw new WMTSException("Malformed URL",e);
275
			}
276
			try {
277
				connector = new WMTSConnector(url);
278
			} catch (ConnectException e) {
279
				throw new WMTSException("Connect exception",e);
280
			} catch (IOException e) {
281
				throw new WMTSException("Connect exception",e);
282
			}
283
		}
284
		return connector;
285
	}
286

  
287
	/**
288
	 * Crea las referencias al fichero y carga
289
	 * las estructuras con la informaci?n y los metadatos.
290
	 * @param proj Proyecci?n
291
	 * @param param Parametros de carga
292
	 * @throws NotSupportedExtensionException
293
	 */
294
	public void init (DataStoreParameters params,
295
			DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
296
		setParam(storeServices, params);
297
		if(((WMTSDataParameters)params).getImageFormat().compareTo("image/gif") == 0) {
298
			setDataType(new int[]{Buffer.TYPE_BYTE});
299
			bandCount = 1;
300
		} else {
301
			setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE});
302
			bandCount = 4;
303
		}
304
		
305
		if(!(param instanceof WMTSDataParameters))
306
			return;
307
		
308
		gridSubsets = hasGridSubsets((WMTSDataParameters)param);
309
		open = true;
310
	}
311
	
312
	/**
313
	 * Returns true if this layer has grid subsets
314
	 * @return
315
	 */
316
	public boolean hasGridSubsets() {
317
		return gridSubsets;
318
	}
319
	
320
	/**
321
	 * Checks if this layer has grid subsets or doesn't
322
	 * @param p
323
	 * @return
324
	 */
325
	@SuppressWarnings("unchecked")
326
	private boolean hasGridSubsets(WMTSDataParameters p) {
327
		ArrayList tileMatrixSetLimits = null;
328
		ArrayList<?> tileMatrixSetLinkList = p.getLayer().getTileMatrixSetLink();
329
		String srs = p.getSRSCode();
330
		for (int i = 0; i < tileMatrixSetLinkList.size(); i++) {
331
			WMTSTileMatrixSetLink tileMatrixSetLink = (WMTSTileMatrixSetLink)tileMatrixSetLinkList.get(i);
332
			WMTSTileMatrixSet tms = tileMatrixSetLink.getTileMatrixSet();
333
			ArrayList tmsl = tileMatrixSetLink.getTileMatrixLimits();
334
			String srsTileSet = tms.getSupportedCRS();
335
			if(srsTileSet.compareTo(srs) == 0) {
336
				tileMatrixSetLimits = tmsl;
337
			}
338
		}
339
		
340
		return tileMatrixSetLimits.size() <= 0 ? false : true; 
341
	}
342
	
343
	/*public static final WMTSConnector getConnectorFromURL(URL url) throws IOException {
344
		WMTSConnector drv = (WMTSConnector) drivers.get(url);
345
		if (drv == null) {
346
			drv = new WMTSConnector(url);
347
			drivers.put(url, drv);
348
		}
349
		return drv;
350
	}*/
351
	
352
	/**
353
	 * Obtiene el objeto que contiene que contiene la interpretaci?n de
354
	 * color por banda
355
	 * @return
356
	 */
357
	public ColorInterpretation getColorInterpretation() {
358
		if(super.getColorInterpretation() == null) {
359
			ColorInterpretation colorInterpretation = new DataStoreColorInterpretation();
360
			colorInterpretation.initColorInterpretation(getBandCount());
361
			if(getBandCount() >= 3) {
362
				colorInterpretation.setColorInterpValue(0, DataStoreColorInterpretation.RED_BAND);
363
				colorInterpretation.setColorInterpValue(1, DataStoreColorInterpretation.GREEN_BAND);
364
				colorInterpretation.setColorInterpValue(2, DataStoreColorInterpretation.BLUE_BAND);
365
			}
366
			if(getBandCount() == 4)
367
				colorInterpretation.setColorInterpValue(3, DataStoreColorInterpretation.ALPHA_BAND);
368
			if(getBandCount() > 4 || getBandCount() < 3) {
369
				for (int i = 0; i < getBandCount(); i++) {
370
					colorInterpretation.setColorInterpValue(i, DataStoreColorInterpretation.UNDEF_BAND);
371
				}
372
			}
373
			setColorInterpretation(colorInterpretation);
374
		}
375
		return super.getColorInterpretation();
376
	}
377
	
378
	/*
379
	 * (non-Javadoc)
380
	 * @see org.gvsig.raster.impl.provider.RasterProvider#isTiled()
381
	 */
382
	public boolean isTiled() {
383
		return true;
384
	}
385
	
386
	/*
387
	 * (non-Javadoc)
388
	 * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getAffineTransform()
389
	 */
390
	public AffineTransform getAffineTransform() {
391
		WMTSDataParameters p = (WMTSDataParameters)parameters;
392
		Extent e = getExtent();
393
		double psX = e.width() / (lastWidthRequest <= 0 ? p.getWidth() : lastWidthRequest);
394
		double psY = -(e.height() / (lastHeightRequest <= 0 ? p.getHeight() : lastHeightRequest));
395
		ownTransformation = new AffineTransform(
396
				psX, 
397
				0, 
398
				0, 
399
				psY, 
400
				e.getULX() - (psX / 2),
401
				e.getULY() - (psY / 2));
402
		externalTransformation = (AffineTransform) ownTransformation.clone();
403
		return ownTransformation;
404
	}
405
	
406
	/**
407
	 * Gets the bounding box in world coordinates. If the layer has grid subsets (TileMatrixLimits) then
408
	 * this will have a only extent but if the layer doesn't have grid subsets then this will have a different
409
	 * extent in each level resolution. In this case we need to know the extent for each level.
410
	 * @return Extent
411
	 */
412
	public Extent getExtent() {
413
		WMTSDataParameters p = (WMTSDataParameters)parameters;
414
		if(gridSubsets) {
415
			WMTSLayer layer = p.getLayer();
416
			WMTSBoundingBox bbox = layer.getWGS84BBox();
417
			return new ExtentImpl(bbox.toRectangle2D());
418
		} else {
419
			WMTSTileMatrixSet tileMatrixSet = getTileMatrixSetLink().getTileMatrixSet();
420
			
421
			//Si ya se han calculado los niveles es q el extent es v?lido sino el nivel ser? el 0
422
			double scale = 0D;
423
			int level = 0;
424
			if(extentByLevel != null && p.getExtent() != null) {
425
				scale = getScale(p.getExtent(), p.getWidth());
426
				try {
427
					level = getLevelFromScale(scale, tileMatrixSet);
428
				} catch (RasterDriverException e) {
429
					e.printStackTrace();
430
				}				
431
			}
432
			
433
			Extent[] ext = getExtentByResolutionLevel();
434
			
435
			if(ext != null && level >= 0 && level < ext.length)
436
				return ext[level];
437
		}
438
		return null;
439
	}
440
	
441
	/**
442
	 * Gets the suffix of the downloaded image
443
	 * @return
444
	 */
445
	public String getFileSuffix() {
446
		WMTSDataParameters p = (WMTSDataParameters)parameters;
447
		String format = p.getImageFormat();
448
		if (format == null){
449
			return "xml";
450
		}
451
		if (format.indexOf("png") >= 0){
452
	        return "png";
453
		}	
454
	    if (format.indexOf("xml") >= 0){
455
	        return "xml";
456
	    }	
457
	    if (format.indexOf("gif") >= 0){
458
	        return "gif";
459
	    }
460
	    if (format.indexOf("tif") >= 0){
461
	        return "tif";
462
	    }
463
	    if (format.indexOf("bmp") >= 0){
464
	        return "bmp";
465
	    }
466
	    if (format.indexOf("jpg") >= 0
467
	        || format.indexOf("jpeg") >= 0){
468
	        return "jpg";			 
469
	    }
470
		return "xml";
471
	}
472

  
473
	/**
474
	 * When a layer doesn't have grid subsets this will have a different bounding
475
	 * box by resolution level. This function calculates and returns the array of
476
	 * extents, one by resolution level.
477
	 * @return
478
	 */
479
	public Extent[] getExtentByResolutionLevel() {
480
		if(extentByLevel == null) {
481
			WMTSDataParameters p = (WMTSDataParameters)parameters;
482
			WMTSTileMatrixSetLink tileMatrixSetLink = getTileMatrixSetLink();
483
			WMTSTileMatrixSet tileMatrixSet = tileMatrixSetLink.getTileMatrixSet();
484
			
485
	    	double widthMtsTile = 0;
486
	    	double heightMtsTile = 0;
487
			ArrayList<?> tileMatrixList = tileMatrixSet.getTileMatrix();
488
			extentByLevel = new ExtentImpl[tileMatrixList.size()];
489
			for (int i = 0; i < tileMatrixList.size(); i++) {
490
				WMTSTileMatrix tileMatrix = (WMTSTileMatrix)tileMatrixList.get(i);
491
		    	if(!p.isProjected()) {
492
		    		widthMtsTile = (tileMatrix.getScaleDenominator() * tileMatrix.getTileWidth() * 0.28) / (MTS_X_GRADO * 1000);
493
		    		heightMtsTile = (tileMatrix.getScaleDenominator() * tileMatrix.getTileWidth() * 0.28) / (MTS_X_GRADO * 1000);
494
		    	} else {
495
		    		widthMtsTile = (tileMatrix.getScaleDenominator() * tileMatrix.getTileWidth() * 0.28) / 1000;
496
		    		heightMtsTile = (tileMatrix.getScaleDenominator() * tileMatrix.getTileWidth() * 0.28) / 1000;
497
		    	}
498
		    	
499
		    	double h = Math.abs(tileMatrix.getTopLeftCorner()[0] - (tileMatrix.getTopLeftCorner()[0] - (tileMatrix.getMatrixHeight() * heightMtsTile)));
500
		    	Rectangle2D r = new Rectangle2D.Double(
501
		    			tileMatrix.getTopLeftCorner()[1], 
502
		    			tileMatrix.getTopLeftCorner()[0] - h,
503
		    			Math.abs(tileMatrix.getTopLeftCorner()[1] - (tileMatrix.getTopLeftCorner()[1] + (tileMatrix.getMatrixWidth() * widthMtsTile))),
504
		    			h);
505
		    	extentByLevel[i] = new ExtentImpl(r);
506
			}
507
			
508
		}
509
		return extentByLevel;
510
	}
511
	
512
	/*
513
	 * (non-Javadoc)
514
	 * @see org.gvsig.raster.impl.provider.RemoteRasterProvider#getLayerExtent(java.lang.String, java.lang.String)
515
	 */
516
	public Rectangle2D getLayerExtent(String layerName, String srs) throws RemoteServiceException {
517
		return null;
518
	}
519

  
520
	/*
521
	 * (non-Javadoc)
522
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#load()
523
	 */
524
	public RasterProvider load() {
525
		return this;
526
	}
527
	
528
	/*
529
	 * (non-Javadoc)
530
	 * @see org.gvsig.raster.impl.provider.RasterProvider#isOpen()
531
	 */
532
	public boolean isOpen() {
533
		return open;
534
	}
535

  
536
	/*
537
	 * (non-Javadoc)
538
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
539
	 */
540
	public void close() {
541
		open = false;
542
	}
543
	
544
	/*
545
	 * (non-Javadoc)
546
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
547
	 */
548
	public Transparency getTransparency() {
549
		if(lastFileTransparency == null) {
550
			lastFileTransparency = new DataStoreTransparency();
551
			lastFileTransparency.setTransparencyBand(3);
552
		}
553
		return lastFileTransparency;
554
	}
555

  
556
	/*
557
	 * (non-Javadoc)
558
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#translateFileName(java.lang.String)
559
	 */
560
	public String translateFileName(String fileName) {
561
		return fileName;
562
	}
563

  
564
	/*
565
	 * (non-Javadoc)
566
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setView(org.gvsig.fmap.dal.coverage.datastruct.Extent)
567
	 */
568
	public void setView(Extent e) {
569
		viewRequest = e;
570
	}
571

  
572
	/*
573
	 * (non-Javadoc)
574
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
575
	 */
576
	public Extent getView() {
577
		return viewRequest;
578
	}
579

  
580
	/*
581
	 * (non-Javadoc)
582
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
583
	 */
584
	public double getWidth() {
585
		WMTSDataParameters p = (WMTSDataParameters)parameters;
586
		if (lastWidthRequest <= 0) 
587
			return p.getWidth();
588
		return lastWidthRequest;
589
	}
590

  
591
	/*
592
	 * (non-Javadoc)
593
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
594
	 */
595
	public double getHeight() {
596
		WMTSDataParameters p = (WMTSDataParameters)parameters;
597
		if (lastHeightRequest <= 0) 
598
			return p.getHeight();
599
		return lastHeightRequest;
600
	}
601

  
602
	/*
603
	 * (non-Javadoc)
604
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#readCompleteLine(int, int)
605
	 */
606
	public Object readCompleteLine(int line, int band)
607
		throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
608
		return null;
609
	}
610
	
611
	/**
612
	 * When the remote layer has fixed size this method downloads the file and return its reference. 
613
	 * File layer has in the long side FIXED_SIZE pixels and the bounding box is complete. This file could be
614
	 * useful to build an histogram or calculate statistics. This represents a sample of data.
615
	 * @return
616
	 * @throws RasterDriverException
617
	 */
618
	public File getFileLayer() throws RasterDriverException {
619
		return null;
620
	}
621

  
622
	/**
623
	 * Reads a complete block of data and returns an tridimensional array of the right type. This function is useful
624
	 * to read a file very fast without setting a view. In a WMTS service when the size is fixed then it will read the
625
	 * entire image but when the source hasn't pixel size it will read a sample of data. This set of data will have
626
	 * the size defined in FIXED_SIZE. 
627
	 * 
628
	 * @param pos Posici?n donde se empieza  a leer
629
	 * @param blockHeight Altura m?xima del bloque leido
630
	 * @return Object que es un array tridimendional del tipo de datos del raster. (Bandas X Filas X Columnas)
631
	 * @throws InvalidSetViewException
632
	 * @throws FileNotOpenException
633
	 * @throws RasterDriverException
634
	 */
635
	public Object readBlock(int pos, int blockHeight, double scale) 
636
	throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
637
		return null;
638
	}
639
	
640
	/*
641
	 * (non-Javadoc)
642
	 * @see org.gvsig.raster.impl.provider.RemoteRasterProvider#getLastRequestHeight()
643
	 */
644
	public double getLastRequestHeight() throws RasterDriverException {
645
		return 0;
646
	}
647
	
648
	/*
649
	 * (non-Javadoc)
650
	 * @see org.gvsig.raster.impl.provider.RemoteRasterProvider#getLastRequestWidth()
651
	 */
652
	public double getLastRequestWidth() throws RasterDriverException {
653
		return 0;
654
	}
655

  
656
	/*
657
	 * (non-Javadoc)
658
	 * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getData(int, int, int)
659
	 */
660
	public Object getData(int x, int y, int band)
661
		throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
662
		return null;
663
	}
664
	
665
	/**
666
	 * Assigns the list of bands RGB and read a window of data
667
	 * @param rasterBuf
668
	 * @param bandList
669
	 * @param lastFile
670
	 * @param ulx
671
	 * @param uly
672
	 * @param lrx
673
	 * @param lry
674
	 * @return
675
	 * @throws RasterDriverException
676
	 * @throws ProcessInterruptedException
677
	 */
678
	public Buffer getBuffer(Buffer rasterBuf, BandList bandList, File lastFile, 
679
			double ulx, double uly, double lrx, double lry) throws RasterDriverException, ProcessInterruptedException {
680
		return null;
681
	}
682
	
683
	/**
684
	 * Gets the tile matrix from the selected level
685
	 * @param level
686
	 * @return
687
	 */
688
	@SuppressWarnings("unchecked")
689
	private WMTSTileMatrix getTileMatrixByLevel(int level) {
690
		level = adjustLevel(level);
691
		
692
		WMTSTileMatrixSetLink tileMatrixSetLink = getTileMatrixSetLink();
693
		WMTSTileMatrixSet tileMatrixSet = tileMatrixSetLink.getTileMatrixSet();
694
		ArrayList tileMatrixSetLimits = tileMatrixSetLink.getTileMatrixLimits();
695
		
696
		WMTSTileMatrixLimits tileMatrixLimits = null;
697
		WMTSTileMatrix tileMatrix = (WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level);
698
		if(hasGridSubsets()) {
699
			tileMatrixLimits = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level);
700
			tileMatrix = tileMatrixLimits.getTileMatrix();
701
		}
702
		return tileMatrix;
703
	}
704
	
705
	/**
706
	 * Returns the number of levels
707
	 * @return
708
	 */
709
	@SuppressWarnings("unchecked")
710
	public int getZoomLevels() {
711
		WMTSTileMatrixSetLink tileMatrixSetLink = getTileMatrixSetLink();
712
		WMTSTileMatrixSet tileMatrixSet = tileMatrixSetLink.getTileMatrixSet();
713
		if(hasGridSubsets()) {
714
			ArrayList tileMatrixSetLimits = tileMatrixSetLink.getTileMatrixLimits();
715
			return Math.min(tileMatrixSet.getTileMatrix().size(), tileMatrixSetLimits.size());
716
		}
717
		return tileMatrixSet.getTileMatrix().size();
718
		
719
	}
720
	
721
	/*
722
	 * (non-Javadoc)
723
	 * @see org.gvsig.raster.impl.provider.RasterProvider#getCoordsInTheNearestLevel(org.gvsig.fmap.dal.coverage.datastruct.Extent, int, int)
724
	 */
725
	public Extent getCoordsInTheNearestLevel(Extent extent, int w, int h) {
726
		double[] pixelSizes = getPixelSizeByLevel();
727
		double currentPixelSize = extent.width() / (double)w;
728
		
729
		int level = 0;
730
		for (int i = 0; i < (pixelSizes.length - 1); i++) {
731
			if(currentPixelSize < pixelSizes[i] && currentPixelSize >= pixelSizes[i + 1]) {
732
				level = i + 1;
733
				break;
734
			}
735
		}
736
		
737
		return getZoomLevelCoordinates(level, extent, w, h);
738
	}
739
	
740
	/*
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff