1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the 2-clause BSD license.
* See license.txt in the OpenLayers distribution or repository for the
* full text of the license. */
/**
* @requires OpenLayers/Format/XML.js
* @requires OpenLayers/Feature/Vector.js
* @requires OpenLayers/Geometry/Point.js
* @requires OpenLayers/Geometry/LineString.js
* @requires OpenLayers/Projection.js
*/
/**
* Class: OpenLayers.Format.GPX
* Read/write GPX parser. Create a new instance with the
* <OpenLayers.Format.GPX> constructor.
*
* Inherits from:
* - <OpenLayers.Format.XML>
*/
OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
/**
* APIProperty: defaultDesc
* {String} Default description for the waypoints/tracks in the case
* where the feature has no "description" attribute.
* Default is "No description available".
*/
defaultDesc: "No description available",
/**
* APIProperty: extractWaypoints
* {Boolean} Extract waypoints from GPX. (default: true)
*/
extractWaypoints: true,
/**
* APIProperty: extractTracks
* {Boolean} Extract tracks from GPX. (default: true)
*/
extractTracks: true,
/**
* APIProperty: extractRoutes
* {Boolean} Extract routes from GPX. (default: true)
*/
extractRoutes: true,
/**
* APIProperty: extractAttributes
* {Boolean} Extract feature attributes from GPX. (default: true)
* NOTE: Attributes as part of extensions to the GPX standard may not
* be extracted.
*/
extractAttributes: true,
/**
* Property: namespaces
* {Object} Mapping of namespace aliases to namespace URIs.
*/
namespaces: {
gpx: "http://www.topografix.com/GPX/1/1",
xsi: "http://www.w3.org/2001/XMLSchema-instance"
},
/**
* Property: schemaLocation
* {String} Schema location. Defaults to
* "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
*/
schemaLocation: "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd",
/**
* APIProperty: creator
* {String} The creator attribute to be added to the written GPX files.
* Defaults to "OpenLayers"
*/
creator: "OpenLayers",
/**
* Constructor: OpenLayers.Format.GPX
* Create a new parser for GPX.
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
initialize: function(options) {
// GPX coordinates are always in longlat WGS84
this.externalProjection = new OpenLayers.Projection("EPSG:4326");
OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
},
/**
* APIMethod: read
* Return a list of features from a GPX doc
*
* Parameters:
* doc - {Element}
*
* Returns:
* Array({<OpenLayers.Feature.Vector>})
*/
read: function(doc) {
if (typeof doc == "string") {
doc = OpenLayers.Format.XML.prototype.read.apply(this, [doc]);
}
var features = [];
if(this.extractTracks) {
var tracks = doc.getElementsByTagName("trk");
for (var i=0, len=tracks.length; i<len; i++) {
// Attributes are only in trk nodes, not trkseg nodes
var attrs = {};
if(this.extractAttributes) {
attrs = this.parseAttributes(tracks[i]);
}
var segs = this.getElementsByTagNameNS(tracks[i], tracks[i].namespaceURI, "trkseg");
for (var j = 0, seglen = segs.length; j < seglen; j++) {
// We don't yet support extraction of trkpt attributes
// All trksegs of a trk get that trk's attributes
var track = this.extractSegment(segs[j], "trkpt");
features.push(new OpenLayers.Feature.Vector(track, attrs));
}
}
}
if(this.extractRoutes) {
var routes = doc.getElementsByTagName("rte");
for (var k=0, klen=routes.length; k<klen; k++) {
var attrs = {};
if(this.extractAttributes) {
attrs = this.parseAttributes(routes[k]);
}
var route = this.extractSegment(routes[k], "rtept");
features.push(new OpenLayers.Feature.Vector(route, attrs));
}
}
if(this.extractWaypoints) {
var waypoints = doc.getElementsByTagName("wpt");
for (var l = 0, len = waypoints.length; l < len; l++) {
var attrs = {};
if(this.extractAttributes) {
attrs = this.parseAttributes(waypoints[l]);
}
var wpt = new OpenLayers.Geometry.Point(waypoints[l].getAttribute("lon"), waypoints[l].getAttribute("lat"));
features.push(new OpenLayers.Feature.Vector(wpt, attrs));
}
}
if (this.internalProjection && this.externalProjection) {
for (var g = 0, featLength = features.length; g < featLength; g++) {
features[g].geometry.transform(this.externalProjection,
this.internalProjection);
}
}
return features;
},
/**
* Method: extractSegment
*
* Parameters:
* segment - {DOMElement} a trkseg or rte node to parse
* segmentType - {String} nodeName of waypoints that form the line
*
* Returns:
* {<OpenLayers.Geometry.LineString>} A linestring geometry
*/
extractSegment: function(segment, segmentType) {
var points = this.getElementsByTagNameNS(segment, segment.namespaceURI, segmentType);
var point_features = [];
for (var i = 0, len = points.length; i < len; i++) {
point_features.push(new OpenLayers.Geometry.Point(points[i].getAttribute("lon"), points[i].getAttribute("lat")));
}
return new OpenLayers.Geometry.LineString(point_features);
},
/**
* Method: parseAttributes
*
* Parameters:
* node - {<DOMElement>}
*
* Returns:
* {Object} An attributes object.
*/
parseAttributes: function(node) {
// node is either a wpt, trk or rte
// attributes are children of the form <attr>value</attr>
var attributes = {};
var attrNode = node.firstChild, value, name;
while(attrNode) {
if(attrNode.nodeType == 1 && attrNode.firstChild) {
value = attrNode.firstChild;
if(value.nodeType == 3 || value.nodeType == 4) {
name = (attrNode.prefix) ?
attrNode.nodeName.split(":")[1] :
attrNode.nodeName;
if(name != "trkseg" && name != "rtept") {
attributes[name] = value.nodeValue;
}
}
}
attrNode = attrNode.nextSibling;
}
return attributes;
},
/**
* APIMethod: write
* Accepts Feature Collection, and returns a string.
*
* Parameters:
* features - {Array(<OpenLayers.Feature.Vector>)} List of features to serialize into a string.
* metadata - {Object} A key/value pairs object to build a metadata node to
* add to the gpx. Supported keys are 'name', 'desc', 'author'.
*/
write: function(features, metadata) {
features = OpenLayers.Util.isArray(features) ?
features : [features];
var gpx = this.createElementNS(this.namespaces.gpx, "gpx");
gpx.setAttribute("version", "1.1");
gpx.setAttribute("creator", this.creator);
this.setAttributes(gpx, {
"xsi:schemaLocation": this.schemaLocation
});
if (metadata && typeof metadata == 'object') {
gpx.appendChild(this.buildMetadataNode(metadata));
}
for(var i=0, len=features.length; i<len; i++) {
gpx.appendChild(this.buildFeatureNode(features[i]));
}
return OpenLayers.Format.XML.prototype.write.apply(this, [gpx]);
},
/**
* Method: buildMetadataNode
* Creates a "metadata" node.
*
* Returns:
* {DOMElement}
*/
buildMetadataNode: function(metadata) {
var types = ['name', 'desc', 'author'],
node = this.createElementNS(this.namespaces.gpx, 'metadata');
for (var i=0; i < types.length; i++) {
var type = types[i];
if (metadata[type]) {
var n = this.createElementNS(this.namespaces.gpx, type);
n.appendChild(this.createTextNode(metadata[type]));
node.appendChild(n);
}
}
return node;
},
/**
* Method: buildFeatureNode
* Accepts an <OpenLayers.Feature.Vector>, and builds a node for it.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>}
*
* Returns:
* {DOMElement} - The created node, either a 'wpt' or a 'trk'.
*/
buildFeatureNode: function(feature) {
var geometry = feature.geometry;
geometry = geometry.clone();
if (this.internalProjection && this.externalProjection) {
geometry.transform(this.internalProjection,
this.externalProjection);
}
if (geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
var wpt = this.buildWptNode(geometry);
this.appendAttributesNode(wpt, feature);
return wpt;
} else {
var trkNode = this.createElementNS(this.namespaces.gpx, "trk");
this.appendAttributesNode(trkNode, feature);
var trkSegNodes = this.buildTrkSegNode(geometry);
trkSegNodes = OpenLayers.Util.isArray(trkSegNodes) ?
trkSegNodes : [trkSegNodes];
for (var i = 0, len = trkSegNodes.length; i < len; i++) {
trkNode.appendChild(trkSegNodes[i]);
}
return trkNode;
}
},
/**
* Method: buildTrkSegNode
* Builds trkseg node(s) given a geometry
*
* Parameters:
* trknode
* geometry - {<OpenLayers.Geometry>}
*/
buildTrkSegNode: function(geometry) {
var node,
i,
len,
point,
nodes;
if (geometry.CLASS_NAME == "OpenLayers.Geometry.LineString" ||
geometry.CLASS_NAME == "OpenLayers.Geometry.LinearRing") {
node = this.createElementNS(this.namespaces.gpx, "trkseg");
for (i = 0, len=geometry.components.length; i < len; i++) {
point = geometry.components[i];
node.appendChild(this.buildTrkPtNode(point));
}
return node;
} else {
nodes = [];
for (i = 0, len = geometry.components.length; i < len; i++) {
nodes.push(this.buildTrkSegNode(geometry.components[i]));
}
return nodes;
}
},
/**
* Method: buildTrkPtNode
* Builds a trkpt node given a point
*
* Parameters:
* point - {<OpenLayers.Geometry.Point>}
*
* Returns:
* {DOMElement} A trkpt node
*/
buildTrkPtNode: function(point) {
var node = this.createElementNS(this.namespaces.gpx, "trkpt");
node.setAttribute("lon", point.x);
node.setAttribute("lat", point.y);
return node;
},
/**
* Method: buildWptNode
* Builds a wpt node given a point
*
* Parameters:
* geometry - {<OpenLayers.Geometry.Point>}
*
* Returns:
* {DOMElement} A wpt node
*/
buildWptNode: function(geometry) {
var node = this.createElementNS(this.namespaces.gpx, "wpt");
node.setAttribute("lon", geometry.x);
node.setAttribute("lat", geometry.y);
return node;
},
/**
* Method: appendAttributesNode
* Adds some attributes node.
*
* Parameters:
* node - {DOMElement} the node to append the attribute nodes to.
* feature - {<OpenLayers.Feature.Vector>}
*/
appendAttributesNode: function(node, feature) {
var name = this.createElementNS(this.namespaces.gpx, 'name');
name.appendChild(this.createTextNode(
feature.attributes.name || feature.id));
node.appendChild(name);
var desc = this.createElementNS(this.namespaces.gpx, 'desc');
desc.appendChild(this.createTextNode(
feature.attributes.description || this.defaultDesc));
node.appendChild(desc);
// TBD - deal with remaining (non name/description) attributes.
},
CLASS_NAME: "OpenLayers.Format.GPX"
});
|