Statistics
| Revision:

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

History | View | Annotate | Download (7.23 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.kxml2.io.KXmlParser;
29
import org.xmlpull.v1.XmlPullParserException;
30

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

    
78
        /**
79
         * Constructor. Asigna la tabla a serializar.
80
         * @param ColorTable tabla a convertir en XML
81
         */
82
        public StatisticsRmfSerializer(IStatistics stat) {
83
                this.stat = stat;
84
        }
85
        
86
        /**
87
         * Constructor. 
88
         */
89
        public StatisticsRmfSerializer() {
90
        }
91
                        
92
        /*
93
         * (non-Javadoc)
94
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
95
         */
96
        public void read(String xml) throws ParsingException {
97
                /*int type = 0;
98
                String paletteName = "";
99
                int[] rgb = null;
100
                double[] range = null;
101
                String[] names = null;
102
                
103
                KXmlParser parser = new KXmlParser();
104
                Reader reader = new StringReader(xml);
105
                try {
106
                        parser.setInput(reader);
107
                } catch (XmlPullParserException e) {
108
                        throw new ParsingException(xml);
109
                }
110
                try {
111
                        int tag = parser.nextTag();
112
                        
113
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){                    
114
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);                            
115
                                while(tag != KXmlParser.END_DOCUMENT) {
116
                                        switch(tag) {
117
                                                case KXmlParser.START_TAG:
118
                                                        if(parser.getName() != null) {         
119
                                                                if (parser.getName().compareTo(MAIN_TAG) == 0) {
120
                                                                        type = Integer.parseInt(parserString(parser, TYPE));
121
                                                                        paletteName = parserString(parser, NAME);
122
                                                                        String rgbList = parserString(parser, RGB);
123
                                                                        if(rgbList != null && rgb == null) {
124
                                                                                rgb = convertStringInIntArray(rgbList);
125
                                                                                rgbList = null;
126
                                                                        }
127
                                                                        String nameList = parserString(parser, NAMES);
128
                                                                        if(nameList != null && names == null) {
129
                                                                                names = nameList.split(" ");
130
                                                                                nameList = null;
131
                                                                        }
132
                                                                        String rangeList = parserString(parser, RANGE);
133
                                                                        if(rangeList != null && range == null) {
134
                                                                                range = convertStringInDoubleArray(rangeList);
135
                                                                                rangeList = null;
136
                                                                        }
137
                                                                }
138
                                                        }        
139
                                                        break;
140
                                                case KXmlParser.END_TAG:                                                                
141
                                                        break;
142
                                                case KXmlParser.TEXT:                                                        
143
                                                        break;
144
                                        }
145
                                        tag = parser.next();
146
                                }
147
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
148
                        }
149
                        
150
                } catch (XmlPullParserException e) {
151
                        throw new ParsingException(xml);
152
                } catch (IOException e) {
153
                        throw new ParsingException(xml);
154
                }
155
                
156
                */
157
        }
158

    
159
        /*
160
         * (non-Javadoc)
161
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
162
         */
163
        public String write() {
164
                StringBuffer b = new StringBuffer();
165
                                
166
                b.append("<" + MAIN_TAG + ">\n");
167
                
168
                putProperty(b, MAX, stat.getMax()[0]);
169
                
170
                b.append("\t<" + MIN  + ">");
171
                b.append(stat.getMin());
172
                b.append("</" + MIN + ">\n");
173
                
174
                b.append("\t<" + SNDMAX  + ">");
175
                b.append(stat.getSecondMax());
176
                b.append("</" + SNDMAX + ">\n");
177
                
178
                b.append("\t<" + SNDMIN  + ">");
179
                b.append(stat.getSecondMin());
180
                b.append("</" + SNDMIN + ">\n");
181
                
182
                b.append("\t<" + MAXIMUN  + ">");
183
                b.append(stat.getMaximun());
184
                b.append("</" + MAXIMUN + ">\n");
185
                
186
                b.append("\t<" + MINIMUN  + ">");
187
                b.append(stat.getMinimun());
188
                b.append("</" + MINIMUN + ">\n");
189
                
190
                b.append("\t<" + MEAN  + ">");
191
                b.append(stat.getMaximun());
192
                b.append("</" + MAXIMUN + ">\n");
193
                
194
                /*b.append("\t<" + RGB + ">");
195
                if(colorTable.getType() == IBuffer.TYPE_BYTE || colorTable.getType() == IBuffer.TYPE_SHORT || colorTable.getType() == IBuffer.TYPE_INT) {
196
                        for (int rgb = 0; rgb < colorTable.getColorTable().length; rgb++)
197
                                b.append(colorTable.getColorTable()[rgb] + " ");
198
                }
199
                if(colorTable.getType() == IBuffer.TYPE_FLOAT || colorTable.getType() == IBuffer.TYPE_DOUBLE) {
200
                        for (int rgb = 0; rgb < colorTable.getColorTable().length; rgb++)
201
                                b.append(colorTable.getColorTable()[rgb] + " ");
202
                }
203
                b.append("</" + RGB + ">\n");
204
                
205
                b.append("\t<" + NAMES + ">");
206
                for (int rgb = 0; rgb < colorTable.getNameClass().length; rgb++)
207
                        b.append(colorTable.getNameClass()[rgb] + " ");
208
                b.append("</" + NAMES + ">\n");
209
                
210
                b.append("\t<" + RANGE + ">");
211
                if(colorTable.getType() == IBuffer.TYPE_BYTE || colorTable.getType() == IBuffer.TYPE_SHORT || colorTable.getType() == IBuffer.TYPE_INT) {
212
                        for (int rgb = 0; rgb < colorTable.getIntRange().length; rgb++)
213
                                b.append(colorTable.getIntRange()[rgb] + " ");
214
                }
215
                if(colorTable.getType() == IBuffer.TYPE_FLOAT || colorTable.getType() == IBuffer.TYPE_DOUBLE) {
216
                        for (int rgb = 0; rgb < colorTable.getDoubleRange().length; rgb++)
217
                                b.append(colorTable.getDoubleRange()[rgb] + " ");
218
                }
219
                b.append("</" + RANGE + ">\n");
220
                
221
                b.append("</" + MAIN_TAG + ">");*/
222
                return b.toString();
223
        }
224
        
225
        /*
226
         * (non-Javadoc)
227
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
228
         */
229
        public Object getResult() {
230
                return stat;
231
        }
232

    
233
        /*
234
         *  (non-Javadoc)
235
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
236
         */
237
        public String getMainTag() {
238
                return MAIN_TAG;
239
        }
240
}