Statistics
| Revision:

root / trunk / libraries / libRaster / src-test / org / gvsig / raster / dataset / TestStatistics.java @ 11074

History | View | Annotate | Download (6.66 KB)

1
/*
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 org.gvsig.raster.dataset;
26

    
27
import org.gvsig.raster.RasterLibrary;
28
import org.gvsig.raster.dataset.FileNotOpenException;
29
import org.gvsig.raster.dataset.IBuffer;
30
import org.gvsig.raster.dataset.NotSupportedExtensionException;
31
import org.gvsig.raster.dataset.RasterDataset;
32
import org.gvsig.raster.dataset.RasterDriverException;
33
import org.gvsig.raster.dataset.io.EcwDriver;
34
import org.gvsig.raster.dataset.io.GdalDriver;
35
import org.gvsig.raster.dataset.properties.DatasetStatistics;
36

    
37
import junit.framework.TestCase;
38

    
39
/**
40
 * Este test prueba el calculo de estadisticas de un dataset. 
41
 * Comprueba que los valores obtenidos en las estadisticas son correctos.
42
 * 
43
 * 
44
 * Algunos costes del calculo de estadistica en el raster completo
45
 * 
46
 * Ordenador: AMD Athlon(tm) XP 1800+ 1540MHz, 256KB Cach?, 500M RAM
47
 *         Coste 1:
48
 *                 Raster: 17910x16062, 275M (GTiff), 1 Banda byte
49
 *                 Tiempo: 74.295 segs (sgtes igual)
50
 *         Coste 2:
51
 *                 Raster: 8955x8031, 69M (GTiff), 1 Banda byte
52
 *          Tiempo: 18.905 segs (sgtes 3.362s)
53
 *  Coste 3:
54
 *                 Raster: 812x586, 2.8M (GTiff), 3 Banda short
55
 *          Tiempo: 0.925 segs (sgtes 0.152s)
56
 *  Coste 4:
57
 *                 Raster: 870x870, 210K (Jpeg), 3 Banda byte
58
 *          Tiempo: 0.318 segs (sgtes 0.255s)
59
 * 
60
 * @author Nacho Brodin (nachobrodin@gmail.com)
61
 *
62
 */
63
public class TestStatistics extends TestCase {
64

    
65
        private String baseDir = "./test-images/";
66
        private String path1 = baseDir + "03AUG23153350-M2AS-000000122423_01_P001-BROWSE.jpg";
67
        private String path2 = baseDir + "wcs16bits.tif";
68

    
69
        private RasterDataset f1 = null;
70
        private RasterDataset f2 = null;
71
        private DatasetStatistics stats1 = null;
72
        private DatasetStatistics stats2 = null;
73
        
74
        static {
75
                RasterLibrary.wakeUp();
76
        }
77
        
78
        public void start() {
79
                this.setUp();
80
                this.testStack();
81
        }
82
        
83
        public void setUp() {
84
                System.err.println("TestStatistics running...");
85
                try {
86
                        f1 = RasterDataset.open(null, path1);
87
                        f2 = RasterDataset.open(null, path2);
88
                } catch (NotSupportedExtensionException e) {
89
                        e.printStackTrace();
90
                } catch (RasterDriverException e) {
91
                        e.printStackTrace();
92
                }
93
                stats1 = f1.getStatistics();
94
                stats2 = f2.getStatistics();
95
        }
96
        
97
        public void testStack(){
98
                try {
99
                        stats1.calcFullStatistics();
100
                } catch (FileNotOpenException e) {
101
                        e.printStackTrace();
102
                } catch (RasterDriverException e) {
103
                        e.printStackTrace();
104
                }
105
                dataTestB1(stats1);
106
                dataTestB2(stats1);
107
                dataTestB3(stats1);
108
                //print(stats1);
109
                try {
110
                        stats2.calcFullStatistics();
111
                } catch (FileNotOpenException e) {
112
                        e.printStackTrace();
113
                } catch (RasterDriverException e) {
114
                        e.printStackTrace();
115
                }
116
                dataTestB1_2(stats2);
117
                dataTestB2_2(stats2);
118
                dataTestB3_2(stats2);
119
                //print(stats2);
120
        }
121
        
122
        private void dataTestB1(DatasetStatistics stats){
123
                assertEquals(new Double(stats.getMax()[0]), new Double(255.0));
124
                assertEquals(new Double(stats.getMin()[0]), new Double(0.0));
125
                assertEquals(new Double(stats.getSecondMax()[0]), new Double(253.0));
126
                assertEquals(new Double(stats.getSecondMin()[0]), new Double(7.0));
127
                assertEquals((int)stats.getMean()[0], 109);
128
                assertEquals((int)stats.getVariance()[0], 6125);
129
        }
130
        
131
        private void dataTestB2(DatasetStatistics stats){
132
                assertEquals(new Double(stats.getMax()[1]), new Double(255.0));
133
                assertEquals(new Double(stats.getMin()[1]), new Double(0.0));
134
                assertEquals(new Double(stats.getSecondMax()[1]), new Double(254.0));
135
                assertEquals(new Double(stats.getSecondMin()[1]), new Double(1.0));
136
                assertEquals((int)stats.getMean()[1], 111);
137
                assertEquals((int)stats.getVariance()[1], 5722);
138
        }
139
        
140
        private void dataTestB3(DatasetStatistics stats){
141
                assertEquals(new Double(stats.getMax()[2]), new Double(255.0));
142
                assertEquals(new Double(stats.getMin()[2]), new Double(0.0));
143
                assertEquals(new Double(stats.getSecondMax()[2]), new Double(253.0));
144
                assertEquals(new Double(stats.getSecondMin()[2]), new Double(5.0));
145
                assertEquals((int)stats.getMean()[2], 98);
146
                assertEquals((int)stats.getVariance()[2], 5495);
147
        }
148
        
149
        private void dataTestB1_2(DatasetStatistics stats){
150
                assertEquals(new Double(stats.getMax()[0]), new Double(1269.0));
151
                assertEquals(new Double(stats.getMin()[0]), new Double(0.0));
152
                assertEquals(new Double(stats.getSecondMax()[0]), new Double(1233.0));
153
                assertEquals(new Double(stats.getSecondMin()[0]), new Double(27.0));
154
                assertEquals((int)stats.getMean()[0], 71);
155
                assertEquals((int)stats.getVariance()[0], 22678);
156
        }
157
        
158
        private void dataTestB2_2(DatasetStatistics stats){
159
                assertEquals(new Double(stats.getMax()[1]), new Double(1525.0));
160
                assertEquals(new Double(stats.getMin()[1]), new Double(0.0));
161
                assertEquals(new Double(stats.getSecondMax()[1]), new Double(1498.0));
162
                assertEquals(new Double(stats.getSecondMin()[1]), new Double(35.0));
163
                assertEquals((int)stats.getMean()[1], 104);
164
                assertEquals((int)stats.getVariance()[1], 37907);
165
        }
166
        
167
        private void dataTestB3_2(DatasetStatistics stats){
168
                assertEquals(new Double(stats.getMax()[2]), new Double(888.0));
169
                assertEquals(new Double(stats.getMin()[2]), new Double(0.0));
170
                assertEquals(new Double(stats.getSecondMax()[2]), new Double(884.0));
171
                assertEquals(new Double(stats.getSecondMin()[2]), new Double(24.0));
172
                assertEquals((int)stats.getMean()[2], 75);
173
                assertEquals((int)stats.getVariance()[2], 17437);
174
        }
175
        
176
        private void print(DatasetStatistics stats){
177
                for(int iBand = 0; iBand < f1.getBandCount(); iBand ++){
178
                        System.out.println("Band " + iBand);
179
                        System.out.println("...Max: " + stats.getMax()[iBand]);
180
                        System.out.println("...Min: " + stats.getMin()[iBand]);
181
                        System.out.println("...SecondMax: " + stats.getSecondMax()[iBand]);
182
                        System.out.println("...SecondMin: " + stats.getSecondMin()[iBand]);
183
                        System.out.println("...Mean: " + stats.getMean()[iBand]);
184
                        System.out.println("...Variance: " + stats.getVariance()[iBand]);
185
                }
186
        }
187

    
188
}