Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / properties / HistogramRmfSerializer.java @ 11336

History | View | Annotate | Download (7.52 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.dataset.properties;
20

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

    
26
import org.gvsig.raster.dataset.io.rmf.ClassSerializer;
27
import org.gvsig.raster.dataset.io.rmf.ParsingException;
28
import org.gvsig.raster.util.Histogram;
29
import org.gvsig.raster.util.HistogramClass;
30
import org.kxml2.io.KXmlParser;
31
import org.xmlpull.v1.XmlPullParserException;
32

    
33
/**
34
 * <P>
35
 * Clase para convertir a XML un histograma y obtener el histograma desde XML.
36
 * Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y 
37
 * lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
38
 * leer datos.
39
 * </P>
40
 * <P>
41
 * La estructura XML de un histograma es la siguiente:
42
 * </P>
43
 * <P>
44
 *\<Histogram\> <BR>
45
 *&nbsp;\<ClassInterval\>1.0\</ClassInterval\><BR>
46
 *&nbsp;\<Min\>0.0\</Min\><BR>
47
 *&nbsp;\<Max\>255.0\</Max\><BR>
48
 *&nbsp;\<BandCount\>3\</BandCount\><BR>
49
 *&nbsp;\<Band\><BR>
50
 *&nbsp;&nbsp;\<Values\>13 2 0 ... 2 1 6\</Values\><BR>
51
 *&nbsp;\</Band\><BR>
52
 *&nbsp;\<Band\><BR>
53
 *&nbsp;&nbsp;\<Values\>6 2 2 ... 3 2 8\</Values\><BR>
54
 *&nbsp;\</Band\><BR>
55
 *&nbsp;\<Band\><BR>
56
 *&nbsp;&nbsp;\<Values\>16 1 1 ... 1 1 9\</Values\><BR>
57
 *&nbsp;\</Band\><BR\>
58
 *\</Histogram\><BR>
59
 *\</P\>
60
 * @version 23/04/2007
61
 * @author Nacho Brodin (nachobrodin@gmail.com)
62
 *
63
 */
64
public class HistogramRmfSerializer extends ClassSerializer {
65
        
66
        //TAGS
67
        public static final String MAIN_TAG = "Histogram";
68
        public static final String CLASSINTERVAL = "ClassInterval";
69
        public static final String MIN = "Min";
70
        public static final String MAX = "Max";
71
        public static final String BAND = "Band";
72
        public static final String VALUES = "Values";
73
        public static final String BAND_COUNT = "BandCount";
74
                
75
        private Histogram  histogram = null;
76

    
77
        /**
78
         * Constructor. Asigna el histograma a serializar.
79
         * @param Histogram histograma a convertir en XML
80
         */
81
        public HistogramRmfSerializer(Histogram hist) {
82
                this.histogram = hist;
83
        }
84
        
85
        /**
86
         * Constructor. 
87
         */
88
        public HistogramRmfSerializer() {
89
        }
90
        
91
        /**
92
         * Convierte una lista de valores en un solo String en un array de long
93
         * @param list Cadena con la lista de valores
94
         * @return Array de valores long
95
         * @throws XmlPullParserException
96
         * @throws IOException
97
         */
98
        private long[] parseLongList(String list) throws XmlPullParserException, IOException {
99
                String[] sValues = list.split(" ");
100
                long[] dValues = new long[sValues.length];
101
                for (int i = 0; i < sValues.length; i++) 
102
                        dValues[i] = Long.parseLong(sValues[i]);
103
                return dValues;
104
        }
105
        
106
        /**
107
         * Parsea el tag Band para extraer la lista de valores (Values) asociada a una banda. 
108
         * @param parser KXmlParser
109
         * @return Array de long
110
         * @throws XmlPullParserException
111
         * @throws IOException
112
         */
113
        private long[] parserHistogramListValues(KXmlParser parser)  throws XmlPullParserException, IOException {
114
                long[] valueList = null;
115
                boolean end = false;
116
                boolean tagOk = false;
117
            int tag = parser.next();
118
            while (!end) {
119
                    switch(tag) {
120
                        case KXmlParser.START_TAG:
121
                                if(parser.getName() != null) {        
122
                                                if (parser.getName().compareTo(VALUES) == 0)
123
                                                        tagOk = true;
124
                                        }                                
125
                                        break;
126
                         case KXmlParser.END_TAG:
127
                                 if (parser.getName().compareTo(BAND) == 0)
128
                                         end = true;
129
                                break;
130
                        case KXmlParser.TEXT:
131
                                if(tagOk) {
132
                                        valueList = parseLongList(parser.getText());
133
                                        tagOk = false;
134
                                }
135
                                break;
136
                    }
137
                    if (!end)
138
                            tag = parser.next();
139
            }
140
            return valueList;
141
        }
142
        
143
        /*
144
         * (non-Javadoc)
145
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
146
         */
147
        public void read(String xml) throws ParsingException {        
148
                double interval = 0D;
149
                double min = 0D;
150
                double max = 0D;
151
                int numBands = 0;
152
                long[][] values = null;
153
                
154
                KXmlParser parser = new KXmlParser();
155
                Reader reader = new StringReader(xml);
156
                try {
157
                        parser.setInput(reader);
158
                } catch (XmlPullParserException e) {
159
                        throw new ParsingException(xml);
160
                }
161
                try {
162
                        int tag = parser.nextTag();
163
                        
164
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){                    
165
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);                            
166
                                while(tag != KXmlParser.END_DOCUMENT) {
167
                                        switch(tag) {
168
                                                case KXmlParser.START_TAG:
169
                                                        if(parser.getName() != null) {         
170
                                                                if (parser.getName().compareTo(MAIN_TAG) == 0) {
171
                                                                        interval = Double.parseDouble(parserString(parser, CLASSINTERVAL));
172
                                                                        min = Double.parseDouble(parserString(parser, MIN));
173
                                                                        max = Double.parseDouble(parserString(parser, MAX));
174
                                                                        numBands = (int)Double.parseDouble(parserString(parser, BAND_COUNT));
175
                                                                        values = new long[numBands][];
176
                                                                        for (int iBand = 0; iBand < numBands; iBand++) 
177
                                                                                values[iBand] = parserHistogramListValues(parser);
178
                                                                }
179
                                                        }        
180
                                                        break;
181
                                                case KXmlParser.END_TAG:                                                                
182
                                                        break;
183
                                                case KXmlParser.TEXT:                                                        
184
                                                        break;
185
                                        }
186
                                        tag = parser.next();
187
                                }
188
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
189
                        }
190
                        
191
                } catch (XmlPullParserException e) {
192
                        throw new ParsingException(xml);
193
                } catch (IOException e) {
194
                        throw new ParsingException(xml);
195
                }
196
                
197
                histogram = new Histogram(numBands, values[0].length, min, max);
198
                histogram.setTable(values);
199
        }
200

    
201
        /*
202
         * (non-Javadoc)
203
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
204
         */
205
        public String write() {
206
                StringBuffer b = new StringBuffer();
207
                HistogramClass[][] c = histogram.getHistogram();
208
                
209
                b.append("<" + MAIN_TAG + ">\n");
210
                
211
                b.append("\t<" + CLASSINTERVAL  + ">");
212
                double interval = c[0][0].getMax() - c[0][0].getMin();
213
                b.append(interval);
214
                b.append("</" + CLASSINTERVAL + ">\n");
215
                
216
                b.append("\t<" + MIN + ">");
217
                b.append(c[0][0].getMin());
218
                b.append("</" + MIN + ">\n");
219
                
220
                b.append("\t<" + MAX + ">");
221
                b.append(c[0][c[0].length - 1].getMax());
222
                b.append("</" + MAX + ">\n");
223
                
224
                b.append("\t<" + BAND_COUNT + ">");
225
                b.append(histogram.getNumBands());
226
                b.append("</" + BAND_COUNT + ">\n");
227
                
228
                for (int iBand = 0; iBand < histogram.getNumBands(); iBand++) {
229
                        b.append("\t<" + BAND + ">\n");
230
                        b.append("\t\t<" + VALUES + ">");
231
                        for (int iValues = 0; iValues < histogram.getNumValues(); iValues++)
232
                                b.append((long)histogram.getHistogramValueByPos(iBand, iValues) + " ");
233
                        b.append("</" + VALUES + ">\n");
234
                        b.append("\t</" + BAND + ">\n");
235
                }
236
                b.append("</" + MAIN_TAG + ">");
237
                return b.toString();
238
        }
239
        
240
        /*
241
         * (non-Javadoc)
242
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
243
         */
244
        public Object getResult() {
245
                return (Histogram)histogram;
246
        }
247

    
248
        /*
249
         *  (non-Javadoc)
250
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
251
         */
252
        public String getMainTag() {
253
                return MAIN_TAG;
254
        }
255
}