Statistics
| Revision:

root / trunk / libraries / libRaster / src-test / org / gvsig / fmap / dataaccess / TestGdalClassHistogram.java @ 10740

History | View | Annotate | Download (5.59 KB)

1 10740 nacho
/* 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.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 setUp() {
49
                System.out.println("TestGdalClassHistogram running...");
50
        }
51
52
        static{
53
                RasterLibrary.wakeUp();
54
        }
55
56
        public void testStack(){
57
                int[] drawableBands = {0, 1, 2};
58
                try {
59
                        f = RasterDataset.openFile(null, path);
60
                } catch (NotSupportedExtensionException e) {
61
                        return;
62
                } catch (RasterDriverException e) {
63
                        return;
64
                }
65
                ds = new DataSource(f);
66
                ds.addDrawableBands(drawableBands);
67
                ds.setAreaOfInterest(0, 0, 28, 25);
68
                //printData();
69
                DataClassList classList = new DataClassList();
70
                DataClass classInterval1 = new DataClass(0D, 1000D, 0, "");
71
                DataClass classInterval2 = new DataClass(1000D, 2000D, 1, "");
72
                classList.addClass(classInterval1);
73
                classList.addClass(classInterval2);
74
                long[][] histogram = null;
75
                try {
76
                        histogram = ds.getHistogram(classList);
77
                } catch (FileNotOpenException e) {
78
                        e.printStackTrace();
79
                } catch (RasterDriverException e) {
80
                        e.printStackTrace();
81
                }
82
                testHist1(histogram);
83
84
                classList.clear();
85
                classInterval1 = new DataClass(0D, 1200D, 0, "");
86
                classInterval2 = new DataClass(1200D, 2000D, 1, "");
87
                classList.addClass(classInterval1);
88
                classList.addClass(classInterval2);
89
                try {
90
                        histogram = ds.getHistogram(classList);
91
                } catch (FileNotOpenException e) {
92
                        e.printStackTrace();
93
                } catch (RasterDriverException e) {
94
                        e.printStackTrace();
95
                }
96
                testHist2(histogram);
97
98
                classList.clear();
99
                classInterval1 = new DataClass(0D, 1150D, 0, "");
100
                classInterval2 = new DataClass(115D, 1175D, 1, "");
101
                DataClass classInterval3 = new DataClass(1175D, 1200D, 2, "");
102
                DataClass classInterval4 = new DataClass(1200D, 1210D, 3, "");
103
                DataClass classInterval5 = new DataClass(1210D, 2000D, 4, "");
104
                classList.addClass(classInterval1);
105
                classList.addClass(classInterval2);
106
                classList.addClass(classInterval3);
107
                classList.addClass(classInterval4);
108
                classList.addClass(classInterval5);
109
                try {
110
                        histogram = ds.getHistogram(classList);
111
                } catch (FileNotOpenException e) {
112
                        e.printStackTrace();
113
                } catch (RasterDriverException e) {
114
                        e.printStackTrace();
115
                }
116
                testHist3(histogram);
117
118
                try {
119
                        histogram = ds.getHistogram(null);
120
                } catch (FileNotOpenException e) {
121
                        e.printStackTrace();
122
                } catch (RasterDriverException e) {
123
                        e.printStackTrace();
124
                }
125
                testHist4(histogram);
126
                //print(histogram);
127
        }
128
129
        private void testHist1(long[][] histogram){
130
                assertEquals(histogram[0][0], 0);
131
                assertEquals(histogram[0][1], 700);
132
        }
133
134
        private void testHist2(long[][] histogram){
135
                assertEquals(histogram[0][0], 180);
136
                assertEquals(histogram[0][1], 520);
137
        }
138
139
        private void testHist3(long[][] histogram){
140
                assertEquals(histogram[0][0], 4);
141
                assertEquals(histogram[0][1], 38);
142
                assertEquals(histogram[0][2], 138);
143
                assertEquals(histogram[0][3], 100);
144
                assertEquals(histogram[0][4], 420);
145
        }
146
147
        private void testHist4(long[][] histogram){
148
                assertEquals(histogram[0][0], 10);
149
                assertEquals(histogram[0][1], 17);
150
                assertEquals(histogram[0][2], 36);
151
                assertEquals(histogram[0][3], 72);
152
                assertEquals(histogram[0][4], 119);
153
                assertEquals(histogram[0][5], 129);
154
                assertEquals(histogram[0][6], 98);
155
                assertEquals(histogram[0][7], 93);
156
                assertEquals(histogram[0][8], 76);
157
                assertEquals(histogram[0][9], 49);
158
        }
159
160
        private void print(long[][] histogram){
161
                for (int i = 0; i < histogram.length; i++) {
162
                        for (int j = 0; j < histogram[i].length; j++){
163
                                System.out.print(histogram[i][j] + " ");
164
                        }
165
                        System.out.println("");
166
                }
167
        }
168
169
        /**
170
         * Imprime todos los pixels de la fuente de datos en RGB
171
         */
172
        private void printData(){
173
                IBuffer raster = ds.getRasterBuf();
174
                for(int line = 0; line < raster.getHeight(); line++){
175
                        for(int col = 0; col < raster.getWidth(); col++)
176
                                System.out.print("(" + raster.getElemFloat(line, col, 0) + ")");
177
                        System.out.println();
178
                }
179
        }
180
}