Revision 3162

View differences:

org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
    <modelVersion>4.0.0</modelVersion>
4
    <artifactId>org.gvsig.raster.wmts</artifactId>
5
    <packaging>pom</packaging>
6
    <version>2.2.5</version>
7
    <name>${project.artifactId}</name>
8
    <description>WMTS client</description>
9
    <inceptionYear>2011</inceptionYear>
10
	
11
    <parent>
12
        <groupId>org.gvsig</groupId>
13
        <artifactId>org.gvsig.desktop</artifactId>
14
        <version>2.0.85</version>
15
    </parent>
16
    
17
    <repositories>
18
        <repository>
19
            <id>gvsig-public-http-repository</id>
20
            <name>gvSIG maven public HTTP repository</name>
21
            <url>http://devel.gvsig.org/m2repo/j2se</url>
22
            <releases>
23
                <enabled>true</enabled>
24
                <updatePolicy>daily</updatePolicy>
25
                <checksumPolicy>warn</checksumPolicy>
26
            </releases>
27
            <snapshots>
28
                <enabled>true</enabled>
29
                <updatePolicy>daily</updatePolicy>
30
                <checksumPolicy>warn</checksumPolicy>
31
            </snapshots>
32
        </repository>
33
    </repositories>
34
    
35
    <scm>
36
        <connection>scm:svn:https://devel.gvsig.org/svn/gvsig-raster/org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5</connection>
37
        <developerConnection>scm:svn:https://devel.gvsig.org/svn/gvsig-raster/org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5</developerConnection>
38
        <url>https://devel.gvsig.org/redmine/projects/gvsig-raster/repository/show/org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5</url>
39
    </scm>
40
    
41
    <dependencyManagement>
42
        <dependencies>
43
                        <dependency>
44
                                <groupId>org.gvsig</groupId>
45
                                <artifactId>org.gvsig.raster</artifactId>
46
                                <version>2.2.10</version>
47
                                <type>pom</type>
48
                                <scope>import</scope>
49
                        </dependency>
50
		
51
            <dependency>
52
                <groupId>org.gvsig</groupId>
53
                <artifactId>org.gvsig.raster.wmts.swing.api</artifactId>
54
                <version>2.2.5</version>
55
            </dependency>
56
            <dependency>
57
                <groupId>org.gvsig</groupId>
58
                <artifactId>org.gvsig.raster.wmts.swing.impl</artifactId>
59
                <version>2.2.5</version>
60
            </dependency>
61
            <dependency>
62
                <groupId>org.gvsig</groupId>
63
                <artifactId>org.gvsig.raster.wmts.ogc.api</artifactId>
64
                <version>2.2.5</version>
65
            </dependency>
66
            <dependency>
67
                <groupId>org.gvsig</groupId>
68
                <artifactId>org.gvsig.raster.wmts.ogc.impl</artifactId>
69
                <version>2.2.5</version>
70
            </dependency>
71
            <dependency>
72
                <groupId>org.gvsig</groupId>
73
                <artifactId>org.gvsig.raster.wmts.app.wmtsclient</artifactId>
74
                <version>2.2.5</version>
75
            </dependency>
76
            <dependency>
77
                <groupId>org.gvsig</groupId>
78
                <artifactId>org.gvsig.raster.wmts.io</artifactId>
79
                <version>2.2.5</version>
80
            </dependency>
81

  
82
        </dependencies>
83
    </dependencyManagement>
84
	
85
    <developers>
86
        <developer>
87
            <id>nbrodin</id>
88
            <name>Nacho Brodin</name>
89
            <email>nachobrodin@gmail.com</email>
90
            <roles>
91
                <role>Architect</role>
92
                <role>Developer</role>
93
            </roles>
94
        </developer>
95
    </developers>
96
	
97
    <build>
98
        <plugins>
99
            <plugin>
100
                <groupId>org.apache.maven.plugins</groupId>
101
                <artifactId>maven-release-plugin</artifactId>
102
                <configuration>
103
                    <tagBase>https://devel.gvsig.org/svn/gvsig-raster/org.gvsig.raster.wmts/tags/</tagBase>
104
                    <goals>deploy</goals>
105
                </configuration>
106
            </plugin>
107
        </plugins>
108
    </build>
109
    
110
    <modules>
111
        <module>org.gvsig.raster.wmts.swing</module>
112
        <module>org.gvsig.raster.wmts.io</module>
113
        <module>org.gvsig.raster.wmts.app.wmtsclient</module>
114
        <module>org.gvsig.raster.wmts.ogc</module>
115
    </modules>
116
</project>
0 117

  
org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5/org.gvsig.raster.wmts.io/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.raster.wmts.io.DefaultWMTSIOLibrary
org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5/org.gvsig.raster.wmts.io/src/main/java/org/gvsig/raster/wmts/io/downloader/TileDownloaderForWMTS.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.downloader;
23

  
24
import java.io.File;
25
import java.io.IOException;
26
import java.net.ConnectException;
27
import java.net.URL;
28

  
29
import org.gvsig.compat.net.ICancellable;
30
import org.gvsig.fmap.dal.coverage.exception.RemoteServiceException;
31
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
32
import org.gvsig.raster.cache.tile.Tile;
33
import org.gvsig.raster.cache.tile.exception.TileGettingException;
34
import org.gvsig.raster.impl.provider.tile.BaseTileDownloader;
35
import org.gvsig.raster.wmts.io.WMTSDataParameters;
36
import org.gvsig.raster.wmts.ogc.WMTSClient;
37
import org.gvsig.raster.wmts.ogc.WMTSOGCLocator;
38
import org.gvsig.raster.wmts.ogc.WMTSStatus;
39
import org.gvsig.raster.wmts.ogc.exception.ServerErrorException;
40
import org.gvsig.raster.wmts.ogc.exception.WMTSException;
41

  
42
/** 
43
 * Tile getter 
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public class TileDownloaderForWMTS extends BaseTileDownloader {
47
	private WMTSClient             ogcClient  = null;
48
	
49
	public TileDownloaderForWMTS(RasterDataStore store, 
50
			int tilePxWidth,
51
			int tilePxHeight) {
52
		super(store, tilePxWidth, tilePxHeight);
53
	}
54
	
55
	/**
56
	 * Gets the connector from the URL
57
	 * @return
58
	 * @throws RemoteServiceException
59
	 */
60
	public WMTSClient getOGCClient() throws WMTSException {
61
		if(ogcClient == null) {
62
			WMTSDataParameters p = (WMTSDataParameters)store.getParameters();
63
			ogcClient = p.getOGCClient();
64
			if(ogcClient != null)
65
				return ogcClient;
66
			
67
			URL url = null;
68
			try {
69
				url = new URL(p.getURI());
70
			} catch (Exception e) {
71
				throw new WMTSException("Malformed URL",e);
72
			}
73
			try {
74
				ogcClient = WMTSOGCLocator.getManager().createWMTSClient(url.toString());
75
				ogcClient.connect(false, new ICancellable() {
76
					public boolean isCanceled() {
77
						return false;
78
					}
79
					
80
					public Object getID() {
81
						return null;
82
					}
83
				});
84
			} catch (ConnectException e) {
85
				throw new WMTSException("Connect exception",e);
86
			} catch (IOException e) {
87
				throw new WMTSException("Connect exception",e);
88
			}
89
		}
90
		return ogcClient;
91
	}
92
	
93
	public synchronized Tile downloadTile(Tile tile) throws TileGettingException {
94
		try {
95
			WMTSStatus status = (WMTSStatus)tile.getDownloaderParams("WMTSStatus");
96
			status.setTileRow(tile.getRow());
97
			status.setTileCol(tile.getCol());
98
			
99
			String urlFromTemplate = status.getResourceURL(tile.getRow() + "" + tile.getCol() + "");
100
			//System.out.println(urlFromTemplate);
101
			File f = null;
102
			if(urlFromTemplate != null)
103
				f = getOGCClient().getTile(urlFromTemplate, tile.getCancelled(), tile.getFile());
104
			else
105
				f = getOGCClient().getTile(status, tile.getCancelled(), tile.getFile());
106
			tile.setFile(f);
107
			//Si borramos el rmf no se puede leer la etiqueta Alpha. En caso de que se modifique jgdal para
108
			//poder guardar esta etiqueta deberiamos borrar el rmf para ahorrar ficheros
109
			//File rmf = new File(tile.getFile().getAbsolutePath() + ".rmf");
110
			//if(rmf.exists())
111
				//rmf.delete();
112
		} catch (WMTSException e) {
113
			throw new TileGettingException(e);
114
		} catch (ServerErrorException e) {
115
			throw new TileGettingException(e);
116
		}
117
		readTileFromDisk(tile);
118
		return tile;
119
	}
120
	
121
}
0 122

  
org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5/org.gvsig.raster.wmts.io/src/main/java/org/gvsig/raster/wmts/io/downloader/WMTSTileServer.java
1
package org.gvsig.raster.wmts.io.downloader;
2

  
3
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
4
import org.gvsig.raster.cache.tile.provider.CacheStruct;
5
import org.gvsig.raster.cache.tile.provider.Downloader;
6
import org.gvsig.raster.cache.tile.provider.TileServer;
7
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrix;
8
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSetLink;
9

  
10
/** 
11
* Data server for the tile cache in a WMTSProvider 
12
* @author Nacho Brodin (nachobrodin@gmail.com)
13
*/
14
public class WMTSTileServer implements TileServer {
15
	private CacheStruct                struct               = null;
16
	private Downloader                 downloader           = null;
17
	private RasterDataStore            store                = null;
18
	private String                     suffix               = ".tif";
19
	private WMTSTileMatrixSetLink      tileMatrixSetLink    = null;
20
	
21
	public WMTSTileServer(RasterDataStore store, 
22
			WMTSTileMatrixSetLink tileMatrixSetLink) {
23
		this.store = store;
24
		this.tileMatrixSetLink = tileMatrixSetLink;
25
	}
26
	
27
	public Downloader getDownloader() {
28
		//if(downloader == null) {
29
			int tileWidth = ((WMTSTileMatrix)tileMatrixSetLink.getTileMatrixSet().getTileMatrix().get(0)).getTileWidth();
30
			int tileHeight = ((WMTSTileMatrix)tileMatrixSetLink.getTileMatrixSet().getTileMatrix().get(0)).getTileHeight();
31
			downloader = new TileDownloaderForWMTS(store, tileWidth, tileHeight);
32
		//}
33
		return downloader;
34
	}
35

  
36
	public CacheStruct getStruct() {
37
		if(struct == null) {
38
			struct = new WMTSCacheStruct(store, tileMatrixSetLink);
39
		}
40
		return struct;
41
	}
42
	
43
	public void setStruct(CacheStruct struct) {
44
		//La structura de cache es proporcionada por el servidor
45
	}
46
	
47
	public String getFileSuffix() {
48
		return suffix;
49
	}
50
	
51
	public void setFileSuffix(String extension) {
52
		this.suffix = extension;
53
	}
54
}
0 55

  
org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5/org.gvsig.raster.wmts.io/src/main/java/org/gvsig/raster/wmts/io/downloader/WMTSCacheStruct.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.downloader;
23

  
24
import java.awt.geom.Point2D;
25
import java.awt.geom.Rectangle2D;
26
import java.util.ArrayList;
27
import java.util.List;
28

  
29
import org.gvsig.fmap.dal.coverage.RasterLocator;
30
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
31
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
32
import org.gvsig.fmap.dal.coverage.util.MathUtils;
33
import org.gvsig.raster.cache.tile.Tile;
34
import org.gvsig.raster.cache.tile.TileCacheLocator;
35
import org.gvsig.raster.cache.tile.exception.TileBuildException;
36
import org.gvsig.raster.cache.tile.provider.CacheStruct;
37
import org.gvsig.raster.wmts.io.WMTSDataParameters;
38
import org.gvsig.raster.wmts.io.WMTSProvider;
39
import org.gvsig.raster.wmts.ogc.WMTSStatus;
40
import org.gvsig.raster.wmts.ogc.struct.WMTSTile;
41
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrix;
42
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixLimits;
43
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSet;
44
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSetLink;
45

  
46
/**
47
 * Implementation for a structure of a cache
48
 * @author Nacho Brodin (nachobrodin@gmail.com)
49
 */
50
public class WMTSCacheStruct implements CacheStruct {
51
    private String                        layerName           = null;
52
	private String                        serverURL           = null;
53
	private WMTSTileMatrixSet             tileMatrixSet       = null;
54
	private List<WMTSTileMatrixLimits>    tileMatrixSetLimits = null;
55
	private double[]                      pixelSize           = null;
56
    private RasterDataStore               store               = null;
57
    
58
    public WMTSCacheStruct(RasterDataStore store, WMTSTileMatrixSetLink tileMatrixSetLink) {
59
    	this.store = store;
60
    	this.tileMatrixSet = tileMatrixSetLink.getTileMatrixSet();
61
		this.tileMatrixSetLimits = tileMatrixSetLink.getTileMatrixLimits();
62
		pixelSize = ((WMTSProvider)store.getProvider()).getPixelSizeByLevel();
63
		serverURL = ((WMTSProvider)store.getProvider()).getURIOfFirstProvider();
64
		layerName = ((WMTSDataParameters)store.getParameters()).getLayer().getTitle();
65
    }
66

  
67
	public int getNumberOfLevels() {
68
		return tileMatrixSet.getTileMatrix().size();
69
	}
70

  
71
	public String getLayerName() {
72
		return layerName;
73
	}
74

  
75
	public String getServerURL() {
76
		return serverURL + getDimension();
77
	}
78

  
79
	public int[] getTileSizeByLevel(int level) {
80
		return new int[] {
81
			((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileWidth(),
82
			((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileHeight()
83
		};
84
	}
85

  
86
	public int getLayerWidthOfTileMatrixByLevel(int level) {
87
		if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) {
88
			WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level);
89
			return l.getMaxTileRow() - l.getMinTileRow();
90
		} else {
91
			WMTSTileMatrix tm = (WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level);
92
			return (int)tm.getMatrixWidth();
93
		}
94
	}
95

  
96
	public int getLayerHeightOfTileMatrixByLevel(int level) {
97
		if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) {
98
			WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level);
99
			return l.getMaxTileCol() - l.getMinTileCol();
100
		} else {
101
			WMTSTileMatrix tm = (WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level);
102
			return (int)tm.getMatrixHeight();
103
		}
104
	}
105

  
106
	public int getLayerInitXTilePositionByLevel(int level) {
107
		if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) {
108
			WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level);
109
			return l.getMinTileCol();
110
		} else
111
			return 0;
112
	}
113

  
114
	public int getLayerInitYTilePositionByLevel(int level) {
115
		if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) {
116
			WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level);
117
			return l.getMinTileRow();
118
		} else 
119
			return 0;
120
	}
121

  
122
	public long getWorldHeightOfTileMatrixByLevel(int level) {
123
		return ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getMatrixWidth();
124
	}
125

  
126
	public long getWorldWidthOfTileMatrixByLevel(int level) {
127
		return ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getMatrixHeight();
128
	}
129

  
130
	public double getPixelSizeByLevel(int level) {
131
		if(level < pixelSize.length)
132
			return pixelSize[level];
133
		else
134
			return pixelSize[pixelSize.length - 1];
135
	}
136

  
137
	public Point2D[] getTileExtent(Tile tile) {
138
		return getTileExtent(tile.getLevel(), tile.getCol(), tile.getRow());
139
	}
140

  
141
	public Point2D[] getTileExtent(int level, int col, int row) {
142
		double[] ul = tileMatrixSet.getBoundingBox().getUpperCorner();
143
		long tileWidth = ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileWidth();
144
		long tileHeight = ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileHeight();
145
		double mtsWidthTile = tileWidth * pixelSize[level];
146
		double mtsHeightTile = tileHeight * pixelSize[level];
147
		double ulx = ul[0] + (col * mtsWidthTile);
148
		double uly = ulx + mtsWidthTile;
149
		double lrx = ul[1] - (row * mtsHeightTile);
150
		double lry = lrx - mtsHeightTile;
151
		return new Point2D[]{new Point2D.Double(ulx, uly), new Point2D.Double(lrx, lry)};
152
	}
153

  
154
	public Tile getTileStructure(int level, int tileCol, int tileRow, Point2D ul, Point2D lr) throws TileBuildException  {
155
		int[] size = getTileSizeByLevel(level);
156
		Tile tile = TileCacheLocator.getManager().createTile(level, tileRow, tileCol);
157
		tile.setUl(ul);
158
		tile.setLr(lr);
159
		tile.setWidthPx(size[0]);
160
		tile.setHeightPx(size[1]);
161
		
162
		Rectangle2D r = new Rectangle2D.Double(Math.min(ul.getX(), lr.getX()), 
163
				Math.min(ul.getY(), lr.getY()), 
164
				Math.abs(ul.getX() - lr.getX()), 
165
				Math.abs(ul.getY() - lr.getY()));
166
		int bufWidth = (int)(r.getWidth() / getPixelSizeByLevel(level));
167
		int bufHeight = (int)(r.getHeight() / getPixelSizeByLevel(level));
168
		try {
169
			WMTSStatus status = ((WMTSProvider)store.getProvider()).buildWMTSStatus(r, bufWidth, bufHeight);
170
			tile.setDownloaderParams("WMTSStatus", status);
171
		} catch (RasterDriverException e) {
172
			throw new TileBuildException(e); 
173
		}
174
			
175
		return tile;
176
	}
177
	
178
	public List<Tile> getTileList(Point2D ul, Point2D lr, double mtsPixelRequest) throws TileBuildException {
179
		Rectangle2D r = new Rectangle2D.Double(Math.min(ul.getX(), lr.getX()), 
180
				Math.min(ul.getY(), lr.getY()), 
181
				Math.abs(ul.getX() - lr.getX()), 
182
				Math.abs(ul.getY() - lr.getY()));
183
		int bufWidth = (int)(r.getWidth() / mtsPixelRequest);
184
		int bufHeight = (int)(r.getHeight() / mtsPixelRequest);
185
		try {
186
			WMTSStatus status = ((WMTSProvider)store.getProvider()).buildWMTSStatus(r, bufWidth, bufHeight);
187
			
188
			//Conversi?n a tiles de la librer?a
189
			List<WMTSTile> tiles = status.getTileList();
190
			List<Tile> tileList = new ArrayList<Tile>();
191
			for (int i = 0; i < tiles.size(); i++) {
192
				WMTSTile tOrigin = (WMTSTile) tiles.get(i);
193
				Tile t = TileCacheLocator.getManager().createTile(status.getLevel(), tOrigin.getCol(), tOrigin.getRow());
194
				t.setUl(new Point2D.Double(tOrigin.getULX(), tOrigin.getULY()));
195
				t.setLr(new Point2D.Double(tOrigin.getLRX(), tOrigin.getLRY()));
196
				t.setWidthPx(tOrigin.getWidthPx());
197
				t.setHeightPx(tOrigin.getHeightPx());
198
				t.setDownloaderParams("WMTSStatus", status.cloneStatus());
199
				tileList.add(t);
200
			}
201
			return tileList;
202
		} catch (RasterDriverException e) {
203
			throw new TileBuildException(e); 
204
		}
205
	}
206

  
207
	public double[] getTileSizeInRealCoordsByLevel(int level) {
208
		return new double[] {
209
				getPixelSizeByLevel(level) * getTileSizeByLevel(level)[0],
210
				getPixelSizeByLevel(level) *  getTileSizeByLevel(level)[1]
211
			};
212
	}
213

  
214
	public String getFileSuffix() {
215
		return ((WMTSProvider)store.getProvider()).getFileSuffix();
216
	}
217

  
218
	public boolean compare(CacheStruct struct) {
219
		MathUtils mu = RasterLocator.getManager().getMathUtils();
220
		//Compara: n?mero de niveles, tama?o de tile, 
221
		//anchoXalto de la matriz, tama?o de pixel por nivel, 
222
		//coordenadas de al menos un tile de la matriz
223
		if(struct.getNumberOfLevels() == getNumberOfLevels()) {
224
			for (int i = 0; i < getNumberOfLevels(); i++) {
225
				if(	struct.getTileSizeByLevel(i)[0] == getTileSizeByLevel(i)[0] && 
226
					struct.getTileSizeByLevel(i)[1] == getTileSizeByLevel(i)[1] &&
227
					struct.getWorldHeightOfTileMatrixByLevel(i) == getWorldHeightOfTileMatrixByLevel(i) &&
228
					struct.getWorldWidthOfTileMatrixByLevel(i) == getWorldWidthOfTileMatrixByLevel(i) &&
229
					mu.clipDecimals(struct.getPixelSizeByLevel(i), 2) == mu.clipDecimals(getPixelSizeByLevel(i), 2) &&
230
					compareExtents(struct.getTileExtent(i, 0, 0), getTileExtent(i, 0, 0))) {
231
					return true;
232
				}
233
			}
234
		}
235
		return false;
236
	}
237
	
238
	private boolean compareExtents(Point2D[] p, Point2D[] p1) {
239
		return (p[0].getX() == p1[0].getX() && p[0].getY() == p1[0].getY() &&
240
				p[1].getX() == p1[1].getX() && p[1].getY() == p1[1].getY());
241
	}
242

  
243
	public String getEPSG() {
244
		/*IProjection proj = provider.getProjection();
245
		if(proj != null)
246
			return proj.getAbrev();
247
		return null;*/
248
		return ((WMTSProvider)store.getProvider()).getSRSCode();
249
	}
250
	
251
	private String getDimension() {
252
		WMTSDataParameters p = (WMTSDataParameters)(((WMTSProvider)store.getProvider()).getDataParameters());
253
		String dimension = p.getDimension();
254
		String dimensionValue = p.getDimensionSelectedValue();
255
		if(dimension != null && dimensionValue != null)
256
			return dimension + "_" + dimensionValue;
257
		return "";
258
	}
259

  
260
	public String getFileSize() {
261
		return "0";
262
	}
263

  
264
	public List<Tile> getTileList(int x, int y, int w, int h) {
265
		return null;
266
	}
267

  
268
	public ArrayList<Tile> getTileList(Rectangle2D r) {
269
		return null;
270
	}
271
}
0 272

  
org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5/org.gvsig.raster.wmts.io/src/main/java/org/gvsig/raster/wmts/io/package.html
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head>
5
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6
<title>org.gvsig.raster.wmts.io package documentation</title>
7
</head>
8
<body>
9

  
10
	<p>WMTS provider</p>
11

  
12
</body>
13
</html>
0 14

  
org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5/org.gvsig.raster.wmts.io/src/main/java/org/gvsig/raster/wmts/io/WMTSServerExplorer.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

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

  
28
package org.gvsig.raster.wmts.io;
29

  
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.net.MalformedURLException;
33
import java.net.URL;
34
import java.net.URLConnection;
35
import java.util.List;
36

  
37
import org.gvsig.compat.net.ICancellable;
38
import org.gvsig.fmap.dal.DALLocator;
39
import org.gvsig.fmap.dal.DataManager;
40
import org.gvsig.fmap.dal.DataServerExplorerParameters;
41
import org.gvsig.fmap.dal.DataStoreParameters;
42
import org.gvsig.fmap.dal.NewDataStoreParameters;
43
import org.gvsig.fmap.dal.coverage.exception.ConnectException;
44
import org.gvsig.fmap.dal.coverage.store.RasterDataServerExplorer;
45
import org.gvsig.fmap.dal.coverage.store.parameter.TileDataParameters;
46
import org.gvsig.fmap.dal.exception.DataException;
47
import org.gvsig.fmap.dal.exception.InitializeException;
48
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
49
import org.gvsig.fmap.dal.spi.DataServerExplorerProvider;
50
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
51
import org.gvsig.raster.wmts.ogc.WMTSClient;
52
import org.gvsig.raster.wmts.ogc.WMTSOGCLocator;
53

  
54
/**
55
 * Explorer for a WMTS server
56
 * @author Nacho Brodin (nachobrodin@gmail.com)
57
 */
58
public class WMTSServerExplorer implements RasterDataServerExplorer, DataServerExplorerProvider {
59
	public static final String           NAME                     = WMTSProvider.NAME;
60
	private WMTSClient                   ogcClient                = null;
61
	private WMTSServerExplorerParameters parameters               = null;
62
	
63
	public WMTSServerExplorer(
64
			WMTSServerExplorerParameters parameters,
65
			DataServerExplorerProviderServices services)
66
			throws InitializeException {
67
		this.parameters = parameters;
68
	}
69
	
70
	public String getDataStoreProviderName() {
71
		return WMTSProvider.NAME;
72
	}
73
	
74
	public String getDescription() {
75
		return WMTSProvider.DESCRIPTION;
76
	}
77
	
78
	public boolean add(String provider, NewDataStoreParameters parameters,
79
			boolean overwrite) throws DataException {
80
		return false;
81
	}
82

  
83
	public boolean canAdd() {
84
		return false;
85
	}
86

  
87
	public boolean canAdd(String storeName) throws DataException {
88
		return false;
89
	}
90

  
91
	public NewDataStoreParameters getAddParameters(String storeName)
92
			throws DataException {
93
		return null;
94
	}
95

  
96
	public List<?> getDataStoreProviderNames() {
97
		return null;
98
	}
99

  
100
	public DataServerExplorerParameters getParameters() {
101
		return parameters;
102
	}
103

  
104
	public List<?> list() throws DataException {
105
		return null;
106
	}
107

  
108
	public List<?> list(int mode) throws DataException {
109
		return null;
110
	}
111

  
112
	public void remove(DataStoreParameters parameters) throws DataException {
113
		
114
	}
115

  
116
	public void dispose() {
117
		
118
	}
119

  
120
	public String getProviderName() {
121
		return WMTSProvider.NAME;
122
	}
123

  
124
	//**********************************************
125
	//Connector
126
	//**********************************************
127
	
128
	public DataStoreParameters getStoredParameters() {
129
		DataManager manager = DALLocator.getDataManager();
130
		WMTSDataParameters wmtsParams = null;
131
		try {
132
			wmtsParams = (WMTSDataParameters) manager.createStoreParameters(this.getDataStoreProviderName());
133
			wmtsParams.setURI(parameters.getHost());
134
			wmtsParams.setOGCClient(getOGCClient());
135

  
136
			if(WMTSProvider.TILED) {
137
				TileDataParameters tileParams = (TileDataParameters) manager.createStoreParameters("Tile Store");
138
				tileParams.setDataParameters(wmtsParams);
139
				return tileParams;
140
			} 
141
		} catch (InitializeException e) {
142
			e.printStackTrace();
143
		} catch (ProviderNotRegisteredException e) {
144
			e.printStackTrace();
145
		}
146
		return wmtsParams;
147
	}
148
	
149
	public void connect(ICancellable cancellable) throws ConnectException {
150
		URL url = null;
151
		boolean override = false;
152
		
153
		try {
154
			url = new URL(parameters.getHost());
155
		} catch (Exception e) {
156
			throw new ConnectException("Malformed URL",e);
157
		}
158
        try {
159
        	ogcClient = WMTSOGCLocator.getManager().createWMTSClient(url.toString());
160
        	ogcClient.setForceChangeAxisOrder(parameters.isLongitudeFirst());
161
        	if (!ogcClient.connect(override, cancellable))
162
        		throw new ConnectException("Error connecting");
163
        } catch (IOException e) {
164
			throw new ConnectException(e.getMessage(), e);
165
		}
166
	}
167
	
168
	public boolean isHostReachable(int timeout) {
169
		URL url = null;
170
		URLConnection con = null;
171
		try {
172
			url = new URL(parameters.getHost());
173
			con = url.openConnection();
174
			if(con == null)
175
				return false;
176
			con.connect();
177
		} catch (MalformedURLException e) {
178
			return false;
179
		} catch (IOException e) {
180
			return false;
181
		}
182
		
183
		InputStream stream = null;
184
		try {
185
			stream = con.getInputStream();
186
			if(stream == null)
187
				return false;
188
		} catch (IOException e) {
189
		}
190
		
191
		return true;
192
	}
193

  
194
	/**
195
	 * Checks if the network and host are reachable
196
	 * @return true if both are reachable and false if they are not
197
	 */
198
	public boolean isHostReachable() {
199
		return isHostReachable(RasterDataServerExplorer.TIME);
200
	}
201

  
202
	public boolean isConnected() {
203
		if(ogcClient != null)
204
			return true;
205
		return false;
206
	}
207
	
208
	public WMTSClient getOGCClient() {
209
		return ogcClient;
210
	}
211

  
212
	public String getHost() {
213
		return parameters.getHost();
214
	}
215

  
216
	public DataServerExplorerProviderServices getServerExplorerProviderServices() {
217
		return null;
218
	}
219
}
0 220

  
org.gvsig.raster.wmts/tags/org.gvsig.raster.wmts-2.2.5/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.Image;
25
import java.awt.Rectangle;
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.NoninvertibleTransformException;
28
import java.awt.geom.Point2D;
29
import java.awt.geom.Rectangle2D;
30
import java.io.File;
31
import java.io.IOException;
32
import java.net.ConnectException;
33
import java.net.MalformedURLException;
34
import java.net.URL;
35
import java.util.List;
36

  
37
import javax.swing.ImageIcon;
38

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

  
190
		public RequestTileLauncher(TilePipe pipe, WMTSStatus status, WMTSTile tile) {
191
			this.pipe = pipe;
192
			this.status = status;
193
			this.tile = tile;
194
		}
195

  
196
		public void run() {
197
			try {
198
				//File file = getConnector().getTile(status, null);
199
				URL url = getOGCClient().getTileURL(status);
200
				tile.setFile(getOGCClient().downloadFile(url, null));
201
				pipe.setTile(tile);
202
			} catch (DownloadException e) {
203
				logger.info("Error downloading files", e);
204
			} catch (MalformedURLException e) {
205
				logger.info("Malformed URL", e);
206
			} catch (WMTSException e) {
207
				logger.info("", e);
208
			}
209
		}
210
	}
211
	
212
	/**
213
	 * Point information 
214
	 * @author Nacho Brodin (nachobrodin@gmail.com)
215
	 */
216
	public class PointInfo {
217
		public Point2D worldCoord;
218
		public Point2D tile;
219
		public Point2D pixelInTile;
220
		public int     level;
221
		
222
		public PointInfo(Point2D worldCoord) {
223
			this.worldCoord = worldCoord;
224
		}
225
	}
226

  
227
	public static void register() {
228
		DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
229
		if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
230
			dataman.registerStoreProvider(NAME,
231
					WMTSProvider.class, WMTSDataParametersImpl.class);
232
		}
233

  
234
		if (!dataman.getExplorerProviders().contains(WMTSServerExplorer.NAME)) {
235
			dataman.registerExplorerProvider(WMTSServerExplorer.NAME, WMTSServerExplorer.class, WMTSServerExplorerParameters.class);
236
		}
237
		dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
238
	}
239
	
240
	public WMTSProvider() throws NotSupportedExtensionException {
241
		super();
242
	}
243
	
244
	/**
245
	 * Constructor. Abre el dataset.
246
	 * @param proj Proyecci?n
247
	 * @param fName Nombre del fichero
248
	 * @throws NotSupportedExtensionException
249
	 */
250
	public WMTSProvider(String params) throws NotSupportedExtensionException {
251
		super(params);
252
		if(params instanceof String) {
253
			WMTSDataParameters p = new WMTSDataParametersImpl();
254
			p.setURI((String)params);
255
			super.init(p, null, ToolsLocator.getDynObjectManager()
256
					.createDynObject(
257
							MetadataLocator.getMetadataManager().getDefinition(
258
									DataStore.METADATA_DEFINITION_NAME)));
259
			init(p, null);
260
		}
261
	}
262
	
263
	public WMTSProvider(WMTSDataParameters params,
264
			DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
265
		super(params, storeServices, ToolsLocator.getDynObjectManager()
266
				.createDynObject(
267
						MetadataLocator.getMetadataManager().getDefinition(
268
								DataStore.METADATA_DEFINITION_NAME)));
269
		init(params, storeServices);
270
	}
271
	
272
	/**
273
	 * Gets the connector from the URL
274
	 * @return
275
	 * @throws RemoteServiceException
276
	 */
277
	public WMTSClient getOGCClient() throws WMTSException {
278
		if(ogcClient == null) {
279
			WMTSDataParameters p = (WMTSDataParameters)parameters;
280
			ogcClient = p.getOGCClient();
281
			if(ogcClient != null)
282
				return ogcClient;
283
			
284
			URL url = null;
285
			try {
286
				url = new URL(p.getURI());
287
			} catch (Exception e) {
288
				throw new WMTSException("Malformed URL",e);
289
			}
290
			try {
291
				ogcClient = WMTSOGCLocator.getManager().createWMTSClient(url.toString());
292
				ogcClient.connect(true, new ICancellable() {
293
					
294
					public boolean isCanceled() {
295
						return false;
296
					}
297
					
298
					public Object getID() {
299
						return null;
300
					}
301
				});
302
			} catch (ConnectException e) {
303
				throw new WMTSException("Connect exception",e);
304
			} catch (IOException e) {
305
				throw new WMTSException("Connect exception",e);
306
			}
307
		}
308
		return ogcClient;
309
	}
310

  
311
	/**
312
	 * Crea las referencias al fichero y carga
313
	 * las estructuras con la informaci?n y los metadatos.
314
	 * @param proj Proyecci?n
315
	 * @param param Parametros de carga
316
	 * @throws NotSupportedExtensionException
317
	 */
318
	public void init (DataStoreParameters params,
319
			DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
320
		setParam(storeServices, params);
321
		if(((WMTSDataParameters)params).getImageFormat().compareTo("image/gif") == 0) {
322
			setDataType(new int[]{Buffer.TYPE_BYTE});
323
			bandCount = 1;
324
		} else {
325
			setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE});
326
			bandCount = 4;
327
		}
328
		
329
		if(!(param instanceof WMTSDataParameters))
330
			return;
331
		
332
		gridSubsets = hasGridSubsets((WMTSDataParameters)param);
333
		open = true;
334
	}
335
	
336
	/**
337
	 * Returns true if this layer has grid subsets
338
	 * @return
339
	 */
340
	public boolean hasGridSubsets() {
341
		return gridSubsets;
342
	}
343
	
344
	/**
345
	 * Checks if this layer has grid subsets or doesn't
346
	 * @param p
347
	 * @return
348
	 */
349
	private boolean hasGridSubsets(WMTSDataParameters p) {
350
		List<WMTSTileMatrixLimits> tileMatrixSetLimits = null;
351
		List<WMTSTileMatrixSetLink> tileMatrixSetLinkList = p.getLayer().getTileMatrixSetLink();
352
		String srs = p.getSRSCode();
353
		for (int i = 0; i < tileMatrixSetLinkList.size(); i++) {
354
			WMTSTileMatrixSetLink tileMatrixSetLink = (WMTSTileMatrixSetLink)tileMatrixSetLinkList.get(i);
355
			WMTSTileMatrixSet tms = tileMatrixSetLink.getTileMatrixSet();
356
			List<WMTSTileMatrixLimits> tmsl = tileMatrixSetLink.getTileMatrixLimits();
357
			String srsTileSet = tms.getSupportedCRS();
358
			if(areSrsEquals(srsTileSet, srs)) {
359
				tileMatrixSetLimits = tmsl;
360
			}
361
		}
362
		
363
		return (tileMatrixSetLimits == null || tileMatrixSetLimits.size() <= 0) ? false : true; 
364
	}
365
	
366
	private boolean areSrsEquals(String sourceSrs, String appSrs) {
367
		if(sourceSrs.compareTo(appSrs) == 0)
368
			return true;
369
		if(sourceSrs.contains("CRS:84") || sourceSrs.contains("CRS84")) { 
370
			if(appSrs.equals("EPSG:4326"))
371
				return true;
372
		}
373
		return false;
374
	}
375
	
376
	/*public static final WMTSConnector getConnectorFromURL(URL url) throws IOException {
377
		WMTSConnector drv = (WMTSConnector) drivers.get(url);
378
		if (drv == null) {
379
			drv = new WMTSConnector(url);
380
			drivers.put(url, drv);
381
		}
382
		return drv;
383
	}*/
384
	
385
	/**
386
	 * Obtiene el objeto que contiene que contiene la interpretaci?n de
387
	 * color por banda
388
	 * @return
389
	 */
390
	public ColorInterpretation getColorInterpretation() {
391
		if(super.getColorInterpretation() == null) {
392
			ColorInterpretation colorInterpretation = new DataStoreColorInterpretation(getBandCount());
393
			
394
			if(getBandCount() == 1) 
395
				colorInterpretation = DataStoreColorInterpretation.createGrayInterpretation();
396
			
397
			if(getBandCount() == 3) 
398
				colorInterpretation = DataStoreColorInterpretation.createRGBInterpretation();
399
			
400
			if(getBandCount() == 4)
401
				colorInterpretation = DataStoreColorInterpretation.createRGBAInterpretation();
402
			
403
			if(getBandCount() > 4 || getBandCount() == 2) {
404
				for (int i = 0; i < getBandCount(); i++) {
405
					colorInterpretation.setColorInterpValue(i, DataStoreColorInterpretation.UNDEF_BAND);
406
				}
407
			}
408
			setColorInterpretation(colorInterpretation);
409
		}
410
		return super.getColorInterpretation();
411
	}
412
	
413
	public boolean isTiled() {
414
		return true;
415
	}
416
	
417
	public AffineTransform getAffineTransform() {
418
		WMTSDataParameters p = (WMTSDataParameters)parameters;
419
		Extent e = getExtent();
420
		double psX = e.width() / (lastWidthRequest <= 0 ? p.getWidth() : lastWidthRequest);
421
		double psY = -(e.height() / (lastHeightRequest <= 0 ? p.getHeight() : lastHeightRequest));
422
		ownTransformation = new AffineTransform(
423
				psX, 
424
				0, 
425
				0, 
426
				psY, 
427
				e.getULX() - (psX / 2),
428
				e.getULY() - (psY / 2));
429
		externalTransformation = (AffineTransform) ownTransformation.clone();
430
		return ownTransformation;
431
	}
432
	
433
	/**
434
	 * <p>
435
	 * Gets the bounding box in world coordinates. 
436
	 * If the layer has defined the BoundingBox tag, we will take this bounding box as entire 
437
	 * extension of this layer, else we'll see if the tag WGS84BoundingBox is defined (it can be approximated).
438
	 * In this case we'll take WGS84BoundingBox as entire extension.
439
	 * </p>
440
	 * <br>
441
	 * Note: 
442
	 * <br>
443
	 * <p>
444
	 * If the layer has grid subsets (TileMatrixLimits) then
445
	 * this will have a only extent but if the layer doesn't have grid subsets then this will have a different
446
	 * extent in each level resolution. In this case we need to know the extent for each level.
447
	 * </p>
448
	 * @return Extent
449
	 */
450
	public Extent getExtent() {
451
		WMTSDataParameters p = (WMTSDataParameters)parameters;
452
		WMTSLayer layer = p.getLayer();
453

  
454
		if(layer.getBBox() != null)
455
			return new ExtentImpl(layer.getBBox().toRectangle2D());
456
		
457
		if(layer.getWGS84BBox() != null) {
458
			String crsCode = p.getSRSCode();
459
			Rectangle2D r = layer.getWGS84BBoxTransformed(crsCode);
460
			if(r != null)
461
				return new ExtentImpl(r);
462
		}
463
			
464
		if(bbox == null)
465
			getExtentByResolutionLevel();
466
		return bbox;
467
		
468
		/*if(gridSubsets) {
469
			WMTSBoundingBox bbox = layer.getWGS84BBox();
470
			return new ExtentImpl(bbox.toRectangle2D());
471
		} else {
472
			if(bbox == null)
473
				getExtentByResolutionLevel();
474
			return bbox;
475
		}*/
476
	}
477
	
478
	/**
479
	 * Gets the suffix of the downloaded image
480
	 * @return
481
	 */
482
	public String getFileSuffix() {
483
		WMTSDataParameters p = (WMTSDataParameters)parameters;
484
		String format = p.getImageFormat();
485
		if (format == null){
486
			return "xml";
487
		}
488
		if (format.indexOf("png") >= 0){
489
	        return "png";
490
		}	
491
	    if (format.indexOf("xml") >= 0){
492
	        return "xml";
493
	    }	
494
	    if (format.indexOf("gif") >= 0){
495
	        return "gif";
496
	    }
497
	    if (format.indexOf("tif") >= 0){
498
	        return "tif";
499
	    }
500
	    if (format.indexOf("bmp") >= 0){
501
	        return "bmp";
502
	    }
503
	    if (format.indexOf("jpg") >= 0
504
	        || format.indexOf("jpeg") >= 0){
505
	        return "jpg";			 
506
	    }
507
		return "xml";
508
	}
509

  
510
	/**
511
	 * When a layer doesn't have grid subsets this will have a different bounding
512
	 * box by resolution level. This function calculates and returns the array of
513
	 * extents, one by resolution level.
514
	 * @return
515
	 */
516
	public Extent[] getExtentByResolutionLevel() {
517
		if(extentByLevel == null) {
518
			WMTSDataParameters p = (WMTSDataParameters)parameters;
519
			WMTSTileMatrixSetLink tileMatrixSetLink = getTileMatrixSetLink();
520
			WMTSTileMatrixSet tileMatrixSet = tileMatrixSetLink.getTileMatrixSet();
521
			
522
	    	double widthMtsTile = 0;
523
	    	double heightMtsTile = 0;
524
			List<WMTSTileMatrix> tileMatrixList = tileMatrixSet.getTileMatrix();
525
			extentByLevel = new ExtentImpl[tileMatrixList.size()];
526
			double minX = 0;
527
			double minY = 0;
528
			double maxX = 0;
529
			double maxY = 0;
530
			for (int i = 0; i < tileMatrixList.size(); i++) {
531
				WMTSTileMatrix tileMatrix = (WMTSTileMatrix)tileMatrixList.get(i);
532
		    	if(!p.isProjected()) {
533
		    		widthMtsTile = (tileMatrix.getScaleDenominator() * tileMatrix.getTileWidth() * 0.28) / (MTS_X_GRADO * 1000);
534
		    		heightMtsTile = (tileMatrix.getScaleDenominator() * tileMatrix.getTileHeight() * 0.28) / (MTS_X_GRADO * 1000);
535
		    	} else {
536
		    		widthMtsTile = (tileMatrix.getScaleDenominator() * tileMatrix.getTileWidth() * 0.28) / 1000;
537
		    		heightMtsTile = (tileMatrix.getScaleDenominator() * tileMatrix.getTileHeight() * 0.28) / 1000;
538
		    	}
539
		    	
540
		    	//TODO: Revisar!!! Creo que el top left sale al rev?s en el de la nasa
541
		    	
542
		    	double h = Math.abs(tileMatrix.getTopLeftCorner()[1] - (tileMatrix.getTopLeftCorner()[1] - (tileMatrix.getMatrixHeight() * heightMtsTile)));
543
		    	Rectangle2D r = new Rectangle2D.Double(
544
		    			tileMatrix.getTopLeftCorner()[0], 
545
		    			tileMatrix.getTopLeftCorner()[1] - h,
546
		    			Math.abs(tileMatrix.getTopLeftCorner()[0] - (tileMatrix.getTopLeftCorner()[0] + (tileMatrix.getMatrixWidth() * widthMtsTile))),
547
		    			h);
548
		    	extentByLevel[i] = new ExtentImpl(r);
549
		    	if(i == 0) {
550
		    		minX = extentByLevel[i].getMin().getX();
551
		    		minY = extentByLevel[i].getMin().getY();
552
		    		maxX = extentByLevel[i].getMax().getX();
553
		    		maxY = extentByLevel[i].getMax().getY();
554
		    	} else {
555
		    		minX = Math.min(minX, extentByLevel[i].getMin().getX());
556
		    		minY = Math.min(minY, extentByLevel[i].getMin().getY());
557
		    		maxX = Math.max(maxX, extentByLevel[i].getMax().getX());
558
		    		maxY = Math.max(maxY, extentByLevel[i].getMax().getY());
559
		    	}
560
			}
561
			bbox = new ExtentImpl(minX, maxY, maxX, minY);
562
		}
563
		return extentByLevel;
564
	}
565
	
566
	public Rectangle2D getLayerExtent(String layerName, String srs) throws RemoteServiceException {
567
		return null;
568
	}
569

  
570
	public RasterProvider load() {
571
		return this;
572
	}
573
	
574
	public boolean isOpen() {
575
		return open;
576
	}
577

  
578
	public void close() {
579
		open = false;
580
	}
581
	
582
	public Transparency getTransparency() {
583
		if(lastFileTransparency == null) {
584
			lastFileTransparency = new DataStoreTransparency(getColorInterpretation());
585
			lastFileTransparency.setTransparencyBand(3);
586
		}
587
		return lastFileTransparency;
588
	}
589
	
590
	public NoData getNoDataValue() {
591
		NoData nodata = super.getNoDataValue();
592
		if(nodata != null)
593
			nodata.setNoDataTransparent(false);
594
		return noData;
595
	}
596

  
597
	public String translateFileName(String fileName) {
598
		return fileName;
599
	}
600

  
601
	public void setView(Extent e) {
602
		viewRequest = e;
603
	}
604

  
605
	public Extent getView() {
606
		return viewRequest;
607
	}
608

  
609
	public double getWidth() {
610
		WMTSDataParameters p = (WMTSDataParameters)parameters;
611
		if (lastWidthRequest <= 0) 
612
			return p.getWidth();
613
		return lastWidthRequest;
614
	}
615

  
616
	public double getHeight() {
617
		WMTSDataParameters p = (WMTSDataParameters)parameters;
618
		if (lastHeightRequest <= 0) 
619
			return p.getHeight();
620
		return lastHeightRequest;
621
	}
622

  
623
	public Object readCompleteLine(int line, int band)
624
		throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
625
		return null;
626
	}
627
	
628
	/**
629
	 * When the remote layer has fixed size this method downloads the file and return its reference. 
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff