Statistics
| Revision:

root / branches / v10 / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / utils / GMLUtilsParser.java @ 12215

History | View | Annotate | Download (10.6 KB)

1
package org.gvsig.remoteClient.gml.utils;
2

    
3
import java.awt.geom.Point2D;
4
import java.awt.geom.Rectangle2D;
5
import java.io.IOException;
6

    
7
import org.gvsig.remoteClient.gml.GMLTags;
8
import org.gvsig.remoteClient.gml.GMLFeaturesParser.Extent;
9
import org.gvsig.remoteClient.gml.exceptions.BaseException;
10
import org.gvsig.remoteClient.gml.exceptions.GMLFileReadException;
11
import org.gvsig.remoteClient.gml.exceptions.GMLNoGeometryException;
12
import org.gvsig.remoteClient.gml.exceptions.GMLParserException;
13
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
14
import org.gvsig.remoteClient.gml.schemas.XMLSchemaParser;
15
import org.kxml2.io.KXmlParser;
16
import org.xmlpull.v1.XmlPullParserException;
17

    
18
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
19
 *
20
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
21
 *
22
 * This program is free software; you can redistribute it and/or
23
 * modify it under the terms of the GNU General Public License
24
 * as published by the Free Software Foundation; either version 2
25
 * of the License, or (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
35
 *
36
 * For more information, contact:
37
 *
38
 *  Generalitat Valenciana
39
 *   Conselleria d'Infraestructures i Transport
40
 *   Av. Blasco Ib??ez, 50
41
 *   46010 VALENCIA
42
 *   SPAIN
43
 *
44
 *      +34 963862235
45
 *   gvsig@gva.es
46
 *      www.gvsig.gva.es
47
 *
48
 *    or
49
 *
50
 *   IVER T.I. S.A
51
 *   Salamanca 50
52
 *   46005 Valencia
53
 *   Spain
54
 *
55
 *   +34 963163400
56
 *   dac@iver.es
57
 */
58
/* CVS MESSAGES:
59
 *
60
 * $Id: GMLUtilsParser.java 12215 2007-06-19 09:44:46Z jorpiell $
61
 * $Log$
62
 * Revision 1.1.2.3  2007-06-19 09:44:46  jorpiell
63
 * It always uses geotools to parse the geometries
64
 *
65
 * Revision 1.1.2.2  2007/02/23 07:57:27  jorpiell
66
 * Capacidad de parsear el BBOX que tenga alg?n problema
67
 *
68
 * Revision 1.1.2.1  2007/01/25 16:12:59  jorpiell
69
 * Se han sustituido las clases por las que hay en el nuevo driver de GML.
70
 *
71
 * Revision 1.3  2007/01/15 13:11:00  csanchez
72
 * Sistema de Warnings y Excepciones adaptado a BasicException
73
 *
74
 * Revision 1.2  2006/12/22 11:25:44  csanchez
75
 * Nuevo parser GML 2.x para gml's sin esquema
76
 *
77
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
78
 * Primer commit del driver de Gml
79
 *
80
 *
81
 */
82
/**
83
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
84
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
85
 */
86
public class GMLUtilsParser {
87

    
88
        /**
89
         * Parses the boundedBy GML tag
90
         * @throws IOException 
91
         * @throws XmlPullParserException 
92
         *
93
         */
94
        public static Extent parseBoundedBy(XMLSchemaParser parser) throws XmlPullParserException, IOException {
95
                int currentTag;
96
                boolean end = false;
97
                Rectangle2D extent = null;        
98
                String srs = null;
99
                
100
                currentTag = parser.getEventType();        
101
                
102
                while (!end){
103
                        switch(currentTag){
104
                        case XMLSchemaParser.START_TAG:
105
                                if (parser.getName().compareTo(GMLTags.GML_BOX)==0){
106
                                        for (int i=0 ; i<parser.getAttributeCount() ;i++){
107
                                                if (parser.getAttributeName(i).compareTo(GMLTags.GML_SRS_NAME) == 0){
108
                                                        srs = parser.getAttributeValue(i);
109
                                                }
110
                                        }
111
                                        extent = parseBox(parser);                                        
112
                                }   
113
                                break;
114
                        case XMLSchemaParser.END_TAG:
115
                                if (parser.getName().compareTo(GMLTags.GML_BOUNDEDBY) == 0)
116
                                        end = true;
117
                                break;
118
                        case XMLSchemaParser.TEXT:                   
119
                                break;
120
                        }
121
                        if (!end){
122
                                currentTag = parser.next();
123
                        }
124
                }        
125
                return new Extent(extent,srs);
126
        }
127
        
128
        /**
129
         * Parses the box GML tag
130
         * @throws IOException 
131
         * @throws XmlPullParserException 
132
         *
133
         */
134
        private static Rectangle2D parseBox(XMLSchemaParser parser) throws XmlPullParserException, IOException {
135
                int currentTag;
136
                boolean end = false;
137
                Point2D ul = null;
138
                Point2D br = null;                
139
                Rectangle2D extent = null;
140
                
141
                currentTag = parser.getEventType();        
142
                
143
                while (!end){
144
                        switch(currentTag){
145
                        case XMLSchemaParser.START_TAG:
146
                                if (parser.getName().compareTo(GMLTags.GML_COORDINATES)==0){
147
                                                extent = parseCoordinates(parser);
148
                                }else if(parser.getName().compareTo(GMLTags.GML_COORD)==0){
149
                                        if (ul == null){
150
                                                ul = parsePoint(parser);
151
                                        }else{
152
                                                br = parsePoint(parser);
153
                                        }
154
                                }   
155
                                break;
156
                        case XMLSchemaParser.END_TAG:
157
                                if (parser.getName().compareTo(GMLTags.GML_BOX) == 0)
158
                                        end = true;
159
                                break;
160
                        case XMLSchemaParser.TEXT:                   
161
                                break;
162
                        }
163
                        if (!end){
164
                                currentTag = parser.next();
165
                        }
166
                }        
167
                if (extent == null){
168
                        return new Rectangle2D.Double(ul.getX(),ul.getY(),br.getX(),br.getY());
169
                }else{
170
                        return extent;
171
                }
172
        }
173

    
174
        /**
175
         * Parses the coordinates GML tag
176
         * @throws IOException 
177
         * @throws XmlPullParserException 
178
         *
179
         */
180
        private static Rectangle2D parseCoordinates(XMLSchemaParser parser) throws XmlPullParserException, IOException {
181
                parser.next();
182
                
183
                for (int i=0 ; i<parser.getAttributeCount() ; i++){
184
                    if (parser.getAttributeName(i).compareTo(GMLTags.GML_COORDINATES_DECIMAL) == 0){
185
                            
186
                    }else if (parser.getAttributeName(i).compareTo(GMLTags.GML_COORDINATES_CS) == 0){
187
                            
188
                    }else if (parser.getAttributeName(i).compareTo(GMLTags.GML_COORDINATES_TS) == 0){
189
                            
190
                    }
191
            }                        
192
                
193
                String[] corners = parser.getText().trim().split(" ");
194
                String[] ul = corners[0].split(",");
195
                String[] br = corners[1].split(",");
196
                
197
                
198
                return new Rectangle2D.Double(Double.parseDouble(ul[0]),
199
                                Double.parseDouble(ul[1]),
200
                                Double.parseDouble(br[0]),
201
                                Double.parseDouble(br[1]));
202
                
203
        }
204
        
205
        /**
206
         * Parses the point GML tag
207
         * @throws IOException 
208
         * @throws XmlPullParserException 
209
         *
210
         */
211
        private static Point2D parsePoint(XMLSchemaParser parser) throws XmlPullParserException, IOException {
212
                int currentTag;
213
                boolean end = false;                
214
                String x = "0";
215
                String y = "0";
216
                
217
                currentTag = parser.getEventType();                
218
                
219
                while (!end){
220
                        switch(currentTag){
221
                        case XMLSchemaParser.START_TAG:
222
                                if (parser.getName().compareTo(GMLTags.GML_COORDINATES)==0){
223
                                        parseCoordinates(parser);                                        
224
                                }
225
                                if (parser.getName().compareTo(GMLTags.GML_X) == 0){
226
                                        parser.next();
227
                                        x = parser.getText();
228
                                }else if (parser.getName().compareTo(GMLTags.GML_Y) == 0){
229
                                        parser.next();
230
                                        y = parser.getText();
231
                                        end = true;
232
                                }
233
                                break;
234
                        case XMLSchemaParser.END_TAG:
235
                                break;
236
                        case XMLSchemaParser.TEXT:                   
237
                                break;
238
                        }
239
                        if (!end){
240
                                currentTag = parser.next();
241
                        }
242
                }        
243
                return new Point2D.Double(Double.parseDouble(x),Double.parseDouble(y));
244
        }
245
        
246
        /**
247
         * Parses a geometry GML tag
248
         * @throws IOException 
249
         * @throws XmlPullParserException 
250
         *
251
         */
252
        public static Object parseGeometry(XMLSchemaParser parser,String gmGeometryTag,IGeometriesFactory factory) throws BaseException{
253
                int currentTag;
254
                boolean end = false;                
255
                
256
                try{                                
257
                        currentTag = parser.getEventType();
258
                        while (!end){                        
259
                                switch(currentTag){
260
                                case KXmlParser.START_TAG:
261
                                        return factory.createGeometry(getGeometryInGML(parser,parser.getName()));                                        
262
                                case KXmlParser.END_TAG:
263
                                        if ((parser.getName().compareTo(gmGeometryTag) == 0))
264
                                                end = true;
265
                                        break;
266
                                case KXmlParser.TEXT:                   
267
                                        break;
268
                                }
269
                                if (!end){
270
                                        currentTag = parser.next();
271
                                }        
272
                        }
273
                }catch (XmlPullParserException e) {
274
                        // Captured KXML parsing Exception
275
                        throw new GMLParserException(e);
276
                } catch (IOException e) {
277
                        // TODO Auto-generated catch block
278
                        throw new GMLFileReadException(e);
279
                }                                         
280
                return null;
281
        }
282
        
283
        /**
284
         * Parses the point GML tag
285
         * @throws IOException 
286
         * @throws XmlPullParserException 
287
         *
288
         */
289
        private static Point2D parsePoint2D(XMLSchemaParser parser) throws BaseException{
290
                int currentTag;
291
                boolean end = false;                
292
                
293
                try{                                
294
                        currentTag = parser.getEventType();                                        
295
                        while (!end){                        
296
                                switch(currentTag){
297
                                case KXmlParser.START_TAG:
298
                                        if ((parser.getName().compareTo(GMLTags.GML_COORDINATES) == 0)){
299
                                                parser.next();
300
                                                String sCoordinates = parser.getText();
301
                                                String[] coordinates = sCoordinates.split(",");
302
                                                return new Point2D.Double(Double.parseDouble(coordinates[0]),
303
                                                                Double.parseDouble(coordinates[1]));
304
                                        }
305
                                        break;
306
                                case KXmlParser.END_TAG:
307
                                        if ((parser.getName().compareTo(GMLTags.GML_POINT) == 0))
308
                                                end = true;
309
                                        break;
310
                                case KXmlParser.TEXT:                   
311
                                        break;
312
                                }
313
                                if (!end){
314
                                        currentTag = parser.next();
315
                                }        
316
                        }
317
                }catch (XmlPullParserException e) {
318
                        //Captured KXML parsing Exception
319
                        throw new GMLParserException(e);
320
                } catch (IOException e) {
321
                        // TODO Auto-generated catch block
322
                        throw new GMLFileReadException(e);
323
                }                                         
324
                return null;        
325
        }
326
        
327
        /**
328
         * Parses the geometry GML tag
329
         * @return
330
         * The geometry in GML
331
         * @throws IOException 
332
         * @throws XmlPullParserException 
333
         *
334
         */
335
        private static String getGeometryInGML(XMLSchemaParser parser,String endGeometryTag) throws BaseException{
336
                int currentTag;
337
                boolean end = false;
338
                StringBuffer gmlString = new StringBuffer();
339
                
340
                try{                
341
                        currentTag = parser.getEventType();                        
342
                        while (!end){                        
343
                                switch(currentTag){
344
                                case KXmlParser.START_TAG:
345
                                        if (parser.getName().compareTo("geometryMember")==0){
346
                                                //Esta etiqueta de gml que nos indica donde empieza y donde acaba un subelemento
347
                                                //de una geometria compleja, como un multipoligono no la admite el parser de Geotools
348
                                                //Por tanto la debemos de ignorar
349
                                                break;
350
                                        }
351
                                        gmlString.append("<" + parser.getName());
352
                                        for (int i=0 ; i<parser.getAttributeCount() ; i++){
353
                                                gmlString.append(" " + parser.getAttributeName(i) + 
354
                                                                "=\"" + parser.getAttributeValue(i) + "\"");
355
                                        }
356
                                        gmlString.append(">");
357
                                        break;
358
                                case KXmlParser.END_TAG:
359
                                        if (parser.getName().compareTo("geometryMember")==0){
360
                                                //Esta etiqueta de gml que nos indica donde empieza y donde acaba un subelemento
361
                                                //de una geometria compleja, como un multipoligono no la admite el parser de Geotools
362
                                                //Por tanto la debemos de ignorar
363
                                                break;
364
                                        }
365
                                        if ((parser.getName().compareTo(endGeometryTag) == 0)){
366
                                                end = true;
367
                                        }
368
                                        gmlString.append("</" + parser.getName() + ">");
369
                                        break;
370
                                case KXmlParser.TEXT:                 
371
                                        gmlString.append(parser.getText());
372
                                        break;
373
                                }
374
                                if (!end){
375
                                        currentTag = parser.next();
376
                                }        
377
                        }
378
                }catch (XmlPullParserException e) {
379
                        //Captured KXML parsing Exception
380
                        throw new GMLParserException(e);
381
                } catch (IOException e) {
382
                        throw new GMLFileReadException(e);
383
                }                                                         
384
                return gmlString.toString();        
385
        }                
386
        
387
}