Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / serializer / HistogramRmfSerializer.java @ 20866

History | View | Annotate | Download (7.42 KB)

1 12383 nacho
/* 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.datastruct.serializer;
20
21
import java.io.IOException;
22
import java.io.Reader;
23
import java.io.StringReader;
24
25
import org.gvsig.raster.dataset.io.rmf.ClassSerializer;
26
import org.gvsig.raster.dataset.io.rmf.ParsingException;
27
import org.gvsig.raster.datastruct.Histogram;
28
import org.gvsig.raster.datastruct.HistogramClass;
29
import org.kxml2.io.KXmlParser;
30
import org.xmlpull.v1.XmlPullParserException;
31
32
/**
33
 * <P>
34
 * Clase para convertir a XML un histograma y obtener el histograma 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 un histograma es la siguiente:
41
 * </P>
42
 * <P>
43
 *\<Histogram\> <BR>
44
 *&nbsp;\<ClassInterval\>1.0\</ClassInterval\><BR>
45
 *&nbsp;\<Min\>0.0\</Min\><BR>
46
 *&nbsp;\<Max\>255.0\</Max\><BR>
47
 *&nbsp;\<BandCount\>3\</BandCount\><BR>
48
 *&nbsp;\<Band\><BR>
49
 *&nbsp;&nbsp;\<Values\>13 2 0 ... 2 1 6\</Values\><BR>
50
 *&nbsp;\</Band\><BR>
51
 *&nbsp;\<Band\><BR>
52
 *&nbsp;&nbsp;\<Values\>6 2 2 ... 3 2 8\</Values\><BR>
53
 *&nbsp;\</Band\><BR>
54
 *&nbsp;\<Band\><BR>
55
 *&nbsp;&nbsp;\<Values\>16 1 1 ... 1 1 9\</Values\><BR>
56
 *&nbsp;\</Band\><BR\>
57
 *\</Histogram\><BR>
58
 *\</P\>
59
 * @version 23/04/2007
60
 * @author Nacho Brodin (nachobrodin@gmail.com)
61
 *
62
 */
63
public class HistogramRmfSerializer extends ClassSerializer {
64
65
        //TAGS
66
        public static final String MAIN_TAG = "Histogram";
67
        public static final String CLASSINTERVAL = "ClassInterval";
68
        public static final String MIN = "Min";
69
        public static final String MAX = "Max";
70
        public static final String BAND = "Band";
71
        public static final String VALUES = "Values";
72
        public static final String BAND_COUNT = "BandCount";
73
74
        private Histogram  histogram = null;
75
76
        /**
77
         * Constructor. Asigna el histograma a serializar.
78
         * @param Histogram histograma a convertir en XML
79
         */
80
        public HistogramRmfSerializer(Histogram hist) {
81
                this.histogram = hist;
82
        }
83
84
        /**
85
         * Constructor.
86
         */
87
        public HistogramRmfSerializer() {
88
        }
89
90
        /**
91
         * Convierte una lista de valores en un solo String en un array de long
92
         * @param list Cadena con la lista de valores
93
         * @return Array de valores long
94
         * @throws XmlPullParserException
95
         * @throws IOException
96
         */
97
        private long[] parseLongList(String list) throws XmlPullParserException, IOException {
98
                String[] sValues = list.split(" ");
99
                long[] dValues = new long[sValues.length];
100
                for (int i = 0; i < sValues.length; i++)
101
                        dValues[i] = Long.parseLong(sValues[i]);
102
                return dValues;
103
        }
104
105
        /**
106
         * Parsea el tag Band para extraer la lista de valores (Values) asociada a una banda.
107
         * @param parser KXmlParser
108
         * @return Array de long
109
         * @throws XmlPullParserException
110
         * @throws IOException
111
         */
112
        private long[] parserHistogramListValues(KXmlParser parser)  throws XmlPullParserException, IOException {
113
                long[] valueList = null;
114
                boolean end = false;
115
                boolean tagOk = false;
116
            int tag = parser.next();
117
            while (!end) {
118
                    switch(tag) {
119
                        case KXmlParser.START_TAG:
120
                                if(parser.getName() != null) {
121
                                                if (parser.getName().compareTo(VALUES) == 0)
122
                                                        tagOk = 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(tagOk) {
131
                                        valueList = parseLongList(parser.getText());
132
                                        tagOk = false;
133
                                }
134
                                break;
135
                    }
136
                    if (!end)
137
                            tag = parser.next();
138
            }
139
            return valueList;
140
        }
141
142
        /*
143
         * (non-Javadoc)
144
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
145
         */
146
        public void read(String xml) throws ParsingException {
147 13353 nacho
                //double interval = 0D;
148 12383 nacho
                double min = 0D;
149
                double max = 0D;
150
                int numBands = 0;
151
                long[][] values = null;
152
153
                KXmlParser parser = new KXmlParser();
154
                Reader reader = new StringReader(xml);
155
                try {
156
                        parser.setInput(reader);
157
                } catch (XmlPullParserException e) {
158
                        throw new ParsingException(xml);
159
                }
160
                try {
161
                        int tag = parser.nextTag();
162
163
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
164
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
165
                                while(tag != KXmlParser.END_DOCUMENT) {
166
                                        switch(tag) {
167
                                                case KXmlParser.START_TAG:
168
                                                        if(parser.getName() != null) {
169
                                                                if (parser.getName().compareTo(MAIN_TAG) == 0) {
170
                                                                        String interStr = parserString(parser, CLASSINTERVAL, new String[]{"FullHistogram"});
171
                                                                        if(interStr == null)
172
                                                                                return;
173 13353 nacho
                                                                        //interval = Double.parseDouble(interStr);
174 12383 nacho
                                                                        min = Double.parseDouble(parserString(parser, MIN, null));
175
                                                                        max = Double.parseDouble(parserString(parser, MAX, null));
176
                                                                        numBands = (int)Double.parseDouble(parserString(parser, BAND_COUNT, null));
177
                                                                        values = new long[numBands][];
178
                                                                        for (int iBand = 0; iBand < numBands; iBand++)
179
                                                                                values[iBand] = parserHistogramListValues(parser);
180
                                                                }
181
                                                        }
182
                                                        break;
183
                                                case KXmlParser.END_TAG:
184
                                                        break;
185
                                                case KXmlParser.TEXT:
186
                                                        break;
187
                                        }
188
                                        tag = parser.next();
189
                                }
190
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
191
                        }
192
193
                } catch (XmlPullParserException e) {
194
                        throw new ParsingException(xml);
195
                } catch (IOException e) {
196
                        throw new ParsingException(xml);
197
                }
198
199
                histogram = new Histogram(numBands, values[0].length, min, max);
200
                histogram.setTable(values);
201
        }
202
203
        /*
204
         * (non-Javadoc)
205
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
206
         */
207
        public String write() {
208
                StringBuffer b = new StringBuffer();
209
                HistogramClass[][] c = histogram.getHistogram();
210
211
                b.append("<" + MAIN_TAG + ">\n");
212
213
                double interval = c[0][0].getMax() - c[0][0].getMin();
214
                putProperty(b, CLASSINTERVAL, interval, 1);
215
                putProperty(b, MIN, c[0][0].getMin(), 1);
216
                putProperty(b, MAX, c[0][c[0].length - 1].getMax(), 1);
217
                putProperty(b, BAND_COUNT, histogram.getNumBands(), 1);
218
219
                for (int iBand = 0; iBand < histogram.getNumBands(); iBand++) {
220
                        b.append("\t<" + BAND + ">\n");
221
                        b.append("\t\t<" + VALUES + ">");
222
                        for (int iValues = 0; iValues < histogram.getNumValues(); iValues++)
223
                                b.append((long)histogram.getHistogramValueByPos(iBand, iValues) + " ");
224
                        b.append("</" + VALUES + ">\n");
225
                        b.append("\t</" + BAND + ">\n");
226
                }
227 12511 nacho
                b.append("</" + MAIN_TAG + ">\n");
228 12383 nacho
                return b.toString();
229
        }
230
231
        /*
232
         * (non-Javadoc)
233
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
234
         */
235
        public Object getResult() {
236
                return (Histogram)histogram;
237
        }
238
239
        /*
240
         *  (non-Javadoc)
241
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
242
         */
243
        public String getMainTag() {
244
                return MAIN_TAG;
245
        }
246
}