Statistics
| Revision:

root / trunk / libraries / libRaster / src-test / org / gvsig / raster / buffer / TestGdalClassHistogram.java @ 11074

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

    
21
import junit.framework.TestCase;
22

    
23
import org.gvsig.raster.RasterLibrary;
24
import org.gvsig.raster.buffer.BufferFactory;
25
import org.gvsig.raster.dataset.FileNotOpenException;
26
import org.gvsig.raster.dataset.IBuffer;
27
import org.gvsig.raster.dataset.NotSupportedExtensionException;
28
import org.gvsig.raster.dataset.RasterDataset;
29
import org.gvsig.raster.dataset.RasterDriverException;
30
import org.gvsig.raster.dataset.RasterMultiDataset;
31
import org.gvsig.raster.util.DataClass;
32
import org.gvsig.raster.util.DataClassList;
33
import org.gvsig.raster.util.Histogram;
34

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

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

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

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