Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster / src / org / gvsig / raster / dataset / serializer / GeoInfoRmfSerializer.java @ 31390

History | View | Annotate | Download (7.29 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.rmf.ClassSerializer;
29
import org.gvsig.raster.dataset.rmf.ParsingException;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.extensionpoint.ExtensionPoint;
32
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
33
import org.kxml2.io.KXmlParser;
34
import org.xmlpull.v1.XmlPullParserException;
35
/**
36
 * <P>
37
 * Clase para convertir a XML la informaci?n geo del raster y obtener esta informaci?n desde XML.
38
 * Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y
39
 * lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
40
 * leer datos.
41
 * </P>
42
 * <P>
43
 * La estructura XML es la siguiente:
44
 * </P>
45
 * <P>
46
 * \<FLyrGeoRaster xmlns="http://www.gvsig.org"\><BR>
47
 * \<Extent\><BR>
48
 * \<X\>4.325548573549895\</X\><BR>
49
 * \<Y\>1952.062652448396\</Y\><BR>
50
 * \<RotationX\>-0.23070178571428576\</RotationX\><BR>
51
 * \<RotationY\>-0.1655108150921638\</RotationY\><BR>
52
 * \<PixelSizeX\>0.04363559968817176\</PixelSizeX\><BR>
53
 * \<PixelSizeY\>-0.43297650488377315\</PixelSizeY\><BR>
54
 * \<Width\>98.92190449308538\</Width\><BR>
55
 * \<Height\>-746.0185179147411\</Height\><BR>
56
 * \</Extent\><BR>
57
 * \<Dimension\><BR>
58
 * \<ImagePxWidth\>2268\</ImagePxWidth\><BR>
59
 * \<ImagePxHeight\>1724\</ImagePxHeight\><BR>
60
 * \</Dimension\><BR>
61
 * \</FLyrGeoRaster\><BR>
62
 * </P>
63
 *
64
 * @version 23/04/2007
65
 * @author Nacho Brodin (nachobrodin@gmail.com)
66
 */
67
public class GeoInfoRmfSerializer extends ClassSerializer {
68

    
69
        //TAGS
70
        public static final String MAIN_TAG = "FLyrGeoRaster";
71
        public static final String EXTENT   = "Extent";
72
        public static final String X        = "X";
73
        public static final String Y        = "Y";
74
        public static final String ROTX     = "RotationX";
75
        public static final String ROTY     = "RotationY";
76
        public static final String PSX      = "PixelSizeX";
77
        public static final String PSY      = "PixelSizeY";
78
        public static final String W        = "Width";
79
        public static final String H        = "Height";
80
        public static final String DIM      = "Dimension";
81
        public static final String IMGW     = "ImagePxWidth";
82
        public static final String IMGH     = "ImagePxHeight";
83

    
84
        private RasterDataset      dataset  = null;
85
        private AffineTransform    at       = null;
86
        private Point2D            dim      = null;
87

    
88
        /**
89
         * Registra GeoInfoRmfSerializer en los puntos de extension de Serializer
90
         */
91
        public static void register() {
92
                ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager();
93
                ExtensionPoint point=extensionPoints.get("Serializer");
94
                point.append("GeoInfo", "", GeoInfoRmfSerializer.class);
95
        }
96

    
97
        /**
98
         * Constructor. Asigna la tabla a serializar.
99
         * @param ColorTable tabla a convertir en XML
100
         */
101
        public GeoInfoRmfSerializer(RasterDataset dataset) {
102
                this.dataset = dataset;
103
        }
104

    
105
        /**
106
         * Constructor.
107
         */
108
        public GeoInfoRmfSerializer(AffineTransform at, Point2D dim) {
109
                this.at = at;
110
                this.dim = dim;
111
        }
112

    
113
        /**
114
         * Constructor.
115
         */
116
        public GeoInfoRmfSerializer() {
117
        }
118

    
119
        /*
120
         * (non-Javadoc)
121
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
122
         */
123
        public void read(String xml) throws ParsingException {
124
                double x = 0, y = 0, rotX = 0, rotY = 0, psX = 0, psY = 0;
125
                double imgW = 0, imgH = 0;
126

    
127
                KXmlParser parser = new KXmlParser();
128
                Reader reader = new StringReader(xml);
129
                try {
130
                        parser.setInput(reader);
131
                } catch (XmlPullParserException e) {
132
                        throw new ParsingException(xml);
133
                }
134
                try {
135
                        int tag = parser.nextTag();
136

    
137
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
138
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
139
                                while(tag != KXmlParser.END_DOCUMENT) {
140
                                        switch(tag) {
141
                                                case KXmlParser.START_TAG:
142
                                                        if (parser.getName().compareTo(MAIN_TAG) == 0) {
143
                                                                x = Double.parseDouble(parserString(parser, X, null));
144
                                                                y = Double.parseDouble(parserString(parser, Y, null));
145
                                                                rotX = Double.parseDouble(parserString(parser, ROTX, null));
146
                                                                rotY = Double.parseDouble(parserString(parser, ROTY, null));
147
                                                                psX = Double.parseDouble(parserString(parser, PSX, null));
148
                                                                psY = Double.parseDouble(parserString(parser, PSY, null));
149
                                                                Double.parseDouble(parserString(parser, W, null));
150
                                                                Double.parseDouble(parserString(parser, H, null));
151
                                                                imgW = Double.parseDouble(parserString(parser, IMGW, null));
152
                                                                imgH = Double.parseDouble(parserString(parser, IMGH, null));
153
                                                        }
154
                                                        break;
155
                                                case KXmlParser.END_TAG:
156
                                                        break;
157
                                                case KXmlParser.TEXT:
158
                                                        break;
159
                                        }
160
                                        tag = parser.next();
161
                                }
162
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
163
                        }
164
                        reader.close();
165
                } catch (XmlPullParserException e) {
166
                        throw new ParsingException(xml);
167
                } catch (IOException e) {
168
                        throw new ParsingException(xml);
169
                }
170

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

    
177
        /*
178
         * (non-Javadoc)
179
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
180
         */
181
        public String write() {
182
                StringBuffer b = new StringBuffer();
183

    
184
                if(dataset != null) {
185
                        at = dataset.getAffineTransform();
186
                        dim = new Point2D.Double(dataset.getWidth(), dataset.getHeight());
187
                }
188

    
189
                b.append("<" + MAIN_TAG + ">\n");
190
                b.append("\t<" + EXTENT + ">\n");
191
                putProperty(b, X, at.getTranslateX(), 3);
192
                putProperty(b, Y, at.getTranslateY(), 3);
193
                putProperty(b, ROTX, at.getShearX(), 3);
194
                putProperty(b, ROTY, at.getShearY(), 3);
195
                double wd = 0;
196
                double hd = 0;
197
                if(dataset != null) {
198
                        wd = (dataset.getExtent().getMax().getX() - dataset.getExtent().getMin().getX());
199
                        hd = (dataset.getExtent().getMax().getY() - dataset.getExtent().getMin().getY());
200
                }
201
                putProperty(b, PSX, at.getScaleX(), 3);
202
                putProperty(b, PSY, at.getScaleY(), 3);
203
                putProperty(b, W, wd, 3);
204
                putProperty(b, H, hd, 3);
205
                b.append("\t</" + EXTENT + ">\n");
206
                b.append("\t<" + DIM + ">\n");
207
                putProperty(b, IMGW, dim.getX(), 3);
208
                putProperty(b, IMGH, dim.getY(), 3);
209
                b.append("\t</" + DIM + ">\n");
210
                b.append("</" + MAIN_TAG + ">\n");
211
                return b.toString();
212
        }
213

    
214
        /*
215
         * (non-Javadoc)
216
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
217
         */
218
        public Object getResult() {
219
                return dataset;
220
        }
221

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