Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / serializer / NoDataRmfSerializer.java @ 20868

History | View | Annotate | Download (5.95 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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.RasterLibrary;
26
import org.gvsig.raster.dataset.IBuffer;
27
import org.gvsig.raster.dataset.io.rmf.ClassSerializer;
28
import org.gvsig.raster.dataset.io.rmf.ParsingException;
29
import org.kxml2.io.KXmlParser;
30
import org.xmlpull.v1.XmlPullParserException;
31
/**
32
 * <P>
33
 * Clase para convertir a XML un valor NoData y obtener el valor desde un 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 un valor NoData es la siguiente:
40
 * </P>
41
 * <P>
42
 * &lt;NoData value="-99.999"><BR>
43
 * &lt;/NoData><BR>
44
 *
45
 * @version 18/12/2007
46
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
47
 */
48
public class NoDataRmfSerializer extends ClassSerializer {
49
        public class NoDataObject {
50
                double value = 0;
51
                int type = -1;
52
                /**
53
                 * @return the noData
54
                 */
55
                public double getValue() {
56
                        return value;
57
                }
58
                /**
59
                 * @param noData the noData to set
60
                 */
61
                public void setValue(double noData) {
62
                        this.value = noData;
63
                }
64
                /**
65
                 * @return the enabled
66
                 */
67
                public int getType() {
68
                        return type;
69
                }
70
                /**
71
                 * @param enabled the enabled to set
72
                 */
73
                public void setType(int type) {
74
                        this.type = type;
75
                }
76
        }
77
        
78
        private final String MAIN_TAG = "NoData";
79
        private NoDataObject noData = new NoDataObject();
80

    
81
        /**
82
         * Constructor. Asigna el valor NoData a serializar
83
         * @param noData   Valor NoData
84
         * @param type     Tipo de NoData
85
         * @param dataType Tipo de datos de la capa
86
         */
87
        public NoDataRmfSerializer(double noData, int type, int dataType) {
88
                switch (dataType) {
89
                        case IBuffer.TYPE_BYTE:
90
                                this.noData.setValue((byte) noData);
91
                                this.noData.setType(RasterLibrary.NODATATYPE_USER);
92
                                break;
93
                        case IBuffer.TYPE_FLOAT:
94
                                this.noData.setValue((float) noData);
95
                                this.noData.setType(RasterLibrary.NODATATYPE_USER);
96
                                break;
97
                        case IBuffer.TYPE_USHORT:
98
                        case IBuffer.TYPE_INT:
99
                                this.noData.setValue((int) noData);
100
                                this.noData.setType(RasterLibrary.NODATATYPE_USER);
101
                                break;
102
                        case IBuffer.TYPE_SHORT:
103
                                this.noData.setValue((short) noData);
104
                                this.noData.setType(RasterLibrary.NODATATYPE_USER);
105
                                break;
106
                        default:
107
                                this.noData.setValue(noData);
108
                                this.noData.setType(type);
109
                                break;
110
                }
111
                if (type == RasterLibrary.NODATATYPE_DISABLED)
112
                        this.noData.setType(type);
113
        }
114
        
115
        /**
116
         * Constructor. Asigna el valor NoData a serializar
117
         * @param noData Valor NoData
118
         * @param type   Tipo de NoData
119
         */
120
        public NoDataRmfSerializer(double noData, int type) {
121
                this(noData, type, IBuffer.TYPE_DOUBLE);
122
        }
123

    
124
        /**
125
         * Constructor.
126
         */
127
        public NoDataRmfSerializer() {
128
        }
129

    
130
        /*
131
         * (non-Javadoc)
132
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
133
         */
134
        public String getMainTag() {
135
                return MAIN_TAG;
136
        }
137

    
138
        /*
139
         * (non-Javadoc)
140
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
141
         */
142
        public Object getResult() {
143
                if (noData.getType() == -1)
144
                        return null;
145
                if (noData.getType() == RasterLibrary.NODATATYPE_LAYER)
146
                        return null;
147
                return noData;
148
        }
149

    
150
        /*
151
         * (non-Javadoc)
152
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
153
         */
154
        public void read(String xml) throws ParsingException {
155
                KXmlParser parser = new KXmlParser();
156
                Reader reader = new StringReader(xml);
157
                try {
158
                        parser.setInput(reader);
159
                } catch (XmlPullParserException e) {
160
                        throw new ParsingException(xml);
161
                }
162

    
163
                try {
164
                        int tag = parser.nextTag();
165

    
166
                        if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
167
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
168

    
169
                                while (tag != KXmlParser.END_DOCUMENT) {
170
                                        switch (tag) {
171
                                                case KXmlParser.START_TAG:
172
                                                        if (parser.getName().equals("Data")) {
173
                                                                for (int i = 0; i < parser.getAttributeCount(); i++) {
174
                                                                        if (parser.getAttributeName(i).equals("value")) {
175
                                                                                noData.setValue(Double.parseDouble((String) parser.getAttributeValue(i)));
176
                                                                        }
177
                                                                        if (parser.getAttributeName(i).equals("type")) {
178
                                                                                noData.setType(Integer.parseInt((String) parser.getAttributeValue(i)));
179
                                                                        }
180
                                                                }
181
                                                        }
182
                                        }
183
                                        tag = parser.next();
184
                                }
185
                        }
186
                        reader.close();
187
                } catch (XmlPullParserException e) {
188
                        throw new ParsingException(xml);
189
                } catch (IOException e) {
190
                        throw new ParsingException(xml);
191
                }
192
        }
193

    
194
        /*
195
         * (non-Javadoc)
196
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
197
         */
198
        public String write() throws IOException {
199
                StringBuffer b = new StringBuffer();
200

    
201
                if (noData.getType() == RasterLibrary.NODATATYPE_LAYER)
202
                        return "";
203

    
204
                b.append("<" + MAIN_TAG + ">\n");
205
                b.append("\t<Data");
206
                if (noData.getType() == RasterLibrary.NODATATYPE_USER)
207
                        b.append(" value=\"" + noData.getValue() + "\"");
208
                b.append(" type=\"" + noData.getType() + "\"/>\n");
209
                b.append("</" + MAIN_TAG + ">\n");
210

    
211
                return b.toString();
212
        }
213
}