svn-gvsig-desktop / branches / v10 / libraries / libGPE-GML / src / org / gvsig / gpe / gml / parser / v2 / geometries / LinearRingTypeBinding.java @ 18882
History | View | Annotate | Download (5.67 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.gvsig.gpe.gml.utils.CoordsContainer; |
9 |
import org.gvsig.gpe.warnings.PolygonNotClosedWarning; |
10 |
import org.xmlpull.v1.XmlPullParser; |
11 |
import org.xmlpull.v1.XmlPullParserException; |
12 |
|
13 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
14 |
*
|
15 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
16 |
*
|
17 |
* This program is free software; you can redistribute it and/or
|
18 |
* modify it under the terms of the GNU General Public License
|
19 |
* as published by the Free Software Foundation; either version 2
|
20 |
* of the License, or (at your option) any later version.
|
21 |
*
|
22 |
* This program is distributed in the hope that it will be useful,
|
23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
25 |
* GNU General Public License for more details.
|
26 |
*
|
27 |
* You should have received a copy of the GNU General Public License
|
28 |
* along with this program; if not, write to the Free Software
|
29 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
30 |
*
|
31 |
* For more information, contact:
|
32 |
*
|
33 |
* Generalitat Valenciana
|
34 |
* Conselleria d'Infraestructures i Transport
|
35 |
* Av. Blasco Ib??ez, 50
|
36 |
* 46010 VALENCIA
|
37 |
* SPAIN
|
38 |
*
|
39 |
* +34 963862235
|
40 |
* gvsig@gva.es
|
41 |
* www.gvsig.gva.es
|
42 |
*
|
43 |
* or
|
44 |
*
|
45 |
* IVER T.I. S.A
|
46 |
* Salamanca 50
|
47 |
* 46005 Valencia
|
48 |
* Spain
|
49 |
*
|
50 |
* +34 963163400
|
51 |
* dac@iver.es
|
52 |
*/
|
53 |
/* CVS MESSAGES:
|
54 |
*
|
55 |
* $Id:LinearRingTypeBinding.java 195 2007-11-26 09:02:22Z jpiera $
|
56 |
* $Log$
|
57 |
* Revision 1.4 2007/05/16 09:29:12 jorpiell
|
58 |
* The polygons has to be closed
|
59 |
*
|
60 |
* Revision 1.3 2007/05/15 11:55:11 jorpiell
|
61 |
* MultiGeometry is now supported
|
62 |
*
|
63 |
* Revision 1.2 2007/05/14 09:31:06 jorpiell
|
64 |
* Add the a new class to compare tags
|
65 |
*
|
66 |
* Revision 1.1 2007/05/07 12:58:42 jorpiell
|
67 |
* Add some methods to manage the multigeometries
|
68 |
*
|
69 |
*
|
70 |
*/
|
71 |
/**
|
72 |
* It parses a gml:linearRingType object. Example:
|
73 |
* <p>
|
74 |
* <pre>
|
75 |
* <code>
|
76 |
* <LinearRing>
|
77 |
* <coordinates>0.0,0.0 100.0,0.0 50.0,100.0 0.0,0.0</coordinates>
|
78 |
* </LinearRing>
|
79 |
* </code>
|
80 |
* </pre>
|
81 |
* </p>
|
82 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
83 |
*/
|
84 |
public class LinearRingTypeBinding extends GeometryBinding{ |
85 |
protected CoordsContainer coordsContainer = null; |
86 |
|
87 |
/**
|
88 |
* It parses the gml:LinearRing tag and return the
|
89 |
* Object
|
90 |
* @param parser
|
91 |
* The XML parser
|
92 |
* @param handler
|
93 |
* The GPE parser that contains the content handler and
|
94 |
* the error handler
|
95 |
* @return
|
96 |
* A linear ring
|
97 |
* @throws XmlPullParserException
|
98 |
* @throws IOException
|
99 |
*/
|
100 |
public Object parse(XmlPullParser parser,GPEDefaultGmlParser handler) throws XmlPullParserException, IOException{ |
101 |
Object linearRing = null; |
102 |
coordsContainer = null;
|
103 |
|
104 |
super.setAtributtes(parser, handler.getErrorHandler());
|
105 |
|
106 |
double[][] coordinates; |
107 |
|
108 |
coordinates = parseCoordinates(parser, handler); |
109 |
|
110 |
linearRing = handler.getContentHandler().startLinearRing(id, coordinates[0], coordinates[1], coordinates[2], srsName); |
111 |
handler.getContentHandler().endLinearRing(linearRing); |
112 |
|
113 |
return linearRing;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* It parses the gml:LinearRing tag and return the
|
118 |
* coordinates
|
119 |
* @param parser
|
120 |
* The XML parser
|
121 |
* @param handler
|
122 |
* The GPE parser that contains the content handler and
|
123 |
* the error handler
|
124 |
* @return
|
125 |
* An array of coordinates
|
126 |
* @throws XmlPullParserException
|
127 |
* @throws IOException
|
128 |
* @throws PolygonNotClosedWarning
|
129 |
*/
|
130 |
public double[][] parseCoordinates(XmlPullParser parser,GPEDefaultGmlParser handler) throws XmlPullParserException, IOException { |
131 |
boolean endFeature = false; |
132 |
int currentTag;
|
133 |
coordsContainer = null;
|
134 |
double[][] coordinates = null; |
135 |
|
136 |
|
137 |
String tag = parser.getName();
|
138 |
currentTag = parser.getEventType(); |
139 |
|
140 |
while (!endFeature){
|
141 |
switch(currentTag){
|
142 |
case XmlPullParser.START_TAG:
|
143 |
coordinates = parseTag_(parser, handler, tag); |
144 |
break;
|
145 |
case XmlPullParser.END_TAG:
|
146 |
if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_LINEARRING)){
|
147 |
endFeature = true;
|
148 |
//If the file contains the COORD tag instead of the
|
149 |
//coordinates
|
150 |
if (coordsContainer != null){ |
151 |
coordinates = coordsContainer.getCoordinates(); |
152 |
} |
153 |
} |
154 |
break;
|
155 |
case XmlPullParser.TEXT:
|
156 |
|
157 |
break;
|
158 |
} |
159 |
if (!endFeature){
|
160 |
currentTag = parser.next(); |
161 |
tag = parser.getName(); |
162 |
} |
163 |
} |
164 |
//The first and the last coordinates has to be the same
|
165 |
if ((coordinates[0][0] != coordinates[0][coordinates[0].length - 1]) || |
166 |
(coordinates[1][0] != coordinates[1][coordinates[1].length - 1]) || |
167 |
(coordinates[2][0] != coordinates[2][coordinates[2].length - 1])){ |
168 |
handler.getErrorHandler().addWarning(new PolygonNotClosedWarning(coordinates));
|
169 |
} |
170 |
return coordinates;
|
171 |
} |
172 |
|
173 |
/**
|
174 |
*
|
175 |
* @param parser
|
176 |
* @param handler
|
177 |
* @param tag
|
178 |
* @param coordsContainer
|
179 |
* @return
|
180 |
* @throws XmlPullParserException
|
181 |
* @throws IOException
|
182 |
*/
|
183 |
protected double[][] parseTag_(XmlPullParser parser,GPEDefaultGmlParser handler, String tag) throws XmlPullParserException, IOException{ |
184 |
double[][] coordinates = null; |
185 |
if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_COORDINATES)){
|
186 |
coordinates = handler.getProfile().getCoordinatesTypeBinding(). |
187 |
parse(parser, handler); |
188 |
}else if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_COORD)){ |
189 |
double[] coord = handler.getProfile().getCoordTypeBinding(). |
190 |
parse(parser, handler); |
191 |
if (coordsContainer == null){ |
192 |
coordsContainer = new CoordsContainer();
|
193 |
} |
194 |
coordsContainer.addCoordinates(coord); |
195 |
} |
196 |
return coordinates;
|
197 |
} |
198 |
|
199 |
|
200 |
} |