Revision 25500

View differences:

tags/3D_Animation_v1_0alpha_Build_5/libraries/libCacheService/src/org/gvsig/cacheservice/FeatureCacheService.java
1
package org.gvsig.cacheservice;
2

  
3
public class FeatureCacheService extends CacheService {
4

  
5
	public FeatureCacheService(String planet, String name) {
6
		super(planet, name);
7
		// TODO Auto-generated constructor stub
8
	}
9

  
10
	public String getFileExtension() {
11
		// TODO Auto-generated method stub
12
		return null;
13
	}
14

  
15
}
tags/3D_Animation_v1_0alpha_Build_5/libraries/libCacheService/src/org/gvsig/cacheservice/CacheServiceException.java
1
package org.gvsig.cacheservice;
2
/**
3
 * 
4
 * @author Rafa Gait�n <rgaitan@dsic.upv.es>
5
 *
6
 */
7
public class CacheServiceException extends Exception {
8

  
9
	/**
10
	 * 
11
	 */
12
	private static final long serialVersionUID = 1L;
13
	
14
	public CacheServiceException(Exception e) {
15
		super(e);
16
	}
17
}
tags/3D_Animation_v1_0alpha_Build_5/libraries/libCacheService/src/org/gvsig/cacheservice/CacheService.java
1
package org.gvsig.cacheservice;
2

  
3
import java.awt.Dimension;
4
import java.awt.Point;
5
import java.awt.geom.Rectangle2D;
6
import java.io.File;
7
/**
8
 *
9
 * @author Rafael Gait�n <rgaitan@dsic.upv.es>, modified code from cq RasterCache of Luis W. Sevilla.
10
 *
11
 */
12
public abstract class CacheService {
13

  
14
	/**
15
	 * Tipo planetario. Las coordenadas son Lat/Lon
16
	 */
17
	public static int SPHERIC	= 0x10;
18
	/**
19
	 * Tipo plano (UTM). Las coordenadas son m�tricas.
20
	 */
21
	public static int PLANE		= 0x20;
22
	/**
23
	 * el cach� se realiza en funci�n del tama�o del fichero,
24
	 * con los tiles relativos a su coordenada 0,0.
25
	 */
26
	public static int LOCAL		= 0x100;
27
	/**
28
	 * el cach� se realiza con parametros globales, de manera que
29
	 * el tileado es con respecto a una rejilla de referencia, y no
30
	 * calculado especialmente para ese raster.
31
	 */
32
	public static int GLOBAL	= 0x200;
33

  
34
	public static final Rectangle2D sphericGlobalBounds =
35
		new Rectangle2D.Double(-180,-90,360,180);
36
	public static final Rectangle2D planeGlobalBounds =
37
		new Rectangle2D.Double(-20000000,-10000000,40000000,20000000);
38

  
39
	public static int TILEID_TYPE_GOOGLE 		= 0x01;
40
	public static int TILEID_TYPE_OSGPLANET		= 0x02;
41
	public static int TILEID_TYPE_VIRTUALEARTH	= 0x03;
42

  
43
	private String _name = "";
44
	private String _planet = "";
45

  
46
	private int _cacheType = GLOBAL | SPHERIC;
47
	private int _tileIdType = TILEID_TYPE_OSGPLANET;
48
	private String _cacheRootDir = "/data/cache";
49
	private String _cacheDir = null;
50
	private int _tileSize = 256;
51

  
52

  
53
	private boolean _withHashDirs = false;
54
	private int _hashDirsNumber = 100;
55

  
56
	private int _minLevel = 0;
57
	private int _maxLevel = 20;
58

  
59
	private Rectangle2D _bounds;
60

  
61
	private Rectangle2D _fullExtent = sphericGlobalBounds;
62

  
63
	/**
64
	 * @param planet
65
	 * @param name
66
	 * @param server
67
	 */
68
	public CacheService(String planet, String name) {
69
		super();
70
		this._name = name;
71
		this._planet = planet;
72
		setBounds(sphericGlobalBounds);
73
	}
74

  
75

  
76
	/**
77
	 * Devuelve el tama�o de Tile como Dimension;
78
	 * @return
79
	 */
80
	public Dimension computeSz() {
81
		return new Dimension(getTileSize(),getTileSize());
82
	}
83

  
84
	public Dimension getFullSize(int level) {
85
		int w = getTileSize() * (int)Math.pow(2,level);
86
		return new Dimension(w, w/2);
87
	}
88

  
89
	/**
90
	 * Devuelve el numero de Tiles (ancho y alto) para ese nivel.
91
	 * @param level
92
	 * @return
93
	 */
94
	public Dimension getLevelSize(int level) {
95
		int w = (int)Math.pow(2,level);
96
		return new Dimension(w, w/2);
97
	}
98

  
99
	public boolean isFileInCache(String fName) {
100
		File cacheFile = new File(fName);
101
		return cacheFile.exists();
102
	}
103

  
104

  
105
	public void setCacheDir(String dir) {
106
		_cacheDir = dir;
107
	}
108
	public String getCacheDir() {
109
		File file;
110
		if (_cacheDir == null) {
111
			// Compruebo si existe el directorio del planeta
112
			_cacheDir = getCacheRootDir()+"/"+getPlanet()+"/";
113
			file = new File(_cacheDir);
114
			if (!file.exists()) file.mkdir();
115
			// Compruebo si existe el directorio del servidor
116
			_cacheDir += getName()+"/";
117
		}
118
		file = new File(_cacheDir);
119
		if (!file.exists()) file.mkdirs();
120
		return _cacheDir;
121
	}
122

  
123
	/**
124
	 * @return Returns the cacheRootDir.
125
	 */
126
	public String getCacheRootDir() {
127
		return _cacheRootDir;
128
	}
129
	/**
130
	 * @param cacheRootDir The cacheRootDir to set.
131
	 */
132
	public void setCacheRootDir(String cacheRootDir) {
133
		_cacheRootDir = cacheRootDir;
134
	}
135

  
136
	/**
137
	 * @return Returns the name.
138
	 */
139
	public String getName() {
140
		return _name;
141
	}
142
	/**
143
	 * @param name The name to set.
144
	 */
145
	public void setName(String name) {
146
		_name = name;
147
	}
148
	/**
149
	 * @return Returns the planet.
150
	 */
151
	public String getPlanet() {
152
		return _planet;
153
	}
154
	/**
155
	 * @param planet The planet to set.
156
	 */
157
	public void setPlanet(String planet) {
158
		_planet = planet;
159
	}
160
	/**
161
	 * @return Returns the tileSize.
162
	 */
163
	public int getTileSize() {
164
		return _tileSize;
165
	}
166
	/**
167
	 * @param tileSize The tileSize to set.
168
	 */
169
	public void setTileSize(int tileSize) {
170
		_tileSize = tileSize;
171
	}
172
	/**
173
	 * @return Returns the tileIdType.
174
	 */
175
	public int getTileIdType() {
176
		return _tileIdType;
177
	}
178
	/**
179
	 * @param tileIdType The tileIdType to set.
180
	 */
181
	public void setTileIdType(int tileIdType) {
182
		_tileIdType = tileIdType;
183
	}
184
	/**
185
	 * @return Returns the maxLevel.
186
	 */
187
	public int getMaxLevel() {
188
		return _maxLevel;
189
	}
190
	/**
191
	 * @param maxLevel The maxLevel to set.
192
	 */
193
	public void setMaxLevel(int maxLevel) {
194
		_maxLevel = maxLevel;
195
	}
196
	/**
197
	 * @return Returns the minLevel.
198
	 */
199
	public int getMinLevel() {
200
		return _minLevel;
201
	}
202
	/**
203
	 * @param minLevel The minLevel to set.
204
	 */
205
	public void setMinLevel(int minLevel) {
206
		_minLevel = minLevel;
207
	}
208
	/**
209
	 * @return Returns the bounds.
210
	 */
211
	public Rectangle2D getBounds() {
212
		return _bounds;
213
	}
214

  
215
	public void setBounds(Rectangle2D r) {
216
		_bounds = r;
217
	}
218
	/**
219
	 * @return Returns the cacheType.
220
	 */
221
	public int getCacheType() {
222
		return _cacheType;
223
	}
224
	/**
225
	 * @param cacheType The cacheType to set.
226
	 */
227
	public void setCacheType(int cacheType) {
228
		_cacheType = cacheType;
229
		if ((cacheType & GLOBAL) == GLOBAL)
230
			if ((cacheType & PLANE) == PLANE)
231
				setFullExtent(planeGlobalBounds);
232
			else if ((cacheType & SPHERIC) == SPHERIC)
233
				setFullExtent(sphericGlobalBounds);
234
	}
235
	/**
236
	 * @return Returns the fullExtent.
237
	 */
238
	public Rectangle2D getFullExtent() {
239
		return _fullExtent;
240
	}
241
	/**
242
	 * @param fullExtent The fullExtent to set.
243
	 */
244
	public void setFullExtent(Rectangle2D fullExtent) {
245
		_fullExtent = fullExtent;
246
	}
247
	/**
248
	 * Gets Number of Hash Directories. 100 by default.
249
	 * @return Returns the hashDirsOrder.
250
	 */
251
	public int getHashDirsNumber() {
252
		return _hashDirsNumber;
253
	}
254
	/**
255
	 * Sets Number of Hash Directories.
256
	 * @param hashDirsOrder The hashDirsOrder to set.
257
	 */
258
	public void setHashDirsNumber(int hashDirsNumber) {
259
		_hashDirsNumber = hashDirsNumber;
260
	}
261
	/**
262
	 * returns if this cache has HashDirectories. False by difault.
263
	 * @return Returns the withHashDirs.
264
	 */
265
	public boolean isWithHashDirs() {
266
		return _withHashDirs;
267
	}
268
	/**
269
	 * sets wheter this cache has HashDirectories or no.
270
	 * @param withHashDirs The withHashDirs to set.
271
	 */
272
	public void setWithHashDirs(boolean withHashDirs) {
273
		_withHashDirs = withHashDirs;
274
	}
275
	/**
276
	 * Compute HashDirNumbre from tileNumber.
277
	 * @param num tileNumber (without level).
278
	 * @return
279
	 */
280
	public String getHashDir(Point num) {
281
		String hd = null;
282
		hd = ""+(num.x % _hashDirsNumber);
283
		return hd;
284
	}
285
}
tags/3D_Animation_v1_0alpha_Build_5/libraries/libCacheService/src/org/gvsig/cacheservice/RasterCacheService.java
1
package org.gvsig.cacheservice;
2

  
3
import java.awt.Image;
4
import java.awt.Point;
5
import java.awt.geom.Rectangle2D;
6
import java.awt.image.RenderedImage;
7
import java.io.File;
8
import java.io.FileOutputStream;
9
import java.io.IOException;
10
import java.net.MalformedURLException;
11

  
12
import javax.imageio.ImageIO;
13

  
14
import com.sun.media.jai.codec.ImageCodec;
15
import com.sun.media.jai.codec.ImageEncoder;
16
import com.sun.media.jai.codec.PNGEncodeParam;
17
import com.sun.media.jai.codec.TIFFEncodeParam;
18

  
19
//import javax.imageio.ImageIO;
20

  
21
public abstract class RasterCacheService extends CacheService {
22

  
23
	private String _ext = ".jpg";
24

  
25
	public RasterCacheService(String planet, String name) {
26
		super(planet, name);
27
		// TODO Auto-generated constructor stub
28
	}
29

  
30
	/**
31
	 * Obtains the full path of the file associated to the given tilenum and the
32
	 * current rastercacheservice configuration.
33
	 * 
34
	 * @param tileNum
35
	 * @return Full path to the filename.
36
	 */
37
	protected String getTileFileName(TileNum tileNum) {
38
		String tileId = tileNum.getTileId(getTileIdType());
39

  
40
		String fileName = getCacheDir();
41
		// Adds HasdDir level to cache image file path.
42
		if (isWithHashDirs()) {
43
			Point num = tileNum.getNum();
44
			fileName += getHashDir(num) + "/";
45
		}
46

  
47
		return fileName + tileId + getFileExtension();
48
	}
49

  
50
	/**
51
	 * Process the tile an returns the image.
52
	 * 
53
	 * @param tileNum
54
	 * @return The image of the.
55
	 * @throws CacheServiceException
56
	 */
57
	abstract public Image getTileAsImage(TileNum tileNum, Rectangle2D extent)
58
			throws CacheServiceException;
59

  
60
	public Image getTileAsImage(int level, Point num, Rectangle2D extent)
61
			throws CacheServiceException {
62
		return getTileAsImage(new TileNum(level, num), extent);
63
	}
64

  
65
	/**
66
	 * devuelve el nombre del fichero en el que se salv� el tile.
67
	 * 
68
	 * @param tileNum
69
	 * @return path completo del fichero de tile.
70
	 * @throws MalformedURLException
71
	 * @throws IOException
72
	 */
73
	abstract public String getTileAsFName(TileNum tileNum, Rectangle2D extent)
74
			throws CacheServiceException;
75

  
76
	/**
77
	 * devuelve el nombre del fichero en el que se salv� el tile.
78
	 * 
79
	 * @param level
80
	 *            nivel de zoom
81
	 * @param num
82
	 *            posici�n del tile
83
	 * @return path completo del fichero de tile.
84
	 * @throws MalformedURLException
85
	 * @throws IOException
86
	 */
87
	public String getTileAsFName(int level, Point num, Rectangle2D extent)
88
			throws CacheServiceException {
89
		return getTileAsFName(new TileNum(level, num), extent);
90
	}
91

  
92
	public Image getImageFromCachedFile(String fName) throws IOException {
93
		File cacheFile = new File(fName);
94
		return ImageIO.read(cacheFile);
95
	}
96

  
97
	public void saveCachedFile(Image img, String format, String fName)
98
			throws IOException {
99
		// ImageIO.write((RenderedImage) img, format, new File(fName));
100
		File file = new File(fName);
101
		FileOutputStream out = new FileOutputStream(file);
102

  
103
		if (format.equals("png")) {
104
			PNGEncodeParam params = new PNGEncodeParam.RGB();
105
			// params.setsetCompression(TIFFEncodeParam.COMPRESSION_NONE);
106
			ImageEncoder encoder = ImageCodec.createImageEncoder("PNG", out,
107
					params);
108
			if (encoder == null) {
109
				System.out.println("imageEncoder is null");
110
				System.exit(0);
111
			}
112
			encoder.encode((RenderedImage) img);
113
		} else if (format.equals("tif")) {
114
			TIFFEncodeParam params = new TIFFEncodeParam();
115
			params.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
116
			ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out,
117
					params);
118
			if (encoder == null) {
119
				System.out.println("imageEncoder is null");
120
				System.exit(0);
121
			}
122
			encoder.encode((RenderedImage) img);
123
		}
124

  
125
	}
126

  
127
	public void saveCachedFile(Image img, String format, File f)
128
			throws IOException {
129
//		ImageIO.write((RenderedImage) img, format, f);
130
//		File file = new File(fName);
131
		FileOutputStream out = new FileOutputStream(f);
132

  
133
		if (format.equals("png")) {
134
			PNGEncodeParam params = new PNGEncodeParam.RGB();
135
			// params.setsetCompression(TIFFEncodeParam.COMPRESSION_NONE);
136
			ImageEncoder encoder = ImageCodec.createImageEncoder("PNG", out,
137
					params);
138
			if (encoder == null) {
139
				System.out.println("imageEncoder is null");
140
				System.exit(0);
141
			}
142
			encoder.encode((RenderedImage) img);
143
		} else if (format.equals("tif")) {
144
			TIFFEncodeParam params = new TIFFEncodeParam();
145
			params.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
146
			ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out,
147
					params);
148
			if (encoder == null) {
149
				System.out.println("imageEncoder is null");
150
				System.exit(0);
151
			}
152
			encoder.encode((RenderedImage) img);
153
		}
154

  
155
	}
156

  
157
	/**
158
	 * @return Returns the ext.
159
	 */
160
	public String getFileExtension() {
161
		// TODO Auto-generated method stub
162
		return _ext;
163
	}
164

  
165
	/**
166
	 * @param ext
167
	 *            The ext to set.
168
	 */
169
	public void setFileExtension(String ext) {
170
		_ext = ext;
171
	}
172

  
173
}
tags/3D_Animation_v1_0alpha_Build_5/libraries/libCacheService/src/org/gvsig/cacheservice/TileNum.java
1
/*
2
 * Created on 25-ago-2005
3
 */
4
package org.gvsig.cacheservice;
5

  
6
import java.awt.Point;
7
import java.util.TreeMap;
8

  
9
/**
10
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
11
 * @author Rafael Gait�n <rgaitan@dsic.upv.es>
12
 * Identificador de Tile.
13
 */
14
public class TileNum {
15
	private int level;
16
	private Point num;
17
	
18
	/**
19
	 * 
20
	 */
21
	public TileNum(int level, Point num) {
22
		super();
23
		this.level = level;
24
		this.num = num;
25
	}
26

  
27
	/**
28
	 * Crea un TileNum desde un tileId de google.
29
	 * Solo es v�lido a partir de un nivel 2 (4x2 tiles).
30
	 * @param tileId
31
	 * @return
32
	 */
33
	
34
	public TileNum(String tileId) {
35
		TreeMap map = new TreeMap();
36
		map.put("q", new Integer(0));
37
		map.put("r", new Integer(1));
38
		map.put("t", new Integer(2));
39
		map.put("s", new Integer(3));
40
		int x=0, y=0;
41
		
42
		for (int i=1; i<tileId.length(); i++) {
43
			int index = ((Integer) map.get(tileId.substring(i,i+1))).intValue();
44
			int ord = (int) Math.pow(2,(tileId.length()-i-1));
45
			x += ord*(index & 0x1);
46
			y += ord*((index & 0x2) >> 0x1);
47
		}
48
		y -= Math.pow(2,(tileId.length()-3));
49
		
50
		num = new Point(x, y);
51
		level = tileId.length()-1;
52
	}
53
	/**
54
	 * @return Returns the level.
55
	 */
56
	public int getLevel() {
57
		return level;
58
	}
59
	/**
60
	 * @return Returns the num.
61
	 */
62
	public Point getNum() {
63
		return num;
64
	}
65
	/**
66
	 * @return
67
	 */
68
	public int getX() {
69
		return num.x;
70
	}
71
	/**
72
	 * @return
73
	 */
74
	public int getY() {
75
		return num.y;
76
	}
77
	
78
	public String toString() {
79
		return "TileNum["+level+",("+num.x+","+num.y+")]";
80
	}
81
	
82
	public String getTileId(int tileIdType) {
83
		if (tileIdType == CacheService.TILEID_TYPE_OSGPLANET)
84
			return numToOpTileId();
85
		else if (tileIdType == CacheService.TILEID_TYPE_VIRTUALEARTH)
86
			return numToVeTileId();
87
		else
88
			return numToGmTileId();
89
	}
90
	
91
	/**
92
	 * Genera un VeTileId (identificador de tile de MSN Virtual Earth) a partir del numero de tile y nivel.
93
	 * @param num numero de tile
94
	 * @param level nivel
95
	 * @return
96
	 */
97
	private String numToVeTileId() {
98
		String tileId = "";
99
		int y = num.y + (int)Math.pow(2,(level-2));
100
		for (int i=0; i<level; i++) {
101
			int ord = (int) Math.pow(2,(level-i-1));
102
			tileId += ""+(2*((y / ord) & 0x1)+((num.x / ord) & 0x1));	
103
		}
104
		return tileId;
105
	}
106

  
107
	private String [] quadKeys = {"q", "r", "t", "s"};
108

  
109
	/**
110
	 * Genera un GmTileId (identificador de tile de GoogleMaps) a partir del numero de tile y nivel.
111
	 * @param num numero de tile
112
	 * @param level nivel
113
	 * @return
114
	 */
115
	private String numToGmTileId() {
116
		String tileId = "t";
117
		int y = num.y + (int)Math.pow(2,(level-2));
118
		for (int i=0; i<level; i++) {
119
			int ord = (int) Math.pow(2,(level-i-1));
120
			tileId += quadKeys[2*((y / ord) & 0x1)+((num.x / ord) & 0x1)];	
121
		}
122
		return tileId;
123
	}
124
	
125

  
126
	/**
127
	 * Genera un OpTileId (identificador de tile de OsgPlanet) a partir del numero de tile y nivel.
128
	 * @param num numero de tile
129
	 * @param level nivel
130
	 * @return
131
	 */
132
	public String numToOpTileId() {
133
		return "L"+(level-1)+"_X"+num.x+"_Y"+num.y;
134
	}
135
	
136

  
137
}
tags/3D_Animation_v1_0alpha_Build_5/libraries/libCacheService/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0"
2
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4
	<modelVersion>4.0.0</modelVersion>
5
	<groupId>org.gvsig</groupId>
6
	<artifactId>libCacheService</artifactId>
7
	<packaging>jar</packaging>
8
	<version>1.0-SNAPSHOT</version>
9
	<name>libCacheService</name>
10
	<url>http://maven.apache.org</url>
11
	<parent>
12
		<groupId>org.gvsig</groupId>
13
		<artifactId>gvsig-library-base-pom</artifactId>
14
		<version>1.0-SNAPSHOT</version>
15
	</parent>
16
	<dependencies>
17
		<dependency>
18
			<groupId>javax.media</groupId>
19
	        <artifactId>jai_core</artifactId>
20
        	<version>1.1.3</version>
21
      	</dependency>
22
      	<dependency>
23
        	<groupId>javax.media</groupId>
24
        	<artifactId>jai_codec</artifactId>
25
        	<version>1.1.3</version>
26
      	</dependency>
27
      	<!--dependency>
28
        	<groupId>javax.media</groupId>
29
        	<artifactId>jai_imageio</artifactId>
30
        	<version>1.1</version>
31
      	</dependency-->
32
	</dependencies>
33
	<build>
34
		<sourceDirectory>src</sourceDirectory>
35
		<testSourceDirectory>test</testSourceDirectory>
36
	</build>
37
</project>
tags/3D_Animation_v1_0alpha_Build_5/libraries/libCacheService/build.xml
1
<project name="libCacheService" default="create-jar" basedir=".">
2
  	<!-- set global properties for this build -->
3
	<property name="src"  location="src"/>
4
	<property name="build"  location="bin"/>
5
	<property name="dist"  location="dist"/>
6
	<property name="targetDir" location="../lib3DMap/lib"/>
7
	<property name="jarName" value="libCacheService"/>
8
	<property name="andamiLibs" location="../_fwAndami/lib" />
9
	<property name="fmapLibs" location="../libFMap" />
10
	<import file="../binaries/ant/utilities.xml"/>
11

  
12

  
13
  <target name="init">
14
    <!-- Create the time stamp -->
15
    <tstamp/>
16
  </target>
17

  
18
	<target name="batch-build"
19
				description="compile the sources, create the jar file"
20
				depends="compile,create-jar">
21
	</target>
22

  
23
	<target name="compile" description="compile the source">
24
		<!-- Compile the Java code from ${src} to ${build} -->
25
		<mkdir dir="${build}" />
26
		<loadEclipseClasspath project="${basedir}"/>
27
		<gvSIG-javac
28
			classpath="${eclipseClasspath}"	/>
29
	</target>
30

  
31
	<target name="create-jar" description="Crea el jar de la aplicacion">
32
		<!--<mkdir dir="${dist}" />-->
33
	  	<jar jarfile="${targetDir}/${jarName}.jar" basedir="${build}"/>
34
	</target>
35

  
36
	<target name="clean" description="clean up">
37
		<!-- Delete the ${build} and ${dist} directory trees -->
38
		<delete dir="${build}" />
39
		<delete dir="${dist}" />
40
	</target>
41

  
42
</project>
43

  
0 44

  

Also available in: Unified diff