Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.main / src / test / java / org / gvsig / fmap / dal / coverage / grid / TestGridReadingFullDatasource.java @ 2443

History | View | Annotate | Download (6.22 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.fmap.dal.coverage.grid;
23

    
24
import org.gvsig.fmap.dal.coverage.BaseTestCase;
25
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
26
import org.gvsig.fmap.dal.coverage.exception.GridException;
27
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
28
import org.gvsig.fmap.dal.coverage.exception.OutOfGridException;
29
import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidAccessException;
30
import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidException;
31
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
32
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
33
import org.gvsig.raster.impl.grid.GridImpl;
34

    
35
/**
36
 * Este test prueba el acceso a datos a traves de un grid.
37
 * A partir de una fuente de datos (cargada en el constructor del Grid). 
38
 * 1-F32 Monobanda: Se har? una petici?n del extent completo de la fuente seleccionando tres bandas.
39
 * 2-Comprobamos el tipo de datos el ancho y el alto
40
 * 3-Modifica algunos valores del grid y se comprueba que las modificaciones han sido efectivas.
41
 * 4-Cambiamos la banda a operar y obtenemos un dato comprobando que tiene que devolver NoData
42
 * 
43
 * 1-RGB: Se har? una petici?n del extent completo de la fuente seleccionando tres bandas.
44
 * 2-Comprobamos el tipo de datos el ancho y el alto
45
 * 3-Modifica algunos valores del grid y se comprueba que las modificaciones han sido efectivas.
46
 * 4-Cambiamos la banda a operar y obtenemos un dato comprobando que el valor que devuelve es correcto
47
 * 5 -Comprobamos que si nos salimos devuelve un NoData
48
 * 
49
 * @author Nacho Brodin (nachobrodin@gmail.com)
50
 *
51
 */
52
public class TestGridReadingFullDatasource extends BaseTestCase {
53
        private String path1 = baseDir + "miniRaster28x25F32.tif";
54
        private String path2 = baseDir + "miniRaster25x24.tif";
55
        private RasterDataStore   f1       = null;
56
        private RasterDataStore   f2       = null;
57
        protected Buffer           buf1     = null;
58
        
59
        public void start() throws Exception {
60
                this.setUp();
61
                this.testStack();
62
        }
63
        
64
        protected void doSetUp() throws Exception {
65
                System.err.println("TGReadingFullDatasourceSelectingBands running...");
66
                
67
                try {
68
                        super.doSetUp();
69
                } catch (Exception e) {
70
                        e.printStackTrace();
71
                }
72
                
73
                try {
74
                        f1 = manager.open(path1);
75
                        f2 = manager.open(path2);
76
                } catch (NotSupportedExtensionException e) {
77
                        e.printStackTrace();
78
                } catch (RasterDriverException e) {
79
                        e.printStackTrace();
80
                }
81
        }
82
        
83
        public void testStack() {
84
                try {
85
                        Grid g = new GridImpl(f1);
86
                        g.setNoDataValue(-99999);
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(10000, 10000), -99999);
103
                        
104
                        g = new GridImpl(f2);
105
                        g.setNoDataValue(-99999);
106
                        assertEquals(g.getDataType(), 0); //Tipo byte
107
                        assertEquals(g.getNY(), 24); //Alto
108
                        assertEquals(g.getNX(), 25); //Ancho
109
                        testCellBufferByte(g, 0);
110
                        g.setCellValue(0, 0, (byte)31);
111
                        g.setCellValue(2, 2, (byte)31);
112
                        g.setCellValue(3, 8, (byte)31);
113
                        g.setCellValue(10, 15, (byte)31);
114
                        testCellBufferByte(g, 0);
115
                        assertEquals(g.getCellValueAsByte(0, 0), 31);
116
                        assertEquals(g.getCellValueAsByte(2, 2), 31);
117
                        assertEquals(g.getCellValueAsByte(3, 8), 31);
118
                        assertEquals(g.getCellValueAsByte(10, 15), 31);
119
                        g.setBandToOperate(2);
120
                        assertEquals((int)g.getCellValueAsByte(10, 15), 107);
121
                        
122
                        //Comprobamos que si nos salimos devuelve un NoData
123
                        assertEquals((int)g.getCellValueAsFloat(100, 100), -99999);
124
                        assertEquals((int)g.getCellValueAsFloat(-1, 10), -99999);
125
                        assertEquals((int)g.getCellValueAsFloat(1000, 2310), -99999);
126
                        
127
                } catch (RasterBufferInvalidException e1) {
128
                        e1.printStackTrace();
129
                } catch (RasterBufferInvalidAccessException e2) {
130
                        e2.printStackTrace();
131
                } catch (OutOfGridException e3) {
132
                        e3.printStackTrace();
133
                } catch (GridException e3) {
134
                        e3.printStackTrace();
135
                }
136
        }
137
                
138
        /**
139
         * Compara todos los elementos del buffer con los del grid para comprobar
140
         * la correcci?n de la lectura.
141
         * @param g
142
         * @throws RasterBufferInvalidAccessException
143
         */
144
        private void testCellBufferFloat(Grid g, int band) throws RasterBufferInvalidAccessException, GridException {
145
                int nCols = (int)f1.getWidth();
146
                int nRows = (int)f1.getHeight(); 
147
                for(int i = 0; i < nRows; i ++) {
148
                        for(int j = 0; j < nCols; j ++)
149
                                assertEquals((int)g.getCellValueAsFloat(j, i), (int)g.getRasterBuf().getElemFloat(i, j, band));
150
                }
151
        }
152

    
153
        /**
154
         * Compara todos los elementos del buffer con los del grid para comprobar
155
         * la correcci?n de la lectura.
156
         * @param g
157
         * @throws RasterBufferInvalidAccessException
158
         */
159
        private void testCellBufferByte(Grid g, int band) throws RasterBufferInvalidAccessException, GridException {
160
                int nCols = (int)f2.getWidth();
161
                int nRows = (int)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)g.getRasterBuf().getElemByte(i, j, band));
165
                }
166
        }
167
}