Revision 12276 trunk/libraries/libRaster/src/org/gvsig/raster/shared/StatisticsRmfSerializer.java
StatisticsRmfSerializer.java | ||
---|---|---|
25 | 25 |
import org.gvsig.raster.dataset.IBuffer; |
26 | 26 |
import org.gvsig.raster.dataset.io.rmf.ClassSerializer; |
27 | 27 |
import org.gvsig.raster.dataset.io.rmf.ParsingException; |
28 |
import org.gvsig.raster.dataset.properties.DatasetStatistics; |
|
28 | 29 |
import org.kxml2.io.KXmlParser; |
29 | 30 |
import org.xmlpull.v1.XmlPullParserException; |
30 | 31 |
|
... | ... | |
61 | 62 |
|
62 | 63 |
//TAGS |
63 | 64 |
public static final String MAIN_TAG = "Statistics"; |
65 |
public static final String BAND = "Band"; |
|
64 | 66 |
public static final String MIN = "Min"; |
65 | 67 |
public static final String MAX = "Max"; |
66 | 68 |
public static final String SNDMIN = "SecondMin"; |
... | ... | |
71 | 73 |
public static final String VARIANCE = "Variance"; |
72 | 74 |
public static final String BANDCOUNT = "BandCount"; |
73 | 75 |
public static final String TAILTRIM = "TailTrim"; |
74 |
|
|
76 |
public static final String KEY = "Key"; |
|
77 |
public static final String VALUE = "Value"; |
|
75 | 78 |
|
76 |
private IStatistics stat = null;
|
|
79 |
private DatasetStatistics stat = null;
|
|
77 | 80 |
|
78 | 81 |
/** |
79 | 82 |
* Constructor. Asigna la tabla a serializar. |
80 | 83 |
* @param ColorTable tabla a convertir en XML |
81 | 84 |
*/ |
82 |
public StatisticsRmfSerializer(IStatistics stat) {
|
|
85 |
public StatisticsRmfSerializer(DatasetStatistics stat) {
|
|
83 | 86 |
this.stat = stat; |
84 | 87 |
} |
85 | 88 |
|
... | ... | |
89 | 92 |
public StatisticsRmfSerializer() { |
90 | 93 |
} |
91 | 94 |
|
95 |
/** |
|
96 |
* Parsea el tag Band para extraer la lista de valores (Values) asociada a una banda. |
|
97 |
* @param parser KXmlParser |
|
98 |
* @return Array de long |
|
99 |
* @throws XmlPullParserException |
|
100 |
* @throws IOException |
|
101 |
*/ |
|
102 |
private long[] parserStatBandValues(KXmlParser parser, int band, double[] max, double[] min, double[] sndmax, double[] sndmin, double[] mean, double[] variance) throws XmlPullParserException, IOException { |
|
103 |
boolean maxOk = false, minOk = false, sndmaxOk = false, sndminOk = false, meanOk = false, varianceOk = false; |
|
104 |
long[] valueList = null; |
|
105 |
boolean end = false; |
|
106 |
int tag = parser.next(); |
|
107 |
while (!end) { |
|
108 |
switch(tag) { |
|
109 |
case KXmlParser.START_TAG: |
|
110 |
if(parser.getName() != null) { |
|
111 |
if (parser.getName().compareTo(MAX) == 0) |
|
112 |
maxOk = true; |
|
113 |
if (parser.getName().compareTo(MIN) == 0) |
|
114 |
minOk = true; |
|
115 |
if (parser.getName().compareTo(SNDMAX) == 0) |
|
116 |
sndmaxOk = true; |
|
117 |
if (parser.getName().compareTo(SNDMIN) == 0) |
|
118 |
sndminOk = true; |
|
119 |
if (parser.getName().compareTo(MEAN) == 0) |
|
120 |
meanOk = true; |
|
121 |
if (parser.getName().compareTo(VARIANCE) == 0) |
|
122 |
varianceOk = 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(maxOk) { |
|
131 |
max[band] = Double.parseDouble(parser.getText()); |
|
132 |
maxOk = false; |
|
133 |
} |
|
134 |
if(minOk) { |
|
135 |
min[band] = Double.parseDouble(parser.getText()); |
|
136 |
minOk = false; |
|
137 |
} |
|
138 |
if(sndmaxOk) { |
|
139 |
sndmax[band] = Double.parseDouble(parser.getText()); |
|
140 |
sndmaxOk = false; |
|
141 |
} |
|
142 |
if(sndminOk) { |
|
143 |
sndmin[band] = Double.parseDouble(parser.getText()); |
|
144 |
sndminOk = false; |
|
145 |
} |
|
146 |
if(meanOk) { |
|
147 |
mean[band] = Double.parseDouble(parser.getText()); |
|
148 |
meanOk = false; |
|
149 |
} |
|
150 |
if(varianceOk) { |
|
151 |
variance[band] = Double.parseDouble(parser.getText()); |
|
152 |
varianceOk = false; |
|
153 |
} |
|
154 |
break; |
|
155 |
} |
|
156 |
if (!end) |
|
157 |
tag = parser.next(); |
|
158 |
} |
|
159 |
return valueList; |
|
160 |
} |
|
161 |
|
|
92 | 162 |
/* |
93 | 163 |
* (non-Javadoc) |
94 | 164 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String) |
95 | 165 |
*/ |
96 | 166 |
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; |
|
167 |
int bandCount = 0; |
|
168 |
double[] max = null; |
|
169 |
double[] min = null; |
|
170 |
double[] secondMax = null; |
|
171 |
double[] secondMin = null; |
|
172 |
double[] mean = null; |
|
173 |
double[] variance = null; |
|
102 | 174 |
|
103 | 175 |
KXmlParser parser = new KXmlParser(); |
104 | 176 |
Reader reader = new StringReader(xml); |
... | ... | |
117 | 189 |
case KXmlParser.START_TAG: |
118 | 190 |
if(parser.getName() != null) { |
119 | 191 |
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; |
|
192 |
bandCount = Integer.parseInt(parserString(parser, BANDCOUNT)); |
|
193 |
if(max == null) { |
|
194 |
max = new double[bandCount]; |
|
195 |
min = new double[bandCount]; |
|
196 |
secondMax = new double[bandCount]; |
|
197 |
secondMin = new double[bandCount]; |
|
198 |
mean = new double[bandCount]; |
|
199 |
variance = new double[bandCount]; |
|
126 | 200 |
} |
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 |
} |
|
201 |
for (int i = 0; i < bandCount; i++) |
|
202 |
parserStatBandValues(parser, i, max, min, secondMax, secondMin, mean, variance); |
|
137 | 203 |
} |
138 | 204 |
} |
139 | 205 |
break; |
... | ... | |
152 | 218 |
} catch (IOException e) { |
153 | 219 |
throw new ParsingException(xml); |
154 | 220 |
} |
155 |
|
|
156 |
*/ |
|
221 |
if(stat == null) |
|
222 |
stat = new DatasetStatistics(null); |
|
223 |
stat.setBandCount(bandCount); |
|
224 |
stat.setMax(max); |
|
225 |
stat.setMin(min); |
|
226 |
stat.setSecondMax(secondMax); |
|
227 |
stat.setSecondMin(secondMin); |
|
228 |
stat.setMean(mean); |
|
229 |
stat.setVariance(variance); |
|
230 |
stat.setCalculated(true); |
|
157 | 231 |
} |
158 | 232 |
|
159 | 233 |
/* |
... | ... | |
164 | 238 |
StringBuffer b = new StringBuffer(); |
165 | 239 |
|
166 | 240 |
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] + " "); |
|
241 |
putProperty(b, BANDCOUNT, stat.getBandCount(), 1); |
|
242 |
for (int i = 0; i < stat.getBandCount(); i++) { |
|
243 |
b.append("\t<" + BAND + ">\n"); |
|
244 |
putProperty(b, MAX, stat.getMax()[i], 2); |
|
245 |
putProperty(b, MIN, stat.getMin()[i], 2); |
|
246 |
putProperty(b, SNDMAX, stat.getSecondMax()[i], 2); |
|
247 |
putProperty(b, SNDMIN, stat.getSecondMin()[i], 2); |
|
248 |
putProperty(b, MEAN, stat.getMean()[i], 2); |
|
249 |
putProperty(b, VARIANCE, stat.getVariance()[i], 2); |
|
250 |
b.append("\t</" + BAND + ">\n"); |
|
198 | 251 |
} |
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] + " "); |
|
252 |
for (int i = 0; i < stat.getTailTrimCount(); i++) { |
|
253 |
b.append("\t<" + TAILTRIM + ">\n"); |
|
254 |
putProperty(b, KEY, ((Double)stat.getTailTrimValue(i)[0]).doubleValue(), 2); |
|
255 |
putProperty(b, VALUE, ((Double)stat.getTailTrimValue(i)[1]).doubleValue(), 2); |
|
256 |
b.append("\t</" + TAILTRIM + ">\n"); |
|
202 | 257 |
} |
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 + ">");*/ |
|
258 |
b.append("</" + MAIN_TAG + ">"); |
|
222 | 259 |
return b.toString(); |
223 | 260 |
} |
224 | 261 |
|
Also available in: Unified diff