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 @ 971

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

    
87
        private SimpleProviderStatistics  stat      = null;
88

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

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

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

    
122
                return stat;
123
        }
124

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

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

    
229
        /*
230
         * (non-Javadoc)
231
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
232
         */
233
        public void read(String xml) throws ParsingException {
234
                int bandCount = 0;
235
                double[] max = null, maxRGB = null;
236
                double[] min = null, minRGB = null;
237
                double[] secondMax = null, secondMaxRGB = null;
238
                double[] secondMin = null, secondMinRGB = null;
239
                double[] mean = null;
240
                double[] variance = null;
241
                long[] nValues = null;
242

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

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

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

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

    
359
        /*
360
         * (non-Javadoc)
361
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
362
         */
363
        public Object getResult() {
364
                return stat;
365
        }
366

    
367
        /*
368
         *  (non-Javadoc)
369
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
370
         */
371
        public String getMainTag() {
372
                return MAIN_TAG;
373
        }
374
}