Revision 193

View differences:

org.gvsig.raster.osm/tags/org.gvsig.raster.osm-2.2.15/org.gvsig.raster.osm.io/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.raster.osm.io.DefaultOSMIOLibrary
org.gvsig.raster.osm/tags/org.gvsig.raster.osm-2.2.15/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.net.HttpURLConnection;
30
import java.net.MalformedURLException;
31
import java.net.URI;
32
import java.net.URISyntaxException;
33
import java.net.URL;
34

  
35
import org.gvsig.compat.net.ICancellable;
36
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
37
import org.gvsig.raster.cache.tile.Tile;
38
import org.gvsig.raster.cache.tile.exception.TileGettingException;
39
import org.gvsig.raster.impl.provider.tile.BaseTileDownloader;
40
import org.gvsig.raster.osm.io.OSMDataParameters;
41

  
42
/** 
43
 * Tile getter for Open Street Map
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public class TileDownloaderForOSM extends BaseTileDownloader {
47
	private String server   = null;
48
	private String suffix   = null;
49
	private String slash    = "/";
50
	
51
	public TileDownloaderForOSM(RasterDataStore store, 
52
			int tileSize) {
53
		super(store, tileSize, tileSize);
54
		server = ((OSMDataParameters)store.getParameters()).getURI();
55
		suffix = ((OSMDataParameters)store.getParameters()).getTileSuffix();
56
	}
57
	
58
	public synchronized Tile downloadTile(Tile tile) throws TileGettingException {
59
		int level = tile.getLevel();
60
		int y = tile.getRow();
61
		int x = tile.getCol();
62
		try {
63
			URI uri = new URI(
64
					server + 
65
					(server.endsWith(slash) ? "" : slash) + 
66
					level + slash + 
67
					x + slash + 
68
					y + "." + suffix);
69

  
70
			downloadFile(uri.toURL(), tile.getFile(), tile.getCancelled());
71
		} catch (MalformedURLException e) {
72
			throw new TileGettingException(e);
73
		} catch (URISyntaxException e) {
74
			throw new TileGettingException(e);
75
		}
76
		readTileFromDisk(tile);
77
		return tile;
78

  
79
	}
80
	
81
	 public synchronized void downloadFile(URL url, File file, ICancellable cancel) throws TileGettingException {
82
	    	int IO_BUFFER_SIZE = 8 * 1024;
83
			int timeout =  30000;
84
			
85
			DataOutputStream dos;
86
			try {
87
				DataInputStream is;
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
				dos.close();
106
				is.close();
107
				is = null;
108
				dos = null;
109
			} catch (Exception e) {
110
				throw new TileGettingException(e);
111
			}
112
	    }
113
	
114
}
0 115

  
org.gvsig.raster.osm/tags/org.gvsig.raster.osm-2.2.15/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.fmap.dal.coverage.store.RasterDataStore;
26
import org.gvsig.raster.cache.tile.provider.CacheStruct;
27
import org.gvsig.raster.cache.tile.provider.Downloader;
28
import org.gvsig.raster.cache.tile.provider.TileServer;
29
import org.gvsig.raster.osm.cachestruct.OSMCacheStruct;
30
import org.gvsig.raster.osm.io.OSMDataParameters;
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 RasterDataStore            store                = 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(RasterDataStore store, String suffix, int levels) {
53
		this.store = store;
54
		this.suffix = suffix;
55
		this.levels = levels;
56
	}
57
	
58
	public Downloader getDownloader() {
59
		if(downloader == null) {
60
			downloader = new TileDownloaderForOSM(store, getStruct().getTileSizeByLevel(0)[0]);
61
		}
62
		return downloader;
63
	}
64

  
65
	public CacheStruct getStruct() {
66
		if(struct == null) {
67
			struct = new OSMCacheStruct(levels, 
68
					tileSize, 
69
					tileSize, 
70
					((OSMDataParameters)store.getParameters()).getOSMLayerName(), 
71
					RasterLibrary.pathTileCache, 
72
					suffix, 
73
					0);
74
		}
75
		return struct;
76
	}
77
	
78
	public void setStruct(CacheStruct struct) {
79
		//La structura de cache es proporcionada por el servidor
80
	}
81
	
82
	public String getFileSuffix() {
83
		return suffix;
84
	}
85
	
86
	public void setFileSuffix(String extension) {
87
		this.suffix = extension;
88
	}
89
}
0 90

  
org.gvsig.raster.osm/tags/org.gvsig.raster.osm-2.2.15/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
		OSMProvider.register();
39
		OSMDataParametersImpl.registerDynClass();
40
	}
41
}
0 42

  
org.gvsig.raster.osm/tags/org.gvsig.raster.osm-2.2.15/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
import java.util.List;
26

  
27
import org.gvsig.compat.net.ICancellable;
28
import org.gvsig.fmap.dal.DataServerExplorerParameters;
29
import org.gvsig.fmap.dal.DataStoreParameters;
30
import org.gvsig.fmap.dal.NewDataStoreParameters;
31
import org.gvsig.fmap.dal.coverage.exception.ConnectException;
32
import org.gvsig.fmap.dal.coverage.store.RasterDataServerExplorer;
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.spi.DataServerExplorerProvider;
35
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
36

  
37

  
38

  
39
public class OSMServerExplorer {
40
	public static final String NAME       = OSMProvider.NAME;
41

  
42
	public String getProviderName() {
43
		return OSMProvider.NAME;
44
	}
45

  
46
	public boolean canAdd() {
47
		return false;
48
	}
49

  
50
	public boolean canAdd(String storeName) throws DataException {
51
		return false;
52
	}
53

  
54
	public List<?> list() throws DataException {
55
		return null;
56
	}
57

  
58
	public List<?> list(int mode) throws DataException {
59
		return null;
60
	}
61

  
62
	public boolean add(String provider, NewDataStoreParameters parameters,
63
			boolean overwrite) throws DataException {
64
		return false;
65
	}
66

  
67
	public void remove(DataStoreParameters parameters) throws DataException {
68
		
69
	}
70

  
71
	public NewDataStoreParameters getAddParameters(String storeName)
72
			throws DataException {
73
		return null;
74
	}
75

  
76
	public DataServerExplorerParameters getParameters() {
77
		return null;
78
	}
79

  
80
	public List<?> getDataStoreProviderNames() {
81
		return null;
82
	}
83

  
84
	public void dispose() {
85
		
86
	}
87

  
88
	public DataServerExplorerProviderServices getServerExplorerProviderServices() {
89
		return null;
90
	}
91

  
92
	public DataStoreParameters getStoredParameters() {
93
		return null;
94
	}
95

  
96
	public void connect(ICancellable cancellable) throws ConnectException {
97
		
98
	}
99

  
100
	public boolean isHostReachable(int timeout) {
101
		return false;
102
	}
103

  
104
	public boolean isHostReachable() {
105
		return isHostReachable(RasterDataServerExplorer.TIME);
106
	}
107

  
108
}
0 109

  
org.gvsig.raster.osm/tags/org.gvsig.raster.osm-2.2.15/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.Extent;
32
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
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.TileServer;
45
import org.gvsig.raster.impl.buffer.SpiRasterQuery;
46
import org.gvsig.raster.impl.datastruct.ExtentImpl;
47
import org.gvsig.raster.impl.provider.AbstractRasterProvider;
48
import org.gvsig.raster.impl.provider.RasterProvider;
49
import org.gvsig.raster.impl.store.DefaultRasterStore;
50
import org.gvsig.raster.impl.store.DefaultStoreFactory;
51
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
52
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
53
import org.gvsig.raster.osm.cachestruct.OSMCacheStruct;
54
import org.gvsig.raster.osm.downloader.OSMTileServer;
55
import org.gvsig.tools.ToolsLocator;
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 AbstractRasterProvider {
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
		
121
		Extent ext = getExtent();
122
		ownTransformation = new AffineTransform(
123
				ext.width() / getWidth(), 0, 
124
				0, -(ext.height() / getHeight()), 
125
				getExtent().getMin().getX(), 
126
				getExtent().getMax().getY());
127
		externalTransformation = (AffineTransform)ownTransformation.clone();
128
		open = true;
129
	}
130
	
131
	public IProjection getProjection() {
132
		if(proj == null) {
133
			try {
134
				proj = CRSFactory.getCRS("EPSG:3857");
135
			} catch(Exception e) {
136
				logger.info("Projection not loaded", e);
137
			}
138
		}
139
		return proj;
140
	}
141
	
142
	/**
143
	 * Gets the bounding box in world coordinates. 
144
	 * @return Extent
145
	 */
146
	public Extent getExtent() {
147
		return ((OSMCacheStruct)getTileServer().getStruct()).getWorldExtent();
148
	}
149
	
150
	/**
151
	 * Reloads metadata using the selected grid
152
	 */
153
	@SuppressWarnings("unused")
154
	private void reloadMetadataFromGrid() {
155
		//wktProjection = null;
156
		//CrsWkt crs = new CrsWkt(wktProjection);
157
		//IProjection proj = CRSFactory.getCRS("EPSG:23030");
158
		
159
		/*ownTransformation = new AffineTransform(
160
				scaleX, 0, 
161
				0, -scaleY, 
162
				pRect.getMinX(), 
163
				pRect.getMaxY());*/
164
		externalTransformation = (AffineTransform)ownTransformation.clone();
165
		bandCount = 3; 
166
		setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE});
167
	}
168
	
169
	public RasterProvider load() {
170
		return this;
171
	}
172
	
173
	public boolean isOpen() {
174
		return open;
175
	}
176

  
177
	public void close() {
178
	}
179

  
180
	public String translateFileName(String fileName) {
181
		return fileName;
182
	}
183
	
184
	public NoData getNoDataValue() {
185
		NoData nodata = super.getNoDataValue();
186
		if(nodata != null)
187
			nodata.setNoDataTransparent(false);
188
		return noData;
189
	}
190

  
191
	/**
192
	 * Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci�n
193
	 * de la vista asignada ya que la petici�n viene en coordenadas del fichero .rmf y la vista (v)
194
	 * ha de estar en coordenadas del fichero.
195
	 */
196
	public void setView(Extent e) {
197
		viewRequest = new ExtentImpl(e);
198
	}
199

  
200
	public Extent getView() {
201
		return viewRequest;
202
	}
203

  
204
	public double getWidth() {
205
		int n = getTileServer().getStruct().getNumberOfLevels() - 1;
206
		return getExtent().width() / getTileServer().getStruct().getPixelSizeByLevel(n);
207
	}
208

  
209
	public double getHeight() {
210
		int n = getTileServer().getStruct().getNumberOfLevels() - 1;
211
		return getExtent().width() / getTileServer().getStruct().getPixelSizeByLevel(n);
212
	}
213

  
214
	public Object readBlock(int pos, int blockHeight, double scale)
215
		throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
216
		return null;
217
	}
218

  
219
	public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
220
		return null;
221
	}
222

  
223
	public int getBlockSize(){
224
		return 0;
225
	}
226

  
227
	/**
228
	 * Informa de si el driver ha supersampleado en el �ltimo dibujado. Es el driver el que colocar�
229
	 * el valor de esta variable cada vez que dibuja.
230
	 * @return true si se ha supersampleado y false si no se ha hecho.
231
	 */
232
	public boolean isSupersampling() {
233
		return false;
234
	}
235

  
236
	public void setAffineTransform(AffineTransform t){
237
		super.setAffineTransform(t);
238
	}
239

  
240
	public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
241
		if(band >= getBandCount())
242
			throw new BandAccessException("Wrong band");
243
		return 0;
244
	}
245

  
246
	public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
247
		if (band >= getBandCount())
248
			throw new BandAccessException("Wrong band");
249
		return 0;
250
	}
251

  
252
	public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
253
		if (band >= getBandCount())
254
			throw new BandAccessException("Wrong band");
255
		return 0;
256
	}
257

  
258
	public boolean isOverviewsSupported() {
259
		return true;
260
	}
261

  
262
	public boolean isReproyectable() {
263
		return true;
264
	}
265

  
266
	public String getProviderName() {
267
		return NAME;
268
	}
269

  
270
        public String getName() {
271
            return this.getParameters().getOSMLayerName();
272
        }
273
        
274
	public OSMDataParameters getParameters() {
275
		return (OSMDataParameters)this.param;
276
	}
277
        
278
	public DataStoreTransparency getTransparency() {
279
		if(fileTransparency == null)
280
			fileTransparency = new DataStoreTransparency(getColorInterpretation());
281
		return fileTransparency;
282
	}
283
	
284
	public ColorInterpretation getColorInterpretation() {
285
		if(super.getColorInterpretation() == null) {
286
			super.setColorInterpretation(DataStoreColorInterpretation.createRGBInterpretation());
287
		}
288
		return super.getColorInterpretation();
289
	}
290
	
291
	public void setStatus(RasterProvider provider) {
292
		if(provider instanceof OSMProvider) {
293
			//Not implemented yet
294
		}
295
	}
296
	
297
	public boolean isTimeSupported() {
298
		return true;
299
	}
300
	
301
	public boolean isTiled() {
302
		return true;
303
	}
304
	
305
	public TileServer getTileServer() {
306
		if(tileServer == null) {
307
			DefaultRasterStore store = new DefaultRasterStore();
308
			store.setProvider(this);
309
			tileServer = new OSMTileServer(store, 
310
					((OSMDataParameters)param).getTileSuffix(), 
311
					((OSMDataParameters)param).getNumberOfLevels());
312
		}
313
		return tileServer;
314
	}
315

  
316
	@Override
317
	public void loadBuffer(SpiRasterQuery query)
318
			throws ProcessInterruptedException, RasterDriverException {
319
		
320
	}
321
}
0 322

  
org.gvsig.raster.osm/tags/org.gvsig.raster.osm-2.2.15/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/tags/org.gvsig.raster.osm-2.2.15/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/tags/org.gvsig.raster.osm-2.2.15/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.fmap.dal.coverage.store.RasterDataServerExplorer;
26
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
27
import org.gvsig.raster.impl.store.AbstractRasterDataParameters;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dynobject.DelegatedDynObject;
30
import org.gvsig.tools.dynobject.DynStruct;
31
import org.gvsig.tools.persistence.PersistenceManager;
32

  
33
/**
34
 * Parameters for OSM provider
35
 * @author Nacho Brodin (nachobrodin@gmail.com)
36
 */
37
public class OSMDataParametersImpl extends AbstractRasterDataParameters implements OSMDataParameters {
38
	private DelegatedDynObject            delegatedDynObject         = null;
39
	private static final String           FIELD_WIDTH                = "width";
40
	private static final String           FIELD_HEIGHT               = "height";
41
	private static final String           FIELD_MAX_RES_LEVEL        = "maxResolutionLevel";
42
	private static final String           FIELD_NAME                 = "name";
43
	private static final String           FIELD_SUFFIX               = "suffix";
44
	
45
	public OSMDataParametersImpl() {
46
		super();
47
		initialize();
48
	}
49
	
50
	protected void initialize() {
51
		this.delegatedDynObject = (DelegatedDynObject) ToolsLocator
52
				.getDynObjectManager().createDynObject(registerDynClass());
53
	}
54
	
55
	public static DynStruct registerDynClass() {
56
		PersistenceManager manager = ToolsLocator.getPersistenceManager();
57
		DynStruct definition = manager.getDefinition("OSMDataParameters_Persistent");
58
		if( definition == null ) {
59
			definition = manager.addDefinition(
60
					OSMDataParametersImpl.class,
61
					"OSMDataParameters_Persistent",
62
					"OSM DataParameters Persistency",
63
					null, 
64
					null
65
			);
66
		}
67

  
68
		AbstractRasterDataParameters.registerDynClass(definition);
69

  
70
		definition.addDynFieldInt(FIELD_MAX_RES_LEVEL)
71
		.setDescription("Maximun levels of resolution")
72
		.setMandatory(false);
73

  
74
		definition.addDynFieldInt(FIELD_WIDTH)
75
		.setDescription("Width")
76
		.setMandatory(false);
77

  
78
		definition.addDynFieldInt(FIELD_HEIGHT)
79
		.setDescription("Height")
80
		.setMandatory(false);
81

  
82
		definition.addDynFieldString(FIELD_NAME)
83
		.setDescription("Layer name")
84
		.setMandatory(false);		
85

  
86
		definition.addDynFieldString(FIELD_SUFFIX)
87
		.setDescription("Field suffix")
88
		.setMandatory(false);
89

  
90
		return definition;
91
	}
92
	
93
	public void assignFields(RasterDataParameters par, RasterDataServerExplorer explorer) {
94
		super.assignFields(par, explorer);
95
		
96
		OSMDataParameters p = null;
97
		if(par instanceof OSMDataParameters)
98
			p = (OSMDataParameters)par;
99
		else
100
			return;
101
		
102
		setTileSuffix(p.getTileSuffix());
103
		setOSMLayerName(p.getOSMLayerName());
104
		setWidth(p.getWidth());
105
		setHeight(p.getHeight());
106
		setNumberOfLevels(p.getNumberOfLevels());
107
	}
108
	
109
	public String getDataStoreName() {
110
		return OSMProvider.NAME;
111
	}
112
	
113
	public String getDescription() {
114
		return OSMProvider.DESCRIPTION;
115
	}
116
	
117
	@Override
118
	protected DelegatedDynObject getDelegatedDynObject() {
119
		return delegatedDynObject;
120
	}
121

  
122
	public boolean isOverridingHost() {
123
		return false;
124
	}
125

  
126
	public void setOverrideHost(boolean over) {
127
		
128
	}
129
	
130
	public void setWidth(int w) {
131
		this.setDynValue(FIELD_WIDTH, new Integer(w));
132
	}
133
	
134
	public void setHeight(int h) {
135
		this.setDynValue(FIELD_HEIGHT, new Integer(h));
136
	}
137
	
138
	public int getWidth() {
139
		Integer b = (Integer)getDynValue(FIELD_WIDTH);
140
		if(b != null)
141
			return ((Integer)b).intValue();
142
		return 0;
143
	}
144
	
145
	public int getHeight() {
146
		Integer b = (Integer)getDynValue(FIELD_HEIGHT);
147
		if(b != null)
148
			return ((Integer)b).intValue();
149
		return 0;
150
	}
151
	
152
	public String getOSMLayerName() {
153
		String b = (String)getDynValue(FIELD_NAME);
154
		if(b != null)
155
			return b;
156
		return null;
157
	}
158
	
159
	public void setOSMLayerName(String name) {
160
		this.setDynValue(FIELD_NAME, name);
161
	}
162
	
163
	public int getNumberOfLevels() {
164
		Integer b = (Integer)getDynValue(FIELD_MAX_RES_LEVEL);
165
		if(b != null)
166
			return ((Integer)b).intValue();
167
		return 0;
168
	}
169
	
170
	public void setNumberOfLevels(int levels) {
171
		this.setDynValue(FIELD_MAX_RES_LEVEL, new Integer(levels));
172
	}
173
	
174
	public String getTileSuffix() {
175
		String b = (String)getDynValue(FIELD_SUFFIX);
176
		if(b != null)
177
			return b;
178
		return null;
179
	}
180
	
181
	public void setTileSuffix(String suffix) {
182
		this.setDynValue(FIELD_SUFFIX, suffix);
183
	}
184
}
0 185

  
org.gvsig.raster.osm/tags/org.gvsig.raster.osm-2.2.15/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

  
162
	public String getServerURL() {
163
		return layerName;
164
	}
165
	
166
	public String getFileSuffix() {
167
		return fileSuffix;
168
	}
169

  
170
	public int[] getTileSizeByLevel(int level) {
171
		return new int[] {
172
			tileMatrixSet.getTileMatrix().get(level).getTileWidth(),
173
			tileMatrixSet.getTileMatrix().get(level).getTileHeight()
174
		};
175
	}
176
	
177
	public double[] getTileSizeInRealCoordsByLevel(int level) {
178
		return new double[] {
179
				tileMatrixSet.getTileMatrix().get(level).getScaleXDenominator() * tileMatrixSet.getTileMatrix().get(level).getTileWidth(),
180
				tileMatrixSet.getTileMatrix().get(level).getScaleYDenominator() *  tileMatrixSet.getTileMatrix().get(level).getTileHeight()
181
			};
182
	}
183

  
184
	public int getLayerWidthOfTileMatrixByLevel(int level) {
185
		TileMatrixLimits l = limits.get(level);
186
		return l.getMaxTileRow() - l.getMinTileRow();
187
	}
188
	
189
	public int getLayerHeightOfTileMatrixByLevel(int level) {
190
		TileMatrixLimits l = limits.get(level);
191
		return l.getMaxTileCol() - l.getMinTileCol();
192
	}
193

  
194
	public int getLayerInitXTilePositionByLevel(int level) {
195
		TileMatrixLimits l = limits.get(level);
196
		return l.getMinTileCol();
197
	}
198

  
199
	public int getLayerInitYTilePositionByLevel(int level) {
200
		TileMatrixLimits l = limits.get(level);
201
		return l.getMinTileRow();
202
	}
203

  
204
	public long getWorldHeightOfTileMatrixByLevel(int level) {
205
		return tileMatrixSet.getTileMatrix().get(level).getMatrixWidth();
206
	}
207

  
208
	public long getWorldWidthOfTileMatrixByLevel(int level) {
209
		return tileMatrixSet.getTileMatrix().get(level).getMatrixHeight();
210
	}
211
	
212
	public double getPixelSizeByLevel(int level) {
213
		if(level < 0)
214
			return realPixelSize;
215
		return tileMatrixSet.getTileMatrix().get(level).getScaleXDenominator();
216
	}
217
	
218
	public Point2D[] getTileExtent(Tile tile) {
219
		return limits.get(tile.getLevel()).getTileExtent(tile.getRow(), tile.getCol());
220
	}
221
	
222
	public Point2D[] getTileExtent(int level, int col, int row) {
223
		return limits.get(level).getTileExtent(row, col);
224
	}
225
	
226
	public List<Tile> getTileList(Rectangle2D r) {
227
		//No se necesita el nivel de resoluci?n real ya que es solo para dibujado
228
		return null;
229
	}
230
	
231
	public List<Tile> getTileList(Point2D ul, Point2D lr, double mtsPixelXRequest) {
232
		//Selecci?n de nivel de resoluci?n
233
		int tilePxWidth = 0;
234
		int tilePxHeight = 0;
235
		double tileWCWidth = 0;
236
		double tileWCHeight = 0;
237
		int level = 0;
238
		double mtsPixelXLevel = 0;
239
		double mtsPixelYLevel = 0;
240
		for (int i = 0; i < tileMatrixSet.getTileMatrix().size(); i++) {
241
			mtsPixelXLevel = tileMatrixSet.getTileMatrix().get(i).getScaleXDenominator();
242
			mtsPixelYLevel = tileMatrixSet.getTileMatrix().get(i).getScaleYDenominator();
243
			if(clipDecimals(mtsPixelXRequest, 6) >= clipDecimals(mtsPixelXLevel, 6) 
244
				|| i == (tileMatrixSet.getTileMatrix().size() - 1)) {
245
				level = Math.max(i, 0);
246
				//Se ajusta al m?ximo nivel disponible para esa capa
247
				if(level > (limits.size() - 1)) {
248
					level = limits.size() - 1;
249
					mtsPixelXLevel = tileMatrixSet.getTileMatrix().get(level).getScaleXDenominator();
250
					mtsPixelYLevel = tileMatrixSet.getTileMatrix().get(level).getScaleYDenominator();
251
				}
252
				
253
				tilePxWidth = tileMatrixSet.getTileMatrix().get(level).getTileWidth();
254
				tilePxHeight = tileMatrixSet.getTileMatrix().get(level).getTileHeight();
255
				
256
				//mtsPixelLevel = tileMatrixSet.getTileMatrix().get(i - 1).getScaleDenominator();
257
				tileWCWidth = tilePxWidth * mtsPixelXLevel;
258
				tileWCHeight = tilePxHeight * mtsPixelYLevel;
259
				break;
260
			}
261
		}
262
		
263
		//Calculo del rectangulo de tiles
264
		int minTileCol = (int)((Math.abs(ul.getX() - worldExtent.getMinX()) / mtsPixelXLevel) / tilePxWidth);
265
		int minTileRow = (int)((Math.abs(ul.getY() - worldExtent.getMaxY()) / mtsPixelYLevel) / tilePxHeight);
266
		double maxTC = ((Math.abs(lr.getX() - worldExtent.getMinX()) / mtsPixelXLevel) / tilePxWidth);
267
		double maxTR = ((Math.abs(lr.getY() - worldExtent.getMaxY()) / mtsPixelYLevel) / tilePxHeight);
268
		int maxTileCol = ((maxTC - ((int)maxTC)) == 0) ? (int)(maxTC - 1) : (int)maxTC;
269
		int maxTileRow = ((maxTR - ((int)maxTR)) == 0) ? (int)(maxTR - 1) : (int)maxTR;
270
		
271
		//Calculo de tiles con sus coordenadas
272
		List<Tile> tileList = new ArrayList<Tile>();
273
		for (int i = minTileRow; i <= maxTileRow; i++) {
274
			for (int j = minTileCol; j <= maxTileCol; j++) {
275
				Point2D ulTile = new Point2D.Double(
276
						worldExtent.getMinX() + (tileWCWidth * j),
277
						worldExtent.getMaxY() - (tileWCHeight * i));
278
				Point2D lrTile = new Point2D.Double(ulTile.getX() + tileWCWidth, ulTile.getY() - tileWCHeight);
279
				Tile t = TileCacheLocator.getManager().createTile(tilePxWidth, tilePxHeight, i, j, ulTile, lrTile);
280
				t.setLevel(level);
281
				tileList.add(t);
282
			}
283
		}
284
		return tileList;
285
	}
286
	
287
	public boolean compare(CacheStruct struct) {
288
		//Compara: n?mero de niveles, tama?o de tile, 
289
		//anchoXalto de la matriz, tama?o de pixel por nivel, 
290
		//coordenadas de al menos un tile de la matriz
291
		if(struct.getNumberOfLevels() == getNumberOfLevels()) {
292
			for (int i = 0; i < getNumberOfLevels(); i++) {
293
				if(	struct.getTileSizeByLevel(i)[0] == getTileSizeByLevel(i)[0] && 
294
					struct.getTileSizeByLevel(i)[1] == getTileSizeByLevel(i)[1] &&
295
					struct.getWorldHeightOfTileMatrixByLevel(i) == getWorldHeightOfTileMatrixByLevel(i) &&
296
					struct.getWorldWidthOfTileMatrixByLevel(i) == getWorldWidthOfTileMatrixByLevel(i) &&
297
					clipDecimals(struct.getPixelSizeByLevel(i), 2) == clipDecimals(getPixelSizeByLevel(i), 2) &&
298
					compareExtents(struct.getTileExtent(i, 0, 0), getTileExtent(i, 0, 0))) {
299
					return true;
300
				}
301
			}
302
		}
303
		return false;
304
	}
305
	
306
	public String getEPSG() {
307
		return epsg;
308
	}
309

  
310
	public String getFileSize() {
311
		return fileSize + "";
312
	}
313
	
314
	private boolean compareExtents(Point2D[] p, Point2D[] p1) {
315
		return (p[0].getX() == p1[0].getX() && p[0].getY() == p1[0].getY() &&
316
				p[1].getX() == p1[1].getX() && p[1].getY() == p1[1].getY());
317
	}
318
	
319
	public double clipDecimals(double num, int n) {
320
		long m = (long) Math.pow(10, n);
321
		long aux = Math.round(num * m);
322
		return (double) aux / (double) m;
323
	}
324

  
325
	public Tile getTileStructure(int level, int tileCol, int tileRow,
326
			Point2D ul, Point2D lr) throws TileBuildException {
327
		int[] size = getTileSizeByLevel(level);
328
		Tile tile = TileCacheLocator.getManager().createTile(level, tileRow, tileCol);
329
		tile.setUl(ul);
330
		tile.setLr(lr);
331
		tile.setWidthPx(size[0]);
332
		tile.setHeightPx(size[1]);
333
			
334
		return tile;
335
	}
336

  
337
}
0 338

  
org.gvsig.raster.osm/tags/org.gvsig.raster.osm-2.2.15/org.gvsig.raster.osm.io/src/main/java/org/gvsig/raster/osm/cachestruct/TileMatrixSet.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.Rectangle2D;
25
import java.io.IOException;
26
import java.util.ArrayList;
27

  
28
import org.kxml2.io.KXmlParser;
29
import org.xmlpull.v1.XmlPullParserException;
30

  
31

  
32
/**
33
 * <p>Defines a set of tiles</p>
34
 * @author Nacho Brodin (nachobrodin@gmail.com)
35
 */
36
public class TileMatrixSet {
37
    protected Rectangle2D                 bbox                = null;
38
    private String                        supportedCRS        = null;
39
    protected ArrayList<TileMatrix>       tileMatrix          = null;
40
    
41
	public void setBbox(Rectangle2D bbox) {
42
		this.bbox = bbox;
43
	}
44
	
45
    public Rectangle2D getBoundingBox() {
46
    	return bbox;
47
    }
48
	
49
	public ArrayList<TileMatrix> getTileMatrix() {
50
		if(tileMatrix == null)
51
			tileMatrix = new ArrayList<TileMatrix>();
52
		return tileMatrix;
53
	}
54

  
55
	public String getSupportedCRS() {
56
		return supportedCRS;
57
	}
58

  
59
	public void setSupportedCRS(String supportedCRS) {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff