Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / serializer / GeoInfoRmfSerializer.java @ 31389

History | View | Annotate | Download (7.18 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.dataset.serializer;
20

    
21
import java.awt.geom.AffineTransform;
22
import java.awt.geom.Point2D;
23
import java.io.IOException;
24
import java.io.Reader;
25
import java.io.StringReader;
26

    
27
import org.gvsig.raster.dataset.RasterDataset;
28
import org.gvsig.raster.dataset.io.rmf.ClassSerializer;
29
import org.gvsig.raster.dataset.io.rmf.ParsingException;
30
import org.gvsig.raster.util.extensionPoints.ExtensionPoint;
31
import org.kxml2.io.KXmlParser;
32
import org.xmlpull.v1.XmlPullParserException;
33
/**
34
 * <P>
35
 * Clase para convertir a XML la informaci?n geo del raster y obtener esta informaci?n desde XML.
36
 * Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y 
37
 * lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
38
 * leer datos.
39
 * </P>
40
 * <P>
41
 * La estructura XML es la siguiente:
42
 * </P>
43
 * <P>
44
 * \<FLyrGeoRaster xmlns="http://www.gvsig.org"\><BR>
45
 * \<Extent\><BR>
46
 * \<X\>4.325548573549895\</X\><BR>
47
 * \<Y\>1952.062652448396\</Y\><BR>
48
 * \<RotationX\>-0.23070178571428576\</RotationX\><BR>
49
 * \<RotationY\>-0.1655108150921638\</RotationY\><BR>
50
 * \<PixelSizeX\>0.04363559968817176\</PixelSizeX\><BR>
51
 * \<PixelSizeY\>-0.43297650488377315\</PixelSizeY\><BR>
52
 * \<Width\>98.92190449308538\</Width\><BR>
53
 * \<Height\>-746.0185179147411\</Height\><BR>
54
 * \</Extent\><BR>
55
 * \<Dimension\><BR>
56
 * \<ImagePxWidth\>2268\</ImagePxWidth\><BR>
57
 * \<ImagePxHeight\>1724\</ImagePxHeight\><BR>
58
 * \</Dimension\><BR>
59
 * \</FLyrGeoRaster\><BR>
60
 * </P>
61
 *
62
 * @version 23/04/2007
63
 * @author Nacho Brodin (nachobrodin@gmail.com)
64
 */
65
public class GeoInfoRmfSerializer extends ClassSerializer {
66
        
67
        //TAGS
68
        public static final String MAIN_TAG = "FLyrGeoRaster";
69
        public static final String EXTENT   = "Extent";
70
        public static final String X        = "X";
71
        public static final String Y        = "Y";
72
        public static final String ROTX     = "RotationX";
73
        public static final String ROTY     = "RotationY";
74
        public static final String PSX      = "PixelSizeX";
75
        public static final String PSY      = "PixelSizeY";
76
        public static final String W        = "Width";
77
        public static final String H        = "Height";
78
        public static final String DIM      = "Dimension";
79
        public static final String IMGW     = "ImagePxWidth";
80
        public static final String IMGH     = "ImagePxHeight";
81

    
82
        private RasterDataset      dataset  = null;
83
        private AffineTransform    at       = null;
84
        private Point2D            dim      = null;
85

    
86
        /**
87
         * Registra GeoInfoRmfSerializer en los puntos de extension de Serializer
88
         */
89
        public static void register() {
90
                ExtensionPoint point = ExtensionPoint.getExtensionPoint("Serializer");
91
                point.register("GeoInfo", GeoInfoRmfSerializer.class);
92
        }
93
        
94
        /**
95
         * Constructor. Asigna la tabla a serializar.
96
         * @param ColorTable tabla a convertir en XML
97
         */
98
        public GeoInfoRmfSerializer(RasterDataset dataset) {
99
                this.dataset = dataset;
100
        }
101

    
102
        /**
103
         * Constructor. 
104
         */
105
        public GeoInfoRmfSerializer(AffineTransform at, Point2D dim) {
106
                this.at = at;
107
                this.dim = dim;
108
        }
109
        
110
        /**
111
         * Constructor. 
112
         */
113
        public GeoInfoRmfSerializer() {
114
        }
115
        
116
        /*
117
         * (non-Javadoc)
118
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
119
         */
120
        public void read(String xml) throws ParsingException {
121
                double x = 0, y = 0, rotX = 0, rotY = 0, psX = 0, psY = 0;
122
                double imgW = 0, imgH = 0;
123
                
124
                KXmlParser parser = new KXmlParser();
125
                Reader reader = new StringReader(xml);
126
                try {
127
                        parser.setInput(reader);
128
                } catch (XmlPullParserException e) {
129
                        throw new ParsingException(xml);
130
                }
131
                try {
132
                        int tag = parser.nextTag();
133
                        
134
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){                    
135
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);                            
136
                                while(tag != KXmlParser.END_DOCUMENT) {
137
                                        switch(tag) {
138
                                                case KXmlParser.START_TAG:
139
                                                        if (parser.getName().compareTo(MAIN_TAG) == 0) {
140
                                                                x = Double.parseDouble(parserString(parser, X, null));
141
                                                                y = Double.parseDouble(parserString(parser, Y, null));
142
                                                                rotX = Double.parseDouble(parserString(parser, ROTX, null));
143
                                                                rotY = Double.parseDouble(parserString(parser, ROTY, null));
144
                                                                psX = Double.parseDouble(parserString(parser, PSX, null));
145
                                                                psY = Double.parseDouble(parserString(parser, PSY, null));
146
                                                                Double.parseDouble(parserString(parser, W, null));
147
                                                                Double.parseDouble(parserString(parser, H, null));
148
                                                                imgW = Double.parseDouble(parserString(parser, IMGW, null));
149
                                                                imgH = Double.parseDouble(parserString(parser, IMGH, null));
150
                                                        }
151
                                                        break;
152
                                                case KXmlParser.END_TAG:                                                                
153
                                                        break;
154
                                                case KXmlParser.TEXT:                                                        
155
                                                        break;
156
                                        }
157
                                        tag = parser.next();
158
                                }
159
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
160
                        }
161
                        reader.close();
162
                } catch (XmlPullParserException e) {
163
                        throw new ParsingException(xml);
164
                } catch (IOException e) {
165
                        throw new ParsingException(xml);
166
                }
167

    
168
                dim = new Point2D.Double(imgW, imgH);
169
                at = new AffineTransform(psX, rotY, rotX, psY, x, y);
170
                if (dataset != null)
171
                        dataset.setAffineTransform(at);
172
        }
173

    
174
        /*
175
         * (non-Javadoc)
176
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
177
         */
178
        public String write() {
179
                StringBuffer b = new StringBuffer();
180
                        
181
                if(dataset != null) {
182
                        at = dataset.getAffineTransform();
183
                        dim = new Point2D.Double(dataset.getWidth(), dataset.getHeight());
184
                }
185
                
186
                b.append("<" + MAIN_TAG + ">\n");
187
                b.append("\t<" + EXTENT + ">\n");
188
                putProperty(b, X, at.getTranslateX(), 3);
189
                putProperty(b, Y, at.getTranslateY(), 3);
190
                putProperty(b, ROTX, at.getShearX(), 3);
191
                putProperty(b, ROTY, at.getShearY(), 3);
192
                double wd = 0;
193
                double hd = 0;
194
                if(dataset != null) {
195
                        wd = (dataset.getExtent().getMax().getX() - dataset.getExtent().getMin().getX());
196
                        hd = (dataset.getExtent().getMax().getY() - dataset.getExtent().getMin().getY());
197
                }
198
                putProperty(b, PSX, at.getScaleX(), 3);
199
                putProperty(b, PSY, at.getScaleY(), 3);
200
                putProperty(b, W, wd, 3);
201
                putProperty(b, H, hd, 3);
202
                b.append("\t</" + EXTENT + ">\n");
203
                b.append("\t<" + DIM + ">\n");
204
                putProperty(b, IMGW, dim.getX(), 3);
205
                putProperty(b, IMGH, dim.getY(), 3);
206
                b.append("\t</" + DIM + ">\n");
207
                b.append("</" + MAIN_TAG + ">\n");
208
                return b.toString();
209
        }
210
        
211
        /*
212
         * (non-Javadoc)
213
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
214
         */
215
        public Object getResult() {
216
                return dataset;
217
        }
218

    
219
        /*
220
         *  (non-Javadoc)
221
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
222
         */
223
        public String getMainTag() {
224
                return MAIN_TAG;
225
        }
226
}