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 | 12383 | nacho | /* 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 | 12557 | nacho | import java.awt.geom.AffineTransform; |
22 | 12793 | nacho | import java.awt.geom.Point2D; |
23 | 12383 | nacho | import java.io.IOException; |
24 | import java.io.Reader; |
||
25 | import java.io.StringReader; |
||
26 | |||
27 | import org.gvsig.raster.dataset.RasterDataset; |
||
28 | 28498 | nbrodin | import org.gvsig.raster.dataset.rmf.ClassSerializer; |
29 | import org.gvsig.raster.dataset.rmf.ParsingException; |
||
30 | 26873 | vcaballero | import org.gvsig.tools.ToolsLocator; |
31 | import org.gvsig.tools.extensionpoint.ExtensionPoint; |
||
32 | import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
||
33 | 12383 | nacho | 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 | 26873 | vcaballero | * Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y
|
39 | 12383 | nacho | * 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 | 26873 | vcaballero | |
69 | 12383 | nacho | //TAGS
|
70 | public static final String MAIN_TAG = "FLyrGeoRaster"; |
||
71 | 22295 | bsanchez | 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 | 12383 | nacho | |
84 | 22295 | bsanchez | private RasterDataset dataset = null; |
85 | private AffineTransform at = null; |
||
86 | private Point2D dim = null; |
||
87 | |||
88 | 12383 | nacho | /**
|
89 | 22295 | bsanchez | * Registra GeoInfoRmfSerializer en los puntos de extension de Serializer
|
90 | */
|
||
91 | public static void register() { |
||
92 | 26873 | vcaballero | ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager(); |
93 | ExtensionPoint point=extensionPoints.get("Serializer");
|
||
94 | point.append("GeoInfo", "", GeoInfoRmfSerializer.class); |
||
95 | 22295 | bsanchez | } |
96 | 26873 | vcaballero | |
97 | 22295 | bsanchez | /**
|
98 | 12383 | nacho | * 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 | 12793 | nacho | |
105 | /**
|
||
106 | 26873 | vcaballero | * Constructor.
|
107 | 12793 | nacho | */
|
108 | public GeoInfoRmfSerializer(AffineTransform at, Point2D dim) { |
||
109 | this.at = at;
|
||
110 | this.dim = dim;
|
||
111 | } |
||
112 | 26873 | vcaballero | |
113 | 12383 | nacho | /**
|
114 | 26873 | vcaballero | * Constructor.
|
115 | 12383 | nacho | */
|
116 | public GeoInfoRmfSerializer() {
|
||
117 | } |
||
118 | 26873 | vcaballero | |
119 | 12383 | nacho | /*
|
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 | 22295 | bsanchez | double imgW = 0, imgH = 0; |
126 | 26873 | vcaballero | |
127 | 12383 | nacho | 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 | 26873 | vcaballero | |
137 | if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
|
||
138 | parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
||
139 | 12383 | nacho | 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 | 22295 | bsanchez | Double.parseDouble(parserString(parser, W, null)); |
150 | Double.parseDouble(parserString(parser, H, null)); |
||
151 | 12383 | nacho | imgW = Double.parseDouble(parserString(parser, IMGW, null)); |
152 | imgH = Double.parseDouble(parserString(parser, IMGH, null)); |
||
153 | } |
||
154 | break;
|
||
155 | 26873 | vcaballero | case KXmlParser.END_TAG:
|
156 | 12383 | nacho | break;
|
157 | 26873 | vcaballero | case KXmlParser.TEXT:
|
158 | 12383 | nacho | break;
|
159 | } |
||
160 | tag = parser.next(); |
||
161 | } |
||
162 | parser.require(KXmlParser.END_DOCUMENT, null, null); |
||
163 | } |
||
164 | 31390 | nbrodin | reader.close(); |
165 | 12383 | nacho | } catch (XmlPullParserException e) {
|
166 | throw new ParsingException(xml); |
||
167 | } catch (IOException e) { |
||
168 | throw new ParsingException(xml); |
||
169 | } |
||
170 | 22295 | bsanchez | |
171 | 12793 | nacho | dim = new Point2D.Double(imgW, imgH); |
172 | at = new AffineTransform(psX, rotY, rotX, psY, x, y); |
||
173 | 22295 | bsanchez | if (dataset != null) |
174 | 12793 | nacho | dataset.setAffineTransform(at); |
175 | 12383 | nacho | } |
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 | 26873 | vcaballero | |
184 | 12793 | nacho | if(dataset != null) { |
185 | at = dataset.getAffineTransform(); |
||
186 | dim = new Point2D.Double(dataset.getWidth(), dataset.getHeight()); |
||
187 | } |
||
188 | 26873 | vcaballero | |
189 | 12383 | nacho | b.append("<" + MAIN_TAG + ">\n"); |
190 | b.append("\t<" + EXTENT + ">\n"); |
||
191 | 12793 | nacho | 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 | 12410 | nacho | putProperty(b, W, wd, 3);
|
204 | putProperty(b, H, hd, 3);
|
||
205 | 12383 | nacho | b.append("\t</" + EXTENT + ">\n"); |
206 | b.append("\t<" + DIM + ">\n"); |
||
207 | 12793 | nacho | putProperty(b, IMGW, dim.getX(), 3);
|
208 | putProperty(b, IMGH, dim.getY(), 3);
|
||
209 | 12383 | nacho | b.append("\t</" + DIM + ">\n"); |
210 | 12511 | nacho | b.append("</" + MAIN_TAG + ">\n"); |
211 | 12383 | nacho | return b.toString();
|
212 | } |
||
213 | 26873 | vcaballero | |
214 | 12383 | nacho | /*
|
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 | } |