svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster / src-test / org / gvsig / raster / BaseTestCase.java @ 30754
History | View | Annotate | Download (8.81 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
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 |
package org.gvsig.raster; |
20 |
|
21 |
import java.awt.geom.Point2D; |
22 |
import java.io.File; |
23 |
|
24 |
import org.gvsig.raster.buffer.BufferFactory; |
25 |
import org.gvsig.raster.dataset.FileNotFoundInListException; |
26 |
import org.gvsig.raster.dataset.IBuffer; |
27 |
import org.gvsig.raster.dataset.IRasterDataSource; |
28 |
import org.gvsig.raster.dataset.InvalidSetViewException; |
29 |
import org.gvsig.raster.dataset.MultiRasterDataset; |
30 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
31 |
import org.gvsig.raster.dataset.RasterDriverException; |
32 |
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation; |
33 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
34 |
/**
|
35 |
* Clase base para todos los tests. Contiene m?todos de uso com?n.
|
36 |
* Los errores no se capturan. Se lanzan en una traza por consola.
|
37 |
*
|
38 |
* 07/05/2008
|
39 |
* @author Nacho Brodin nachobrodin@gmail.com
|
40 |
*/
|
41 |
public class BaseTestCase extends AbstractLibraryAutoInitTestCase { |
42 |
/**
|
43 |
* Directorio donde est?n las imagenes de pruebas
|
44 |
*/
|
45 |
protected String baseDir = "./test-images/"; |
46 |
/**
|
47 |
* Directorio para la generaci?n de imagenes temporales en los tests
|
48 |
*/
|
49 |
protected String tempDir = "/tmp"; |
50 |
|
51 |
protected MultiRasterDataset dataset = null; |
52 |
protected String out = null; |
53 |
protected long t1, t2; |
54 |
|
55 |
protected void doSetUp() throws Exception { |
56 |
// Nothing to do
|
57 |
} |
58 |
|
59 |
/**
|
60 |
* Resetea el contador de tiempo
|
61 |
*/
|
62 |
protected void resetTime() { |
63 |
t1 = System.currentTimeMillis();
|
64 |
} |
65 |
|
66 |
/**
|
67 |
* Obtiene el tiempo transcurrido desde que se reseteo el tiempo
|
68 |
* @return
|
69 |
*/
|
70 |
protected double getTime() { |
71 |
t2 = System.currentTimeMillis();
|
72 |
return ((t2 - t1) / 1000D); |
73 |
} |
74 |
|
75 |
/**
|
76 |
* Abre el dataset
|
77 |
* @param s
|
78 |
*/
|
79 |
protected MultiRasterDataset open(String s) { |
80 |
try {
|
81 |
dataset = MultiRasterDataset.open(null, s);
|
82 |
return dataset;
|
83 |
} catch (NotSupportedExtensionException e1) {
|
84 |
e1.printStackTrace(); |
85 |
} catch (RasterDriverException e1) {
|
86 |
e1.printStackTrace(); |
87 |
} |
88 |
return null; |
89 |
} |
90 |
|
91 |
/**
|
92 |
* Abre el dataset
|
93 |
* @param s
|
94 |
*/
|
95 |
protected MultiRasterDataset open(String[] s) { |
96 |
try {
|
97 |
if(s == null) |
98 |
return null; |
99 |
for (int i = 0; i < s.length; i++) |
100 |
if(i == 0) |
101 |
dataset = MultiRasterDataset.open(null, s[0]); |
102 |
else
|
103 |
dataset.addDataset(new String[]{s[i]}); |
104 |
return dataset;
|
105 |
} catch (NotSupportedExtensionException e1) {
|
106 |
e1.printStackTrace(); |
107 |
} catch (RasterDriverException e1) {
|
108 |
e1.printStackTrace(); |
109 |
} catch (FileNotFoundInListException e2) {
|
110 |
e2.printStackTrace(); |
111 |
} |
112 |
return null; |
113 |
} |
114 |
|
115 |
/**
|
116 |
* Borra el RMF de un fichero raster
|
117 |
* @param file
|
118 |
*/
|
119 |
protected void deleteRMF(String file) { |
120 |
int last = file.lastIndexOf("."); |
121 |
if (last == -1) |
122 |
last = file.length(); |
123 |
|
124 |
File fichero = new File(file.substring(0, last) + ".rmf"); |
125 |
fichero.delete(); |
126 |
} |
127 |
|
128 |
/**
|
129 |
* Obtiene un nombre aleatorio para fichero temporal
|
130 |
* @return
|
131 |
*/
|
132 |
public String getFileTemp() { |
133 |
out = tempDir + "/test-" + System.currentTimeMillis(); |
134 |
return out;
|
135 |
} |
136 |
|
137 |
/**
|
138 |
* Crea un objeto de interpretaci?n de color
|
139 |
* @param nBands
|
140 |
* @return
|
141 |
*/
|
142 |
protected DatasetColorInterpretation getColorInterpretation(int nBands) { |
143 |
String[] ci = new String[nBands]; |
144 |
if(nBands == 1) |
145 |
ci[0] = DatasetColorInterpretation.GRAY_BAND;
|
146 |
else
|
147 |
for (int j = 0; j < ci.length; j++) |
148 |
switch (j) {
|
149 |
case 0: ci[j] = DatasetColorInterpretation.RED_BAND; break; |
150 |
case 1: ci[j] = DatasetColorInterpretation.GREEN_BAND; break; |
151 |
case 2: ci[j] = DatasetColorInterpretation.BLUE_BAND; break; |
152 |
default: ci[j] = DatasetColorInterpretation.UNDEF_BAND; break; |
153 |
} |
154 |
return new DatasetColorInterpretation(ci); |
155 |
} |
156 |
|
157 |
/**
|
158 |
* Compara un dataset completo (ds2) con una parte de otro dataset (ds1)
|
159 |
* @param ds1
|
160 |
* @param ds2
|
161 |
* @param coordsDs1 Coordenada x, y del dataset 1 a partir de la cual empieza la comparaci?n
|
162 |
* @param step Proporci?n de tama?o del dataset 2 con respecto al 1. Si el step es 2 quiere decir que el
|
163 |
* dataset 2 es el doble que el 1
|
164 |
* @param drawableBands
|
165 |
* @param dataType
|
166 |
*/
|
167 |
protected void compareDatasets(IRasterDataSource ds1, IRasterDataSource ds2, Point2D coordsDs1, int step, int[] drawableBands, int dataType) { |
168 |
BufferFactory bufferFactory1 = new BufferFactory(ds1);
|
169 |
bufferFactory1.setDrawableBands(drawableBands); |
170 |
BufferFactory bufferFactory2 = new BufferFactory(ds2);
|
171 |
bufferFactory2.setDrawableBands(drawableBands); |
172 |
try {
|
173 |
bufferFactory1.setAreaOfInterest((int)coordsDs1.getX(), (int)coordsDs1.getY(), (int)(ds2.getWidth() / step), (int)(ds2.getHeight() / step)); |
174 |
IBuffer buf1 = bufferFactory1.getRasterBuf(); |
175 |
bufferFactory2.setAreaOfInterest(); |
176 |
IBuffer buf2 = bufferFactory2.getRasterBuf(); |
177 |
for (int band = 0; band < buf1.getBandCount(); band++) |
178 |
for (int row = 0; row < (buf2.getHeight() / step); row++) |
179 |
for (int col = 0; col < (buf2.getWidth() / step); col++) |
180 |
switch (dataType) {
|
181 |
case IBuffer.TYPE_BYTE: assertEquals(buf1.getElemByte(row, col, band), buf2.getElemByte(row * step, col * step, band)); break; |
182 |
case IBuffer.TYPE_SHORT: assertEquals(buf1.getElemShort(row, col, band), buf2.getElemShort(row * step, col * step, band)); break; |
183 |
case IBuffer.TYPE_INT: assertEquals(buf1.getElemInt(row, col, band), buf2.getElemInt(row * step, col * step, band)); break; |
184 |
case IBuffer.TYPE_FLOAT: assertEquals((int)buf1.getElemFloat(row, col, band), (int)buf2.getElemFloat(row * step, col * step, band)); break; |
185 |
case IBuffer.TYPE_DOUBLE: assertEquals((int)buf1.getElemDouble(row, col, band), (int)buf2.getElemDouble(row * step, col * step, band)); break; |
186 |
} |
187 |
} catch (RasterDriverException e) {
|
188 |
e.printStackTrace(); |
189 |
} catch (InvalidSetViewException e) {
|
190 |
e.printStackTrace(); |
191 |
} catch (InterruptedException e) { |
192 |
e.printStackTrace(); |
193 |
} |
194 |
} |
195 |
|
196 |
/**
|
197 |
* Compara la banda de un dataset completo (ds2) con una parte de otro dataset (ds1)
|
198 |
* @param banda del dataset 1 que corresponde con el dataset 2
|
199 |
* @param ds1
|
200 |
* @param ds2
|
201 |
* @param coordsDs1 Coordenada x, y del dataset 1 a partir de la cual empieza la comparaci?n
|
202 |
* @param step Proporci?n de tama?o del dataset 2 con respecto al 1. Si el step es 2 quiere decir que el
|
203 |
* dataset 2 es el
|
204 |
* @param drawableBands
|
205 |
* @param dataType
|
206 |
*/
|
207 |
protected void compareDatasets(int band, IRasterDataSource ds1, IRasterDataSource ds2, Point2D coordsDs1, int step, int[] drawableBands, int dataType) { |
208 |
BufferFactory bufferFactory1 = new BufferFactory(ds1);
|
209 |
bufferFactory1.setDrawableBands(drawableBands); |
210 |
BufferFactory bufferFactory2 = new BufferFactory(ds2);
|
211 |
bufferFactory2.setDrawableBands(drawableBands); |
212 |
try {
|
213 |
bufferFactory1.setAreaOfInterest((int)coordsDs1.getX(), (int)coordsDs1.getY(), (int)(ds2.getWidth() / step), (int)(ds2.getHeight() / step)); |
214 |
IBuffer buf1 = bufferFactory1.getRasterBuf(); |
215 |
bufferFactory2.setAreaOfInterest(); |
216 |
IBuffer buf2 = bufferFactory2.getRasterBuf(); |
217 |
for (int row = 0; row < buf2.getHeight(); row++) |
218 |
for (int col = 0; col < buf2.getWidth(); col++) |
219 |
switch (dataType) {
|
220 |
case IBuffer.TYPE_BYTE: assertEquals(buf1.getElemByte(row, col, band), buf2.getElemByte(row * step, col * step, 0)); break; |
221 |
case IBuffer.TYPE_SHORT: assertEquals(buf1.getElemShort(row, col, band), buf2.getElemShort(row * step, col * step, 0)); break; |
222 |
case IBuffer.TYPE_INT: assertEquals(buf1.getElemInt(row, col, band), buf2.getElemInt(row * step, col * step, 0)); break; |
223 |
case IBuffer.TYPE_FLOAT: assertEquals((int)buf1.getElemFloat(row, col, band), (int)buf2.getElemFloat(row * step, col * step, 0)); break; |
224 |
case IBuffer.TYPE_DOUBLE: assertEquals((int)buf1.getElemDouble(row, col, band), (int)buf2.getElemDouble(row * step, col * step, 0)); break; |
225 |
} |
226 |
} catch (RasterDriverException e) {
|
227 |
e.printStackTrace(); |
228 |
} catch (InvalidSetViewException e) {
|
229 |
e.printStackTrace(); |
230 |
} catch (InterruptedException e) { |
231 |
e.printStackTrace(); |
232 |
} |
233 |
} |
234 |
|
235 |
/**
|
236 |
* Detiene la ejecuci?n del thread actual durante n milisegundos
|
237 |
* @param n Numero de milisegundos detenido
|
238 |
*/
|
239 |
protected void pause(int n) { |
240 |
try {
|
241 |
Thread.sleep(n);
|
242 |
} catch (InterruptedException e) { |
243 |
e.printStackTrace(); |
244 |
} |
245 |
} |
246 |
} |