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 @ 162
History | View | Annotate | Download (12.2 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.DataStoreStatistics; |
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 |
* \<Max\>0\</Max\><BR>
|
50 |
* \<SecondMax\>0\</SecondMax\><BR>
|
51 |
* \<Min\>0\</Min\><BR>
|
52 |
* \<SecondMin\>0\</SecondMin\><BR>
|
53 |
* \<Maximun\>0\</Maximun\><BR>
|
54 |
* \<Minimun\>0\</Minimun\><BR>
|
55 |
* \<Mean\>0\</Mean\><BR>
|
56 |
* \<Variance\>0\</Variance\><BR>
|
57 |
* \<BandCount\>0\</BandCount\><BR>
|
58 |
* \<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 BANDCOUNT = "BandCount"; |
82 |
public static final String TAILTRIM = "TailTrim"; |
83 |
public static final String KEY = "Key"; |
84 |
public static final String VALUE = "Value"; |
85 |
|
86 |
private DataStoreStatistics stat = null; |
87 |
|
88 |
/**
|
89 |
* Registra StatisticsRmfSerializer en los puntos de extension de Serializer
|
90 |
*/
|
91 |
public static void register() { |
92 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
93 |
ExtensionPoint point = extensionPoints.get("Serializer");
|
94 |
point.append("Statistics", "", StatisticsRmfSerializer.class); |
95 |
} |
96 |
|
97 |
/**
|
98 |
* Constructor. Asigna la tabla a serializar.
|
99 |
* @param ColorTable tabla a convertir en XML
|
100 |
*/
|
101 |
public StatisticsRmfSerializer(Statistics stat) {
|
102 |
if (stat != null) |
103 |
stat.setCalculated(false);
|
104 |
|
105 |
this.stat = (DataStoreStatistics)stat;
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* Devuelve el objeto estadisticas de un dataset, en caso de no existir, lo crea.
|
110 |
* @return
|
111 |
*/
|
112 |
private DataStoreStatistics getDatasetStatistics() {
|
113 |
if (stat == null) |
114 |
stat = new DataStoreStatistics(null); |
115 |
|
116 |
return stat;
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* Constructor.
|
121 |
*/
|
122 |
public StatisticsRmfSerializer() {
|
123 |
} |
124 |
|
125 |
/**
|
126 |
* Parsea el tag Band para extraer la lista de valores (Values) asociada a una banda.
|
127 |
* @param parser KXmlParser
|
128 |
* @return Array de long
|
129 |
* @throws XmlPullParserException
|
130 |
* @throws IOException
|
131 |
*/
|
132 |
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 { |
133 |
boolean maxOk = false, minOk = false, sndmaxOk = false, sndminOk = false, meanOk = false, varianceOk = false; |
134 |
boolean maxRGBOk = false, minRGBOk = false, sndmaxRGBOk = false, sndminRGBOk = false; |
135 |
long[] valueList = null; |
136 |
boolean end = false; |
137 |
int tag = parser.next();
|
138 |
while (!end) {
|
139 |
switch(tag) {
|
140 |
case KXmlParser.START_TAG:
|
141 |
if(parser.getName() != null) { |
142 |
if (parser.getName().compareTo(MAX) == 0) |
143 |
maxOk = true;
|
144 |
if (parser.getName().compareTo(MIN) == 0) |
145 |
minOk = true;
|
146 |
if (parser.getName().compareTo(SNDMAX) == 0) |
147 |
sndmaxOk = true;
|
148 |
if (parser.getName().compareTo(SNDMIN) == 0) |
149 |
sndminOk = true;
|
150 |
if (parser.getName().compareTo(MAXRGB) == 0) |
151 |
maxRGBOk = true;
|
152 |
if (parser.getName().compareTo(MINRGB) == 0) |
153 |
minRGBOk = true;
|
154 |
if (parser.getName().compareTo(SNDMAXRGB) == 0) |
155 |
sndmaxRGBOk = true;
|
156 |
if (parser.getName().compareTo(SNDMINRGB) == 0) |
157 |
sndminRGBOk = true;
|
158 |
if (parser.getName().compareTo(MEAN) == 0) |
159 |
meanOk = true;
|
160 |
if (parser.getName().compareTo(VARIANCE) == 0) |
161 |
varianceOk = true;
|
162 |
} |
163 |
break;
|
164 |
case KXmlParser.END_TAG:
|
165 |
if (parser.getName().compareTo(BAND) == 0) |
166 |
end = true;
|
167 |
break;
|
168 |
case KXmlParser.TEXT:
|
169 |
if(maxOk) {
|
170 |
max[band] = Double.parseDouble(parser.getText());
|
171 |
maxOk = false;
|
172 |
} |
173 |
if(minOk) {
|
174 |
min[band] = Double.parseDouble(parser.getText());
|
175 |
minOk = false;
|
176 |
} |
177 |
if(sndmaxOk) {
|
178 |
sndmax[band] = Double.parseDouble(parser.getText());
|
179 |
sndmaxOk = false;
|
180 |
} |
181 |
if(sndminOk) {
|
182 |
sndmin[band] = Double.parseDouble(parser.getText());
|
183 |
sndminOk = false;
|
184 |
} |
185 |
if(maxRGBOk) {
|
186 |
maxRGB[band] = Double.parseDouble(parser.getText());
|
187 |
maxRGBOk = false;
|
188 |
} |
189 |
if(minRGBOk) {
|
190 |
minRGB[band] = Double.parseDouble(parser.getText());
|
191 |
minRGBOk = false;
|
192 |
} |
193 |
if(sndmaxRGBOk) {
|
194 |
sndmaxRGB[band] = Double.parseDouble(parser.getText());
|
195 |
sndmaxRGBOk = false;
|
196 |
} |
197 |
if(sndminRGBOk) {
|
198 |
sndminRGB[band] = Double.parseDouble(parser.getText());
|
199 |
sndminRGBOk = false;
|
200 |
} |
201 |
if(meanOk) {
|
202 |
mean[band] = Double.parseDouble(parser.getText());
|
203 |
meanOk = false;
|
204 |
} |
205 |
if(varianceOk) {
|
206 |
variance[band] = Double.parseDouble(parser.getText());
|
207 |
varianceOk = false;
|
208 |
} |
209 |
break;
|
210 |
} |
211 |
if (!end)
|
212 |
tag = parser.next(); |
213 |
} |
214 |
return valueList;
|
215 |
} |
216 |
|
217 |
/*
|
218 |
* (non-Javadoc)
|
219 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
|
220 |
*/
|
221 |
public void read(String xml) throws ParsingException { |
222 |
int bandCount = 0; |
223 |
double[] max = null, maxRGB = null; |
224 |
double[] min = null, minRGB = null; |
225 |
double[] secondMax = null, secondMaxRGB = null; |
226 |
double[] secondMin = null, secondMinRGB = null; |
227 |
double[] mean = null; |
228 |
double[] variance = null; |
229 |
|
230 |
KXmlParser parser = new KXmlParser();
|
231 |
Reader reader = new StringReader(xml); |
232 |
try {
|
233 |
parser.setInput(reader); |
234 |
} catch (XmlPullParserException e) {
|
235 |
throw new ParsingException(xml); |
236 |
} |
237 |
try {
|
238 |
int tag = parser.nextTag();
|
239 |
|
240 |
if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
|
241 |
parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
242 |
while(tag != KXmlParser.END_DOCUMENT) {
|
243 |
switch(tag) {
|
244 |
case KXmlParser.START_TAG:
|
245 |
if(parser.getName() != null) { |
246 |
if (parser.getName().compareTo(MAIN_TAG) == 0) { |
247 |
bandCount = Integer.parseInt(parserString(parser, BANDCOUNT, null)); |
248 |
if(max == null) { |
249 |
max = new double[bandCount]; |
250 |
min = new double[bandCount]; |
251 |
secondMax = new double[bandCount]; |
252 |
secondMin = new double[bandCount]; |
253 |
maxRGB = new double[bandCount]; |
254 |
minRGB = new double[bandCount]; |
255 |
secondMaxRGB = new double[bandCount]; |
256 |
secondMinRGB = new double[bandCount]; |
257 |
mean = new double[bandCount]; |
258 |
variance = new double[bandCount]; |
259 |
} |
260 |
for (int i = 0; i < bandCount; i++) |
261 |
parserStatBandValues(parser, i, max, min, secondMax, secondMin, maxRGB, minRGB, secondMaxRGB, secondMinRGB, mean, variance); |
262 |
} |
263 |
} |
264 |
break;
|
265 |
case KXmlParser.END_TAG:
|
266 |
break;
|
267 |
case KXmlParser.TEXT:
|
268 |
break;
|
269 |
} |
270 |
tag = parser.next(); |
271 |
} |
272 |
parser.require(KXmlParser.END_DOCUMENT, null, null); |
273 |
} |
274 |
|
275 |
} catch (XmlPullParserException e) {
|
276 |
throw new ParsingException(xml); |
277 |
} catch (IOException e) { |
278 |
throw new ParsingException(xml); |
279 |
} |
280 |
getDatasetStatistics().setBandCount(bandCount); |
281 |
getDatasetStatistics().setMax(max); |
282 |
getDatasetStatistics().setMin(min); |
283 |
getDatasetStatistics().setSecondMax(secondMax); |
284 |
getDatasetStatistics().setSecondMin(secondMin); |
285 |
getDatasetStatistics().setMaxRGB(maxRGB); |
286 |
getDatasetStatistics().setMinRGB(minRGB); |
287 |
getDatasetStatistics().setSecondMaxRGB(secondMaxRGB); |
288 |
getDatasetStatistics().setSecondMinRGB(secondMinRGB); |
289 |
getDatasetStatistics().setMean(mean); |
290 |
getDatasetStatistics().setVariance(variance); |
291 |
getDatasetStatistics().setCalculated(true);
|
292 |
} |
293 |
|
294 |
/*
|
295 |
* (non-Javadoc)
|
296 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
|
297 |
*/
|
298 |
public String write() { |
299 |
if (stat == null) |
300 |
return ""; |
301 |
|
302 |
StringBuffer b = new StringBuffer(); |
303 |
|
304 |
b.append("<" + MAIN_TAG + ">\n"); |
305 |
putProperty(b, BANDCOUNT, getDatasetStatistics().getBandCount(), 1);
|
306 |
for (int i = 0; i < getDatasetStatistics().getBandCount(); i++) { |
307 |
b.append("\t<" + BAND + ">\n"); |
308 |
if (getDatasetStatistics().getMax() != null) |
309 |
putProperty(b, MAX, getDatasetStatistics().getMax()[i], 2);
|
310 |
if (getDatasetStatistics().getMin() != null) |
311 |
putProperty(b, MIN, getDatasetStatistics().getMin()[i], 2);
|
312 |
if (getDatasetStatistics().getSecondMax() != null) |
313 |
putProperty(b, SNDMAX, getDatasetStatistics().getSecondMax()[i], 2);
|
314 |
if (getDatasetStatistics().getSecondMin() != null) |
315 |
putProperty(b, SNDMIN, getDatasetStatistics().getSecondMin()[i], 2);
|
316 |
if (getDatasetStatistics().getMaxByteUnsigned() != null) |
317 |
putProperty(b, MAXRGB, getDatasetStatistics().getMaxByteUnsigned()[i], 2);
|
318 |
if (getDatasetStatistics().getMinByteUnsigned() != null) |
319 |
putProperty(b, MINRGB, getDatasetStatistics().getMinByteUnsigned()[i], 2);
|
320 |
if (getDatasetStatistics().getSecondMaxByteUnsigned() != null) |
321 |
putProperty(b, SNDMAXRGB, getDatasetStatistics().getSecondMaxByteUnsigned()[i], 2);
|
322 |
if (getDatasetStatistics().getSecondMinByteUnsigned() != null) |
323 |
putProperty(b, SNDMINRGB, getDatasetStatistics().getSecondMinByteUnsigned()[i], 2);
|
324 |
if (getDatasetStatistics().getMean() != null) |
325 |
putProperty(b, MEAN, getDatasetStatistics().getMean()[i], 2);
|
326 |
if (getDatasetStatistics().getVariance() != null) |
327 |
putProperty(b, VARIANCE, getDatasetStatistics().getVariance()[i], 2);
|
328 |
b.append("\t</" + BAND + ">\n"); |
329 |
} |
330 |
for (int i = 0; i < getDatasetStatistics().getTailTrimCount(); i++) { |
331 |
if (getDatasetStatistics().getTailTrimValue(i) != null) { |
332 |
b.append("\t<" + TAILTRIM + ">\n"); |
333 |
putProperty(b, KEY, ((Double) getDatasetStatistics().getTailTrimValue(i)[0]).doubleValue(), 2); |
334 |
putProperty(b, VALUE, ((Double) getDatasetStatistics().getTailTrimValue(i)[1]).doubleValue(), 2); |
335 |
b.append("\t</" + TAILTRIM + ">\n"); |
336 |
} |
337 |
} |
338 |
b.append("</" + MAIN_TAG + ">\n"); |
339 |
return b.toString();
|
340 |
} |
341 |
|
342 |
/*
|
343 |
* (non-Javadoc)
|
344 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
|
345 |
*/
|
346 |
public Object getResult() { |
347 |
return stat;
|
348 |
} |
349 |
|
350 |
/*
|
351 |
* (non-Javadoc)
|
352 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
|
353 |
*/
|
354 |
public String getMainTag() { |
355 |
return MAIN_TAG;
|
356 |
} |
357 |
} |