gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / datastruct / serializer / NoDataRmfSerializer.java @ 2328
History | View | Annotate | Download (5.85 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.datastruct.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.dataset.Buffer; |
29 |
import org.gvsig.fmap.dal.coverage.exception.ParsingException; |
30 |
import org.gvsig.raster.impl.datastruct.DefaultNoData; |
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 |
* This object converts a nodata value to XML. This class implements an IRmfBlock
|
40 |
* interface with the method to read and write. These will be used by the RMF file
|
41 |
* manager to read and write data.
|
42 |
* </P>
|
43 |
* <P>
|
44 |
* A raster will have a nodata value by band and its structure will be something like that:
|
45 |
* </P>
|
46 |
* <P>
|
47 |
* \<NoDataValue datatype=3 transparent=true\><BR>
|
48 |
* \<Band number=0 value=-99999 \><BR>
|
49 |
* \<Band number=1 value=-99999 \><BR>
|
50 |
* \<Band number=2 value=-99999 \><BR>
|
51 |
* \</NoDataValue\><BR>
|
52 |
*
|
53 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
54 |
*/
|
55 |
public class NoDataRmfSerializer extends ClassSerializer { |
56 |
private final String MAIN_TAG = "NoDataValue"; |
57 |
private DefaultNoData noData = null; |
58 |
|
59 |
/**
|
60 |
* Registra NoDataRmfSerializer en los puntos de extension de Serializer
|
61 |
*/
|
62 |
public static void register() { |
63 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
64 |
ExtensionPoint point = extensionPoints.get("Serializer");
|
65 |
point.append("NoDataValue", "", NoDataRmfSerializer.class); |
66 |
} |
67 |
|
68 |
/**
|
69 |
* Sets the nodata value to serialize
|
70 |
* @param noData Valor NoData
|
71 |
*/
|
72 |
public NoDataRmfSerializer(DefaultNoData noData) {
|
73 |
this.noData = noData;
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* Constructor.
|
78 |
*/
|
79 |
public NoDataRmfSerializer() {}
|
80 |
|
81 |
public String getMainTag() { |
82 |
return MAIN_TAG;
|
83 |
} |
84 |
|
85 |
/**
|
86 |
* Devuelve el valor noData, en caso de no existir lo crea.
|
87 |
* @return
|
88 |
*/
|
89 |
private DefaultNoData getNoData() {
|
90 |
if (noData == null) |
91 |
noData = new DefaultNoData();
|
92 |
return noData;
|
93 |
} |
94 |
|
95 |
public Object getResult() { |
96 |
return noData;
|
97 |
} |
98 |
|
99 |
public void read(String xml) throws ParsingException { |
100 |
KXmlParser parser = new KXmlParser();
|
101 |
Reader reader = new StringReader(xml); |
102 |
try {
|
103 |
parser.setInput(reader); |
104 |
} catch (XmlPullParserException e) {
|
105 |
throw new ParsingException(xml); |
106 |
} |
107 |
|
108 |
getNoData().delete(); |
109 |
int bands = 0; |
110 |
try {
|
111 |
int tag = parser.nextTag();
|
112 |
|
113 |
if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
|
114 |
parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
115 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
116 |
if (parser.getAttributeName(i).equals("transparent")) { |
117 |
getNoData().setNoDataTransparent(new Boolean(parser.getAttributeValue(i))); |
118 |
} |
119 |
if (parser.getAttributeName(i).equals("datatype")) { |
120 |
getNoData().setDataType(new Integer(parser.getAttributeValue(i))); |
121 |
} |
122 |
} |
123 |
|
124 |
|
125 |
while (tag != KXmlParser.END_DOCUMENT) {
|
126 |
switch (tag) {
|
127 |
case KXmlParser.START_TAG:
|
128 |
if (parser.getName().equals("Band")) { |
129 |
bands ++; |
130 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
131 |
if (parser.getAttributeName(i).equals("value")) { |
132 |
String str = (String) parser.getAttributeValue(i); |
133 |
if(str.compareTo("null") == 0) |
134 |
getNoData().setValue(null);
|
135 |
else
|
136 |
getNoData().setValue(Double.parseDouble(str));
|
137 |
} |
138 |
} |
139 |
} |
140 |
} |
141 |
tag = parser.next(); |
142 |
} |
143 |
} |
144 |
reader.close(); |
145 |
getNoData().setBandCount(bands); |
146 |
} catch (XmlPullParserException e) {
|
147 |
throw new ParsingException(xml); |
148 |
} catch (IOException e) { |
149 |
throw new ParsingException(xml); |
150 |
} |
151 |
} |
152 |
|
153 |
public String write() throws IOException { |
154 |
if (noData == null) |
155 |
return ""; |
156 |
|
157 |
StringBuffer b = new StringBuffer(); |
158 |
|
159 |
b.append("<" + MAIN_TAG + " datatype=\"" + getNoData().getDataType() + "\" transparent=\"" + getNoData().isNoDataTransparent() + "\">\n"); |
160 |
for (int i = 0; i < getNoData().getBandCount(); i++) { |
161 |
Number value = getNoData().getValueByBand(i);
|
162 |
b.append("\t<Band");
|
163 |
b.append(" number=\"" + i + "\""); |
164 |
if(value != null) { |
165 |
switch (getNoData().getDataType()) {
|
166 |
case Buffer.TYPE_BYTE: |
167 |
b.append(" value=\"" + value.byteValue() + "\"/>\n"); |
168 |
break;
|
169 |
case Buffer.TYPE_SHORT: |
170 |
b.append(" value=\"" + value.shortValue() + "\"/>\n"); |
171 |
break;
|
172 |
case Buffer.TYPE_INT: |
173 |
b.append(" value=\"" + value.intValue() + "\"/>\n"); |
174 |
break;
|
175 |
case Buffer.TYPE_FLOAT: |
176 |
b.append(" value=\"" + value.floatValue() + "\"/>\n"); |
177 |
break;
|
178 |
case Buffer.TYPE_DOUBLE: |
179 |
b.append(" value=\"" + value.doubleValue() + "\"/>\n"); |
180 |
break;
|
181 |
} |
182 |
} else {
|
183 |
b.append(" value=\"null\"/>\n");
|
184 |
} |
185 |
} |
186 |
b.append("</" + MAIN_TAG + ">\n"); |
187 |
|
188 |
return b.toString();
|
189 |
} |
190 |
} |