svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster / src / org / gvsig / raster / datastruct / serializer / HistogramRmfSerializer.java @ 26873
History | View | Annotate | Download (7.82 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.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.gvsig.tools.ToolsLocator; |
30 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
31 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
32 |
import org.kxml2.io.KXmlParser; |
33 |
import org.xmlpull.v1.XmlPullParserException; |
34 |
/**
|
35 |
* <P>
|
36 |
* Clase para convertir a XML un histograma y obtener el histograma desde XML.
|
37 |
* Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y
|
38 |
* lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
|
39 |
* leer datos.
|
40 |
* </P>
|
41 |
* <P>
|
42 |
* La estructura XML de un histograma es la siguiente:
|
43 |
* </P>
|
44 |
* <P>
|
45 |
*\<Histogram\> <BR>
|
46 |
* \<ClassInterval\>1.0\</ClassInterval\><BR>
|
47 |
* \<Min\>0.0\</Min\><BR>
|
48 |
* \<Max\>255.0\</Max\><BR>
|
49 |
* \<BandCount\>3\</BandCount\><BR>
|
50 |
* \<Band\><BR>
|
51 |
* \<Values\>13 2 0 ... 2 1 6\</Values\><BR>
|
52 |
* \</Band\><BR>
|
53 |
* \<Band\><BR>
|
54 |
* \<Values\>6 2 2 ... 3 2 8\</Values\><BR>
|
55 |
* \</Band\><BR>
|
56 |
* \<Band\><BR>
|
57 |
* \<Values\>16 1 1 ... 1 1 9\</Values\><BR>
|
58 |
* \</Band\><BR\>
|
59 |
*\</Histogram\><BR>
|
60 |
*\</P\>
|
61 |
*
|
62 |
* @version 23/04/2007
|
63 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
64 |
*/
|
65 |
public class HistogramRmfSerializer extends ClassSerializer { |
66 |
//TAGS
|
67 |
public static final String MAIN_TAG = "Histogram"; |
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 |
public static final String DATA_TYPE = "DataType"; |
74 |
|
75 |
private Histogram histogram = null; |
76 |
|
77 |
/**
|
78 |
* Registra HistogramRmfSerializer en los puntos de extension de Serializer
|
79 |
*/
|
80 |
public static void register() { |
81 |
ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager(); |
82 |
ExtensionPoint point=extensionPoints.get("Serializer");
|
83 |
point.append("Histogram", "", HistogramRmfSerializer.class); |
84 |
} |
85 |
|
86 |
/**
|
87 |
* Constructor. Asigna el histograma a serializar.
|
88 |
* @param Histogram histograma a convertir en XML
|
89 |
*/
|
90 |
public HistogramRmfSerializer(Histogram hist) {
|
91 |
this.histogram = hist;
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* Constructor.
|
96 |
*/
|
97 |
public HistogramRmfSerializer() {}
|
98 |
|
99 |
/**
|
100 |
* Convierte una lista de valores en un solo String en un array de long
|
101 |
* @param list Cadena con la lista de valores
|
102 |
* @return Array de valores long
|
103 |
* @throws XmlPullParserException
|
104 |
* @throws IOException
|
105 |
*/
|
106 |
private long[] parseLongList(String list) throws XmlPullParserException, IOException { |
107 |
String[] sValues = list.split(" "); |
108 |
long[] dValues = new long[sValues.length]; |
109 |
for (int i = 0; i < sValues.length; i++) |
110 |
dValues[i] = Long.parseLong(sValues[i]);
|
111 |
return dValues;
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* Parsea el tag Band para extraer la lista de valores (Values) asociada a una banda.
|
116 |
* @param parser KXmlParser
|
117 |
* @return Array de long
|
118 |
* @throws XmlPullParserException
|
119 |
* @throws IOException
|
120 |
*/
|
121 |
private long[] parserHistogramListValues(KXmlParser parser) throws XmlPullParserException, IOException { |
122 |
long[] valueList = null; |
123 |
boolean end = false; |
124 |
boolean tagOk = false; |
125 |
int tag = parser.next();
|
126 |
while (!end) {
|
127 |
switch (tag) {
|
128 |
case KXmlParser.START_TAG:
|
129 |
if (parser.getName() != null) { |
130 |
if (parser.getName().compareTo(VALUES) == 0) |
131 |
tagOk = true;
|
132 |
} |
133 |
break;
|
134 |
case KXmlParser.END_TAG:
|
135 |
if (parser.getName().compareTo(BAND) == 0) |
136 |
end = true;
|
137 |
break;
|
138 |
case KXmlParser.TEXT:
|
139 |
if (tagOk) {
|
140 |
valueList = parseLongList(parser.getText()); |
141 |
tagOk = false;
|
142 |
} |
143 |
break;
|
144 |
} |
145 |
if (!end)
|
146 |
tag = parser.next(); |
147 |
} |
148 |
return valueList;
|
149 |
} |
150 |
|
151 |
/*
|
152 |
* (non-Javadoc)
|
153 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
|
154 |
*/
|
155 |
public void read(String xml) throws ParsingException { |
156 |
// double interval = 0D;
|
157 |
double min = 0D; |
158 |
double max = 0D; |
159 |
int numBands = 0; |
160 |
int dataType = 0; |
161 |
long[][] values = null; |
162 |
|
163 |
KXmlParser parser = new KXmlParser();
|
164 |
Reader reader = new StringReader(xml); |
165 |
try {
|
166 |
parser.setInput(reader); |
167 |
} catch (XmlPullParserException e) {
|
168 |
throw new ParsingException(xml); |
169 |
} |
170 |
try {
|
171 |
int tag = parser.nextTag();
|
172 |
|
173 |
if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
|
174 |
parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
175 |
while (tag != KXmlParser.END_DOCUMENT) {
|
176 |
switch (tag) {
|
177 |
case KXmlParser.START_TAG:
|
178 |
if (parser.getName() != null) { |
179 |
if (parser.getName().compareTo(MAIN_TAG) == 0) { |
180 |
numBands = (int) Double.parseDouble(parserString(parser, BAND_COUNT, null)); |
181 |
dataType = Integer.parseInt(parserString(parser, DATA_TYPE, null)); |
182 |
values = new long[numBands][]; |
183 |
for (int iBand = 0; iBand < numBands; iBand++) { |
184 |
min = Double.parseDouble(parserString(parser, MIN, null)); |
185 |
max = Double.parseDouble(parserString(parser, MAX, null)); |
186 |
values[iBand] = parserHistogramListValues(parser); |
187 |
} |
188 |
} |
189 |
} |
190 |
break;
|
191 |
case KXmlParser.END_TAG:
|
192 |
break;
|
193 |
case KXmlParser.TEXT:
|
194 |
break;
|
195 |
} |
196 |
tag = parser.next(); |
197 |
} |
198 |
parser.require(KXmlParser.END_DOCUMENT, null, null); |
199 |
} |
200 |
|
201 |
} catch (XmlPullParserException e) {
|
202 |
throw new ParsingException(xml); |
203 |
} catch (IOException e) { |
204 |
throw new ParsingException(xml); |
205 |
} |
206 |
|
207 |
double[] mins = new double[numBands]; |
208 |
double[] maxs = new double[numBands]; |
209 |
for (int i = 0; i < numBands; i++) { |
210 |
mins[i] = min; |
211 |
maxs[i] = max; |
212 |
} |
213 |
histogram = new Histogram(numBands, mins, maxs, dataType);
|
214 |
histogram.setTable(values); |
215 |
} |
216 |
|
217 |
/*
|
218 |
* (non-Javadoc)
|
219 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
|
220 |
*/
|
221 |
public String write() { |
222 |
StringBuffer b = new StringBuffer(); |
223 |
HistogramClass[][] c = histogram.getHistogram(); |
224 |
|
225 |
b.append("<" + MAIN_TAG + ">\n"); |
226 |
|
227 |
putProperty(b, BAND_COUNT, histogram.getNumBands(), 1);
|
228 |
putProperty(b, DATA_TYPE, histogram.getDataType(), 1);
|
229 |
|
230 |
for (int iBand = 0; iBand < histogram.getNumBands(); iBand++) { |
231 |
b.append("\t<" + BAND + ">\n"); |
232 |
putProperty(b, MIN, c[iBand][0].getMin(), 2); |
233 |
putProperty(b, MAX, c[iBand][c[iBand].length - 1].getMin(), 2); |
234 |
b.append("\t\t<" + VALUES + ">"); |
235 |
for (int iValues = 0; iValues < histogram.getNumValues(); iValues++) { |
236 |
if (iValues != 0) |
237 |
b.append(" ");
|
238 |
b.append((long) histogram.getHistogramValueByPos(iBand, iValues));
|
239 |
} |
240 |
b.append("</" + VALUES + ">\n"); |
241 |
b.append("\t</" + BAND + ">\n"); |
242 |
} |
243 |
b.append("</" + MAIN_TAG + ">\n"); |
244 |
return b.toString();
|
245 |
} |
246 |
|
247 |
/*
|
248 |
* (non-Javadoc)
|
249 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
|
250 |
*/
|
251 |
public Object getResult() { |
252 |
return (Histogram) histogram;
|
253 |
} |
254 |
|
255 |
/*
|
256 |
* (non-Javadoc)
|
257 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
|
258 |
*/
|
259 |
public String getMainTag() { |
260 |
return MAIN_TAG;
|
261 |
} |
262 |
} |