Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libGPE-KML / src / org / gvsig / gpe / kml / parser / v21 / geometries / LinearRingBinding.java @ 18284

History | View | Annotate | Download (4.04 KB)

1
package org.gvsig.gpe.kml.parser.v21.geometries;
2

    
3
import java.io.IOException;
4

    
5
import org.gvsig.gpe.kml.GPEDeafultKmlParser;
6
import org.gvsig.gpe.kml.KmlTags;
7
import org.gvsig.gpe.warnings.PolygonNotClosedWarning;
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:LinearRingBinding.java 357 2008-01-09 17:50:08Z jpiera $
55
 * $Log$
56
 * Revision 1.3  2007/06/07 14:53:59  jorpiell
57
 * Add the schema support
58
 *
59
 * Revision 1.2  2007/05/16 09:30:09  jorpiell
60
 * the writting methods has to have the errorHandler argument
61
 *
62
 * Revision 1.1  2007/05/11 07:06:29  jorpiell
63
 * Refactoring of some package names
64
 *
65
 * Revision 1.3  2007/05/08 08:22:37  jorpiell
66
 * Add comments to create javadocs
67
 *
68
 * Revision 1.2  2007/04/14 16:08:07  jorpiell
69
 * Kml writing support added
70
 *
71
 * Revision 1.1  2007/04/13 13:16:21  jorpiell
72
 * Add KML reading support
73
 *
74
 *
75
 */
76
/**
77
 * It parses the linearRing KML tag. 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
 * @see http://code.google.com/apis/kml/documentation/kml_tags_21.html#linearring
89
 */
90
public class LinearRingBinding {
91

    
92
        /**
93
         * It parses the linearRing tag
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
         * It retuns a matrix of doubles with 3 columns (x,y,z) and
101
         * one row for each coordinate.
102
         * @throws IOException 
103
         * @throws XmlPullParserException 
104
         * @throws XmlPullParserException
105
         * @throws IOException
106
         */
107
        public double[][] parse(XmlPullParser parser,GPEDeafultKmlParser handler) throws XmlPullParserException, IOException {
108
                boolean endFeature = false;
109
                int currentTag;
110
                double[][] coordinates = null;
111

    
112
                String tag = parser.getName();
113
                currentTag = parser.getEventType();
114

    
115
                while (!endFeature){
116
                        switch(currentTag){
117
                        case KXmlParser.START_TAG:
118
                                if (tag.compareTo(KmlTags.COORDINATES) == 0){
119
                                        coordinates =  handler.getProfile().getCoordinatesTypeBinding().parse(parser, handler);
120
                                }
121
                                break;
122
                        case KXmlParser.END_TAG:
123
                                if (tag.compareTo(KmlTags.LINEARRING) == 0){                                                
124
                                        endFeature = true;
125
                                }
126
                                break;
127
                        case KXmlParser.TEXT:                                        
128

    
129
                                break;
130
                        }
131
                        if (!endFeature){                                        
132
                                currentTag = parser.next();
133
                                tag = parser.getName();
134
                        }
135
                }        
136
                //The first and the last coordinates has to be the same
137
                if ((coordinates[0][0] != coordinates[0][coordinates[0].length - 1]) ||
138
                                (coordinates[1][0] != coordinates[1][coordinates[1].length - 1]) ||
139
                                (coordinates[2][0] != coordinates[2][coordinates[2].length - 1])){
140
                        handler.getErrorHandler().addWarning(new PolygonNotClosedWarning(coordinates));
141
                }
142
                return coordinates;
143
        }
144
}