Revision 67

View differences:

org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/downloader/TileDownloaderForOSM.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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.osm.downloader;
23

  
24
import java.io.BufferedOutputStream;
25
import java.io.DataInputStream;
26
import java.io.DataOutputStream;
27
import java.io.File;
28
import java.io.FileOutputStream;
29
import java.io.OutputStreamWriter;
30
import java.net.HttpURLConnection;
31
import java.net.MalformedURLException;
32
import java.net.URL;
33

  
34
import org.gvsig.compat.net.ICancellable;
35
import org.gvsig.raster.cache.tile.Tile;
36
import org.gvsig.raster.cache.tile.exception.TileGettingException;
37
import org.gvsig.raster.impl.provider.tile.BaseTileDownloader;
38
import org.gvsig.raster.osm.io.OSMDataParameters;
39
import org.gvsig.raster.osm.io.OSMProvider;
40

  
41
/** 
42
 * Tile getter for Open Street Map
43
 * @author Nacho Brodin (nachobrodin@gmail.com)
44
 */
45
public class TileDownloaderForOSM extends BaseTileDownloader {
46
	private String server   = null;
47
	private String suffix   = null;
48
	
49
	public TileDownloaderForOSM(OSMProvider prov, 
50
			int tileSize) {
51
		super(prov, tileSize, tileSize);
52
		server = ((OSMDataParameters)prov.getDataParameters()).getURI();
53
		suffix = ((OSMDataParameters)prov.getDataParameters()).getTileSuffix();
54
	}
55
	
56
	/*
57
	 * (non-Javadoc)
58
	 * @see org.gvsig.raster.cache.tile.provider.Downloader#getTile(org.gvsig.raster.cache.tile.Tile)
59
	 */
60
	public synchronized Tile downloadTile(Tile tile) throws TileGettingException {
61
		int level = tile.getLevel();
62
		int y = tile.getRow();
63
		int x = tile.getCol();
64
		String url = server + 
65
					(server.endsWith(File.separator) ? "" : File.separator) + 
66
					level + File.separator + 
67
					x + File.separator + 
68
					y + "." + suffix;
69
		try {
70
			//System.out.println("====>>>>" + url);
71
			downloadFile(new URL(url), tile.getFile(), tile.getCancelled());
72
		} catch (MalformedURLException e) {
73
			throw new TileGettingException(e);
74
		}
75
		readTileFromDisk(tile);
76
		return tile;
77

  
78
	}
79
	
80
	 public synchronized void downloadFile(URL url, File file, ICancellable cancel) throws TileGettingException {
81
	    	int IO_BUFFER_SIZE = 8 * 1024;
82
			int timeout =  30000;
83
			
84
			DataOutputStream dos;
85
			try {
86
				DataInputStream is;
87
				OutputStreamWriter os = null;
88
				HttpURLConnection connection = null;
89

  
90
				connection = (HttpURLConnection)url.openConnection();
91
				connection.setConnectTimeout(timeout);
92
				
93
				is = new DataInputStream(url.openStream());
94
				
95
				dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(file)));
96
				byte[] buffer = new byte[IO_BUFFER_SIZE];
97

  
98
				int read;
99
				while ((read = is.read(buffer)) != -1) {
100
					dos.write(buffer, 0, read);
101
					if(cancel != null && cancel.isCanceled())
102
						return;
103
				}
104
				
105
				if(os != null) {
106
					os.close();
107
				}
108
				dos.close();
109
				is.close();
110
				is = null;
111
				dos = null;
112
			} catch (Exception e) {
113
				throw new TileGettingException(e);
114
			}
115
	    }
116
	
117
}
0 118

  
org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/downloader/OSMTileServer.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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.osm.downloader;
23

  
24
import org.gvsig.fmap.dal.coverage.RasterLibrary;
25
import org.gvsig.raster.cache.tile.provider.CacheStruct;
26
import org.gvsig.raster.cache.tile.provider.Downloader;
27
import org.gvsig.raster.cache.tile.provider.TileServer;
28
import org.gvsig.raster.osm.cachestruct.OSMCacheStruct;
29
import org.gvsig.raster.osm.io.OSMDataParameters;
30
import org.gvsig.raster.osm.io.OSMProvider;
31

  
32
/** 
33
* Data server for the tile cache in a OSMProvider 
34
* @author Nacho Brodin (nachobrodin@gmail.com)
35
*/
36
public class OSMTileServer implements TileServer {
37
	private CacheStruct                struct               = null;
38
	private Downloader                 downloader           = null;
39
	private OSMProvider                provider             = null;
40
	private String                     suffix               = null;
41
	private int                        tileSize             = 256;
42
	private int                        levels               = 0;
43
	
44
	/**
45
	 * Creates a OSMTileServer
46
	 * @param prov
47
	 * @param suffix
48
	 * 			it depends on the layer
49
	 * @param levels
50
	 * 			it depends on the layer
51
	 */
52
	public OSMTileServer(OSMProvider prov, String suffix, int levels) {
53
		this.provider = prov;
54
		this.suffix = suffix;
55
		this.levels = levels;
56
	}
57
	
58
	/*
59
	 * (non-Javadoc)
60
	 * @see org.gvsig.raster.cache.tile.provider.TileServer#getDownloader()
61
	 */
62
	public Downloader getDownloader() {
63
		if(downloader == null) {
64
			downloader = new TileDownloaderForOSM(provider, getStruct().getTileSizeByLevel(0)[0]);
65
		}
66
		return downloader;
67
	}
68

  
69
	/*
70
	 * (non-Javadoc)
71
	 * @see org.gvsig.raster.cache.tile.provider.TileServer#getStruct()
72
	 */
73
	public CacheStruct getStruct() {
74
		if(struct == null) {
75
			struct = new OSMCacheStruct(levels, 
76
					tileSize, 
77
					tileSize, 
78
					((OSMDataParameters)provider.getDataParameters()).getOSMLayerName(), 
79
					RasterLibrary.pathTileCache, 
80
					suffix, 
81
					0);
82
		}
83
		return struct;
84
	}
85
	
86
	/*
87
	 * (non-Javadoc)
88
	 * @see org.gvsig.raster.cache.tile.provider.TileServer#setStruct(org.gvsig.raster.cache.tile.provider.CacheStruct)
89
	 */
90
	public void setStruct(CacheStruct struct) {
91
		//La structura de cache es proporcionada por el servidor
92
	}
93
	
94
	/*
95
	 * (non-Javadoc)
96
	 * @see org.gvsig.raster.cache.tile.provider.TileServer#getFileSuffix()
97
	 */
98
	public String getFileSuffix() {
99
		return suffix;
100
	}
101
	
102
	/*
103
	 * (non-Javadoc)
104
	 * @see org.gvsig.raster.cache.tile.provider.TileServer#setFileExtension(java.lang.String)
105
	 */
106
	public void setFileSuffix(String extension) {
107
		this.suffix = extension;
108
	}
109
}
0 110

  
org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/io/OSMServerExplorer.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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
package org.gvsig.raster.osm.io;
24

  
25

  
26
public class OSMServerExplorer {
27
	public static final String NAME       = OSMProvider.NAME;
28

  
29
}
0 30

  
org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/io/OSMProvider.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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.osm.io;
23

  
24
import java.awt.geom.AffineTransform;
25

  
26
import org.cresques.cts.IProjection;
27
import org.gvsig.fmap.crs.CRSFactory;
28
import org.gvsig.fmap.dal.DALLocator;
29
import org.gvsig.fmap.dal.DataStore;
30
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
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.ProcessInterruptedException;
38
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
39
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
40
import org.gvsig.fmap.dal.exception.OpenException;
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.DefaultStoreFactory;
50
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
51
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
52
import org.gvsig.raster.osm.cachestruct.OSMCacheStruct;
53
import org.gvsig.raster.osm.downloader.OSMTileServer;
54
import org.gvsig.tools.ToolsLocator;
55
import org.gvsig.tools.task.TaskStatus;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58
/**
59
 * Data provider for OSM servers
60
 *
61
 * @author Nacho Brodin (nachobrodin@gmail.com)
62
 */
63
public class OSMProvider extends DefaultRasterProvider {
64
	public static String                     NAME                     = "OSM Raster";
65
	public static String                     DESCRIPTION              = "OSM Raster Server";
66
	public final String                      METADATA_DEFINITION_NAME = NAME;
67
	private Extent                           viewRequest              = null;
68
	private TileServer                       tileServer               = null;
69
	private boolean                          open                     = false;
70
    private DataStoreTransparency            fileTransparency         = null;
71
    private static final Logger              logger                   = LoggerFactory.getLogger(OSMProvider.class);
72
    
73
	public static void register() {
74
		DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
75
		
76
		if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
77
			dataman.registerStoreProvider(NAME,
78
					OSMProvider.class, OSMDataParametersImpl.class);
79
		}
80
		
81
		dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
82
	}
83
	
84
	public OSMProvider() {
85
	}
86
	
87
	/**
88
	 * Opens the dataset.
89
	 * @param proj Projection
90
	 * @param fName File name
91
	 * @throws NotSupportedExtensionException
92
	 */
93
	public OSMProvider(String params) throws NotSupportedExtensionException, OpenException {
94
		super(params);
95
		
96
	}
97
	
98
	public OSMProvider (OSMDataParametersImpl params,
99
			DataStoreProviderServices storeServices) throws NotSupportedExtensionException, OpenException {
100
		super(params, storeServices, ToolsLocator.getDynObjectManager()
101
				.createDynObject(
102
						MetadataLocator.getMetadataManager().getDefinition(
103
								DataStore.METADATA_DEFINITION_NAME)));
104
		init(params, storeServices);
105
	}
106

  
107
	/**
108
	 * Build file references
109
	 * @param proj Projection
110
	 * @param param Load parameters
111
	 * @throws NotSupportedExtensionException
112
	 */
113
	public void init (OSMDataParametersImpl params,
114
			DataStoreProviderServices storeServices) throws NotSupportedExtensionException, OpenException {
115
		setParam(storeServices, params);
116
		setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE});
117
		bandCount = 4;
118
		super.init();
119
		fileSize = (long)(getWidth() * getHeight() * 3);
120
		open = true;
121
	}
122
	
123
	public IProjection getProjection() {
124
		if(proj == null) {
125
			try {
126
				proj = CRSFactory.getCRS("EPSG:3785");
127
			} catch(Exception e) {
128
				logger.info("Projection not loaded", e);
129
			}
130
		}
131
		return proj;
132
	}
133
	
134
	/**
135
	 * Gets the bounding box in world coordinates. 
136
	 * @return Extent
137
	 */
138
	public Extent getExtent() {
139
		return ((OSMCacheStruct)getTileServer().getStruct()).getWorldExtent();
140
	}
141
	
142
	/**
143
	 * Reloads metadata using the selected grid
144
	 */
145
	@SuppressWarnings("unused")
146
	private void reloadMetadataFromGrid() {
147
		//wktProjection = null;
148
		//CrsWkt crs = new CrsWkt(wktProjection);
149
		//IProjection proj = CRSFactory.getCRS("EPSG:23030");
150
		
151
		/*ownTransformation = new AffineTransform(
152
				scaleX, 0, 
153
				0, -scaleY, 
154
				pRect.getMinX(), 
155
				pRect.getMaxY());*/
156
		externalTransformation = (AffineTransform)ownTransformation.clone();
157
		bandCount = 3; 
158
		setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE});
159
	}
160
	
161
	public RasterProvider load() {
162
		return this;
163
	}
164
	
165
	public boolean isOpen() {
166
		return open;
167
	}
168

  
169
	public void close() {
170
	}
171

  
172
	public String translateFileName(String fileName) {
173
		return fileName;
174
	}
175

  
176
	/**
177
	 * Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci�n
178
	 * de la vista asignada ya que la petici�n viene en coordenadas del fichero .rmf y la vista (v)
179
	 * ha de estar en coordenadas del fichero.
180
	 */
181
	public void setView(Extent e) {
182
		viewRequest = new ExtentImpl(e);
183
	}
184

  
185
	public Extent getView() {
186
		return viewRequest;
187
	}
188

  
189
	public double getWidth() {
190
		int n = getTileServer().getStruct().getNumberOfLevels() - 1;
191
		return getExtent().width() / getTileServer().getStruct().getPixelSizeByLevel(n);
192
	}
193

  
194
	public double getHeight() {
195
		int n = getTileServer().getStruct().getNumberOfLevels() - 1;
196
		return getExtent().width() / getTileServer().getStruct().getPixelSizeByLevel(n);
197
	}
198

  
199
	public Object readBlock(int pos, int blockHeight, double scale)
200
		throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
201
		return null;
202
	}
203

  
204
	public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
205
		return null;
206
	}
207

  
208
	public int getBlockSize(){
209
		return 0;
210
	}
211

  
212
	public DataStoreTransparency getTransparency() {
213
		if(fileTransparency == null)
214
			fileTransparency = new DataStoreTransparency();
215
		return fileTransparency;
216
	}
217

  
218
	/**
219
	 * Informa de si el driver ha supersampleado en el �ltimo dibujado. Es el driver el que colocar�
220
	 * el valor de esta variable cada vez que dibuja.
221
	 * @return true si se ha supersampleado y false si no se ha hecho.
222
	 */
223
	public boolean isSupersampling() {
224
		return false;
225
	}
226

  
227
	public void setAffineTransform(AffineTransform t){
228
		super.setAffineTransform(t);
229
	}
230

  
231
	public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
232
		if(band >= getBandCount())
233
			throw new BandAccessException("Wrong band");
234
		return 0;
235
	}
236

  
237
	public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
238
		if (band >= getBandCount())
239
			throw new BandAccessException("Wrong band");
240
		return 0;
241
	}
242

  
243
	public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
244
		if (band >= getBandCount())
245
			throw new BandAccessException("Wrong band");
246
		return 0;
247
	}
248

  
249
	public boolean isOverviewsSupported() {
250
		return true;
251
	}
252

  
253
	public boolean isReproyectable() {
254
		return true;
255
	}
256

  
257
	public String getName() {
258
		return NAME;
259
	}
260
	
261
	public ColorInterpretation getColorInterpretation() {
262
		if(super.getColorInterpretation() == null) {
263
			super.setColorInterpretation(DataStoreColorInterpretation.createRGBInterpretation());
264
		}
265
		return super.getColorInterpretation();
266
	}
267
	
268
	public void setStatus(RasterProvider provider) {
269
		if(provider instanceof OSMProvider) {
270
			//Not implemented yet
271
		}
272
	}
273
	
274
	public boolean isTimeSupported() {
275
		return true;
276
	}
277
	
278
	public TileServer getTileServer() {
279
		if(tileServer == null)
280
			tileServer = new OSMTileServer(this, 
281
					((OSMDataParameters)param).getTileSuffix(), 
282
					((OSMDataParameters)param).getNumberOfLevels());
283
		return tileServer;
284
	}
285

  
286
	@Override
287
	public void getWindow(Extent ex, int bufWidth, int bufHeight,
288
			BandList bandList, TileListener listener, TaskStatus taskStatus)
289
			throws ProcessInterruptedException, RasterDriverException {
290
		
291
	}
292

  
293
	@Override
294
	public Buffer getWindow(Extent extent, BandList bandList,
295
			Buffer rasterBuf, TaskStatus status)
296
			throws ProcessInterruptedException, RasterDriverException {
297
		return null;
298
	}
299

  
300
	@Override
301
	public Buffer getWindow(double x, double y, double w, double h,
302
			BandList bandList, Buffer rasterBuf, boolean adjustToExtent,
303
			TaskStatus status) throws ProcessInterruptedException,
304
			RasterDriverException {
305
		return null;
306
	}
307

  
308
	@Override
309
	public Buffer getWindow(Extent extent, int bufWidth, int bufHeight,
310
			BandList bandList, Buffer rasterBuf, boolean adjustToExtent,
311
			TaskStatus status) throws ProcessInterruptedException,
312
			RasterDriverException {
313
		return null;
314
	}
315

  
316
	@Override
317
	public Buffer getWindow(int x, int y, int w, int h, BandList bandList,
318
			Buffer rasterBuf, TaskStatus status)
319
			throws ProcessInterruptedException, RasterDriverException {
320
		return null;
321
	}
322
}
0 323

  
org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/io/OSMDataParameters.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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.osm.io;
23

  
24

  
25
/**
26
 * Parameters for the WMS provider
27
 * @author Nacho Brodin (nachobrodin@gmail.com)
28
 */
29
public interface OSMDataParameters {
30
	public void setURI(String string);
31

  
32
	public String getURI();
33

  
34
	public int getWidth();
35

  
36
	public int getHeight();
37
	
38
	public void setWidth(int w);
39
	
40
	public void setHeight(int h);
41
	
42
	public String getOSMLayerName();
43
	
44
	public void setOSMLayerName(String name);
45
	
46
	public int getNumberOfLevels();
47
	
48
	public void setNumberOfLevels(int levels);
49
	
50
	public String getTileSuffix();
51
	
52
	public void setTileSuffix(String suffix);
53
}
0 54

  
org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/io/OSMException.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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.osm.io;
23

  
24
import java.util.Hashtable;
25
import java.util.Map;
26

  
27
/**
28
 * @author Nacho Brodin (nachobrodin@gmail.com)
29
 */
30
public class OSMException extends Exception {
31
    private static final long serialVersionUID = 1476084093500156070L;
32
    private String            wmtsCode         = null;
33
    protected String          formatString     = null;
34
    protected String          messageKey       = null;
35
    protected long            code             = 0;
36

  
37
    public OSMException() {		
38
        init();			
39
    }	
40

  
41
    public OSMException(Throwable cause) {	
42
    	super(cause);
43
        init();	
44
        initCause(cause);
45
    }
46
    
47
    public OSMException(String msg, Throwable cause) {	
48
    	super(msg, cause);
49
        init();	
50
        this.formatString = msg;
51
    }
52

  
53
    public OSMException(String wfsCode,String message) {		
54
        init();	
55
        formatString = message;
56
        this.wmtsCode = wfsCode;
57
    }	
58

  
59
    public OSMException(String message) {        
60
        init(); 
61
        formatString = message;       
62
    }   
63

  
64
	@SuppressWarnings("unchecked")
65
	protected Map values() {		
66
        Hashtable params;
67
        params = new Hashtable();		
68
        return params;
69
    }
70

  
71
    public void init() {
72
        messageKey = "wmts_exception";
73
        formatString = "WMTS Exception";
74
        code = serialVersionUID;
75
    }
76

  
77
    /**
78
     * @return Returns the wfsCode.
79
     */
80
    public String getWfsCode() {
81
        return wmtsCode;
82
    }	
83
}
0 84

  
org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/io/OSMDataParametersImpl.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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
package org.gvsig.raster.osm.io;
24

  
25
import org.gvsig.raster.impl.store.AbstractRasterDataParameters;
26
import org.gvsig.tools.ToolsLocator;
27
import org.gvsig.tools.dataTypes.DataTypes;
28
import org.gvsig.tools.dynobject.DelegatedDynObject;
29
import org.gvsig.tools.dynobject.DynClass;
30
import org.gvsig.tools.dynobject.DynField;
31
import org.gvsig.tools.dynobject.DynObjectManager;
32
import org.gvsig.tools.dynobject.DynStruct;
33
import org.gvsig.tools.persistence.PersistenceManager;
34
import org.gvsig.tools.persistence.PersistentState;
35
import org.gvsig.tools.persistence.exception.PersistenceException;
36

  
37
/**
38
 * Parameters for OSM provider
39
 * @author Nacho Brodin (nachobrodin@gmail.com)
40
 */
41
public class OSMDataParametersImpl extends AbstractRasterDataParameters implements OSMDataParameters {
42
	public static final String            PARAMETERS_DEFINITION_NAME = "OSMRasterDataParameters";
43
	private static DynClass               DYNCLASS                   = null;
44
	private DelegatedDynObject            delegatedDynObject         = null;
45
	private static final String           FIELD_WIDTH                = "width";
46
	private static final String           FIELD_HEIGHT               = "height";
47
	private static final String           FIELD_MAX_RES_LEVEL        = "maxResolutionLevel";
48
	private static final String           FIELD_NAME                 = "name";
49
	private static final String           FIELD_SUFFIX               = "suffix";
50
	
51
	public OSMDataParametersImpl() {
52
		super();
53
		initialize();
54
	}
55
	
56
	protected void initialize() {
57
		delegatedDynObject = (DelegatedDynObject) ToolsLocator
58
				.getDynObjectManager().createDynObject(
59
						DYNCLASS);
60
	}
61
	
62
	@SuppressWarnings("deprecation")
63
	public static void registerDynClass() {
64
		DynObjectManager dynman = ToolsLocator.getDynObjectManager();
65
		DynClass dynClass;
66
		DynField field;
67
		
68
		if(dynman == null)
69
			return;
70
		
71
		if (DYNCLASS == null) {
72
			dynClass = AbstractRasterDataParameters.registerDynClass(PARAMETERS_DEFINITION_NAME);
73
			//dynClass = dynman.add(AbstractRasterDataParameters.DYNCLASS_NAME);
74
			
75
			field = dynClass.addDynField(FIELD_WIDTH);
76
			field.setTheTypeOfAvailableValues(DynField.ANY);
77
			field.setDescription("Width");
78
			field.setType(DataTypes.INT);
79
			field.setMandatory(false);
80
			
81
			field = dynClass.addDynField(FIELD_HEIGHT);
82
			field.setTheTypeOfAvailableValues(DynField.ANY);
83
			field.setDescription("Height");
84
			field.setType(DataTypes.INT);
85
			field.setMandatory(false);
86
			
87
			field = dynClass.addDynField(FIELD_MAX_RES_LEVEL);
88
			field.setTheTypeOfAvailableValues(DynField.ANY);
89
			field.setDescription("Maximun levels of resolution");
90
			field.setType(DataTypes.INT);
91
			field.setMandatory(false);
92
			
93
			field = dynClass.addDynFieldString(FIELD_NAME);
94
			field.setDescription("Layer name");
95
			field.setClassOfValue(String.class);
96
			field.setMandatory(false);
97
			
98
			field = dynClass.addDynFieldString(FIELD_SUFFIX);
99
			field.setDescription("Tile suffix");
100
			field.setClassOfValue(String.class);
101
			field.setMandatory(false);
102

  
103
			DYNCLASS = dynClass;
104
		}
105
	}
106
	
107
	/*
108
	 * (non-Javadoc)
109
	 * @see org.gvsig.fmap.dal.DataStoreParameters#getDataStoreName()
110
	 */
111
	public String getDataStoreName() {
112
		return OSMProvider.NAME;
113
	}
114
	
115
	/*
116
	 * (non-Javadoc)
117
	 * @see org.gvsig.fmap.dal.DataStoreParameters#getDescription()
118
	 */
119
	public String getDescription() {
120
		return OSMProvider.DESCRIPTION;
121
	}
122
	
123
	public static void registerPersistence() {
124
		PersistenceManager manager = ToolsLocator.getPersistenceManager();
125
		DynStruct definition = manager.getDefinition("OSMDataParameters_Persistent");
126
		if( definition == null ) {
127
			definition = manager.addDefinition(
128
					OSMDataParametersImpl.class,
129
					"OSMDataParameters_Persistent",
130
					"OSM DataParameters Persistency",
131
					null, 
132
					null
133
			);
134
			AbstractRasterDataParameters.registerPersistence(definition);
135
			
136
			/*definition.addDynFieldString(FIELD_VARIABLE).setMandatory(false);
137
			definition.addDynFieldInt(FIELD_LEVEL).setMandatory(false);
138
			definition.addDynFieldInt(FIELD_TIME).setMandatory(false);
139
			definition.addDynFieldString(FIELD_XDIM).setMandatory(false);
140
			definition.addDynFieldString(FIELD_YDIM).setMandatory(false);*/
141
		}
142
	}
143
	
144
	@Override
145
	public void saveToState(PersistentState state) throws PersistenceException {
146
		super.saveToState(state);
147
		
148
		/*state.set(FIELD_VARIABLE, getStringVariable());	
149
		state.set(FIELD_LEVEL, getFieldLevel());
150
		state.set(FIELD_TIME, getFieldTime());
151
		state.set(FIELD_XDIM, getXDimVariable());
152
		state.set(FIELD_YDIM, getYDimVariable());*/
153
	}
154
	
155
	@Override
156
	public void loadFromState(PersistentState state)
157
			throws PersistenceException {
158
		super.loadFromState(state);
159
		
160
		/*setFieldVariable(state.getString(FIELD_VARIABLE));
161
		setFieldTime(state.getInt(FIELD_TIME));
162
		setFieldLevel(state.getInt(FIELD_LEVEL));
163
		xDimVariable = state.getString(FIELD_XDIM);
164
		yDimVariable = state.getString(FIELD_YDIM);*/
165
	}
166

  
167
	@Override
168
	protected DelegatedDynObject getDelegatedDynObject() {
169
		return delegatedDynObject;
170
	}
171

  
172
	public boolean isOverridingHost() {
173
		return false;
174
	}
175

  
176
	public void setOverrideHost(boolean over) {
177
		
178
	}
179
	
180
	public void setWidth(int w) {
181
		this.setDynValue(FIELD_WIDTH, new Integer(w));
182
	}
183
	
184
	public void setHeight(int h) {
185
		this.setDynValue(FIELD_HEIGHT, new Integer(h));
186
	}
187
	
188
	public int getWidth() {
189
		Integer b = (Integer)getDynValue(FIELD_WIDTH);
190
		if(b != null)
191
			return ((Integer)b).intValue();
192
		return 0;
193
	}
194
	
195
	public int getHeight() {
196
		Integer b = (Integer)getDynValue(FIELD_HEIGHT);
197
		if(b != null)
198
			return ((Integer)b).intValue();
199
		return 0;
200
	}
201
	
202
	public String getOSMLayerName() {
203
		String b = (String)getDynValue(FIELD_NAME);
204
		if(b != null)
205
			return b;
206
		return null;
207
	}
208
	
209
	public void setOSMLayerName(String name) {
210
		this.setDynValue(FIELD_NAME, name);
211
	}
212
	
213
	public int getNumberOfLevels() {
214
		Integer b = (Integer)getDynValue(FIELD_MAX_RES_LEVEL);
215
		if(b != null)
216
			return ((Integer)b).intValue();
217
		return 0;
218
	}
219
	
220
	public void setNumberOfLevels(int levels) {
221
		this.setDynValue(FIELD_MAX_RES_LEVEL, new Integer(levels));
222
	}
223
	
224
	public String getTileSuffix() {
225
		String b = (String)getDynValue(FIELD_SUFFIX);
226
		if(b != null)
227
			return b;
228
		return null;
229
	}
230
	
231
	public void setTileSuffix(String suffix) {
232
		this.setDynValue(FIELD_SUFFIX, suffix);
233
	}
234
}
0 235

  
org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/io/DefaultOSMIOLibrary.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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.osm.io;
23

  
24
import org.gvsig.tools.library.AbstractLibrary;
25
import org.gvsig.tools.library.LibraryException;
26
/**
27
 *
28
 * @author Nacho Brodin (nachobrodin@gmail.com)
29
 */
30
public class DefaultOSMIOLibrary extends AbstractLibrary {	
31
	
32
	@Override
33
	protected void doInitialize() throws LibraryException {
34
	}
35

  
36
	@Override
37
	protected void doPostInitialize() throws LibraryException {
38
		OSMDataParametersImpl.registerDynClass();
39
		OSMProvider.register();
40
		OSMDataParametersImpl.registerPersistence();
41
	}
42
}
0 43

  
org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/cachestruct/TileMatrix.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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.osm.cachestruct;
23

  
24

  
25
/**
26
 * Description of a tile matrix
27
 * @author Nacho Brodin (nachobrodin@gmail.com)
28
 */
29
public class TileMatrix {
30
    private double                      scaleXDenominator    = 0;
31
    private double                      scaleYDenominator    = 0;
32
    private double[]                    topLeftCorner       = null;
33
    private int                         tileWidth           = 0;
34
    private int                         tileHeight          = 0;
35
    private long                        matrixWidth         = 0;
36
    private long                        matrixHeight        = 0;
37
    private static final double         MTS_X_GRADO         = 111319.490793274;
38
    
39
	public double getScaleXDenominator() {
40
		return scaleXDenominator;
41
	}
42

  
43
	public void setScaleXDenominator(double scaleXDenominator) {
44
		this.scaleXDenominator = scaleXDenominator;
45
	}
46
	
47
	public double getScaleYDenominator() {
48
		return scaleYDenominator;
49
	}
50

  
51
	public void setScaleYDenominator(double scaleYDenominator) {
52
		this.scaleYDenominator = scaleYDenominator;
53
	}
54

  
55
	public int getTileWidth() {
56
		return tileWidth;
57
	}
58

  
59
	public void setTileWidth(int tileWidth) {
60
		this.tileWidth = tileWidth;
61
	}
62

  
63
	public int getTileHeight() {
64
		return tileHeight;
65
	}
66

  
67
	public void setTileHeight(int tileHeight) {
68
		this.tileHeight = tileHeight;
69
	}
70

  
71
	public long getMatrixWidth() {
72
		return matrixWidth;
73
	}
74

  
75
	public void setMatrixWidth(long matrixWidth) {
76
		this.matrixWidth = matrixWidth;
77
	}
78

  
79
	public long getMatrixHeight() {
80
		return matrixHeight;
81
	}
82

  
83
	public void setMatrixHeight(long matrixHeight) {
84
		this.matrixHeight = matrixHeight;
85
	}
86

  
87
	public double[] getTopLeftCorner() {
88
		return topLeftCorner;
89
	}
90
	
91
	public void setTopLeftCorner(double[] topLeftCorner) {
92
		this.topLeftCorner = topLeftCorner;
93
	}
94
    
95
    /**
96
     * Gets the width in meters of a tile
97
     * @param projected
98
     * @return
99
     */
100
    public double getWidthMtsTile(boolean projected) {
101
    	if(!projected) {
102
    		return (scaleXDenominator * tileWidth * 0.28) / (MTS_X_GRADO * 1000);
103
    	} else {
104
    		return (scaleXDenominator * tileWidth * 0.28) / 1000;
105
    	}
106
    }
107
    
108
    /**
109
     * Gets the height in meters of a tile
110
     * @param projected
111
     * @return
112
     */
113
    public double getHeightMtsTile(boolean projected) {
114
    	if(!projected) {
115
    		return (scaleYDenominator * tileHeight * 0.28) / (MTS_X_GRADO * 1000);
116
    	} else {
117
    		return (scaleYDenominator * tileHeight * 0.28) / 1000;
118
    	}
119
    }
120
    
121
	public void print() {
122
		System.out.println("   *****TileMatrix******");
123
		System.out.println("   scaleXDenominator:" + getScaleXDenominator());
124
		System.out.println("   scaleYDenominator:" + getScaleYDenominator());
125
		if(topLeftCorner != null)
126
			System.out.println("   topLeftCorner:" + topLeftCorner[0] + ", " + topLeftCorner[1]);
127
		System.out.println("   tileWidth:" + getTileWidth());
128
		System.out.println("   tileHeight:" + getTileHeight());
129
		System.out.println("   matrixWidth:" + getMatrixWidth());
130
		System.out.println("   matrixHeight:" + getMatrixHeight());
131
	}
132
}
0 133

  
org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/cachestruct/TileMatrixLimits.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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.osm.cachestruct;
23

  
24
import java.awt.geom.Point2D;
25

  
26
/**
27
 * Limits of a tile matrix
28
 * @author Nacho Brodin (nachobrodin@gmail.com)
29
 */
30
public class TileMatrixLimits {
31
	private TileMatrix             tileMatrix           = null;
32
	private int                    minTileRow           = 0;
33
	private int                    maxTileRow           = 0;
34
	private int                    minTileCol           = 0;
35
	private int                    maxTileCol           = 0;
36
	
37
	public TileMatrix getTileMatrix() {
38
		if(tileMatrix == null)
39
			tileMatrix = new TileMatrix();
40
		return tileMatrix;
41
	}
42

  
43
	/**
44
	 * Gets the upper left corner and the lower right corner of the 
45
	 * selected tile
46
	 * @param x X tile position
47
	 * @param y Y tile position
48
	 * @return
49
	 */
50
	public Point2D[] getTileExtent(int x, int y) {
51
		double sizeXTile = getTileMatrix().getTileWidth() * getTileMatrix().getScaleXDenominator();
52
		double sizeYTile = getTileMatrix().getTileWidth() * getTileMatrix().getScaleYDenominator();
53
		double initX = getTileMatrix().getTopLeftCorner()[0] + (sizeXTile * x);
54
		double initY = getTileMatrix().getTopLeftCorner()[1] - (sizeYTile * y);
55
		Point2D[] p = new Point2D[2];
56
		p[0] = new Point2D.Double(initX, initY);
57
		p[1] = new Point2D.Double(initX + sizeXTile, initY - sizeYTile);
58
		return p;
59
	}
60
	
61
	public int getMinTileRow() {
62
		return minTileRow;
63
	}
64
	
65
	public void setMinTileRow(int minTileRow) {
66
		this.minTileRow = minTileRow;
67
	}
68
	
69
	public int getMaxTileRow() {
70
		return maxTileRow;
71
	}
72
	
73
	public void setMaxTileRow(int maxTileRow) {
74
		this.maxTileRow = maxTileRow;
75
	}
76
	
77
	public int getMinTileCol() {
78
		return minTileCol;
79
	}
80
	
81
	public void setMinTileCol(int minTileCol) {
82
		this.minTileCol = minTileCol;
83
	}
84
	
85
	public int getMaxTileCol() {
86
		return maxTileCol;
87
	}
88
	
89
	public void setMaxTileCol(int maxTileCol) {
90
		this.maxTileCol = maxTileCol;
91
	}
92
	
93
	public void print() {
94
		System.out.println("  *****TileMatrixLimits******");
95
		System.out.println("  MaxTileCol:" + getMaxTileCol());
96
		System.out.println("  MaxTileRow:" + getMaxTileRow());
97
		System.out.println("  MinTileCol:" + getMinTileCol());
98
		System.out.println("  MinTileRow:" + getMinTileRow());
99
		if(tileMatrix != null)
100
			tileMatrix.print();
101
	}
102
}
0 103

  
org.gvsig.raster.osm/branches/org.gvsig.raster.osm_dataaccess_refactoring/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/cachestruct/OSMCacheStruct.java
1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
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.osm.cachestruct;
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.datastruct.Extent;
31
import org.gvsig.raster.cache.tile.Tile;
32
import org.gvsig.raster.cache.tile.TileCacheLocator;
33
import org.gvsig.raster.cache.tile.exception.TileBuildException;
34
import org.gvsig.raster.cache.tile.provider.CacheStruct;
35

  
36
/**
37
 * Implementation for a structure of a cache
38
 * @author Nacho Brodin (nachobrodin@gmail.com)
39
 */
40
public class OSMCacheStruct implements CacheStruct {
41
    private TileMatrixSet                 tileMatrixSet  = null;
42
    private ArrayList<TileMatrixLimits>   limits         = null;
43
    private String                        layerName      = null;
44
	private Rectangle2D                   worldExtent    = null;
45
	private String                        fileSuffix     = null;
46
	private String                        epsg           = null;
47
	private long                          fileSize       = 0;
48
	private double                        realPixelSize  = 0;
49
    
50
    public OSMCacheStruct(int levels, 
51
    		int tilePxWidth,
52
    		int tilePxHeight,
53
    		String layerName,
54
    		String baseDir,
55
    		String fileSuffix,
56
    		long size) {
57
    	this.layerName = layerName;
58
    	this.fileSuffix = fileSuffix;
59
    	this.fileSize = size;
60
    	this.epsg = "EPSG:3857"; //WGS87 Pseudomercator
61
    	
62
    	//Coordenadas del mundo en geogr?ficas y planas
63
    	
64
    	//worldExtent = new Rectangle2D.Double(-180, -85.0511, 360, 170.1022); // arctan(sin(PI)) = 85.0511
65
    	//worldExtent = new Rectangle2D.Double(-180, -180, 360, 360);
66
    	worldExtent = new Rectangle2D.Double(-20037508, -20037508, 40075016, 40075016);
67
    	double[][] pixelSizeList = buildWorldMatrix(levels, tilePxWidth, tilePxHeight, worldExtent);
68
        buildLayerMatrix(levels, tilePxWidth, tilePxHeight, worldExtent, pixelSizeList);
69
        
70
    }
71
    
72
    private void buildLayerMatrix(int levels, 
73
    		int tilePxWidth, 
74
    		int tilePxHeight,
75
    		Rectangle2D worldExtent,
76
    		double[][] pixelSizeList) {
77
    	limits = new ArrayList<TileMatrixLimits>();
78

  
79
    	for (int i = 0; i < levels; i++) {
80
    		//Calculo de tiles para ese nivel en la capa actual
81
    		int minTileCol = 0;
82
    		int minTileRow = 0;
83
    		int maxTileCol = (i == 0) ? 0 : (int)Math.pow(2, i) - 1;
84
    		int maxTileRow = (i == 0) ? 0 : (int)Math.pow(2, i) - 1;
85
    		
86
    		TileMatrixLimits limit = new TileMatrixLimits();
87
    		limit.setMinTileRow(minTileRow);
88
    		limit.setMinTileCol(minTileCol);
89
    		limit.setMaxTileRow(maxTileRow);
90
    		limit.setMaxTileCol(maxTileCol);
91
    		
92
    		//Calcula las coordenadas de la esquina superior izquierda del TileMatrixLimits
93
    		double ulx = worldExtent.getMinX() + (minTileCol * tilePxWidth * pixelSizeList[0][i]);
94
    		double uly = worldExtent.getMaxY() - (minTileRow * tilePxHeight * pixelSizeList[1][i]);
95
    		limit.getTileMatrix().setTopLeftCorner(new double[]{ulx, uly});
96
    		limit.getTileMatrix().setScaleXDenominator(pixelSizeList[0][i]);
97
    		limit.getTileMatrix().setScaleYDenominator(pixelSizeList[1][i]);
98
    		limit.getTileMatrix().setTileWidth(tilePxWidth);
99
    		limit.getTileMatrix().setTileHeight(tilePxHeight);
100
    		
101
    		limits.add(limit);
102
    	}
103
    }
104
    
105
    private double[][] buildWorldMatrix(int levels, 
106
    		int tilePxWidth, 
107
    		int tilePxHeight,
108
    		Rectangle2D worldExtent) {
109
    	double[] pixelSizeXList = new double[levels];
110
    	double[] pixelSizeYList = new double[levels];
111
    	
112
    	limits = new ArrayList<TileMatrixLimits>();
113
    	tileMatrixSet = new TileMatrixSet();
114
    	tileMatrixSet.setBbox(worldExtent);
115
    	
116
    	int nTilesWidth = 0;
117
    	int nTilesHeight = 0;
118
    		
119
    	for (int i = 0; i < levels; i++) {
120
    		TileMatrix tm = new TileMatrix();
121
    		tm.setTileWidth(tilePxWidth);
122
    		tm.setTileHeight(tilePxHeight);
123
    	
124
    		nTilesWidth = (i == 0) ? 1 : nTilesWidth * 2;
125
    		nTilesHeight = (i == 0) ? 1 : nTilesHeight * 2;
126
    		
127
    		tm.setMatrixWidth(nTilesWidth);
128
    		tm.setMatrixHeight(nTilesHeight);
129
    		if((tilePxWidth * nTilesWidth) != 0)
130
    			pixelSizeXList[i] = worldExtent.getWidth() / ((long)tilePxWidth * (long)nTilesWidth);
131
    		else
132
    			pixelSizeXList[i] = 0;
133
    		
134
    		if((tilePxHeight * nTilesHeight) != 0)
135
    			pixelSizeYList[i] = worldExtent.getHeight() / ((long)tilePxHeight * (long)nTilesHeight);
136
    		else
137
    			pixelSizeYList[i] = 0;
138
    		//System.out.println("-Level:" + i + " " + nTilesWidth + " " + nTilesHeight + " " + pixelSizeList[i]);
139
    		tm.setScaleXDenominator(pixelSizeXList[i]);
140
    		tm.setScaleYDenominator(pixelSizeYList[i]);
141
    		tileMatrixSet.getTileMatrix().add(tm);
142
    	}
143
    	
144
    	double[][] r = new double [2][];
145
    	r[0] = pixelSizeXList;
146
    	r[1] = pixelSizeYList;
147
    	return r;
148
    }
149
    
150
    public Extent getWorldExtent() {
151
    	return RasterLocator.getManager().getDataStructFactory().createExtent(worldExtent);
152
    }
153

  
154
	public int getNumberOfLevels() {
155
		return tileMatrixSet.getTileMatrix().size();
156
	}
157

  
158
	public String getLayerName() {
159
		return layerName;
160
	}
161

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff