gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / main / java / org / gvsig / raster / cache / buffer / impl / BufferServices.java @ 992
History | View | Annotate | Download (3.09 KB)
1 |
package org.gvsig.raster.cache.buffer.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.FileNotFoundException; |
6 |
import java.io.FileOutputStream; |
7 |
import java.io.IOException; |
8 |
import java.io.InputStream; |
9 |
import java.io.OutputStream; |
10 |
|
11 |
import org.gvsig.raster.cache.buffer.impl.datasource.IRasterDataSource; |
12 |
|
13 |
|
14 |
/**
|
15 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
16 |
*/
|
17 |
public class BufferServices { |
18 |
|
19 |
/**
|
20 |
* Dadas unas coordenadas pixel y un n?mero de bandas, esta funci?n comprueba si
|
21 |
* el tama?o de ventana que va a generarse supera el tama?o de la cach?
|
22 |
* @param coords Coordenadas pixel del raster
|
23 |
* @param bands N?mero de bandas
|
24 |
* @param ds
|
25 |
* @return true si la ventana supera el tama?o de la cach? o false si no lo supera.
|
26 |
*/
|
27 |
public static boolean isBufferTooBig(double[] coords, double resolution, int bands) { |
28 |
double wPx = (coords[0] - coords[2]) / resolution; |
29 |
double hPx = (coords[1] - coords[3]) / resolution; |
30 |
return isBufferTooBig(new double[]{0, 0, wPx, hPx}, bands); |
31 |
} |
32 |
|
33 |
/**
|
34 |
* Dadas unas coordenadas pixel y un n?mero de bandas, esta funci?n comprueba si
|
35 |
* el tama?o de ventana que va a generarse supera el tama?o de la cach?
|
36 |
* @param coords Coordenadas pixel del raster
|
37 |
* @param bands N?mero de bandas
|
38 |
* @param ds
|
39 |
* @return true si la ventana supera el tama?o de la cach? o false si no lo supera.
|
40 |
*/
|
41 |
public static boolean isBufferTooBig(double[] coords, int bands) { |
42 |
int w = (int)Math.abs(coords[2] - coords[0]); |
43 |
int h = (int)Math.abs(coords[3] - coords[1]); |
44 |
|
45 |
long windowSize = w * h * bands;
|
46 |
return (windowSize > (BufferCacheManagerImpl.cacheSize * 1048576)); |
47 |
} |
48 |
|
49 |
/**
|
50 |
* Devuelve true si el tama?o del dataset es menor que el de la cach? y false
|
51 |
* si no lo es.
|
52 |
* @param datasource Fuente de datos
|
53 |
* @return true si podemos cargar en memoria el raster
|
54 |
*/
|
55 |
public static boolean loadInMemory(IRasterDataSource datasource) { |
56 |
return (datasource.getFileSize() < (BufferCacheManagerImpl.cacheSize * 1048576)); |
57 |
} |
58 |
|
59 |
/**
|
60 |
* Copia de ficheros
|
61 |
* @param pathOrig Ruta de origen
|
62 |
* @param pathDst Ruta de destino.
|
63 |
*/
|
64 |
public static void copyFile(String pathOrig, String pathDst) throws FileNotFoundException, IOException { |
65 |
InputStream in;
|
66 |
OutputStream out;
|
67 |
|
68 |
if (pathOrig == null || pathDst == null) { |
69 |
System.err.println("Error en path"); |
70 |
return;
|
71 |
} |
72 |
|
73 |
File orig = new File(pathOrig); |
74 |
if (!orig.exists() || !orig.isFile() || !orig.canRead()) {
|
75 |
System.err.println("Error copying the file:" + pathOrig + " <Source Exists:" + orig.exists() + ", Source is file:" + orig.isFile() + ", Source can be read:" + orig.canRead() + ">"); |
76 |
return;
|
77 |
} |
78 |
|
79 |
File dest = new File(pathDst); |
80 |
String file = new File(pathOrig).getName(); |
81 |
if (dest.isDirectory())
|
82 |
pathDst += file; |
83 |
|
84 |
dest = new File(pathDst); |
85 |
if(!dest.exists())
|
86 |
dest.createNewFile(); |
87 |
|
88 |
in = new FileInputStream(pathOrig); |
89 |
out = new FileOutputStream(pathDst); |
90 |
|
91 |
byte[] buf = new byte[1024]; |
92 |
int len;
|
93 |
|
94 |
while ((len = in.read(buf)) > 0) |
95 |
out.write(buf, 0, len);
|
96 |
|
97 |
in.close(); |
98 |
out.close(); |
99 |
} |
100 |
|
101 |
public static String getTemporalPath() { |
102 |
return System.getProperty("java.io.tmpdir"); |
103 |
} |
104 |
|
105 |
} |