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