Revision 10413

View differences:

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

  
24
import java.io.File;
25
import java.io.IOException;
26
import java.util.ArrayList;
27

  
28
import org.gvsig.raster.cache.tile.TileCache;
29
import org.gvsig.raster.cache.tile.TileCacheLibrary;
30
import org.gvsig.raster.cache.tile.TileCacheLocator;
31
import org.gvsig.raster.cache.tile.disk.ITileFileSystemStrategy;
32
import org.gvsig.raster.cache.tile.impl.layer.TiledLayerImpl;
33
import org.gvsig.raster.cache.tile.layer.TiledLayer;
34
import org.gvsig.raster.cache.tile.provider.TileServer;
35

  
36
/**
37
 * Main implementation for the tile cache
38
 *
39
 * @author Nacho Brodin (nachobrodin@gmail.com)
40
 */
41
public class TileCacheImpl implements TileCache {
42
	private ArrayList<TiledLayerImpl>   layerList            = null;
43
	//private MemoryManager               memoryManager        = null;
44
	private String                      baseDir              = null;
45
	private double                      currentSize          = 0;
46
	protected String                    preffixSizeFileName  = "c_len";
47
	protected String                    configDir            = ".metadata";
48
	
49
	public TileCacheImpl(String baseDir) {
50
		this.layerList = new ArrayList<TiledLayerImpl>();
51
		//memoryManager = new MemoryManager();
52
		this.baseDir = baseDir;
53
		loadSize();
54
	}
55
	
56
	public void updateBaseDirectory(String dir) {
57
		this.baseDir = dir;
58
		for (int i = 0; i < layerList.size(); i++) {
59
			layerList.get(i).updateBaseDirectory(dir);
60
		}
61
	}
62
	
63
	/**
64
	 * Updates the size of the tiles downloaded
65
	 * @param sizeBytes
66
	 */
67
	public synchronized void updateSize(long sizeBytes) {
68
		currentSize +=  ((double)sizeBytes);
69
	}
70
	
71
	/**
72
	 * Loads the size of the directory
73
	 */
74
	private void loadSize() {
75
		File f = new File(baseDir + File.separator + configDir);
76
		if(!f.exists()) {
77
			f.mkdirs();
78
			saveSize();
79
		}
80
		String[] list = f.list();
81
		for (int i = 0; i < list.length; i++) {
82
			if(list[i].startsWith(preffixSizeFileName)) {
83
				String s = list[i].substring(5, list[i].length());
84
				try {
85
					currentSize = Double.valueOf(s);
86
				} catch (NumberFormatException e) {
87
				}
88
			}
89
		}
90
	}
91
	
92
	/**
93
	 * Saves a size file
94
	 * @throws IOException
95
	 */
96
	public synchronized void saveSize() {
97
		String dir = baseDir + File.separator + configDir;
98
		File f = new File(dir);
99
		if(!f.exists()) {
100
			f.mkdirs();
101
		}
102
		String[] list = f.list();
103
		if(list != null) {
104
			for (int i = 0; i < list.length; i++) {
105
				if(list[i].startsWith(preffixSizeFileName)) {
106
					new File(dir + File.separator + list[i]).delete();
107
				}
108
			}
109
		}
110
		try {
111
			new File(dir + File.separator + preffixSizeFileName + (int)currentSize).createNewFile();
112
		} catch (IOException e) {
113
		}
114
	}
115
	
116
	public boolean isFullDiskCache() {
117
		return currentSize >= (TileCacheLibrary.MAX_CACHE_SIZE *  1048576);
118
	}
119
	
120
	public TiledLayer createLayer(TileServer provider, String strategyType) {
121
		ITileFileSystemStrategy strat = ((DefaultTileCacheManager)TileCacheLocator.getManager()).createStrategy(strategyType);
122
		TiledLayerImpl l = new TiledLayerImpl(this, provider, strat/*, memoryManager.createLRUTileCache()*/);
123
		layerList.add(l);
124
		return l;
125
	}
126
	
127
	/**
128
	 * Gets the base directory
129
	 * @return
130
	 */
131
	public String getBaseDirectory() {
132
		return baseDir;
133
	}
134
	
135
	public String getConfigurationDirectory() {
136
		return configDir;
137
	}
138
	
139
	public void removeLayer(TiledLayer layer) {
140
		layerList.remove(layer);
141
		updateSize(-layer.delete());
142
		saveSize();
143
	}
144
}
0 145

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

  
24

  
25
/**
26
 * Manager for the memory distribution among different layers when the LRUTileCache
27
 * is used
28
 *
29
 * @author Nacho Brodin (nachobrodin@gmail.com)
30
 */
31
public class MemoryManager {
32

  
33
	/**
34
	 * Gets a new LRUTileCache object
35
	 * @return
36
	 */
37
	public LRUTileCache createLRUTileCache() {
38
		return null;
39
	}
40

  
41
}
0 42

  
org.gvsig.raster.cache/tags/org.gvsig.raster.cache-2.2.41/org.gvsig.raster.cache.lib.impl/src/main/java/org/gvsig/raster/cache/tile/impl/memory/LRUTileCache.java
1
/* gvSIG Mini. A free mobile phone viewer of free maps.
2
 *
3
 * Copyright (C) 2009 Prodevelop.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Prodevelop, S.L.
22
 *   Pza. Don Juan de Villarrasa, 14 - 5
23
 *   46001 Valencia
24
 *   Spain
25
 *
26
 *   +34 963 510 612
27
 *   +34 963 510 968
28
 *   prode@prodevelop.es
29
 *   http://www.prodevelop.es
30
 *
31
 *   gvSIG Mini has been partially funded by IMPIVA (Instituto de la Peque�a y
32
 *   Mediana Empresa de la Comunidad Valenciana) &
33
 *   European Union FEDER funds.
34
 *   
35
 *   2009.
36
 *   author Rub�n Blanco rblanco@prodevelop.es
37
 *
38
 *
39
 * Original version of the code made by Nicolas Gramlich.
40
 * No header license code found on the original source file.
41
 * 
42
 * Original source code downloaded from http://code.google.com/p/osmdroid/
43
 * package org.andnav.osm.views.util;
44
 * 
45
 * License stated in that website is 
46
 * http://www.gnu.org/licenses/lgpl.html
47
 * As no specific LGPL version was specified, LGPL license version is LGPL v2.1
48
 * is assumed.
49
 *  
50
 * 
51
 */
52
package org.gvsig.raster.cache.tile.impl.memory;
53

  
54

  
55
//import org.gvsig.raster.cache.buffer.Buffer;
56

  
57
/**
58
 * A Last Recent Used cache of Bitmaps
59
 * @author aromeu 
60
 * @author rblanco
61
 *
62
 */
63
public class LRUTileCache {
64
//	extends HashMap<String, Buffer> {
65
//	private static final long        serialVersionUID = 6215141;
66
//	private final int                maxCacheSize;
67
//	private LinkedList<String>       list             = null;
68
//	private final static Logger      log              = Logger.getLogger("LRUTileCache");
69
//
70
//	/**
71
//	 * The Constructor
72
//	 * @param maxCacheSize The cache size in number of Bitmaps
73
//	 */
74
//	public LRUTileCache(final int maxCacheSize) {
75
//		super(maxCacheSize);
76
//		this.maxCacheSize = Math.max(0, maxCacheSize);
77
//		this.list = new LinkedList<String>();
78
//	}
79
//
80
//	/**
81
//	 * Clears the cache
82
//	 */
83
//	public synchronized void clear() {		
84
//		try {
85
//			/*for (final Buffer b : this.values()) {
86
//				if (b != null) {
87
//					b.recycle();
88
//				}
89
//			}*/
90
//			super.clear();
91
//			list.clear();
92
//		} catch (Exception e) {
93
//			log.log(Level.SEVERE, "clear cache: ", e);
94
//		}
95
//	}
96
//
97
//	/**
98
//	 * Adds a Bitmap to the cache. If the cache is full, removes the last
99
//	 */
100
//	public synchronized Buffer put(final String key, final Buffer value) {
101
//		try {
102
//			if (maxCacheSize == 0) {
103
//				return null;
104
//			}
105
//
106
//			// if the key isn't in the cache and the cache is full...
107
//			if (!super.containsKey(key) && !list.isEmpty()
108
//					&& list.size() + 1 > maxCacheSize) {
109
//				final Object deadKey = list.removeLast();
110
//				super.remove(deadKey);
111
//				//if (bitmap != null)
112
//					//bitmap.recycle();
113
//			}
114
//
115
//			updateKey(key);			
116
//		} catch (Exception e) {
117
//			log.log(Level.SEVERE, "put", e);
118
//		}
119
//		return super.put(key, value);		
120
//	}
121
//
122
//	/**
123
//	 * Returns a cached Bitmap given a key
124
//	 * @param key The key of the Bitmap stored on the HashMap
125
//	 * @return The Bitmap or null if it is not stored in the cache
126
//	 */
127
//	public synchronized Buffer get(final String key) {
128
//		try {
129
//			final Buffer value = super.get(key);
130
//			if (value != null) {
131
//				updateKey(key);
132
//			}
133
//			return value;
134
//		} catch (Exception e) {
135
//			log.log(Level.SEVERE, "get",  e);
136
//			return null;
137
//		}		
138
//	} 
139
//
140
//	/**
141
//	 * Removes a Bitmap from the cache
142
//	 * @param key The key of the Bitmap to remove
143
//	 */
144
//	public synchronized void remove(final String key) {
145
//		try {
146
//			list.remove(key);			
147
//		} catch (Exception e) {
148
//			log.log(Level.SEVERE, "remove", e);			
149
//		}
150
//		super.remove(key);
151
//		//if (bitmap != null)
152
//			//bitmap.recycle();		
153
//	}
154
//
155
//	/**
156
//	 * The key is touched (recent used) and added to the top of the list
157
//	 * @param key The key to be updated
158
//	 */
159
//	private void updateKey(final String key) {
160
//		try {
161
//			list.remove(key);
162
//			list.addFirst(key);
163
//		} catch (Exception e) {
164
//			log.log(Level.SEVERE, "updatekey", e);
165
//		}		
166
//	}
167
}
0 168

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

  
24
import java.util.Collections;
25
import java.util.LinkedList;
26

  
27
import org.gvsig.raster.cache.tile.Tile;
28
import org.gvsig.raster.cache.tile.exception.TileGettingException;
29
import org.gvsig.raster.cache.tile.pool.AtomicTask;
30
import org.gvsig.raster.cache.tile.pool.ThreadPool;
31
import org.gvsig.raster.cache.tile.pool.TilePipe;
32
import org.gvsig.raster.cache.tile.provider.TileListener;
33

  
34
/**
35
 * Pool of threads. This class contains a list of tasks and a limited number
36
 * of threads to execute them. When a thread is free will execute the next task 
37
 * in the list. 
38
 *
39
 * @author Nacho Brodin (nachobrodin@gmail.com)
40
 */
41
public class ThreadPoolImpl implements ThreadPool {
42
	private int                        NTHREADS_POOL   = 5;
43
	private ThreadTask[]               threadList      = null;
44
	private LinkedList<AtomicTask>     tasks           = new LinkedList<AtomicTask>();
45
	private boolean                    priorityActive  = false;
46
	private TilePipe                   tilePipe        = null;
47

  
48
	/**
49
	 * Constructor. Only for testing purposes. After this constructor the method init
50
	 * has to be called
51
	 */
52
	public ThreadPoolImpl() {
53
		this.priorityActive = true;
54
		threadList = new ThreadTask[NTHREADS_POOL];
55
	}
56
	
57
	/**
58
	 * Constructor
59
	 * @param priorityActive true if the pool runs using priorities and false if doesn't
60
	 * @param nThreads Number of threads of this pool
61
	 */
62
	public ThreadPoolImpl(boolean priorityActive, int nThreads) {
63
		this.priorityActive = true;
64
		NTHREADS_POOL = nThreads;
65
		init();
66
	}
67
	
68
	/**
69
	 * Constructor
70
	 * @param priorityActive true if the pool runs using priorities and false if doesn't
71
	 */
72
	public ThreadPoolImpl(boolean priorityActive) {
73
		this.priorityActive = priorityActive;
74
		init();
75
	}
76
	
77
	public void init() {
78
		if(threadList == null)
79
			threadList = new ThreadTask[NTHREADS_POOL];
80
		for (int i = 0; i < NTHREADS_POOL; i++) {
81
			threadList[i] = new ThreadTask(this);
82
			threadList[i].start();
83
		}
84
	}
85
	
86
	/**
87
	 * Cancel the pool of threads and destroy the pool
88
	 * @param listener
89
	 */
90
	public void cancelPool(TileListener listener) {
91
		if(threadList != null) {
92
			for (int i = 0; i < NTHREADS_POOL; i++) {
93
				if(threadList[i] != null) {
94
					Runnable r = threadList[i].stopThread();
95
					try {
96
						if(listener != null)
97
							listener.tileReady((Tile)r);
98
					} catch (TileGettingException e) {
99
					}
100
				}
101
			}
102
		}
103
		for (int i = 0; i < tasks.size(); i++) {
104
			try {
105
				if(listener != null)
106
					listener.tileReady((Tile)tasks.get(i));
107
			} catch (TileGettingException e) {
108
			}
109
		}
110
		synchronized (tasks) {
111
			tasks.notifyAll();
112
		}
113
		destroyPool();
114
	}
115
	
116
	/**
117
	 * Cleans the input and output pool
118
	 * @param listener
119
	 */
120
	public void emptyPool() {
121
		//Don't get and set more tiles
122
		for (int i = 0; i < threadList.length; i++) {
123
			threadList[i].cancelTaskInProgress();
124
		}
125
		
126
		//Cleans queues
127
		synchronized (tasks) {
128
			tasks.clear();
129
			getTilePipe().clear();
130
			tasks.notifyAll();
131
		}
132
		
133
		//Gets tiles again
134
		for (int i = 0; i < threadList.length; i++) {
135
			threadList[i].resumeTasks();
136
		}
137
	}
138
	
139
	public LinkedList<AtomicTask> getInputTaskList() {
140
		return tasks;
141
	}
142
	
143
	/*
144
	 * (non-Javadoc)
145
	 * @see org.gvsig.raster.cache.tile.pool.ThreadPool#getTilePipe()
146
	 */
147
	public TilePipe getTilePipe() {
148
		if(tilePipe == null)
149
			tilePipe = new TilePipeImpl();
150
		return tilePipe;
151
	}
152
	
153
	/**
154
	 * Stops all threads in the list and sets the list to null
155
	 * @deprecated
156
	 */
157
	public void destroyPool() {
158
		for (int i = 0; i < NTHREADS_POOL; i++) {
159
			threadList[i].stopThread();
160
			threadList[i] = null;
161
		}
162
		threadList = null;
163
	}
164
	
165
	/**
166
	 * Returns true if the pool is valid
167
	 * @return
168
	 */
169
	public boolean isPoolValid() {
170
		return !(threadList == null);
171
	}
172

  
173
	/**
174
	 * Adds a new task to the list
175
	 * @param task
176
	 */
177
	public void addTask(AtomicTask task) {
178
		synchronized (tasks) {
179
			tasks.addLast(task);
180
			tasks.notify();
181
		}
182
	}
183

  
184
	/**
185
	 * Gets the next task in the list
186
	 * @return
187
	 */
188
	public AtomicTask getNext() {
189
		AtomicTask returnVal = null;
190
		synchronized (tasks) {
191
			while (tasks.isEmpty()) {
192
				try {
193
					tasks.wait();
194
				} catch (InterruptedException ex) {
195
					System.err.println("Interrupted");
196
				}
197
			}
198
			if(priorityActive)
199
				sort(tasks);
200
			returnVal = tasks.removeFirst();
201
		}
202
		return returnVal;
203
	}
204
	
205
	/**
206
	 * Sorts the array of tasks. Each atomic task have to implement AtomicTask interface
207
	 * which is Runnable and Comparable. The last one allow to sort the array 
208
	 * by priorities.
209
	 * @param tasks
210
	 */
211
	private void sort(LinkedList<AtomicTask> tasks) {
212
		Collections.sort(tasks);
213
	}
214
}
0 215

  
org.gvsig.raster.cache/tags/org.gvsig.raster.cache-2.2.41/org.gvsig.raster.cache.lib.impl/src/main/java/org/gvsig/raster/cache/tile/impl/pool/ThreadTask.java
1
package org.gvsig.raster.cache.tile.impl.pool;
2

  
3
import org.gvsig.raster.cache.tile.pool.AtomicTask;
4

  
5
/**
6
 * This represents one thread in the pool. This thread gets a new task in
7
 * the pool and executes it.
8
 *
9
 * @author Nacho Brodin (nachobrodin@gmail.com)
10
 */
11
class ThreadTask extends Thread {
12
	private ThreadPoolImpl pool         = null;
13
	private boolean        stop         = false;
14
	private AtomicTask     currentJob   = null; 
15
	private boolean        getMoreTiles = true;
16

  
17
	public ThreadTask(ThreadPoolImpl thePool) {
18
		pool = thePool;
19
	}
20

  
21
	public void run() {
22
		while (!stop) {
23
			if(getMoreTiles) {
24
				currentJob = pool.getNext();
25
				currentJob.run();
26
			} else {
27
				try {
28
					Thread.sleep(500);
29
				} catch (InterruptedException e) {
30
				}
31
			}
32
		}
33
	}
34
	
35
	/**
36
	 * Stops this thread
37
	 */
38
	public Runnable stopThread() {
39
		stop = true;
40
		return currentJob;
41
	}
42
	
43
	/**
44
	 * Cancels the task in progress and avoids that next tiles are gotten
45
	 */
46
	public void cancelTaskInProgress() {
47
		if(currentJob != null)
48
			currentJob.cancelTask();
49
		getMoreTiles = false;
50
	}
51
	
52
	/**
53
	 * Gets new tiles to processed of the input queue
54
	 */
55
	public void resumeTasks() {
56
		getMoreTiles = true;
57
	}
58
}
0 59

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

  
24
import java.util.LinkedList;
25
import java.util.Queue;
26

  
27
import org.gvsig.raster.cache.tile.Tile;
28
import org.gvsig.raster.cache.tile.pool.AtomicTask;
29
import org.gvsig.raster.cache.tile.pool.TilePipe;
30

  
31
/**
32
 * This class manages transactions between the producer and the consumer. The
33
 * producer download tiles from a server and the consumer is a tile provider
34
 * which will return downloaded tiles.
35
 *
36
 * @author Nacho Brodin (nachobrodin@gmail.com)
37
 */
38
public class TilePipeImpl implements TilePipe {
39
	private boolean              isEmpty        = true;
40
	private Queue<AtomicTask>    buffer         = new LinkedList<AtomicTask>();
41
	private boolean              debug          = false;
42
	
43
	public TilePipeImpl() {
44
		this.debug = false;
45
	}
46
	
47
	public TilePipeImpl(boolean debug) {
48
		this.debug = debug;
49
	}
50
	
51
	/**
52
	 * Sets a tile in the pipe
53
	 * @param tile
54
	 */
55
	public synchronized void setAtomicTask(AtomicTask task) {
56
		if(task != null)
57
			buffer.add(task);
58
		isEmpty = false;
59
		
60
		if(debug)
61
			System.err.println("Setting tile " + ((Tile)task).getCol() + " " + ((Tile)task).getRow());
62
		
63
		notify();
64
	}
65

  
66
	/**
67
	 * Gets a tile from the pipe
68
	 * @return
69
	 */
70
	public synchronized AtomicTask getAtomicTask() {
71
		if(debug)
72
			System.out.println("0-Getting tile ");
73
		while( isEmpty == true ) {
74
			try {
75
				wait();
76
			} catch( InterruptedException e ) {
77
			}
78
		}
79

  
80
		AtomicTask task = buffer.poll() ;
81
		
82
		if(debug)
83
			System.out.println("1-Getting tile " + ((Tile)task).getCol() + " " + ((Tile)task).getRow());
84
		
85
		if( getSize() == 0 )
86
			isEmpty = true;
87

  
88
		return task;
89
	}
90
	
91
	/**
92
	 * Returns the number of elements in the queue
93
	 * @return
94
	 */
95
	public synchronized int getSize() {
96
		return buffer.size();
97
	}
98
	
99
	/*
100
	 * (non-Javadoc)
101
	 * @see org.gvsig.raster.cache.tile.pool.TilePipe#clear()
102
	 */
103
	public void clear() {
104
		synchronized (buffer) {
105
			buffer.clear();
106
		}
107
	}
108
}
0 109

  
org.gvsig.raster.cache/tags/org.gvsig.raster.cache-2.2.41/org.gvsig.raster.cache.lib.impl/src/main/java/org/gvsig/raster/cache/tile/impl/disk/DefaultFileSystemStrategy.java
1
package org.gvsig.raster.cache.tile.impl.disk;
2

  
3
import org.gvsig.raster.cache.tile.disk.ITileFileSystemStrategy;
4

  
5

  
6
public abstract class DefaultFileSystemStrategy implements ITileFileSystemStrategy {
7
		
8
	private String              tileNameSuffix;
9
	private final static String DEFAULT_TILE_SUFFIX = ".tile.gvsig";
10
	
11
	public DefaultFileSystemStrategy() {
12
		
13
	}
14
	
15
	public DefaultFileSystemStrategy(String tileNameSuffix) {	
16
		this.tileNameSuffix = tileNameSuffix;	
17
	}
18
	
19
	public String getTileNameSuffix() {
20
		if (tileNameSuffix == null)
21
			return DEFAULT_TILE_SUFFIX;
22
		return this.tileNameSuffix;
23
	}
24
	
25
	public void setTileNameSuffix(String suffix) {
26
		this.tileNameSuffix = suffix;
27
	}
28
}
0 29

  
org.gvsig.raster.cache/tags/org.gvsig.raster.cache-2.2.41/org.gvsig.raster.cache.lib.impl/src/main/java/org/gvsig/raster/cache/tile/impl/disk/FlatXFileSystemStrategy.java
1
/* TileRasterCache, a library to download tiles from several sources.
2
 *
3
 * Copyright (C) 2010 Prodevelop SL.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *  
21
 *   Prodevelop Integraci�n de Tecnolog�as SL
22
 *   Plaza don Juan de Vilarrasa , 14-5
23
 *   46000 Valencia
24
 *   Spain
25
 *
26
 *   +34 963 510 612
27
 *   +34 963 510 968
28
 *   gis@prodevelop.es
29
 *   http://www.prodevelop.es
30
 *   
31
 *   author: Alberto Romeu Carrasco (aromeu@prodevelop.es)
32
 */
33

  
34
package org.gvsig.raster.cache.tile.impl.disk;
35

  
36
import java.io.File;
37

  
38

  
39
/**
40
 * This IFileSystemStrategy creates a directory for each zoom level of a layer.
41
 * Inside the directory of a zoom level, creates a directory for each X row and
42
 * inside the directory of an X row stores a file with the Y row, so
43
 * OSM/0/0/0.tile represents the tile http://osm/0/0/0.png
44
 * 
45
 * This strategy is the most spread for mobile map viewers (although is not the most efficient)
46
 * @author aromeu
47
 *
48
 */
49
public class FlatXFileSystemStrategy extends DefaultFileSystemStrategy {
50
	
51
	public FlatXFileSystemStrategy() {
52
		super();
53
	}
54

  
55
	public FlatXFileSystemStrategy(String tileNameSuffix) {
56
		super(tileNameSuffix);		
57
	}
58

  
59
	public String getRelativeToLayerDirTilePath(int row, 
60
			int col, 
61
			int zoomLevel, 
62
			String var, 
63
			String z, 
64
			String time) {
65
		if(zoomLevel < 0) {  //Real zoom level
66
			return new StringBuffer()
67
			.append(var)
68
			.append(File.separator)
69
			.append(z)
70
			.append(File.separator)
71
			.append(time)
72
			.append(File.separator)
73
			.append("real")
74
			.append(File.separator)
75
			.append(col)
76
			.append(File.separator)
77
			.append(row).toString();
78
		} else {
79
			return new StringBuffer()
80
			.append(var)
81
			.append(File.separator)
82
			.append(z)
83
			.append(File.separator)
84
			.append(time)
85
			.append(File.separator)
86
			.append(zoomLevel)
87
			.append(File.separator)
88
			.append(col)
89
			.append(File.separator)
90
			.append(row).toString();
91
		}
92
	}
93
	
94
	public String getLayerNameSuffix() {
95
		return "_flatx";
96
	}	
97

  
98
	public String getName() {
99
		return FLATX;
100
	}	
101
}
0 102

  
org.gvsig.raster.cache/tags/org.gvsig.raster.cache-2.2.41/org.gvsig.raster.cache.lib.impl/src/main/java/org/gvsig/raster/cache/tile/impl/provider/TileMatrixLimits.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.cache.tile.impl.provider;
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 sizeTile = getTileMatrix().getTileWidth() * getTileMatrix().getScaleDenominator();
52
		double initX = getTileMatrix().getTopLeftCorner()[0] + (sizeTile * x);
53
		double initY = getTileMatrix().getTopLeftCorner()[1] - (sizeTile * y);
54
		Point2D[] p = new Point2D[2];
55
		p[0] = new Point2D.Double(initX, initY);
56
		p[1] = new Point2D.Double(initX + sizeTile, initY - sizeTile);
57
		return p;
58
	}
59
	
60
	public int getMinTileRow() {
61
		return minTileRow;
62
	}
63
	
64
	public void setMinTileRow(int minTileRow) {
65
		this.minTileRow = minTileRow;
66
	}
67
	
68
	public int getMaxTileRow() {
69
		return maxTileRow;
70
	}
71
	
72
	public void setMaxTileRow(int maxTileRow) {
73
		this.maxTileRow = maxTileRow;
74
	}
75
	
76
	public int getMinTileCol() {
77
		return minTileCol;
78
	}
79
	
80
	public void setMinTileCol(int minTileCol) {
81
		this.minTileCol = minTileCol;
82
	}
83
	
84
	public int getMaxTileCol() {
85
		return maxTileCol;
86
	}
87
	
88
	public void setMaxTileCol(int maxTileCol) {
89
		this.maxTileCol = maxTileCol;
90
	}
91
	
92
	public void print() {
93
		System.out.println("  *****TileMatrixLimits******");
94
		System.out.println("  MaxTileCol:" + getMaxTileCol());
95
		System.out.println("  MaxTileRow:" + getMaxTileRow());
96
		System.out.println("  MinTileCol:" + getMinTileCol());
97
		System.out.println("  MinTileRow:" + getMinTileRow());
98
		if(tileMatrix != null)
99
			tileMatrix.print();
100
	}
101
}
0 102

  
org.gvsig.raster.cache/tags/org.gvsig.raster.cache-2.2.41/org.gvsig.raster.cache.lib.impl/src/main/java/org/gvsig/raster/cache/tile/impl/provider/DefaultCacheStruct.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.cache.tile.impl.provider;
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.raster.cache.tile.Tile;
30
import org.gvsig.raster.cache.tile.TileCacheLocator;
31
import org.gvsig.raster.cache.tile.disk.ITileFileSystemStrategy;
32
import org.gvsig.raster.cache.tile.exception.TileBuildException;
33
import org.gvsig.raster.cache.tile.impl.TileImpl;
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 DefaultCacheStruct implements CacheStruct {
41
    private TileMatrixSet                 tileMatrixSet  = null;
42
    private ArrayList<TileMatrixLimits>   limits         = null;
43
    private String                        layerName      = null;
44
    private String                        uri            = null;
45
	private Rectangle2D                   worldExtent    = null;
46
	private String                        fileSuffix     = null;
47
	private String                        epsg           = null;
48
	private long                          fileSize       = 0;
49
	private double                        realPixelSize  = 0;
50
	private Rectangle2D                   layerExtent    = null;
51
    
52
    public DefaultCacheStruct(int typeOfCoords, 
53
    		int levels, 
54
    		Rectangle2D layerExtent, 
55
    		double pixelSize,
56
    		int tilePxWidth,
57
    		int tilePxHeight,
58
    		String uri,
59
    		String layerName,
60
    		ITileFileSystemStrategy strategy,
61
    		String baseDir,
62
    		String fileSuffix,
63
    		String epsg,
64
    		long size) {
65
    	this.uri = uri;
66
    	this.layerName = layerName;
67
    	this.fileSuffix = fileSuffix;
68
    	this.fileSize = size;
69
    	this.realPixelSize = pixelSize;
70
    	this.layerExtent = layerExtent;
71
    	if(epsg != null && epsg.startsWith("EPSG:"))
72
    		this.epsg = epsg;
73
    	
74
    	//Coordenadas del mundo en geogr�ficas y planas
75
    	
76
    	if(typeOfCoords == CacheStruct.GEOGRAFIC) {
77
    		worldExtent = new Rectangle2D.Double(-180, -90, 360, 180);
78
    	} 
79
    	if(typeOfCoords == CacheStruct.FLAT) {
80
    		worldExtent = new Rectangle2D.Double(-20037508, -20037508, 40075016, 40075016);
81
    	}
82
    	
83
    	double[] pixelSizeList = buildWorldMatrix(typeOfCoords, levels, tilePxWidth, tilePxHeight, worldExtent);
84
        buildLayerMatrix(levels, tilePxWidth, tilePxHeight, worldExtent, layerExtent, pixelSizeList, pixelSize);
85
    }
86
    
87
    private void buildLayerMatrix(int levels, 
88
    		int tilePxWidth, 
89
    		int tilePxHeight,
90
    		Rectangle2D worldExtent,
91
    		Rectangle2D layerExtent,
92
    		double[] pixelSizeList,
93
    		double maxPixelSize) {
94
    	limits = new ArrayList<TileMatrixLimits>();
95

  
96
    	for (int i = 0; i < levels; i++) {
97
    		//Calculo de tiles para ese nivel en la capa actual
98
    		int minTileCol = (int)(Math.abs(layerExtent.getMinX() - worldExtent.getMinX()) / pixelSizeList[i]) / tilePxWidth;
99
    		int minTileRow = (int)(Math.abs(layerExtent.getMaxY() - worldExtent.getMaxY()) / pixelSizeList[i]) / tilePxHeight;
100
    		int maxTileCol = (int)(Math.abs(layerExtent.getMaxX() - worldExtent.getMinX()) / pixelSizeList[i]) / tilePxWidth;
101
    		int maxTileRow = (int)(Math.abs(layerExtent.getMinY() - worldExtent.getMaxY()) / pixelSizeList[i]) / tilePxHeight;
102
    		
103
    		//int imageTileWidth = (int)Math.ceil((layerExtent.getWidth() / pixelSizeList[i]) / tilePxWidth);
104
    		//int imageTileHeight = (int)Math.ceil((layerExtent.getHeight() / pixelSizeList[i]) / tilePxHeight);
105
    		TileMatrixLimits limit = new TileMatrixLimits();
106
    		limit.setMinTileRow(minTileRow);
107
    		limit.setMinTileCol(minTileCol);
108
    		limit.setMaxTileRow(maxTileRow);
109
    		limit.setMaxTileCol(maxTileCol);
110
    		
111
    		//Calcula las coordenadas de la esquina superior izquierda del TileMatrixLimits
112
    		double ulx = worldExtent.getMinX() + (minTileCol * tilePxWidth * pixelSizeList[i]);
113
    		double uly = worldExtent.getMaxY() - (minTileRow * tilePxHeight * pixelSizeList[i]);
114
    		limit.getTileMatrix().setTopLeftCorner(new double[]{ulx, uly});
115
    		limit.getTileMatrix().setScaleDenominator(pixelSizeList[i]);
116
    		limit.getTileMatrix().setTileWidth(tilePxWidth);
117
    		limit.getTileMatrix().setTileHeight(tilePxHeight);
118
    		
119
    		limits.add(limit);
120
    		if(i == (levels - 1) || (maxPixelSize != Double.POSITIVE_INFINITY && maxPixelSize != Double.NEGATIVE_INFINITY && pixelSizeList[i] < maxPixelSize)) {
121
    			//System.out.println("Image res:" + maxPixelSize + " Real res:" + pixelSizeList[i]);
122
    			break;
123
    		}
124
    	}
125
    }
126
    
127
    private double[] buildWorldMatrix(int typeOfCoords,
128
    		int levels, 
129
    		int tilePxWidth, 
130
    		int tilePxHeight,
131
    		Rectangle2D worldExtent) {
132
    	double[] pixelSizeList = new double[levels];
133
    	
134
    	limits = new ArrayList<TileMatrixLimits>();
135
    	tileMatrixSet = new TileMatrixSet();
136
    	tileMatrixSet.setBbox(worldExtent);
137
    	
138
    	int nTilesWidth = 0;
139
    	int nTilesHeight = 0;
140
    		
141
    	for (int i = 0; i < levels; i++) {
142
    		TileMatrix tm = new TileMatrix();
143
    		tm.setTileWidth(tilePxWidth);
144
    		tm.setTileHeight(tilePxHeight);
145
    		if(i == 0) {
146
    			nTilesWidth = (typeOfCoords == CacheStruct.FLAT) ? 1 : 2;
147
    			nTilesHeight = 1;
148
    		} else {
149
    			nTilesWidth *= 2;
150
    			nTilesHeight *= 2;
151
    		}
152
    		
153
    		tm.setMatrixWidth(nTilesWidth);
154
    		tm.setMatrixHeight(nTilesHeight);
155
    		if(tilePxWidth > 0 && nTilesWidth > 0)
156
    			pixelSizeList[i] = worldExtent.getWidth() / ((long)tilePxWidth * (long)nTilesWidth);
157
    		else
158
    			pixelSizeList[i] = 0;
159
    		//System.out.println("-Level:" + i + " " + nTilesWidth + " " + nTilesHeight + " " + pixelSizeList[i]);
160
    		tm.setScaleDenominator(pixelSizeList[i]);
161
    		tileMatrixSet.getTileMatrix().add(tm);
162
    	}
163
    	return pixelSizeList;
164
    }
165
    
166
    public Rectangle2D getWorldExtent() {
167
    	return worldExtent;
168
    }
169

  
170
	public int getNumberOfLevels() {
171
		return tileMatrixSet.getTileMatrix().size();
172
	}
173

  
174
	public String getLayerName() {
175
		return layerName;
176
	}
177

  
178
	public String getServerURL() {
179
		return uri;
180
	}
181

  
182
	public String getFileSuffix() {
183
		return fileSuffix;
184
	}
185

  
186
	public int[] getTileSizeByLevel(int level) {
187
		return new int[] {
188
			tileMatrixSet.getTileMatrix().get(level).getTileWidth(),
189
			tileMatrixSet.getTileMatrix().get(level).getTileHeight()
190
		};
191
	}
192

  
193
	public double[] getTileSizeInRealCoordsByLevel(int level) {
194
		return new double[] {
195
				getPixelSizeByLevel(level) * tileMatrixSet.getTileMatrix().get(level).getTileWidth(),
196
				getPixelSizeByLevel(level) *  tileMatrixSet.getTileMatrix().get(level).getTileHeight()
197
			};
198
	}
199

  
200
	public int getLayerWidthOfTileMatrixByLevel(int level) {
201
		if(level < limits.size()) {
202
			TileMatrixLimits l = limits.get(level);
203
			return (l.getMaxTileRow() - l.getMinTileRow()) + 1;
204
		}
205
		return 0;
206
	}
207

  
208
	public int getLayerHeightOfTileMatrixByLevel(int level) {
209
		if(level < limits.size()) {
210
			TileMatrixLimits l = limits.get(level);
211
			return (l.getMaxTileCol() - l.getMinTileCol()) + 1;
212
		}
213
		return 0;
214
	}
215

  
216
	public int getLayerInitXTilePositionByLevel(int level) {
217
		TileMatrixLimits l = limits.get(level);
218
		return l.getMinTileCol();
219
	}
220

  
221
	public int getLayerInitYTilePositionByLevel(int level) {
222
		TileMatrixLimits l = limits.get(level);
223
		return l.getMinTileRow();
224
	}
225

  
226
	public long getWorldHeightOfTileMatrixByLevel(int level) {
227
		return tileMatrixSet.getTileMatrix().get(level).getMatrixWidth();
228
	}
229

  
230
	public long getWorldWidthOfTileMatrixByLevel(int level) {
231
		return tileMatrixSet.getTileMatrix().get(level).getMatrixHeight();
232
	}
233
	
234
	public double getPixelSizeByLevel(int level) {
235
		if(level < 0)
236
			return realPixelSize;
237
		return tileMatrixSet.getTileMatrix().get(level).getScaleDenominator();
238
	}
239
	
240
	public Point2D[] getTileExtent(Tile tile) {
241
		return limits.get(tile.getLevel()).getTileExtent(tile.getRow(), tile.getCol());
242
	}
243
	
244
	public Point2D[] getTileExtent(int level, int col, int row) {
245
		return limits.get(level).getTileExtent(row, col);
246
	}
247
	
248
	public Tile getTileStructure(int level, int tileCol, int tileRow, Point2D ul, Point2D lr) throws TileBuildException  {
249
		int[] size = getTileSizeByLevel(level);
250
		Tile tile = TileCacheLocator.getManager().createTile(level, tileRow, tileCol);
251
		tile.setUl(ul);
252
		tile.setLr(lr);
253
		tile.setWidthPx(size[0]);
254
		tile.setHeightPx(size[1]);
255
		
256
		return tile;
257
	}
258
	
259
	public List<Tile> getTileList(Rectangle2D r) {
260
		int tilePxWidth = tileMatrixSet.getTileMatrix().get(0).getTileWidth();
261
		int tilePxHeight = tileMatrixSet.getTileMatrix().get(0).getTileHeight();
262
		int minTileCol = (int)(r.getX() / tilePxWidth);
263
		int minTileRow = (int)(r.getY() / tilePxHeight);
264
		int maxTileCol = (int)((r.getX() + r.getWidth()) / tilePxWidth);
265
		int maxTileRow = (int)((r.getY() + r.getHeight()) / tilePxHeight);
266
		double tileWCWidth = tilePxWidth * realPixelSize;
267
		double tileWCHeight = tilePxHeight * realPixelSize;
268
		
269
		//Calculo de tiles con sus coordenadas
270
		List<Tile> tileList = new ArrayList<Tile>();
271
		for (int i = minTileRow; i <= maxTileRow; i++) {
272
			for (int j = minTileCol; j <= maxTileCol; j++) {
273
				Point2D ulTile = new Point2D.Double(
274
						layerExtent.getMinX() + (tileWCWidth * j),
275
						layerExtent.getMaxY() - (tileWCHeight * i));
276
				Point2D lrTile = new Point2D.Double(ulTile.getX() + tileWCWidth, ulTile.getY() - tileWCHeight);
277
				Tile t = new TileImpl(tilePxWidth, tilePxHeight, i, j, ulTile, lrTile);
278
				t.setLevel(-1);
279
				int initTileX = tilePxWidth * j;
280
				int initTileY = tilePxHeight * i;
281
				Rectangle2D rTile = new Rectangle2D.Double(initTileX, initTileY, tilePxWidth, tilePxHeight);
282
				t.setCoordsPx(rTile);
283
				tileList.add(t);
284
			}
285
		}
286
		return tileList;
287
	}
288
	
289
	public List<Tile> getTileList(Point2D ul, Point2D lr, double mtsPixelRequest) {
290
		return getTileList(ul, lr, mtsPixelRequest, false);
291
	}
292
	
293
	public List<Tile> getTileList(Point2D ul, Point2D lr, double mtsPixelRequest, boolean increaseResolution) {
294
		//Selecci?n de nivel de resoluci?n
295
		int tilePxWidth = 0;
296
		int tilePxHeight = 0;
297
		double tileWCWidth = 0;
298
		double tileWCHeight = 0;
299
		int level = 0;
300
		double mtsPixelLevel = 0;
301
		boolean levelAssigned = false;
302
		for (int i = 0; i < tileMatrixSet.getTileMatrix().size(); i++) {
303
			mtsPixelLevel = tileMatrixSet.getTileMatrix().get(i).getScaleDenominator();
304
			if(clipDecimals(mtsPixelRequest, 6) >= clipDecimals(mtsPixelLevel, 6) || 
305
			   (!levelAssigned && i == (tileMatrixSet.getTileMatrix().size() - 1))) {
306
				level = Math.max(i, 0);
307
				if(increaseResolution)
308
					level ++;
309
				
310
				//Se ajusta al m?ximo nivel disponible para esa capa
311
				if(level > (limits.size() - 1)) {
312
					level = limits.size() - 1;
313
				}
314
				
315
				mtsPixelLevel = tileMatrixSet.getTileMatrix().get(level).getScaleDenominator();
316
				
317
				tilePxWidth = tileMatrixSet.getTileMatrix().get(level).getTileWidth();
318
				tilePxHeight = tileMatrixSet.getTileMatrix().get(level).getTileHeight();
319
				
320
				tileWCWidth = tilePxWidth * mtsPixelLevel;
321
				tileWCHeight = tilePxHeight * mtsPixelLevel;
322
				levelAssigned = true;
323
				break;
324
			}
325
		}
326
		
327
		//Calculo del rectangulo de tiles
328
		int minTileCol = (int)((Math.abs(ul.getX() - worldExtent.getMinX()) / mtsPixelLevel) / tilePxWidth);
329
		int minTileRow = (int)((Math.abs(ul.getY() - worldExtent.getMaxY()) / mtsPixelLevel) / tilePxHeight);
330
		double maxTC = ((Math.abs(lr.getX() - worldExtent.getMinX()) / mtsPixelLevel) / tilePxWidth);
331
		double maxTR = ((Math.abs(lr.getY() - worldExtent.getMaxY()) / mtsPixelLevel) / tilePxHeight);
332
		int maxTileCol = ((maxTC - ((int)maxTC)) == 0) ? (int)(maxTC - 1) : (int)maxTC;
333
		int maxTileRow = ((maxTR - ((int)maxTR)) == 0) ? (int)(maxTR - 1) : (int)maxTR;
334
		
335
		//Calculo de tiles con sus coordenadas
336
		List<Tile> tileList = new ArrayList<Tile>();
337
		for (int i = minTileRow; i <= maxTileRow; i++) {
338
			for (int j = minTileCol; j <= maxTileCol; j++) {
339
				Point2D ulTile = new Point2D.Double(
340
						worldExtent.getMinX() + (tileWCWidth * j),
341
						worldExtent.getMaxY() - (tileWCHeight * i));
342
				Point2D lrTile = new Point2D.Double(ulTile.getX() + tileWCWidth, ulTile.getY() - tileWCHeight);
343
				Tile t = new TileImpl(tilePxWidth, tilePxHeight, i, j, ulTile, lrTile);
344
				t.setLevel(level);
345
				tileList.add(t);
346
			}
347
		}
348
		return tileList;
349
	}
350
	
351
	public boolean compare(CacheStruct struct) {
352
		//Compara: n?mero de niveles, tama?o de tile, 
353
		//anchoXalto de la matriz, tama?o de pixel por nivel, 
354
		//coordenadas de al menos un tile de la matriz
355
		if(struct.getNumberOfLevels() == getNumberOfLevels()) {
356
			for (int i = 0; i < getNumberOfLevels(); i++) {
357
				if(	struct.getTileSizeByLevel(i)[0] == getTileSizeByLevel(i)[0] && 
358
					struct.getTileSizeByLevel(i)[1] == getTileSizeByLevel(i)[1] &&
359
					struct.getWorldHeightOfTileMatrixByLevel(i) == getWorldHeightOfTileMatrixByLevel(i) &&
360
					struct.getWorldWidthOfTileMatrixByLevel(i) == getWorldWidthOfTileMatrixByLevel(i) &&
361
					clipDecimals(struct.getPixelSizeByLevel(i), 2) == clipDecimals(getPixelSizeByLevel(i), 2) &&
362
					compareExtents(struct.getTileExtent(i, 0, 0), getTileExtent(i, 0, 0))) {
363
					return true;
364
				}
365
			}
366
		}
367
		return false;
368
	}
369
	
370
	public String getEPSG() {
371
		return epsg;
372
	}
373

  
374
	public String getFileSize() {
375
		return fileSize + "";
376
	}
377
	
378
	private boolean compareExtents(Point2D[] p, Point2D[] p1) {
379
		return (p[0].getX() == p1[0].getX() && p[0].getY() == p1[0].getY() &&
380
				p[1].getX() == p1[1].getX() && p[1].getY() == p1[1].getY());
381
	}
382
	
383
	public double clipDecimals(double num, int n) {
384
		long m = (long) Math.pow(10, n);
385
		long aux = Math.round(num * m);
386
		return (double) aux / (double) m;
387
	}
388

  
389
}
0 390

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

Also available in: Unified diff