svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / serializer / StatisticsRmfSerializer.java @ 21575
History | View | Annotate | Download (10.2 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.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.dataset.properties.DatasetStatistics; |
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 |
* \<Max\>0\</Max\><BR>
|
44 |
* \<SecondMax\>0\</SecondMax\><BR>
|
45 |
* \<Min\>0\</Min\><BR>
|
46 |
* \<SecondMin\>0\</SecondMin\><BR>
|
47 |
* \<Maximun\>0\</Maximun\><BR>
|
48 |
* \<Minimun\>0\</Minimun\><BR>
|
49 |
* \<Mean\>0\</Mean\><BR>
|
50 |
* \<Variance\>0\</Variance\><BR>
|
51 |
* \<BandCount\>0\</BandCount\><BR>
|
52 |
* \<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 BAND = "Band"; |
65 |
public static final String MIN = "Min"; |
66 |
public static final String MAX = "Max"; |
67 |
public static final String SNDMIN = "SecondMin"; |
68 |
public static final String SNDMAX = "SecondMax"; |
69 |
public static final String MINRGB = "MinRGB"; |
70 |
public static final String MAXRGB = "MaxRGB"; |
71 |
public static final String SNDMINRGB = "SecondMinRGB"; |
72 |
public static final String SNDMAXRGB = "SecondMaxRGB"; |
73 |
public static final String MAXIMUN = "Maximun"; |
74 |
public static final String MINIMUN = "Minimun"; |
75 |
public static final String MEAN = "Mean"; |
76 |
public static final String VARIANCE = "Variance"; |
77 |
public static final String BANDCOUNT = "BandCount"; |
78 |
public static final String TAILTRIM = "TailTrim"; |
79 |
public static final String KEY = "Key"; |
80 |
public static final String VALUE = "Value"; |
81 |
|
82 |
private DatasetStatistics stat = null; |
83 |
|
84 |
/**
|
85 |
* Constructor. Asigna la tabla a serializar.
|
86 |
* @param ColorTable tabla a convertir en XML
|
87 |
*/
|
88 |
public StatisticsRmfSerializer(DatasetStatistics stat) {
|
89 |
this.stat = stat;
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* Constructor.
|
94 |
*/
|
95 |
public StatisticsRmfSerializer() {
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* Parsea el tag Band para extraer la lista de valores (Values) asociada a una banda.
|
100 |
* @param parser KXmlParser
|
101 |
* @return Array de long
|
102 |
* @throws XmlPullParserException
|
103 |
* @throws IOException
|
104 |
*/
|
105 |
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) throws XmlPullParserException, IOException { |
106 |
boolean maxOk = false, minOk = false, sndmaxOk = false, sndminOk = false, meanOk = false, varianceOk = false; |
107 |
boolean maxRGBOk = false, minRGBOk = false, sndmaxRGBOk = false, sndminRGBOk = false; |
108 |
long[] valueList = null; |
109 |
boolean end = false; |
110 |
int tag = parser.next();
|
111 |
while (!end) {
|
112 |
switch(tag) {
|
113 |
case KXmlParser.START_TAG:
|
114 |
if(parser.getName() != null) { |
115 |
if (parser.getName().compareTo(MAX) == 0) |
116 |
maxOk = true;
|
117 |
if (parser.getName().compareTo(MIN) == 0) |
118 |
minOk = true;
|
119 |
if (parser.getName().compareTo(SNDMAX) == 0) |
120 |
sndmaxOk = true;
|
121 |
if (parser.getName().compareTo(SNDMIN) == 0) |
122 |
sndminOk = true;
|
123 |
if (parser.getName().compareTo(MAXRGB) == 0) |
124 |
maxRGBOk = true;
|
125 |
if (parser.getName().compareTo(MINRGB) == 0) |
126 |
minRGBOk = true;
|
127 |
if (parser.getName().compareTo(SNDMAXRGB) == 0) |
128 |
sndmaxRGBOk = true;
|
129 |
if (parser.getName().compareTo(SNDMINRGB) == 0) |
130 |
sndminRGBOk = true;
|
131 |
if (parser.getName().compareTo(MEAN) == 0) |
132 |
meanOk = true;
|
133 |
if (parser.getName().compareTo(VARIANCE) == 0) |
134 |
varianceOk = true;
|
135 |
} |
136 |
break;
|
137 |
case KXmlParser.END_TAG:
|
138 |
if (parser.getName().compareTo(BAND) == 0) |
139 |
end = true;
|
140 |
break;
|
141 |
case KXmlParser.TEXT:
|
142 |
if(maxOk) {
|
143 |
max[band] = Double.parseDouble(parser.getText());
|
144 |
maxOk = false;
|
145 |
} |
146 |
if(minOk) {
|
147 |
min[band] = Double.parseDouble(parser.getText());
|
148 |
minOk = false;
|
149 |
} |
150 |
if(sndmaxOk) {
|
151 |
sndmax[band] = Double.parseDouble(parser.getText());
|
152 |
sndmaxOk = false;
|
153 |
} |
154 |
if(sndminOk) {
|
155 |
sndmin[band] = Double.parseDouble(parser.getText());
|
156 |
sndminOk = false;
|
157 |
} |
158 |
if(maxRGBOk) {
|
159 |
maxRGB[band] = Double.parseDouble(parser.getText());
|
160 |
maxRGBOk = false;
|
161 |
} |
162 |
if(minRGBOk) {
|
163 |
minRGB[band] = Double.parseDouble(parser.getText());
|
164 |
minRGBOk = false;
|
165 |
} |
166 |
if(sndmaxRGBOk) {
|
167 |
sndmaxRGB[band] = Double.parseDouble(parser.getText());
|
168 |
sndmaxRGBOk = false;
|
169 |
} |
170 |
if(sndminRGBOk) {
|
171 |
sndminRGB[band] = Double.parseDouble(parser.getText());
|
172 |
sndminRGBOk = false;
|
173 |
} |
174 |
if(meanOk) {
|
175 |
mean[band] = Double.parseDouble(parser.getText());
|
176 |
meanOk = false;
|
177 |
} |
178 |
if(varianceOk) {
|
179 |
variance[band] = Double.parseDouble(parser.getText());
|
180 |
varianceOk = false;
|
181 |
} |
182 |
break;
|
183 |
} |
184 |
if (!end)
|
185 |
tag = parser.next(); |
186 |
} |
187 |
return valueList;
|
188 |
} |
189 |
|
190 |
/*
|
191 |
* (non-Javadoc)
|
192 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
|
193 |
*/
|
194 |
public void read(String xml) throws ParsingException { |
195 |
int bandCount = 0; |
196 |
double[] max = null, maxRGB = null; |
197 |
double[] min = null, minRGB = null; |
198 |
double[] secondMax = null, secondMaxRGB = null; |
199 |
double[] secondMin = null, secondMinRGB = null; |
200 |
double[] mean = null; |
201 |
double[] variance = null; |
202 |
|
203 |
KXmlParser parser = new KXmlParser();
|
204 |
Reader reader = new StringReader(xml); |
205 |
try {
|
206 |
parser.setInput(reader); |
207 |
} catch (XmlPullParserException e) {
|
208 |
throw new ParsingException(xml); |
209 |
} |
210 |
try {
|
211 |
int tag = parser.nextTag();
|
212 |
|
213 |
if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
|
214 |
parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
215 |
while(tag != KXmlParser.END_DOCUMENT) {
|
216 |
switch(tag) {
|
217 |
case KXmlParser.START_TAG:
|
218 |
if(parser.getName() != null) { |
219 |
if (parser.getName().compareTo(MAIN_TAG) == 0) { |
220 |
bandCount = Integer.parseInt(parserString(parser, BANDCOUNT, null)); |
221 |
if(max == null) { |
222 |
max = new double[bandCount]; |
223 |
min = new double[bandCount]; |
224 |
secondMax = new double[bandCount]; |
225 |
secondMin = new double[bandCount]; |
226 |
maxRGB = new double[bandCount]; |
227 |
minRGB = new double[bandCount]; |
228 |
secondMaxRGB = new double[bandCount]; |
229 |
secondMinRGB = new double[bandCount]; |
230 |
mean = new double[bandCount]; |
231 |
variance = new double[bandCount]; |
232 |
} |
233 |
for (int i = 0; i < bandCount; i++) |
234 |
parserStatBandValues(parser, i, max, min, secondMax, secondMin, maxRGB, minRGB, secondMaxRGB, secondMinRGB, mean, variance); |
235 |
} |
236 |
} |
237 |
break;
|
238 |
case KXmlParser.END_TAG:
|
239 |
break;
|
240 |
case KXmlParser.TEXT:
|
241 |
break;
|
242 |
} |
243 |
tag = parser.next(); |
244 |
} |
245 |
parser.require(KXmlParser.END_DOCUMENT, null, null); |
246 |
} |
247 |
|
248 |
} catch (XmlPullParserException e) {
|
249 |
throw new ParsingException(xml); |
250 |
} catch (IOException e) { |
251 |
throw new ParsingException(xml); |
252 |
} |
253 |
if(stat == null) |
254 |
stat = new DatasetStatistics(null); |
255 |
stat.setBandCount(bandCount); |
256 |
stat.setMax(max); |
257 |
stat.setMin(min); |
258 |
stat.setSecondMax(secondMax); |
259 |
stat.setSecondMin(secondMin); |
260 |
stat.setMaxRGB(maxRGB); |
261 |
stat.setMinRGB(minRGB); |
262 |
stat.setSecondMaxRGB(secondMaxRGB); |
263 |
stat.setSecondMinRGB(secondMinRGB); |
264 |
stat.setMean(mean); |
265 |
stat.setVariance(variance); |
266 |
stat.setCalculated(true);
|
267 |
} |
268 |
|
269 |
/*
|
270 |
* (non-Javadoc)
|
271 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
|
272 |
*/
|
273 |
public String write() { |
274 |
StringBuffer b = new StringBuffer(); |
275 |
|
276 |
b.append("<" + MAIN_TAG + ">\n"); |
277 |
putProperty(b, BANDCOUNT, stat.getBandCount(), 1);
|
278 |
for (int i = 0; i < stat.getBandCount(); i++) { |
279 |
b.append("\t<" + BAND + ">\n"); |
280 |
putProperty(b, MAX, stat.getMax()[i], 2);
|
281 |
putProperty(b, MIN, stat.getMin()[i], 2);
|
282 |
putProperty(b, SNDMAX, stat.getSecondMax()[i], 2);
|
283 |
putProperty(b, SNDMIN, stat.getSecondMin()[i], 2);
|
284 |
putProperty(b, MAXRGB, stat.getMaxRGB()[i], 2);
|
285 |
putProperty(b, MINRGB, stat.getMinRGB()[i], 2);
|
286 |
putProperty(b, SNDMAXRGB, stat.getSecondMaxRGB()[i], 2);
|
287 |
putProperty(b, SNDMINRGB, stat.getSecondMinRGB()[i], 2);
|
288 |
putProperty(b, MEAN, stat.getMean()[i], 2);
|
289 |
putProperty(b, VARIANCE, stat.getVariance()[i], 2);
|
290 |
b.append("\t</" + BAND + ">\n"); |
291 |
} |
292 |
for (int i = 0; i < stat.getTailTrimCount(); i++) { |
293 |
b.append("\t<" + TAILTRIM + ">\n"); |
294 |
putProperty(b, KEY, ((Double) stat.getTailTrimValue(i)[0]).doubleValue(), 2); |
295 |
putProperty(b, VALUE, ((Double) stat.getTailTrimValue(i)[1]).doubleValue(), 2); |
296 |
b.append("\t</" + TAILTRIM + ">\n"); |
297 |
} |
298 |
b.append("</" + MAIN_TAG + ">\n"); |
299 |
return b.toString();
|
300 |
} |
301 |
|
302 |
/*
|
303 |
* (non-Javadoc)
|
304 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
|
305 |
*/
|
306 |
public Object getResult() { |
307 |
return stat;
|
308 |
} |
309 |
|
310 |
/*
|
311 |
* (non-Javadoc)
|
312 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
|
313 |
*/
|
314 |
public String getMainTag() { |
315 |
return MAIN_TAG;
|
316 |
} |
317 |
} |