Statistics
| Revision:

root / trunk / libraries / libRaster / src-test / org / gvsig / raster / BaseTestCase.java @ 22936

History | View | Annotate | Download (8.82 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 junit.framework.TestCase;
25

    
26
import org.gvsig.raster.buffer.BufferFactory;
27
import org.gvsig.raster.dataset.FileNotFoundInListException;
28
import org.gvsig.raster.dataset.IBuffer;
29
import org.gvsig.raster.dataset.IRasterDataSource;
30
import org.gvsig.raster.dataset.InvalidSetViewException;
31
import org.gvsig.raster.dataset.MultiRasterDataset;
32
import org.gvsig.raster.dataset.NotSupportedExtensionException;
33
import org.gvsig.raster.dataset.io.RasterDriverException;
34
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
35
/**
36
 * Clase base para todos los tests. Contiene m?todos de uso com?n.
37
 * Los errores no se capturan. Se lanzan en una traza por consola.
38
 * 
39
 * 07/05/2008
40
 * @author Nacho Brodin nachobrodin@gmail.com
41
 */
42
public class BaseTestCase extends TestCase {
43
        /**
44
         * Directorio donde est?n las imagenes de pruebas
45
         */
46
        protected String               baseDir       = "./test-images/";
47
        /**
48
         * Directorio para la generaci?n de imagenes temporales en los tests
49
         */
50
        protected String               tempDir       = "/tmp"; 
51
        
52
        protected MultiRasterDataset   dataset       = null;
53
        protected String               out           = null;
54
        protected long                 t1, t2;
55
        
56
        static {
57
                RasterLibrary.wakeUp();
58
        }
59
        
60
        /**
61
         * Resetea el contador de tiempo
62
         */
63
        protected void resetTime() {
64
                t1 = System.currentTimeMillis();
65
        }
66
        
67
        /**
68
         * Obtiene el tiempo transcurrido desde que se reseteo el tiempo
69
         * @return
70
         */
71
        protected double getTime() {
72
                t2 = System.currentTimeMillis();
73
                return ((t2 - t1) / 1000D);
74
        }
75
        
76
        /**
77
         * Abre el dataset
78
         * @param s
79
         */
80
        protected MultiRasterDataset open(String s) {
81
                try {
82
                        dataset = MultiRasterDataset.open(null, s);
83
                        return dataset;
84
                } catch (NotSupportedExtensionException e1) {
85
                        e1.printStackTrace();
86
                } catch (RasterDriverException e1) {
87
                        e1.printStackTrace();
88
                }
89
                return null;
90
        }
91
        
92
        /**
93
         * Abre el dataset
94
         * @param s
95
         */
96
        protected MultiRasterDataset open(String[] s) {
97
                try {
98
                        if(s == null)
99
                                return null;
100
                        for (int i = 0; i < s.length; i++) {
101
                                if(i == 0)
102
                                        dataset = MultiRasterDataset.open(null, s[0]);
103
                                else
104
                                        dataset.addDataset(new String[]{s[i]});
105
                                
106
                        }
107
                        return dataset;
108
                } catch (NotSupportedExtensionException e1) {
109
                        e1.printStackTrace();
110
                } catch (RasterDriverException e1) {
111
                        e1.printStackTrace();
112
                } catch (FileNotFoundInListException e2) {
113
                        e2.printStackTrace();
114
                }
115
                return null;
116
        }
117

    
118
        /**
119
         * Borra el RMF de un fichero raster
120
         * @param file
121
         */
122
        protected void deleteRMF(String file) {
123
                int last = file.lastIndexOf(".");
124
                if (last == -1)
125
                        last = file.length();
126

    
127
                File fichero = new File(file.substring(0, last) + ".rmf");
128
                fichero.delete();
129
        }
130
        
131
        /**
132
         * Obtiene un nombre aleatorio para fichero temporal
133
         * @return
134
         */
135
        public String getFileTemp() {
136
                out = tempDir + "/test-" + System.currentTimeMillis();
137
                return out;
138
        }
139
        
140
        /**
141
         * Crea un objeto de interpretaci?n de color
142
         * @param nBands
143
         * @return
144
         */
145
        protected DatasetColorInterpretation getColorInterpretation(int nBands) {
146
                String[] ci = new String[nBands];
147
                if(nBands == 1)
148
                        ci[0] = DatasetColorInterpretation.GRAY_BAND;
149
                else {
150
                        for (int j = 0; j < ci.length; j++) {
151
                                switch (j) {
152
                                case 0: ci[j] = DatasetColorInterpretation.RED_BAND; break;
153
                                case 1: ci[j] = DatasetColorInterpretation.GREEN_BAND; break;
154
                                case 2: ci[j] = DatasetColorInterpretation.BLUE_BAND; break;
155
                                default: ci[j] = DatasetColorInterpretation.UNDEF_BAND; break;
156
                                }
157
                        }
158
                }
159
                return new DatasetColorInterpretation(ci);
160
        }
161
        
162
        /**
163
         * Compara un dataset completo (ds2) con una parte de otro dataset (ds1)
164
         * @param ds1
165
         * @param ds2
166
         * @param coordsDs1 Coordenada x, y del dataset 1 a partir de la cual empieza la comparaci?n
167
         * @param step Proporci?n de tama?o del dataset 2 con respecto al 1. Si el step es 2 quiere decir que el 
168
         * dataset 2 es el doble que el 1
169
         * @param drawableBands
170
         * @param dataType
171
         */
172
        protected void compareDatasets(IRasterDataSource ds1, IRasterDataSource ds2, Point2D coordsDs1, int step, int[] drawableBands, int dataType) {
173
                BufferFactory bufferFactory1 = new BufferFactory(ds1);
174
                bufferFactory1.setDrawableBands(drawableBands);
175
                BufferFactory bufferFactory2 = new BufferFactory(ds2);
176
                bufferFactory2.setDrawableBands(drawableBands);
177
                try {
178
                        bufferFactory1.setAreaOfInterest((int)coordsDs1.getX(), (int)coordsDs1.getY(), (int)(ds2.getWidth() / step), (int)(ds2.getHeight() / step));
179
                        IBuffer buf1 = bufferFactory1.getRasterBuf();
180
                        bufferFactory2.setAreaOfInterest();
181
                        IBuffer buf2 = bufferFactory2.getRasterBuf();
182
                        for (int band = 0; band < buf1.getBandCount(); band++) {
183
                                for (int row = 0; row < (buf2.getHeight() / step); row++) {
184
                                        for (int col = 0; col < (buf2.getWidth() / step); col++) {
185
                                                switch (dataType) {
186
                                                case IBuffer.TYPE_BYTE: assertEquals(buf1.getElemByte(row, col, band), buf2.getElemByte(row * step, col * step, band)); break;
187
                                                case IBuffer.TYPE_SHORT: assertEquals(buf1.getElemShort(row, col, band), buf2.getElemShort(row * step, col * step, band)); break;
188
                                                case IBuffer.TYPE_INT: assertEquals(buf1.getElemInt(row, col, band), buf2.getElemInt(row * step, col * step, band)); break;
189
                                                case IBuffer.TYPE_FLOAT: assertEquals((int)buf1.getElemFloat(row, col, band), (int)buf2.getElemFloat(row * step, col * step, band)); break;
190
                                                case IBuffer.TYPE_DOUBLE: assertEquals((int)buf1.getElemDouble(row, col, band), (int)buf2.getElemDouble(row * step, col * step, band)); break;
191
                                                }
192
                                        }
193
                                }
194
                        }
195
                } catch (RasterDriverException e) {
196
                        e.printStackTrace();
197
                } catch (InvalidSetViewException e) {
198
                        e.printStackTrace();
199
                } catch (InterruptedException e) {
200
                        e.printStackTrace();
201
                }
202
        }
203
        
204
        /**
205
         * Compara la banda de un dataset completo (ds2) con una parte de otro dataset (ds1)
206
         * @param banda del dataset 1 que corresponde con el dataset 2
207
         * @param ds1
208
         * @param ds2
209
         * @param coordsDs1 Coordenada x, y del dataset 1 a partir de la cual empieza la comparaci?n
210
         * @param step Proporci?n de tama?o del dataset 2 con respecto al 1. Si el step es 2 quiere decir que el 
211
         * dataset 2 es el 
212
         * @param drawableBands
213
         * @param dataType
214
         */
215
        protected void compareDatasets(int band, IRasterDataSource ds1, IRasterDataSource ds2, Point2D coordsDs1, int step, int[] drawableBands, int dataType) {
216
                BufferFactory bufferFactory1 = new BufferFactory(ds1);
217
                bufferFactory1.setDrawableBands(drawableBands);
218
                BufferFactory bufferFactory2 = new BufferFactory(ds2);
219
                bufferFactory2.setDrawableBands(drawableBands);
220
                try {
221
                        bufferFactory1.setAreaOfInterest((int)coordsDs1.getX(), (int)coordsDs1.getY(), (int)(ds2.getWidth() / step), (int)(ds2.getHeight() / step));
222
                        IBuffer buf1 = bufferFactory1.getRasterBuf();
223
                        bufferFactory2.setAreaOfInterest();
224
                        IBuffer buf2 = bufferFactory2.getRasterBuf();
225
                        for (int row = 0; row < buf2.getHeight(); row++) {
226
                                for (int col = 0; col < buf2.getWidth(); col++) {
227
                                        switch (dataType) {
228
                                        case IBuffer.TYPE_BYTE: assertEquals(buf1.getElemByte(row, col, band), buf2.getElemByte(row * step, col * step, 0)); break;
229
                                        case IBuffer.TYPE_SHORT: assertEquals(buf1.getElemShort(row, col, band), buf2.getElemShort(row * step, col * step, 0)); break;
230
                                        case IBuffer.TYPE_INT: assertEquals(buf1.getElemInt(row, col, band), buf2.getElemInt(row * step, col * step, 0)); break;
231
                                        case IBuffer.TYPE_FLOAT: assertEquals((int)buf1.getElemFloat(row, col, band), (int)buf2.getElemFloat(row * step, col * step, 0)); break;
232
                                        case IBuffer.TYPE_DOUBLE: assertEquals((int)buf1.getElemDouble(row, col, band), (int)buf2.getElemDouble(row * step, col * step, 0)); break;
233
                                        }
234
                                }
235
                        }
236
                } catch (RasterDriverException e) {
237
                        e.printStackTrace();
238
                } catch (InvalidSetViewException e) {
239
                        e.printStackTrace();
240
                } catch (InterruptedException e) {
241
                        e.printStackTrace();
242
                }
243
        }
244
        
245
        /**
246
         * Detiene la ejecuci?n del thread actual durante n milisegundos
247
         * @param n Numero de milisegundos detenido
248
         */
249
        protected void pause(int n) {
250
                try {
251
                        Thread.sleep(n);
252
                } catch (InterruptedException e) {
253
                        e.printStackTrace();
254
                }
255
        }
256
}