Statistics
| Revision:

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

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

    
21
import junit.framework.TestCase;
22

    
23
import org.gvsig.raster.dataaccess.DataSource;
24
import org.gvsig.raster.driver.FileNotOpenException;
25
import org.gvsig.raster.driver.IBuffer;
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.shared.DataClass;
30
import org.gvsig.raster.shared.DataClassList;
31
import org.gvsig.raster.shared.RasterLibrary;
32

    
33
/**
34
 * Test a un histograma usando clases. El test se realiza sobre un raster en 
35
 * coma flotante de 28x25 pixels. Se crean clases de tres tipo de intervalos distintos y
36
 * se testean los valores obtenidos en el histograma resultante. El ?ltimo
37
 * test comprueba la generaci?n autom?tica de clases.
38
 * 
39
 * @author Nacho Brodin (nachobrodin@gmail.com)
40
 */
41
public class TestGdalClassHistogram extends TestCase{
42

    
43
        private String baseDir = "./test-images/";
44
        private String path = baseDir + "miniRaster28x25F32.tif";        
45
        private RasterDataset f = null;
46
        private DataSource ds = null;
47
        
48
        public void start(){
49
                this.setUp();
50
                this.testStack();
51
        }
52
        
53
        public void setUp() {
54
                System.err.println("TestGdalClassHistogram running...");
55
        }
56

    
57
        static{
58
                RasterLibrary.wakeUp();
59
        }
60
        
61
        public void testStack(){
62
                int[] drawableBands = {0, 1, 2};
63
                try {
64
                        f = RasterDataset.openFile(null, path);
65
                } catch (NotSupportedExtensionException e) {
66
                        return;
67
                } catch (RasterDriverException e) {
68
                        return;
69
                }
70
                ds = new DataSource(f);
71
                ds.addDrawableBands(drawableBands);
72
                ds.setAreaOfInterest(0, 0, 28, 25);
73
                //printData();
74
                DataClassList classList = new DataClassList();
75
                DataClass classInterval1 = new DataClass(0D, 1000D, 0, "");
76
                DataClass classInterval2 = new DataClass(1000D, 2000D, 1, "");
77
                classList.addClass(classInterval1);
78
                classList.addClass(classInterval2);
79
                long[][] histogram = null;
80
                try {
81
                        histogram = ds.getHistogram(classList);
82
                } catch (FileNotOpenException e) {
83
                        e.printStackTrace();
84
                } catch (RasterDriverException e) {
85
                        e.printStackTrace();
86
                }
87
                testHist1(histogram);
88
                
89
                classList.clear();
90
                classInterval1 = new DataClass(0D, 1200D, 0, "");
91
                classInterval2 = new DataClass(1200D, 2000D, 1, "");
92
                classList.addClass(classInterval1);
93
                classList.addClass(classInterval2);
94
                try {
95
                        histogram = ds.getHistogram(classList);
96
                } catch (FileNotOpenException e) {
97
                        e.printStackTrace();
98
                } catch (RasterDriverException e) {
99
                        e.printStackTrace();
100
                }
101
                testHist2(histogram);
102
                
103
                classList.clear();
104
                classInterval1 = new DataClass(0D, 1150D, 0, "");
105
                classInterval2 = new DataClass(115D, 1175D, 1, "");
106
                DataClass classInterval3 = new DataClass(1175D, 1200D, 2, "");
107
                DataClass classInterval4 = new DataClass(1200D, 1210D, 3, "");
108
                DataClass classInterval5 = new DataClass(1210D, 2000D, 4, "");
109
                classList.addClass(classInterval1);
110
                classList.addClass(classInterval2);
111
                classList.addClass(classInterval3);
112
                classList.addClass(classInterval4);
113
                classList.addClass(classInterval5);
114
                try {
115
                        histogram = ds.getHistogram(classList);
116
                } catch (FileNotOpenException e) {
117
                        e.printStackTrace();
118
                } catch (RasterDriverException e) {
119
                        e.printStackTrace();
120
                }
121
                testHist3(histogram);
122
                
123
                try {
124
                        histogram = ds.getHistogram(null);
125
                } catch (FileNotOpenException e) {
126
                        e.printStackTrace();
127
                } catch (RasterDriverException e) {
128
                        e.printStackTrace();
129
                }
130
                testHist4(histogram);
131
                //print(histogram);
132
        }
133
        
134
        private void testHist1(long[][] histogram){
135
                assertEquals(histogram[0][0], 0);
136
                assertEquals(histogram[0][1], 700);
137
        } 
138

    
139
        private void testHist2(long[][] histogram){
140
                assertEquals(histogram[0][0], 180);
141
                assertEquals(histogram[0][1], 520);
142
        }
143
        
144
        private void testHist3(long[][] histogram){
145
                assertEquals(histogram[0][0], 4);
146
                assertEquals(histogram[0][1], 38);
147
                assertEquals(histogram[0][2], 138);
148
                assertEquals(histogram[0][3], 100);
149
                assertEquals(histogram[0][4], 420);
150
        }
151
        
152
        private void testHist4(long[][] histogram){
153
                assertEquals(histogram[0][0], 10);
154
                assertEquals(histogram[0][1], 17);
155
                assertEquals(histogram[0][2], 36);
156
                assertEquals(histogram[0][3], 72);
157
                assertEquals(histogram[0][4], 119);
158
                assertEquals(histogram[0][5], 129);
159
                assertEquals(histogram[0][6], 98);
160
                assertEquals(histogram[0][7], 93);
161
                assertEquals(histogram[0][8], 76);
162
                assertEquals(histogram[0][9], 49);
163
        }
164
        
165
        private void print(long[][] histogram){
166
                for (int i = 0; i < histogram.length; i++) {
167
                        for (int j = 0; j < histogram[i].length; j++){
168
                                System.out.print(histogram[i][j] + " ");
169
                        }
170
                        System.out.println("");
171
                }
172
        }
173
        
174
        /**
175
         * Imprime todos los pixels de la fuente de datos en RGB
176
         */
177
        private void printData(){
178
                IBuffer raster = ds.getRasterBuf();
179
                for(int line = 0; line < raster.getHeight(); line++){
180
                        for(int col = 0; col < raster.getWidth(); col++)
181
                                System.out.print("(" + raster.getElemFloat(line, col, 0) + ")");
182
                        System.out.println();
183
                }
184
        }
185
}