Statistics
| Revision:

root / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / gml / parser / v2 / geometries / CoordinatesTypeBinding.java @ 18773

History | View | Annotate | Download (4.9 KB)

1
package org.gvsig.gpe.gml.parser.v2.geometries;
2

    
3
import java.io.IOException;
4

    
5
import org.gvsig.gpe.gml.GMLTags;
6
import org.gvsig.gpe.gml.GPEDefaultGmlParser;
7
import org.gvsig.gpe.gml.utils.CompareUtils;
8
import org.kxml2.io.KXmlParser;
9
import org.xmlpull.v1.XmlPullParser;
10
import org.xmlpull.v1.XmlPullParserException;
11

    
12
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
13
 *
14
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
29
 *
30
 * For more information, contact:
31
 *
32
 *  Generalitat Valenciana
33
 *   Conselleria d'Infraestructures i Transport
34
 *   Av. Blasco Ib??ez, 50
35
 *   46010 VALENCIA
36
 *   SPAIN
37
 *
38
 *      +34 963862235
39
 *   gvsig@gva.es
40
 *      www.gvsig.gva.es
41
 *
42
 *    or
43
 *
44
 *   IVER T.I. S.A
45
 *   Salamanca 50
46
 *   46005 Valencia
47
 *   Spain
48
 *
49
 *   +34 963163400
50
 *   dac@iver.es
51
 */
52
/* CVS MESSAGES:
53
*
54
* $Id:CoordinatesTypeBinding.java 371 2008-01-10 09:30:19Z jpiera $
55
* $Log$
56
* Revision 1.3  2007/05/14 09:52:53  jorpiell
57
* Nullpointer exception fixed
58
*
59
* Revision 1.2  2007/05/14 09:31:06  jorpiell
60
* Add the a new class to compare tags
61
*
62
* Revision 1.1  2007/05/07 12:58:42  jorpiell
63
* Add some methods to manage the multigeometries
64
*
65
* Revision 1.1  2007/05/07 07:06:46  jorpiell
66
* Add a constructor with the name and the description fields
67
*
68
*
69
*/
70
/**
71
 * It parses a gml:CoordinatesType object. Example:
72
 * <p>
73
 * <pre>
74
 * <code>
75
 * &lt;coordinates&gt;60.0,60.0 60.0,90.0 90.0,90.0&lt;/coordinates&gt;
76
 * </code>
77
 * </pre>
78
 * </p> 
79
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
80
 */
81
public class CoordinatesTypeBinding {
82
        
83
        /**
84
         * It parses the gml:coordinates tag
85
         * @param parser
86
         * The XML parser
87
         * @param handler
88
         * The GPE parser that contains the content handler and
89
         * the error handler
90
         * @return
91
         * It retuns a matrix of doubles with 3 columns (x,y,z) and
92
         * one row for each coordinate.
93
         * @throws XmlPullParserException
94
         * @throws IOException
95
         */
96
        public double[][] parse(XmlPullParser parser,GPEDefaultGmlParser handler) throws XmlPullParserException, IOException  {
97
                boolean endFeature = false;
98
                int currentTag;
99
                
100
                String TUPLES_SEPARATOR  = GMLTags.GML_DEFAULT_TUPLES_SEPARATOR;
101
                String COORDINATES_SEPARATOR = GMLTags.GML_DEFAULT_COORDINATES_SEPARATOR;
102
                String COORDINATES_DECIMAL = GMLTags.GML_DEFAULT_COORDINATES_DECIMAL;                
103
                
104
                double[][] aCoordinates = null;
105
                
106
                for (int i=0 ; i<parser.getAttributeCount() ; i++){
107
                        if (parser.getAttributeName(i).compareTo(GMLTags.GML_COORDINATES_DECIMAL) == 0){
108
                                COORDINATES_DECIMAL = parser.getAttributeValue(i);
109
                        }else if (parser.getAttributeName(i).compareTo(GMLTags.GML_COORDINATES_CS) == 0){
110
                                COORDINATES_SEPARATOR = parser.getAttributeValue(i);
111
                        }else if (parser.getAttributeName(i).compareTo(GMLTags.GML_COORDINATES_TS) == 0){
112
                                TUPLES_SEPARATOR = parser.getAttributeValue(i);
113
                        }
114
                }                
115

    
116
                String tag = parser.getName();
117
                currentTag = parser.getEventType();
118

    
119
                while (!endFeature){
120
                        switch(currentTag){
121
                        case KXmlParser.START_TAG:
122
                                if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_COORDINATES)){
123
                                        parser.next();                                                
124
                                        String[] coordinates = org.gvsig.gpe.gml.utils.StringUtils.splitString(parser.getText().trim(),TUPLES_SEPARATOR);
125
                                        //String[] coordinates = parser.getText().trim().split(TUPLES_SEPARATOR);
126
                                        aCoordinates = new double[3][coordinates.length];
127
                                        for (int i=0 ; i<coordinates.length ; i++){                                        
128
                                                String[] coordinate = org.gvsig.gpe.gml.utils.StringUtils.splitString(coordinates[i].trim(),COORDINATES_SEPARATOR);
129
                                                //String[] coordinate = coordinates[i].trim().split(COORDINATES_SEPARATOR);
130
                                                aCoordinates[0][i] = DoubleTypeBinding.parse(coordinate[0], COORDINATES_DECIMAL);
131
                                                aCoordinates[1][i] = DoubleTypeBinding.parse(coordinate[1], COORDINATES_DECIMAL);
132
                                                if (coordinate.length == 3){
133
                                                        aCoordinates[2][i] = DoubleTypeBinding.parse(coordinate[2], COORDINATES_DECIMAL);
134
                                                }else{
135
                                                        aCoordinates[2][i] = 0.0;
136
                                                }
137
                                        }                                        
138
                                }
139
                                break;
140
                        case KXmlParser.END_TAG:
141
                                if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_COORDINATES)){                                                
142
                                        endFeature = true;
143
                                }
144
                                break;
145
                        case KXmlParser.TEXT:                                        
146

    
147
                                break;
148
                        }
149
                        if (!endFeature){                                        
150
                                currentTag = parser.next();
151
                                tag = parser.getName();
152
                        }
153
                }                        
154

    
155
                return aCoordinates;        
156
        }
157

    
158
}