Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / serializer / StatisticsRmfSerializer.java @ 2623

History | View | Annotate | Download (12.5 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.store.serializer;
23

    
24
import java.io.IOException;
25
import java.io.Reader;
26
import java.io.StringReader;
27

    
28
import org.gvsig.fmap.dal.coverage.exception.ParsingException;
29
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
30
import org.gvsig.raster.impl.store.properties.AbstractStatistics;
31
import org.gvsig.raster.impl.store.properties.SimpleProviderStatistics;
32
import org.gvsig.raster.impl.store.rmf.ClassSerializer;
33
import org.gvsig.tools.ToolsLocator;
34
import org.gvsig.tools.extensionpoint.ExtensionPoint;
35
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
36
import org.kxml2.io.KXmlParser;
37
import org.xmlpull.v1.XmlPullParserException;
38
/**
39
 * <P>
40
 * Clase para convertir a XML las estadisticas y obtener las estad?sticas desde XML.
41
 * Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y
42
 * lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
43
 * leer datos.
44
 * </P>
45
 * <P>
46
 * La estructura XML de las estad?sticas es la siguiente:
47
 * </P>
48
 * <P>
49
 *\<Statistics\> <BR>
50
 *&nbsp;\<Max\>0\</Max\><BR>
51
 *&nbsp;\<SecondMax\>0\</SecondMax\><BR>
52
 *&nbsp;\<Min\>0\</Min\><BR>
53
 *&nbsp;\<SecondMin\>0\</SecondMin\><BR>
54
 *&nbsp;\<Maximun\>0\</Maximun\><BR>
55
 *&nbsp;\<Minimun\>0\</Minimun\><BR>
56
 *&nbsp;\<Mean\>0\</Mean\><BR>
57
 *&nbsp;\<Variance\>0\</Variance\><BR>
58
 *&nbsp;\<BandCount\>0\</BandCount\><BR>
59
 *&nbsp;\<TailTrim\>1.0:23 2.0:34 .... \</TailTrim\><BR>
60
 *\</Statistics\><BR>
61
 *</P>
62
 *
63
 * @version 23/04/2007
64
 * @author Nacho Brodin (nachobrodin@gmail.com)
65
 */
66
public class StatisticsRmfSerializer extends ClassSerializer {
67
        // TAGS
68
        public static final String MAIN_TAG  = "Statistics";
69
        public static final String BAND      = "Band";
70
        public static final String MIN       = "Min";
71
        public static final String MAX       = "Max";
72
        public static final String SNDMIN    = "SecondMin";
73
        public static final String SNDMAX    = "SecondMax";
74
        public static final String MINRGB    = "MinRGB";
75
        public static final String MAXRGB    = "MaxRGB";
76
        public static final String SNDMINRGB = "SecondMinRGB";
77
        public static final String SNDMAXRGB = "SecondMaxRGB";
78
        public static final String MAXIMUN   = "Maximun";
79
        public static final String MINIMUN   = "Minimun";
80
        public static final String MEAN      = "Mean";
81
        public static final String VARIANCE  = "Variance";
82
        public static final String NVALUES   = "NValues";
83
        public static final String BANDCOUNT = "BandCount";
84
        public static final String TAILTRIM  = "TailTrim";
85
        public static final String KEY       = "Key";
86
        public static final String VALUE     = "Value";
87

    
88
        private AbstractStatistics  stat      = null;
89

    
90
        /**
91
         * Registra StatisticsRmfSerializer en los puntos de extension de Serializer
92
         */
93
        public static void register() {
94
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
95
                ExtensionPoint point = extensionPoints.get("Serializer");
96
                point.append("Statistics", "", StatisticsRmfSerializer.class);
97
        }
98

    
99
        /**
100
         * Constructor. Asigna la tabla a serializar.
101
         * @param ColorTable tabla a convertir en XML
102
         */
103
        public StatisticsRmfSerializer(Statistics stat) {
104
                this.stat = (AbstractStatistics)stat;
105
        }
106
        
107
        /**
108
         * Constructor. Asigna la tabla a serializar.
109
         * @param ColorTable tabla a convertir en XML
110
         */
111
        public StatisticsRmfSerializer(AbstractStatistics stat) {
112
                this.stat = stat;
113
        }
114

    
115
        /**
116
         * Devuelve el objeto estadisticas de un dataset, en caso de no existir, lo crea.
117
         * @return
118
         */
119
        private AbstractStatistics getDatasetStatistics() {
120
                if (stat == null)
121
                        stat = new SimpleProviderStatistics(null);
122

    
123
                return stat;
124
        }
125

    
126
        /**
127
         * Constructor.
128
         */
129
        public StatisticsRmfSerializer() {
130
        }
131

    
132
        /**
133
         * Parsea el tag Band para extraer la lista de valores (Values) asociada a una banda.
134
         * @param parser KXmlParser
135
         * @return Array de long
136
         * @throws XmlPullParserException
137
         * @throws IOException
138
         */
139
        private long[] parserStatBandValues(KXmlParser parser, int band, double[] max, double[] min, double[] sndmax, double[] sndmin, double[] maxRGB, double[] minRGB, double[] sndmaxRGB, double[] sndminRGB, double[] mean, double[] variance, long[] nValues)  throws XmlPullParserException, IOException {
140
                boolean maxOk = false, minOk = false, sndmaxOk = false, sndminOk = false, meanOk = false, varianceOk = false, nvaluesOk = false;
141
                boolean maxRGBOk = false, minRGBOk = false, sndmaxRGBOk = false, sndminRGBOk = false;
142
                long[] valueList = null;
143
                boolean end = false;
144
                        int tag = parser.next();
145
                        while (!end) {
146
                                switch(tag) {
147
                                case KXmlParser.START_TAG:
148
                                        if(parser.getName() != null) {
149
                                                if (parser.getName().compareTo(MAX) == 0)
150
                                                        maxOk = true;
151
                                                if (parser.getName().compareTo(MIN) == 0)
152
                                                        minOk = true;
153
                                                if (parser.getName().compareTo(SNDMAX) == 0)
154
                                                        sndmaxOk = true;
155
                                                if (parser.getName().compareTo(SNDMIN) == 0)
156
                                                        sndminOk = true;
157
                                                if (parser.getName().compareTo(MAXRGB) == 0)
158
                                                        maxRGBOk = true;
159
                                                if (parser.getName().compareTo(MINRGB) == 0)
160
                                                        minRGBOk = true;
161
                                                if (parser.getName().compareTo(SNDMAXRGB) == 0)
162
                                                        sndmaxRGBOk = true;
163
                                                if (parser.getName().compareTo(SNDMINRGB) == 0)
164
                                                        sndminRGBOk = true;
165
                                                if (parser.getName().compareTo(MEAN) == 0)
166
                                                        meanOk = true;
167
                                                if (parser.getName().compareTo(VARIANCE) == 0)
168
                                                        varianceOk = true;
169
                                                if (parser.getName().compareTo(NVALUES) == 0)
170
                                                        nvaluesOk = true;
171
                                        }
172
                                        break;
173
                                case KXmlParser.END_TAG:
174
                                        if (parser.getName().compareTo(BAND) == 0)
175
                                                end = true;
176
                                        break;
177
                                case KXmlParser.TEXT:
178
                                        if(maxOk) {
179
                                                max[band] = Double.parseDouble(parser.getText());
180
                                                maxOk = false;
181
                                        }
182
                                        if(minOk) {
183
                                                min[band] = Double.parseDouble(parser.getText());
184
                                                minOk = false;
185
                                        }
186
                                        if(sndmaxOk) {
187
                                                sndmax[band] = Double.parseDouble(parser.getText());
188
                                                sndmaxOk = false;
189
                                        }
190
                                        if(sndminOk) {
191
                                                sndmin[band] = Double.parseDouble(parser.getText());
192
                                                sndminOk = false;
193
                                        }
194
                                        if(maxRGBOk) {
195
                                                maxRGB[band] = Double.parseDouble(parser.getText());
196
                                                maxRGBOk = false;
197
                                        }
198
                                        if(minRGBOk) {
199
                                                minRGB[band] = Double.parseDouble(parser.getText());
200
                                                minRGBOk = false;
201
                                        }
202
                                        if(sndmaxRGBOk) {
203
                                                sndmaxRGB[band] = Double.parseDouble(parser.getText());
204
                                                sndmaxRGBOk = false;
205
                                        }
206
                                        if(sndminRGBOk) {
207
                                                sndminRGB[band] = Double.parseDouble(parser.getText());
208
                                                sndminRGBOk = false;
209
                                        }
210
                                        if(meanOk) {
211
                                                mean[band] = Double.parseDouble(parser.getText());
212
                                                meanOk = false;
213
                                        }
214
                                        if(varianceOk) {
215
                                                variance[band] = Double.parseDouble(parser.getText());
216
                                                varianceOk = false;
217
                                        }
218
                                        if(nvaluesOk) {
219
                                                nValues[band] = Long.parseLong(parser.getText());
220
                                                nvaluesOk = false;
221
                                        }
222
                                        break;
223
                                }
224
                                if (!end)
225
                                        tag = parser.next();
226
                        }
227
                        return valueList;
228
        }
229

    
230
        public void read(String xml) throws ParsingException {
231
                int bandCount = 0;
232
                double[] max = null, maxRGB = null;
233
                double[] min = null, minRGB = null;
234
                double[] secondMax = null, secondMaxRGB = null;
235
                double[] secondMin = null, secondMinRGB = null;
236
                double[] mean = null;
237
                double[] variance = null;
238
                long[] nValues = null;
239

    
240
                KXmlParser parser = new KXmlParser();
241
                Reader reader = new StringReader(xml);
242
                try {
243
                        parser.setInput(reader);
244
                } catch (XmlPullParserException e) {
245
                        throw new ParsingException(xml);
246
                }
247
                try {
248
                        int tag = parser.nextTag();
249

    
250
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
251
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
252
                                while(tag != KXmlParser.END_DOCUMENT) {
253
                                        switch(tag) {
254
                                                case KXmlParser.START_TAG:
255
                                                        if(parser.getName() != null) {
256
                                                                if (parser.getName().compareTo(MAIN_TAG) == 0) {
257
                                                                        bandCount = Integer.parseInt(parserString(parser, BANDCOUNT, null));
258
                                                                        if(max == null) {
259
                                                                                max = new double[bandCount];
260
                                                                                min = new double[bandCount];
261
                                                                                secondMax = new double[bandCount];
262
                                                                                secondMin = new double[bandCount];
263
                                                                                maxRGB = new double[bandCount];
264
                                                                                minRGB = new double[bandCount];
265
                                                                                secondMaxRGB = new double[bandCount];
266
                                                                                secondMinRGB = new double[bandCount];
267
                                                                                mean = new double[bandCount];
268
                                                                                variance = new double[bandCount];
269
                                                                                nValues = new long[bandCount];
270
                                                                        }
271
                                                                        for (int i = 0; i < bandCount; i++)
272
                                                                                parserStatBandValues(parser, i, max, min, secondMax, secondMin, maxRGB, minRGB, secondMaxRGB, secondMinRGB, mean, variance, nValues);
273
                                                                }
274
                                                        }
275
                                                        break;
276
                                                case KXmlParser.END_TAG:
277
                                                        break;
278
                                                case KXmlParser.TEXT:
279
                                                        break;
280
                                        }
281
                                        tag = parser.next();
282
                                }
283
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
284
                        }
285

    
286
                } catch (XmlPullParserException e) {
287
                        throw new ParsingException(xml);
288
                } catch (IOException e) {
289
                        throw new ParsingException(xml);
290
                }
291
                getDatasetStatistics().setBandCount(bandCount);
292
                getDatasetStatistics().setMax(max);
293
                getDatasetStatistics().setMin(min);
294
                getDatasetStatistics().setSecondMax(secondMax);
295
                getDatasetStatistics().setSecondMin(secondMin);
296
                getDatasetStatistics().setMaxRGB(maxRGB);
297
                getDatasetStatistics().setMinRGB(minRGB);
298
                getDatasetStatistics().setSecondMaxRGB(secondMaxRGB);
299
                getDatasetStatistics().setSecondMinRGB(secondMinRGB);
300
                getDatasetStatistics().setMean(mean);
301
                getDatasetStatistics().setVariance(variance);
302
                getDatasetStatistics().setNumberOfValues(nValues);
303
                getDatasetStatistics().setCalculated(true);
304
        }
305

    
306
        public String write() {
307
                if (stat == null)
308
                        return "";
309
                
310
                StringBuffer b = new StringBuffer();
311
                                
312
                b.append("<" + MAIN_TAG + ">\n");
313
                putProperty(b, BANDCOUNT, getDatasetStatistics().getBandCount(), 1);
314
                for (int i = 0; i < getDatasetStatistics().getBandCount(); i++) {
315
                        b.append("\t<" + BAND + ">\n");
316
                        if (getDatasetStatistics().getMax() != null)
317
                                putProperty(b, MAX, getDatasetStatistics().getMax()[i], 2);
318
                        if (getDatasetStatistics().getMin() != null)
319
                                putProperty(b, MIN, getDatasetStatistics().getMin()[i], 2);
320
                        if (getDatasetStatistics().getSecondMax() != null)
321
                                putProperty(b, SNDMAX, getDatasetStatistics().getSecondMax()[i], 2);
322
                        if (getDatasetStatistics().getSecondMin() != null)
323
                                putProperty(b, SNDMIN, getDatasetStatistics().getSecondMin()[i], 2);
324
                        if (getDatasetStatistics().getMaxByteUnsigned() != null)
325
                                putProperty(b, MAXRGB, getDatasetStatistics().getMaxByteUnsigned()[i], 2);
326
                        if (getDatasetStatistics().getMinByteUnsigned() != null)
327
                                putProperty(b, MINRGB, getDatasetStatistics().getMinByteUnsigned()[i], 2);
328
                        if (getDatasetStatistics().getSecondMaxByteUnsigned() != null)
329
                                putProperty(b, SNDMAXRGB, getDatasetStatistics().getSecondMaxByteUnsigned()[i], 2);
330
                        if (getDatasetStatistics().getSecondMinByteUnsigned() != null)
331
                                putProperty(b, SNDMINRGB, getDatasetStatistics().getSecondMinByteUnsigned()[i], 2);
332
                        if (getDatasetStatistics().getMean() != null)
333
                                putProperty(b, MEAN, getDatasetStatistics().getMean()[i], 2);
334
                        if (getDatasetStatistics().getVariance() != null)
335
                                putProperty(b, VARIANCE, getDatasetStatistics().getVariance()[i], 2);
336
                        if (getDatasetStatistics().getNumberOfValues() != null)
337
                                putProperty(b, NVALUES, new Long(getDatasetStatistics().getNumberOfValues()[i]).toString(), 2);
338
                        b.append("\t</" + BAND + ">\n");
339
                }
340
                /*for (int i = 0; i < getDatasetStatistics().getTailTrimCount(); i++) {
341
                        if (getDatasetStatistics().getTailTrimValue(i) != null) {
342
                                b.append("\t<" + TAILTRIM + ">\n");
343
                                putProperty(b, KEY, ((Double) getDatasetStatistics().getTailTrimValue(i)[0]).doubleValue(), 2);
344
                                putProperty(b, VALUE, ((Double) getDatasetStatistics().getTailTrimValue(i)[1]).doubleValue(), 2);
345
                                b.append("\t</" + TAILTRIM + ">\n");
346
                        }
347
                }*/
348
                b.append("</" + MAIN_TAG + ">\n");
349
                return b.toString();
350
        }
351

    
352
        public Object getResult() {
353
                return stat;
354
        }
355

    
356
        public String getMainTag() {
357
                return MAIN_TAG;
358
        }
359
}