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. */
6
* @requires OpenLayers/Format/XML.js
7
* @requires OpenLayers/Feature/Vector.js
8
* @requires OpenLayers/Geometry/Point.js
9
* @requires OpenLayers/Geometry/LineString.js
13
* Class: OpenLayers.Format.GPX
14
* Read/write GPX parser. Create a new instance with the
15
* <OpenLayers.Format.GPX> constructor.
18
* - <OpenLayers.Format.XML>
20
OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
22
* APIProperty: extractWaypoints
23
* {Boolean} Extract waypoints from GPX. (default: true)
25
extractWaypoints: true,
28
* APIProperty: extractTracks
29
* {Boolean} Extract tracks from GPX. (default: true)
34
* APIProperty: extractRoutes
35
* {Boolean} Extract routes from GPX. (default: true)
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
45
extractAttributes: true,
48
* Constructor: OpenLayers.Format.GPX
49
* Create a new parser for GPX.
52
* options - {Object} An optional object whose properties will be set on
55
initialize: function(options) {
56
OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
61
* Return a list of features from a GPX doc
67
* An Array of <OpenLayers.Feature.Vector>s
70
if (typeof doc == "string") {
71
doc = OpenLayers.Format.XML.prototype.read.apply(this, [doc]);
75
if(this.extractWaypoints) {
76
var waypoints = doc.getElementsByTagName("wpt");
77
for (var l = 0, len = waypoints.length; l < len; l++) {
79
if(this.extractAttributes) {
80
attrs = this.parseAttributes(waypoints[l]);
82
var wpt = new OpenLayers.Geometry.Point(waypoints[l].getAttribute("lon"), waypoints[l].getAttribute("lat"));
83
features.push(new OpenLayers.Feature.Vector(wpt, attrs));
87
if(this.extractTracks) {
88
var tracks = doc.getElementsByTagName("trk");
89
for (var i=0, len=tracks.length; i<len; i++) {
90
// Attributes are only in trk nodes, not trkseg nodes
92
if(this.extractAttributes) {
93
attrs = this.parseAttributes(tracks[i]);
96
var segs = this.getElementsByTagNameNS(tracks[i], tracks[i].namespaceURI, "trkseg");
97
for (var j = 0, seglen = segs.length; j < seglen; j++) {
98
// We don't yet support extraction of trkpt attributes
99
// All trksegs of a trk get that trk's attributes
100
var track = this.extractSegment(segs[j], "trkpt");
101
features.push(new OpenLayers.Feature.Vector(track, attrs));
106
if(this.extractRoutes) {
107
var routes = doc.getElementsByTagName("rte");
108
for (var k=0, klen=routes.length; k<klen; k++) {
110
if(this.extractAttributes) {
111
attrs = this.parseAttributes(routes[k]);
113
var route = this.extractSegment(routes[k], "rtept");
114
features.push(new OpenLayers.Feature.Vector(route, attrs));
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);
129
* Method: extractSegment
132
* segment - {<DOMElement>} a trkseg or rte node to parse
133
* segmentType - {String} nodeName of waypoints that form the line
136
* {<OpenLayers.Geometry.LineString>} A linestring geometry
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")));
144
return new OpenLayers.Geometry.LineString(point_features);
148
* Method: parseAttributes
151
* node - {<DOMElement>}
154
* {Object} An attributes object.
156
parseAttributes: function(node) {
157
// node is either a wpt, trk or rte
158
// attributes are children of the form <attr>value</attr>
160
var attrNode = node.firstChild;
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] :
168
attributes[name] = value.nodeValue;
171
attrNode = attrNode.nextSibling;
176
CLASS_NAME: "OpenLayers.Format.GPX"