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