Statistics
| Revision:

root / trunk / libraries / libRaster / src-test / tmp / TestGrid.java @ 11068

History | View | Annotate | Download (6.2 KB)

1 10740 nacho
/*
2
 * Created on 9-ago-2006
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 */
25
package tmp;
26
27
import junit.framework.TestCase;
28
29 10960 nacho
import org.gvsig.raster.RasterLibrary;
30 10740 nacho
import org.gvsig.raster.dataaccess.DataSource;
31
import org.gvsig.raster.dataaccess.buffer.RasterBufferInvalidAccessException;
32
import org.gvsig.raster.dataaccess.buffer.RasterBufferInvalidException;
33 10939 nacho
import org.gvsig.raster.dataset.NotSupportedExtensionException;
34
import org.gvsig.raster.dataset.RasterDataset;
35
import org.gvsig.raster.dataset.RasterDriverException;
36 10740 nacho
import org.gvsig.raster.grid.Grid;
37
import org.gvsig.raster.grid.GridExtent;
38
import org.gvsig.raster.grid.GridInterpolated;
39
import org.gvsig.raster.grid.OutOfGridException;
40
import org.gvsig.raster.shared.Extent;
41
42
/**
43
 * Este test prueba el acceso a datos a traves de un grid
44 10756 nacho
 * @author Nacho Brodin (nachobrodin@gmail.com)
45 10740 nacho
 *
46
 */
47
public class TestGrid extends TestCase{
48
49
        private String baseDir = "./test-images/";
50
        private String path = baseDir + "03AUG23153350-M2AS-000000122423_01_P001-BROWSE.jpg";
51
        private RasterDataset f1 = null;
52
        private DataSource ds = null;
53
        /**
54
         * Casos de prueba para el grid
55
         * caso 0: Inicializaci?n de grid a partir de un rasterbuffer creado con coordenadas pixel de la imagen completa
56
         * caso 1: Inicializaci?n de grid a partir de una fuente de datos y un extent de imagen completo en coordenadas reales
57
         * caso 2: Grabado de datos sobre un grid y uso de operaciones
58
         * caso 3: Acceso a datos interpolados
59
         */
60
        private int caso = 2;
61
62
        static{
63
                RasterLibrary.wakeUp();
64
        }
65
66
        public void setUp() {
67
                System.out.println("TestGrid running...");
68
                try {
69
                        f1 = RasterDataset.openFile(null, path);
70
                } catch (NotSupportedExtensionException e) {
71
                        e.printStackTrace();
72
                } catch (RasterDriverException e) {
73
                        e.printStackTrace();
74
                }
75
                ds = new DataSource(f1);
76
        }
77
78
79
80
        public void testStack(){
81
                int[] drawableBands = {0, 1, 2};
82
83
                switch(caso){
84
                case 0:        ds.addDrawableBands(drawableBands);
85
                                ds.setAreaOfInterest(0, 0, f1.getWidth(), f1.getHeight());
86
                                try {
87
                                        Grid g = new Grid(ds, drawableBands);
88
                                        System.out.println (g.getDataType());
89
                                        System.out.println (g.getNY() + " " + g.getNX());
90
                                        testCellBuffer(g);
91
                                } catch (RasterBufferInvalidException e1) {
92
                                        e1.printStackTrace();
93
                                } catch (RasterBufferInvalidAccessException e2) {
94
                                        e2.printStackTrace();
95
                                }
96
                                break;
97
                case 1:        GridExtent ge = new GridExtent(ds.getExtent(), ds.getXCellSize());
98
                                ds.addDrawableBands(drawableBands);
99
                                Extent e = f1.getExtent();
100
                                double minX = e.getMin().getX();
101
                                double minY = e.getMin().getY();
102
                                double maxX = e.getMax().getX();
103
                                double maxY = e.getMax().getY();
104
                                ds.setAreaOfInterest(minX, maxY, maxX - minX, maxY - minY, true);
105
                                try {
106
                                        Grid g = new Grid(ds, drawableBands, ge);
107
                                        System.out.println (g.getDataType());
108
                                        System.out.println (g.getNY() + " " + g.getNX());
109
                                        testCellBuffer(g);
110
                                } catch (RasterBufferInvalidException e1) {
111
                                        e1.printStackTrace();
112
                                } catch (RasterBufferInvalidAccessException e2) {
113
                                        e2.printStackTrace();
114
                                }
115
                                break;
116
                case 2: ds.addDrawableBands(drawableBands);
117
                                ds.setAreaOfInterest(0, 0, f1.getWidth(), f1.getHeight());
118
                                try {
119
                                        Grid g = new Grid(ds, drawableBands);
120
                                        for(int i = 400; i < 410; i ++)
121
                                                System.out.print("("+g.getCellValueAsByte(i, 400)+","+ds.getRasterBuf().getElemByte(400, i, 0)+") ");
122
                                        System.out.println();
123
                                        g.setCellValue(400, 400, (byte)10);
124
                                        g.addToCellValue(401, 400, (byte)2);
125
                                        g.multiply(0.5);
126
                                        for(int i = 400; i < 410; i ++)
127
                                                System.out.print("("+g.getCellValueAsByte(i, 400)+","+ds.getRasterBuf().getElemByte(400, i, 0)+") ");
128
                                        System.out.println();
129
                                        g.assign((byte)3);
130
                                        for(int i = 400; i < 410; i ++)
131
                                                System.out.print("("+g.getCellValueAsByte(i, 400)+","+ds.getRasterBuf().getElemByte(400, i, 0)+") ");
132
133
                                } catch (RasterBufferInvalidException e1) {
134
                                        e1.printStackTrace();
135
                                } catch (RasterBufferInvalidAccessException e2) {
136
                                        e2.printStackTrace();
137
                                } catch (OutOfGridException e2) {
138
                                        e2.printStackTrace();
139
                                }
140
                                break;
141
                case 3: //TODO: TEST: Probar acceso datos interpolados
142
                                ds.addDrawableBands(drawableBands);
143
                                ds.setAreaOfInterest(0, 0, f1.getWidth(), f1.getHeight());
144
                                try {
145
                                        Grid g = new Grid(ds, drawableBands);
146
                                        g.setInterpolationMethod(GridInterpolated.INTERPOLATION_Bilinear);
147
                                        for(int i = 400; i < 410; i ++)
148
                                                System.out.print("("+g.getCellValueAsByte(i, 400)+","+ds.getRasterBuf().getElemByte(400, i, 0)+") ");
149
                                        System.out.println();
150
                                } catch (RasterBufferInvalidException e1) {
151
                                        e1.printStackTrace();
152
                                } catch (RasterBufferInvalidAccessException e2) {
153
                                        e2.printStackTrace();
154
                                }
155
                                break;
156
                }
157
158
159
        }
160
161
        /**
162
         * Compara todos los elementos del buffer con los del grid para comprobar
163
         * la correcci?n de la lectura.
164
         * @param g
165
         * @throws RasterBufferInvalidAccessException
166
         */
167
        private void testCellBuffer(Grid g) throws RasterBufferInvalidAccessException{
168
                int nCols = f1.getWidth();
169
                int nRows = f1.getHeight();
170
                for(int i = 0; i < nRows; i ++){
171
                        for(int j = 0; j < nCols; j ++){
172
                                assertEquals(g.getCellValueAsByte(j, i), ds.getRasterBuf().getElemByte(i, j, 0));
173
                                //System.out.print("("+g.getCellValueAsByte(i, 400)+","+ds.getRasterBuf().getElemByte(400, i, 0)+") ");
174
                        }
175
                        //System.out.println();
176
                }
177
        }
178
179
}