Statistics
| Revision:

root / branches / v1_9_0 / libGPE-GML / src / org / gvsig / gpe / gml / parser / v2 / geometries / LinearRingTypeBinding.java @ 53

History | View | Annotate | Download (5.74 KB)

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

    
3
import java.io.IOException;
4

    
5
import javax.xml.namespace.QName;
6

    
7
import org.gvsig.gpe.gml.parser.GPEDefaultGmlParser;
8
import org.gvsig.gpe.gml.parser.v2.coordinates.CoordTypeIterator;
9
import org.gvsig.gpe.gml.parser.v2.coordinates.CoordinatesTypeIterator;
10
import org.gvsig.gpe.gml.utils.GMLTags;
11
import org.gvsig.gpe.parser.ICoordinateIterator;
12
import org.gvsig.gpe.warnings.PolygonNotClosedWarning;
13
import org.gvsig.gpe.xml.stream.EventType;
14
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
15
import org.gvsig.gpe.xml.stream.XmlStreamException;
16
import org.gvsig.gpe.xml.utils.CompareUtils;
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:LinearRingTypeBinding.java 195 2007-11-26 09:02:22Z jpiera $
61
 * $Log$
62
 * Revision 1.4  2007/05/16 09:29:12  jorpiell
63
 * The polygons has to be closed
64
 *
65
 * Revision 1.3  2007/05/15 11:55:11  jorpiell
66
 * MultiGeometry is now supported
67
 *
68
 * Revision 1.2  2007/05/14 09:31:06  jorpiell
69
 * Add the a new class to compare tags
70
 *
71
 * Revision 1.1  2007/05/07 12:58:42  jorpiell
72
 * Add some methods to manage the multigeometries
73
 *
74
 *
75
 */
76
/**
77
 * It parses a gml:linearRingType object. Example:
78
 * <p>
79
 * <pre>
80
 * <code>
81
 * &lt;LinearRing&gt;
82
 * &lt;coordinates&gt;0.0,0.0 100.0,0.0 50.0,100.0 0.0,0.0&lt;/coordinates&gt;
83
 * &lt;/LinearRing&gt;
84
 * </code>
85
 * </pre>
86
 * </p> 
87
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
88
 */
89
public class LinearRingTypeBinding extends GeometryBinding{
90
        
91
        /**
92
         * It parses the gml:LinearRing tag and return the 
93
         * Object
94
         * @param parser
95
         * The XML parser
96
         * @param handler
97
         * The GPE parser that contains the content handler and
98
         * the error handler
99
         * @return
100
         * A linear ring
101
         * @throws XmlStreamException
102
         * @throws IOException
103
         */
104
        public Object parse(IXmlStreamReader parser,GPEDefaultGmlParser handler) throws XmlStreamException, IOException{
105

    
106
        parser.require(EventType.START_ELEMENT, GMLTags.GML_LINEARRING);
107
        
108
                Object linearRing = null;
109
                                
110
                super.setAtributtes(parser, handler.getErrorHandler());
111
                        
112
                ICoordinateIterator coordinatesIteartor = parseCoordinates(parser, handler);        
113
                
114
                linearRing = handler.getContentHandler().startLinearRing(id, coordinatesIteartor, srsName);
115
                handler.getContentHandler().endLinearRing(linearRing);
116
                
117
                return linearRing;
118
        }
119
        
120
        /**
121
         * It parses the gml:LinearRing tag and return the 
122
         * coordinates
123
         * @param parser
124
         * The XML parser
125
         * @param handler
126
         * The GPE parser that contains the content handler and
127
         * the error handler
128
         * @return
129
         * An array of coordinates
130
         * @throws XmlStreamException
131
         * @throws IOException
132
         * @throws PolygonNotClosedWarning 
133
         */
134
        public ICoordinateIterator parseCoordinates(IXmlStreamReader parser,GPEDefaultGmlParser handler) throws XmlStreamException, IOException {
135
                boolean endFeature = false;
136
                EventType currentTag;
137
                
138
                QName tag = parser.getElementName();
139
                currentTag = parser.getEventType();
140

    
141
                while (!endFeature){
142
                        switch(currentTag.getCode()){
143
                        case EventType.START_ELEMENT_CODE:
144
                                ICoordinateIterator iterator = parseTag_(parser, handler, tag);
145
                                if (iterator != null){
146
                                        return iterator;
147
                                }
148
                        case EventType.END_ELEMENT_CODE:
149
                                if (CompareUtils.compareWithNamespace(tag,GMLTags.GML_LINEARRING)){                                                
150
                                                                
151
                                }
152
                                break;
153
                        }
154
                        if (!endFeature){                                        
155
                                currentTag = parser.nextTag();
156
                                tag = parser.getElementName();
157
                        }
158
                }                        
159
                //The first and the last coordinates has to be the same
160
//                if ((coordinates[0][0] != coordinates[0][coordinates[0].length - 1]) ||
161
//                                (coordinates[1][0] != coordinates[1][coordinates[1].length - 1]) ||
162
//                                (coordinates[2][0] != coordinates[2][coordinates[2].length - 1])){
163
//                        handler.getErrorHandler().addWarning(new PolygonNotClosedWarning(coordinates));
164
//                }
165
                return null;
166
        }
167
        
168
        /**
169
         * 
170
         * @param parser
171
         * @param handler
172
         * @param tag
173
         * @param coordsContainer
174
         * @return
175
         * @throws XmlStreamException
176
         * @throws IOException
177
         */
178
        protected ICoordinateIterator parseTag_(IXmlStreamReader parser,GPEDefaultGmlParser handler, QName tag) throws XmlStreamException, IOException{
179
                if (CompareUtils.compareWithNamespace(tag,GMLTags.GML_COORDINATES)){
180
                        CoordinatesTypeIterator coordinatesIterator = handler.getProfile().getCoordinatesTypeBinding();
181
                        coordinatesIterator.initialize(parser, handler,GMLTags.GML_LINEARRING);
182
                        return coordinatesIterator;
183
                }else if (CompareUtils.compareWithNamespace(tag,GMLTags.GML_COORD)){
184
                        CoordTypeIterator coordinatesIterator = handler.getProfile().getCoordTypeBinding();
185
                        coordinatesIterator.initialize(parser, handler,GMLTags.GML_LINEARRING);
186
                        return coordinatesIterator;
187
                }
188
                return null;
189
        }
190
        
191
        
192
}