Skip to content

Commit

Permalink
Merge pull request openlayers#200 from pgiraud/gpx
Browse files Browse the repository at this point in the history
Adding missing schemalocation, version. Better management of the namespaces.
  • Loading branch information
Pierre GIRAUD committed Feb 8, 2012
2 parents e7eae00 + 734f150 commit 85b9d5f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
46 changes: 29 additions & 17 deletions lib/OpenLayers/Format/GPX.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,20 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
extractAttributes: true,

/**
* APIProperty: gpxns
* {String} GPX namespace to use. Defaults to
* "http://www.topografix.com/GPX/1/1"
* Property: namespaces
* {Object} Mapping of namespace aliases to namespace URIs.
*/
gpxns: "http://www.topografix.com/GPX/1/1",
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",

/**
* Constructor: OpenLayers.Format.GPX
Expand Down Expand Up @@ -207,17 +216,20 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
* add to the gpx. Supported keys are 'name', 'desc', 'author'.
*/
write: function(features, metadata) {
var gpx;
features = OpenLayers.Util.isArray(features) ?
features : [features];
gpx = this.createElementNS(this.gpxns, "gpx");
var gpx = this.createElementNSPlus("gpx:gpx");
gpx.setAttribute("version", "1.1");
this.setAttributes(gpx, {
"xsi:schemaLocation": this.schemaLocation
});

for(var i=0, len=features.length; i<len; i++) {
gpx.appendChild(this.buildFeatureNode(features[i]));
}
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]);
},

Expand All @@ -230,11 +242,11 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
*/
buildMetadataNode: function(metadata) {
var types = ['name', 'desc', 'author'],
node = this.createElementNS(this.gpxns, 'metadata');
node = this.createElementNSPlus('gpx:metadata');
for (var i=0; i < types.length; i++) {
var type = types[i];
if (metadata[type]) {
var n = this.createElementNS(this.gpxns, type);
var n = this.createElementNSPlus("gpx:" + type);
n.appendChild(this.createTextNode(metadata[type]));
node.appendChild(n);
}
Expand Down Expand Up @@ -263,7 +275,7 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
var wpt = this.buildWptNode(feature);
return wpt;
} else {
var trkNode = this.createElementNS(this.gpxns, "trk");
var trkNode = this.createElementNSPlus("gpx:trk");
this.appendAttributesNode(trkNode, feature);
var trkSegNodes = this.buildTrkSegNode(geometry);
trkSegNodes = OpenLayers.Util.isArray(trkSegNodes) ?
Expand Down Expand Up @@ -291,7 +303,7 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
nodes;
if (geometry.CLASS_NAME == "OpenLayers.Geometry.LineString" ||
geometry.CLASS_NAME == "OpenLayers.Geometry.LinearRing") {
node = this.createElementNS(this.gpxns, "trkseg");
node = this.createElementNSPlus("gpx:trkseg");
for (i = 0, len=geometry.components.length; i < len; i++) {
point = geometry.components[i];
node.appendChild(this.buildTrkPtNode(point));
Expand All @@ -317,7 +329,7 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
* {DOMElement} A trkpt node
*/
buildTrkPtNode: function(point) {
var node = this.createElementNS(this.gpxns, "trkpt");
var node = this.createElementNSPlus("gpx:trkpt");
node.setAttribute("lon", point.x);
node.setAttribute("lat", point.y);
return node;
Expand All @@ -334,7 +346,7 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
* {DOMElement} A wpt node
*/
buildWptNode: function(feature) {
var node = this.createElementNS(this.gpxns, "wpt");
var node = this.createElementNSPlus("gpx:wpt");
node.setAttribute("lon", feature.geometry.x);
node.setAttribute("lat", feature.geometry.y);
this.appendAttributesNode(node, feature);
Expand All @@ -350,11 +362,11 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
* feature - {<OpenLayers.Feature.Vector>}
*/
appendAttributesNode: function(node, feature) {
var name = this.createElementNS(this.gpxns, 'name');
var name = this.createElementNSPlus('gpx:name');
name.appendChild(this.createTextNode(
feature.attributes.name || feature.id));
node.appendChild(name);
var desc = this.createElementNS(this.gpxns, 'desc');
var desc = this.createElementNSPlus('gpx:desc');
desc.appendChild(this.createTextNode(
feature.attributes.description || this.defaultDesc));
node.appendChild(desc);
Expand Down
4 changes: 4 additions & 0 deletions notes/2.12.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ People can override this rule to use other transition settings. To remove tile a

# Behavior Changes from Past Releases

## GPX API change

The `gpxns` API property has been removed. The GPX namespace is now defined in the `namespaces` property but is not intended to be overriden.

## Function return values

[Previously][prev] a few functions in the library displayed error messages and returned `undefined`, `null` or `false` if the parameters passed in were bad. In 2.12 these functions now just throw an error/exception. People relying on return values to know if a function call is successful may need to change their code. Here are the modified functions:
Expand Down
12 changes: 6 additions & 6 deletions tests/Format/GPX.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
new OpenLayers.Feature.Vector(point2, {name: 'foo', description: 'bar'})
];
var data = parser.write(features);
t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><wpt lon="-111.04" lat="45.68"><name>foo</name><desc>bar</desc></wpt><wpt lon="-112.04" lat="45.68"><name>foo</name><desc>bar</desc></wpt></gpx>', 'GPX serializes points correctly');
t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><wpt lon="-111.04" lat="45.68"><name>foo</name><desc>bar</desc></wpt><wpt lon="-112.04" lat="45.68"><name>foo</name><desc>bar</desc></wpt></gpx>', 'GPX serializes points correctly');
}
function test_Format_GPX_serialize_line(t) {
t.plan(1);
Expand All @@ -58,7 +58,7 @@
var line = new OpenLayers.Geometry.LineString([point, point2]);
var f = new OpenLayers.Feature.Vector(line, {name: 'foo', description: 'bar'});
var data = parser.write(f);
t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg></trk></gpx>', 'GPX serializes line correctly');
t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg></trk></gpx>', 'GPX serializes line correctly');
}
function test_Format_GPX_serialize_lines(t) {
t.plan(1);
Expand All @@ -74,7 +74,7 @@
var f = new OpenLayers.Feature.Vector(line, {name: 'foo', description: 'bar'});
var f2 = new OpenLayers.Feature.Vector(line2, {name: 'dude', description: 'truite'});
var data = parser.write([f, f2]);
t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg></trk><trk><name>dude</name><desc>truite</desc><trkseg><trkpt lon="1" lat="2"/><trkpt lon="3" lat="4"/></trkseg></trk></gpx>', 'GPX serializes lines correctly');
t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg></trk><trk><name>dude</name><desc>truite</desc><trkseg><trkpt lon="1" lat="2"/><trkpt lon="3" lat="4"/></trkseg></trk></gpx>', 'GPX serializes lines correctly');
}
function test_Format_GPX_serialize_multiline(t) {
t.plan(1);
Expand All @@ -90,7 +90,7 @@
var multiline = new OpenLayers.Geometry.MultiLineString([line, line2]);
var f = new OpenLayers.Feature.Vector(multiline, {name: 'foo', description: 'bar'});
var data = parser.write([f]);
t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg><trkseg><trkpt lon="1" lat="2"/><trkpt lon="3" lat="4"/></trkseg></trk></gpx>', 'GPX serializes multiline correctly');
t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg><trkseg><trkpt lon="1" lat="2"/><trkpt lon="3" lat="4"/></trkseg></trk></gpx>', 'GPX serializes multiline correctly');
}
function test_Format_GPX_serialize_polygon(t) {
t.plan(1);
Expand All @@ -103,15 +103,15 @@
var polygon = new OpenLayers.Geometry.Polygon([linearRing]);
var f = new OpenLayers.Feature.Vector(polygon, {name: 'foo', description: 'bar'});
var data = parser.write([f]);
t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/><trkpt lon="-111.04" lat="45.68"/></trkseg></trk></gpx>', 'GPX serializes polygon correctly');
t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/><trkpt lon="-111.04" lat="45.68"/></trkseg></trk></gpx>', 'GPX serializes polygon correctly');
}
function test_Format_GPX_serialize_metadata(t) {
t.plan(1);

var parser = new OpenLayers.Format.GPX();

var data = parser.write([], {name: 'foo', desc: 'bar'});
t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><metadata><name>foo</name><desc>bar</desc></metadata></gpx>', 'GPX serializes metadata correctly');
t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><metadata><name>foo</name><desc>bar</desc></metadata></gpx>', 'GPX serializes metadata correctly');
}
</script>
</head>
Expand Down

0 comments on commit 85b9d5f

Please sign in to comment.