]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Format/GPX.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Format / GPX.js
1 /* Copyright (c) 2006-2007 MetaCarta, Inc., published under a modified BSD license.
2  * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt 
3  * for the full text of the license. */
4
5 /**
6  * @requires OpenLayers/Format/XML.js
7  * @requires OpenLayers/Feature/Vector.js
8  * @requires OpenLayers/Geometry/Point.js
9  * @requires OpenLayers/Geometry/LineString.js
10  */
11
12 /**
13  * Class: OpenLayers.Format.GPX
14  * Read/write GPX parser. Create a new instance with the 
15  *     <OpenLayers.Format.GPX> constructor.
16  *
17  * Inherits from:
18  *  - <OpenLayers.Format.XML>
19  */
20 OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
21    /**
22     * APIProperty: extractWaypoints
23     * {Boolean} Extract waypoints from GPX. (default: true)
24     */
25     extractWaypoints: true,
26     
27    /**
28     * APIProperty: extractTracks
29     * {Boolean} Extract tracks from GPX. (default: true)
30     */
31     extractTracks: true,
32     
33    /**
34     * APIProperty: extractRoutes
35     * {Boolean} Extract routes from GPX. (default: true)
36     */
37     extractRoutes: true,
38     
39     /**
40      * APIProperty: extractAttributes
41      * {Boolean} Extract feature attributes from GPX. (default: true)
42      *     NOTE: Attributes as part of extensions to the GPX standard may not
43      *     be extracted.
44      */
45     extractAttributes: true,
46     
47     /**
48      * Constructor: OpenLayers.Format.GPX
49      * Create a new parser for GPX.
50      *
51      * Parameters:
52      * options - {Object} An optional object whose properties will be set on
53      *     this instance.
54      */
55     initialize: function(options) {
56         OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
57     },
58     
59     /**
60      * APIMethod: read
61      * Return a list of features from a GPX doc
62      *
63      * Parameters:
64      * doc - {Element} 
65      *
66      * Returns:
67      * An Array of <OpenLayers.Feature.Vector>s
68      */
69     read: function(doc) {
70         if (typeof doc == "string") { 
71             doc = OpenLayers.Format.XML.prototype.read.apply(this, [doc]);
72         }
73         var features = [];
74         
75         if(this.extractTracks) {
76             var tracks = doc.getElementsByTagName("trk");
77             for (var i=0, len=tracks.length; i<len; i++) {
78                 // Attributes are only in trk nodes, not trkseg nodes
79                 var attrs = {};
80                 if(this.extractAttributes) {
81                     attrs = this.parseAttributes(tracks[i]);
82                 }
83                 
84                 var segs = this.getElementsByTagNameNS(tracks[i], tracks[i].namespaceURI, "trkseg");
85                 for (var j = 0, seglen = segs.length; j < seglen; j++) {
86                     // We don't yet support extraction of trkpt attributes
87                     // All trksegs of a trk get that trk's attributes
88                     var track = this.extractSegment(segs[j], "trkpt");
89                     features.push(new OpenLayers.Feature.Vector(track, attrs));
90                 }
91             }
92         }
93         
94         if(this.extractRoutes) {
95             var routes = doc.getElementsByTagName("rte");
96             for (var k=0, klen=routes.length; k<klen; k++) {
97                 var attrs = {};
98                 if(this.extractAttributes) {
99                     attrs = this.parseAttributes(routes[k]);
100                 }
101                 var route = this.extractSegment(routes[k], "rtept");
102                 features.push(new OpenLayers.Feature.Vector(route, attrs));
103             }
104         }
105         
106         if(this.extractWaypoints) {
107             var waypoints = doc.getElementsByTagName("wpt");
108             for (var l = 0, len = waypoints.length; l < len; l++) {
109                 var attrs = {};
110                 if(this.extractAttributes) {
111                     attrs = this.parseAttributes(waypoints[l]);
112                 }
113                 var wpt = new OpenLayers.Geometry.Point(waypoints[l].getAttribute("lon"), waypoints[l].getAttribute("lat"));
114                 features.push(new OpenLayers.Feature.Vector(wpt, attrs));
115             }
116         }
117         
118         if (this.internalProjection && this.externalProjection) {
119             for (var g = 0, featLength = features.length; g < featLength; g++) {
120                 features[g].geometry.transform(this.externalProjection,
121                                     this.internalProjection);
122             }
123         }
124         
125         return features;
126     },
127     
128    /**
129     * Method: extractSegment
130     *
131     * Parameters:
132     * segment - {<DOMElement>} a trkseg or rte node to parse
133     * segmentType - {String} nodeName of waypoints that form the line
134     *
135     * Returns:
136     * {<OpenLayers.Geometry.LineString>} A linestring geometry
137     */
138     extractSegment: function(segment, segmentType) {
139         var points = this.getElementsByTagNameNS(segment, segment.namespaceURI, segmentType);
140         var point_features = [];
141         for (var i = 0, len = points.length; i < len; i++) {
142             point_features.push(new OpenLayers.Geometry.Point(points[i].getAttribute("lon"), points[i].getAttribute("lat")));
143         }
144         return new OpenLayers.Geometry.LineString(point_features);
145     },
146     
147     /**
148      * Method: parseAttributes
149      *
150      * Parameters:
151      * node - {<DOMElement>}
152      *
153      * Returns:
154      * {Object} An attributes object.
155      */
156     parseAttributes: function(node) {
157         // node is either a wpt, trk or rte
158         // attributes are children of the form <attr>value</attr>
159         var attributes = {};
160         var attrNode = node.firstChild;
161         while(attrNode) {
162             if(attrNode.nodeType == 1) {
163                 var value = attrNode.firstChild;
164                 if(value.nodeType == 3 || value.nodeType == 4) {
165                     name = (attrNode.prefix) ?
166                         attrNode.nodeName.split(":")[1] :
167                         attrNode.nodeName;
168                     if(name != "trkseg" && name != "rtept") {
169                         attributes[name] = value.nodeValue;
170                     }
171                 }
172             }
173             attrNode = attrNode.nextSibling;
174         }
175         return attributes;
176     },
177     
178     CLASS_NAME: "OpenLayers.Format.GPX"
179 });