Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / shared / StatisticsRmfSerializer.java @ 12307

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

    
21
import java.io.IOException;
22
import java.io.Reader;
23
import java.io.StringReader;
24

    
25
import org.gvsig.raster.dataset.IBuffer;
26
import org.gvsig.raster.dataset.io.rmf.ClassSerializer;
27
import org.gvsig.raster.dataset.io.rmf.ParsingException;
28
import org.gvsig.raster.dataset.properties.DatasetStatistics;
29
import org.kxml2.io.KXmlParser;
30
import org.xmlpull.v1.XmlPullParserException;
31

    
32
/**
33
 * <P>
34
 * Clase para convertir a XML las estadisticas y obtener las estad?sticas desde XML.
35
 * Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y 
36
 * lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
37
 * leer datos.
38
 * </P>
39
 * <P>
40
 * La estructura XML de las estad?sticas es la siguiente:
41
 * </P>
42
 * <P>
43
 *\<Statistics\> <BR>
44
 *&nbsp;\<Max\>0\</Max\><BR>
45
 *&nbsp;\<SecondMax\>0\</SecondMax\><BR>
46
 *&nbsp;\<Min\>0\</Min\><BR>
47
 *&nbsp;\<SecondMin\>0\</SecondMin\><BR>
48
 *&nbsp;\<Maximun\>0\</Maximun\><BR>
49
 *&nbsp;\<Minimun\>0\</Minimun\><BR>
50
 *&nbsp;\<Mean\>0\</Mean\><BR>
51
 *&nbsp;\<Variance\>0\</Variance\><BR>
52
 *&nbsp;\<BandCount\>0\</BandCount\><BR>
53
 *&nbsp;\<TailTrim\>1.0:23 2.0:34 .... \</TailTrim\><BR>
54
 *\</Statistics\><BR>
55
 *</P>
56
 *
57
 * @version 23/04/2007
58
 * @author Nacho Brodin (nachobrodin@gmail.com)
59
 *
60
 */
61
public class StatisticsRmfSerializer extends ClassSerializer {
62
        
63
        //TAGS
64
        public static final String MAIN_TAG = "Statistics";
65
        public static final String BAND = "Band";
66
        public static final String MIN = "Min";
67
        public static final String MAX = "Max";
68
        public static final String SNDMIN = "SecondMin";
69
        public static final String SNDMAX = "SecondMax";
70
        public static final String MAXIMUN = "Maximun";
71
        public static final String MINIMUN = "Minimun";
72
        public static final String MEAN = "Mean";
73
        public static final String VARIANCE = "Variance";
74
        public static final String BANDCOUNT = "BandCount";
75
        public static final String TAILTRIM = "TailTrim";
76
        public static final String KEY = "Key";
77
        public static final String VALUE = "Value";
78
                
79
        private DatasetStatistics  stat = null;
80

    
81
        /**
82
         * Constructor. Asigna la tabla a serializar.
83
         * @param ColorTable tabla a convertir en XML
84
         */
85
        public StatisticsRmfSerializer(DatasetStatistics stat) {
86
                this.stat = stat;
87
        }
88
        
89
        /**
90
         * Constructor. 
91
         */
92
        public StatisticsRmfSerializer() {
93
        }
94
                        
95
        /**
96
         * Parsea el tag Band para extraer la lista de valores (Values) asociada a una banda. 
97
         * @param parser KXmlParser
98
         * @return Array de long
99
         * @throws XmlPullParserException
100
         * @throws IOException
101
         */
102
        private long[] parserStatBandValues(KXmlParser parser, int band, double[] max, double[] min, double[] sndmax, double[] sndmin, double[] mean, double[] variance)  throws XmlPullParserException, IOException {
103
                boolean maxOk = false, minOk = false, sndmaxOk = false, sndminOk = false, meanOk = false, varianceOk = false;
104
                long[] valueList = null;
105
                boolean end = false;
106
            int tag = parser.next();
107
            while (!end) {
108
                    switch(tag) {
109
                        case KXmlParser.START_TAG:
110
                                if(parser.getName() != null) {        
111
                                                if (parser.getName().compareTo(MAX) == 0)
112
                                                        maxOk = true;
113
                                                if (parser.getName().compareTo(MIN) == 0)
114
                                                        minOk = true;
115
                                                if (parser.getName().compareTo(SNDMAX) == 0)
116
                                                        sndmaxOk = true;
117
                                                if (parser.getName().compareTo(SNDMIN) == 0)
118
                                                        sndminOk = true;
119
                                                if (parser.getName().compareTo(MEAN) == 0)
120
                                                        meanOk = true;
121
                                                if (parser.getName().compareTo(VARIANCE) == 0)
122
                                                        varianceOk = true;
123
                                        }                                
124
                                        break;
125
                         case KXmlParser.END_TAG:
126
                                 if (parser.getName().compareTo(BAND) == 0)
127
                                         end = true;
128
                                break;
129
                        case KXmlParser.TEXT:
130
                                if(maxOk) {
131
                                        max[band] = Double.parseDouble(parser.getText());
132
                                        maxOk = false;
133
                                }
134
                                if(minOk) {
135
                                        min[band] = Double.parseDouble(parser.getText());
136
                                        minOk = false;
137
                                }
138
                                if(sndmaxOk) {
139
                                        sndmax[band] = Double.parseDouble(parser.getText());
140
                                        sndmaxOk = false;
141
                                }
142
                                if(sndminOk) {
143
                                        sndmin[band] = Double.parseDouble(parser.getText());
144
                                        sndminOk = false;
145
                                }
146
                                if(meanOk) {
147
                                        mean[band] = Double.parseDouble(parser.getText());
148
                                        meanOk = false;
149
                                }
150
                                if(varianceOk) {
151
                                        variance[band] = Double.parseDouble(parser.getText());
152
                                        varianceOk = false;
153
                                }
154
                                break;
155
                    }
156
                    if (!end)
157
                            tag = parser.next();
158
            }
159
            return valueList;
160
        }
161
        
162
        /*
163
         * (non-Javadoc)
164
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
165
         */
166
        public void read(String xml) throws ParsingException {
167
                int bandCount = 0;
168
                double[] max = null;
169
                double[] min = null;
170
                double[] secondMax = null;
171
                double[] secondMin = null;
172
                double[] mean = null;
173
                double[] variance = null;
174
                
175
                KXmlParser parser = new KXmlParser();
176
                Reader reader = new StringReader(xml);
177
                try {
178
                        parser.setInput(reader);
179
                } catch (XmlPullParserException e) {
180
                        throw new ParsingException(xml);
181
                }
182
                try {
183
                        int tag = parser.nextTag();
184
                        
185
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){                    
186
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);                            
187
                                while(tag != KXmlParser.END_DOCUMENT) {
188
                                        switch(tag) {
189
                                                case KXmlParser.START_TAG:
190
                                                        if(parser.getName() != null) {         
191
                                                                if (parser.getName().compareTo(MAIN_TAG) == 0) {
192
                                                                        bandCount = Integer.parseInt(parserString(parser, BANDCOUNT, null));
193
                                                                        if(max == null) {
194
                                                                                max = new double[bandCount];
195
                                                                                min = new double[bandCount];
196
                                                                                secondMax = new double[bandCount];
197
                                                                                secondMin = new double[bandCount];
198
                                                                                mean = new double[bandCount];
199
                                                                                variance = new double[bandCount];
200
                                                                        }
201
                                                                        for (int i = 0; i < bandCount; i++)
202
                                                                                parserStatBandValues(parser, i, max, min, secondMax, secondMin, mean, variance);
203
                                                                }
204
                                                        }        
205
                                                        break;
206
                                                case KXmlParser.END_TAG:                                                                
207
                                                        break;
208
                                                case KXmlParser.TEXT:                                                        
209
                                                        break;
210
                                        }
211
                                        tag = parser.next();
212
                                }
213
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
214
                        }
215
                        
216
                } catch (XmlPullParserException e) {
217
                        throw new ParsingException(xml);
218
                } catch (IOException e) {
219
                        throw new ParsingException(xml);
220
                }
221
                if(stat == null)
222
                        stat = new DatasetStatistics(null);
223
                stat.setBandCount(bandCount);
224
                stat.setMax(max);
225
                stat.setMin(min);
226
                stat.setSecondMax(secondMax);
227
                stat.setSecondMin(secondMin);
228
                stat.setMean(mean);
229
                stat.setVariance(variance);
230
                stat.setCalculated(true);
231
        }
232

    
233
        /*
234
         * (non-Javadoc)
235
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
236
         */
237
        public String write() {
238
                StringBuffer b = new StringBuffer();
239
                                
240
                b.append("<" + MAIN_TAG + ">\n");
241
                putProperty(b, BANDCOUNT, stat.getBandCount(), 1);
242
                for (int i = 0; i < stat.getBandCount(); i++) {
243
                        b.append("\t<" + BAND + ">\n");
244
                        putProperty(b, MAX, stat.getMax()[i], 2);
245
                        putProperty(b, MIN, stat.getMin()[i], 2);
246
                        putProperty(b, SNDMAX, stat.getSecondMax()[i], 2);
247
                        putProperty(b, SNDMIN, stat.getSecondMin()[i], 2);
248
                        putProperty(b, MEAN, stat.getMean()[i], 2);
249
                        putProperty(b, VARIANCE, stat.getVariance()[i], 2);        
250
                        b.append("\t</" + BAND + ">\n");
251
                }
252
                for (int i = 0; i < stat.getTailTrimCount(); i++) {
253
                        b.append("\t<" + TAILTRIM + ">\n");
254
                        putProperty(b, KEY, ((Double)stat.getTailTrimValue(i)[0]).doubleValue(), 2);
255
                        putProperty(b, VALUE, ((Double)stat.getTailTrimValue(i)[1]).doubleValue(), 2);
256
                        b.append("\t</" + TAILTRIM + ">\n");
257
                }
258
                b.append("</" + MAIN_TAG + ">");
259
                return b.toString();
260
        }
261
        
262
        /*
263
         * (non-Javadoc)
264
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
265
         */
266
        public Object getResult() {
267
                return stat;
268
        }
269

    
270
        /*
271
         *  (non-Javadoc)
272
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
273
         */
274
        public String getMainTag() {
275
                return MAIN_TAG;
276
        }
277
}