Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src-test / org / gvsig / raster / grid / TGReadingFullDatasourceSelectingBands.java @ 10768

History | View | Annotate | Download (6 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.dataaccess.DataSource;
24
import org.gvsig.raster.dataaccess.buffer.RasterBufferInvalidAccessException;
25
import org.gvsig.raster.dataaccess.buffer.RasterBufferInvalidException;
26
import org.gvsig.raster.driver.NotSupportedExtensionException;
27
import org.gvsig.raster.driver.RasterDataset;
28
import org.gvsig.raster.driver.RasterDriverException;
29
import org.gvsig.raster.grid.Grid;
30
import org.gvsig.raster.grid.OutOfGridException;
31
import org.gvsig.raster.shared.RasterLibrary;
32

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

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

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

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