Statistics
| Revision:

root / trunk / libraries / libRaster / src-test / org / gvsig / fmap / grid / TGReadingFullDatasourceSelectingBands.java @ 10740

History | View | Annotate | Download (5.93 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.fmap.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 setUp() {
65
                System.out.println("TGReadingFullDatasourceSelectingBands running...");
66
                try {
67
                        f1 = RasterDataset.openFile(null, path1);
68
                        f2 = RasterDataset.openFile(null, path2);
69
                } catch (NotSupportedExtensionException e) {
70
                        e.printStackTrace();
71
                } catch (RasterDriverException e) {
72
                        e.printStackTrace();
73
                }
74
                ds1 = new DataSource(f1);
75
                ds2 = new DataSource(f2);
76
        }
77
        
78
        public void testStack(){
79
                int[] drawableBands = {0, 1, 2};
80
                try {
81
                        Grid g = new Grid(ds1, drawableBands);
82
                        assertEquals(g.getDataType(), 4); //Tipo float
83
                        assertEquals(g.getNY(), 25); //Alto
84
                        assertEquals(g.getNX(), 28); //Ancho
85

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

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