Statistics
| Revision:

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

History | View | Annotate | Download (5.87 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
        }
112
        
113
        /**
114
         * Constructor. Asigna el valor NoData a serializar
115
         * @param noData Valor NoData
116
         * @param type   Tipo de NoData
117
         */
118
        public NoDataRmfSerializer(double noData, int type) {
119
                this(noData, type, IBuffer.TYPE_DOUBLE);
120
        }
121

    
122
        /**
123
         * Constructor.
124
         */
125
        public NoDataRmfSerializer() {
126
        }
127

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

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

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

    
161
                try {
162
                        int tag = parser.nextTag();
163

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

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

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

    
199
                if (noData.getType() == RasterLibrary.NODATATYPE_LAYER)
200
                        return "";
201

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

    
209
                return b.toString();
210
        }
211
}