Statistics
| Revision:

root / trunk / libraries / libRaster / src-test / org / gvsig / raster / grid / TGReadingFullDatasourceSelectingBands.java @ 27361

History | View | Annotate | Download (6.14 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.grid;
20

    
21
import junit.framework.TestCase;
22

    
23
import org.gvsig.raster.RasterLibrary;
24
import org.gvsig.raster.buffer.BufferFactory;
25
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException;
26
import org.gvsig.raster.buffer.RasterBufferInvalidException;
27
import org.gvsig.raster.dataset.NotSupportedExtensionException;
28
import org.gvsig.raster.dataset.RasterDataset;
29
import org.gvsig.raster.dataset.io.RasterDriverException;
30

    
31
/**
32
 * Este test prueba el acceso a datos a traves de un grid.
33
 * A partir de una fuente de datos (cargada en el constructor del Grid). 
34
 * 1-F32 Monobanda: Se har? una petici?n del extent completo de la fuente seleccionando tres bandas.
35
 * 2-Comprobamos el tipo de datos el ancho y el alto
36
 * 3-Modifica algunos valores del grid y se comprueba que las modificaciones han sido efectivas.
37
 * 4-Cambiamos la banda a operar y obtenemos un dato comprobando que tiene que devolver NoData
38
 * 
39
 * 1-RGB: Se har? una petici?n del extent completo de la fuente seleccionando tres bandas.
40
 * 2-Comprobamos el tipo de datos el ancho y el alto
41
 * 3-Modifica algunos valores del grid y se comprueba que las modificaciones han sido efectivas.
42
 * 4-Cambiamos la banda a operar y obtenemos un dato comprobando que el valor que devuelve es correcto
43
 * 5 -Comprobamos que si nos salimos devuelve un NoData
44
 * 
45
 * @author Nacho Brodin (nachobrodin@gmail.com)
46
 *
47
 */
48
public class TGReadingFullDatasourceSelectingBands extends TestCase{
49

    
50
        private String baseDir = "./test-images/";
51
        private String path1 = baseDir + "miniRaster28x25F32.tif";
52
        private String path2 = baseDir + "miniRaster25x24.tif";
53
        private RasterDataset f1 = null;
54
        private RasterDataset f2 = null;
55
        private BufferFactory ds1 = null;
56
        private BufferFactory ds2 = null;
57
        
58
        static {
59
                RasterLibrary.wakeUp();        
60
        }
61
        
62
        public void start() {
63
                this.setUp();
64
                this.testStack();
65
        }
66
        
67
        public void setUp() {
68
                System.err.println("TGReadingFullDatasourceSelectingBands running...");
69
                try {
70
                        f1 = RasterDataset.open(null, path1);
71
                        f2 = RasterDataset.open(null, path2);
72
                } catch (NotSupportedExtensionException e) {
73
                        e.printStackTrace();
74
                } catch (RasterDriverException e) {
75
                        e.printStackTrace();
76
                }
77
                ds1 = new BufferFactory(f1);
78
                ds2 = new BufferFactory(f2);
79
        }
80
        
81
        public void testStack() {
82
                int[] drawableBands = {0, 1, 2};
83
                try {
84
                        Grid g = new Grid(ds1, drawableBands);
85
                        assertEquals(g.getDataType(), 4); //Tipo float
86
                        assertEquals(g.getNY(), 25); //Alto
87
                        assertEquals(g.getNX(), 28); //Ancho
88

    
89
                        testCellBufferFloat(g, 0);
90
                        g.setCellValue(0, 0, (float)31.22);
91
                        g.setCellValue(2, 2, (float)31.22);
92
                        g.setCellValue(3, 8, (float)31.22);
93
                        g.setCellValue(10, 15, (float)31.22);
94
                        testCellBufferFloat(g, 0);
95
                        assertEquals((int)g.getCellValueAsFloat(0, 0), 31);
96
                        assertEquals((int)g.getCellValueAsFloat(2, 2), 31);
97
                        assertEquals((int)g.getCellValueAsFloat(3, 8), 31);
98
                        assertEquals((int)g.getCellValueAsFloat(10, 15), 31);
99
//                        g.setBandToOperate(2);
100
                        assertEquals((int)g.getCellValueAsFloat(1000, 1000), -99999);
101
                        
102
                        g = new Grid(ds2, drawableBands);
103
                        assertEquals(g.getDataType(), 0); //Tipo byte
104
                        assertEquals(g.getNY(), 24); //Alto
105
                        assertEquals(g.getNX(), 25); //Ancho
106
                        testCellBufferByte(g, 0);
107
                        g.setCellValue(0, 0, (byte)31);
108
                        g.setCellValue(2, 2, (byte)31);
109
                        g.setCellValue(3, 8, (byte)31);
110
                        g.setCellValue(10, 15, (byte)31);
111
                        testCellBufferByte(g, 0);
112
                        assertEquals(g.getCellValueAsByte(0, 0), 31);
113
                        assertEquals(g.getCellValueAsByte(2, 2), 31);
114
                        assertEquals(g.getCellValueAsByte(3, 8), 31);
115
                        assertEquals(g.getCellValueAsByte(10, 15), 31);
116
                        g.setBandToOperate(2);
117
                        assertEquals((int)g.getCellValueAsByte(10, 15), 107);
118
                        
119
                        //Comprobamos que si nos salimos devuelve un NoData
120
                        assertEquals((int)g.getCellValueAsFloat(100, 100), -99999);
121
                        assertEquals((int)g.getCellValueAsFloat(-1, 10), -99999);
122
                        assertEquals((int)g.getCellValueAsFloat(1000, 2310), -99999);
123
                        
124
                } catch (RasterBufferInvalidException e1) {
125
                        e1.printStackTrace();
126
                } catch (RasterBufferInvalidAccessException e2) {
127
                        e2.printStackTrace();
128
                } catch (OutOfGridException e3) {
129
                        e3.printStackTrace();
130
                } catch (GridException e3) {
131
                        e3.printStackTrace();
132
                } catch (InterruptedException e) {
133
                }
134
        }
135
                
136
        /**
137
         * Compara todos los elementos del buffer con los del grid para comprobar
138
         * la correcci?n de la lectura.
139
         * @param g
140
         * @throws RasterBufferInvalidAccessException
141
         * @throws InterruptedException 
142
         */
143
        private void testCellBufferFloat(Grid g, int band) throws RasterBufferInvalidAccessException, GridException, InterruptedException {
144
                int nCols = f1.getWidth();
145
                int nRows = f1.getHeight(); 
146
                for(int i = 0; i < nRows; i ++) {
147
                        for(int j = 0; j < nCols; j ++)
148
                                assertEquals((int)g.getCellValueAsFloat(j, i), (int)ds1.getRasterBuf().getElemFloat(i, j, band));
149
                }
150
        }
151

    
152
        /**
153
         * Compara todos los elementos del buffer con los del grid para comprobar
154
         * la correcci?n de la lectura.
155
         * @param g
156
         * @throws RasterBufferInvalidAccessException
157
         * @throws InterruptedException 
158
         */
159
        private void testCellBufferByte(Grid g, int band) throws RasterBufferInvalidAccessException, GridException, InterruptedException {
160
                int nCols = f2.getWidth();
161
                int nRows = f2.getHeight(); 
162
                for(int i = 0; i < nRows; i ++) {
163
                        for(int j = 0; j < nCols; j ++)
164
                                assertEquals((int)g.getCellValueAsByte(j, i), (int)ds2.getRasterBuf().getElemByte(i, j, band));
165
                }
166
        }
167
}