From 9ea3c2f12110d8b3b5f3d563b1e29fe5e0011f22 Mon Sep 17 00:00:00 2001 From: Peter Robins Date: Mon, 14 May 2012 08:49:48 +0100 Subject: [PATCH 1/2] Fix GPX write Point transform --- lib/OpenLayers/Format/GPX.js | 12 ++++++------ notes/2.12.md | 2 ++ tests/Format/GPX.html | 32 +++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/OpenLayers/Format/GPX.js b/lib/OpenLayers/Format/GPX.js index 16b979fd1b..c8d447899d 100644 --- a/lib/OpenLayers/Format/GPX.js +++ b/lib/OpenLayers/Format/GPX.js @@ -280,7 +280,8 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { this.externalProjection); } if (geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { - var wpt = this.buildWptNode(feature); + var wpt = this.buildWptNode(geometry); + this.appendAttributesNode(wpt, feature); return wpt; } else { var trkNode = this.createElementNSPlus("gpx:trk"); @@ -348,16 +349,15 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { * Builds a wpt node given a point * * Parameters: - * feature - {} + * geometry - {} * * Returns: * {DOMElement} A wpt node */ - buildWptNode: function(feature) { + buildWptNode: function(geometry) { var node = this.createElementNSPlus("gpx:wpt"); - node.setAttribute("lon", feature.geometry.x); - node.setAttribute("lat", feature.geometry.y); - this.appendAttributesNode(node, feature); + node.setAttribute("lon", geometry.x); + node.setAttribute("lat", geometry.y); return node; }, diff --git a/notes/2.12.md b/notes/2.12.md index 0ffca1769f..6a6bdb8fee 100644 --- a/notes/2.12.md +++ b/notes/2.12.md @@ -152,6 +152,8 @@ Corresponding issues/pull requests: The `gpxns` API property has been removed. The GPX namespace is now defined in the `namespaces` property but is not intended to be overriden. +GPX also now has a basic write function. + Corresponding issues/pull requests: * https://github.com/openlayers/openlayers/pull/221 diff --git a/tests/Format/GPX.html b/tests/Format/GPX.html index 0b0f1d2aca..65e6677685 100644 --- a/tests/Format/GPX.html +++ b/tests/Format/GPX.html @@ -19,13 +19,21 @@ "default external projection is EPSG:4326"); } function test_Format_GPX_read(t) { - t.plan(4); + t.plan(7); var f = new OpenLayers.Format.GPX(); var features = f.read(gpx_data); t.eq(features.length, 3, "Number of features read is correct"); t.eq(features[2].geometry.toString(), "POINT(-0.1853562259 51.3697845627)", "waypoint feature correctly created"); t.eq(features[0].geometry.toString(), "LINESTRING(-0.1721292044 51.3768216433,-0.1649230916 51.370833767,-0.1736741378 51.3644368725,-0.166259525 51.3576354272)", "track feature correctly created"); t.eq(features[1].geometry.toString(), "LINESTRING(-0.1829991904 51.3761803674,-0.1758887005 51.3697894659,-0.1833202965 51.3639790884,-0.1751119509 51.3567607069)", "route feature correctly created"); + + f.internalProjection = new OpenLayers.Projection("EPSG:3857"); + features = f.read(gpx_data); + t.eq(features[2].geometry.toString(), "POINT(-20633.760679678744 6686966.841929403)", "transformed waypoint feature correctly created"); + features[0].geometry.components.pop();//hack to suppress rounding errors + t.eq(features[0].geometry.toString(), "LINESTRING(-19161.33538179203 6688221.743275255,-18359.1545744088 6687153.931130851,-19333.316581165607 6686013.33343931)", "transformed track feature correctly created"); + features[1].geometry.components.pop();//hack to suppress rounding errors + t.eq(features[1].geometry.toString(), "LINESTRING(-20371.3766880736 6688107.378491073,-19579.84057322507 6686967.716235109,-20407.12205561124 6685931.714395953)", "transformed route feature correctly created"); } function test_format_GPX_read_attributes(t) { t.plan(2); @@ -35,7 +43,7 @@ t.eq(features[2].attributes['sym'], "Flag", "CDATA attribute node read correctly."); } function test_Format_GPX_serialize_points(t) { - t.plan(1); + t.plan(2); var parser = new OpenLayers.Format.GPX(); @@ -47,9 +55,19 @@ ]; var data = parser.write(features); t.xml_eq(data, 'foobarfoobar', 'GPX serializes points correctly'); + + parser.internalProjection = new OpenLayers.Projection("EPSG:3857"); + point = new OpenLayers.Geometry.Point(-12367595.42541111, 5621521.485409545); + point2 = new OpenLayers.Geometry.Point(-12472235.746742222, 5621521.485409545); + features = [ + new OpenLayers.Feature.Vector(point, {name: 'foo', description: 'bar'}), + new OpenLayers.Feature.Vector(point2, {name: 'foo', description: 'bar'}) + ]; + data = parser.write(features); + t.xml_eq(data, 'foobarfoobar', 'GPX serializes transformed points correctly'); } function test_Format_GPX_serialize_line(t) { - t.plan(1); + t.plan(2); var parser = new OpenLayers.Format.GPX(); @@ -59,6 +77,14 @@ var f = new OpenLayers.Feature.Vector(line, {name: 'foo', description: 'bar'}); var data = parser.write(f); t.xml_eq(data, 'foobar', 'GPX serializes line correctly'); + + parser.internalProjection = new OpenLayers.Projection("EPSG:3857"); + point = new OpenLayers.Geometry.Point(-12367595.42541111, 5621521.485409545); + point2 = new OpenLayers.Geometry.Point(-12472235.746742222, 5621521.485409545); + line = new OpenLayers.Geometry.LineString([point, point2]); + f = new OpenLayers.Feature.Vector(line, {name: 'foo', description: 'bar'}); + data = parser.write(f); + t.xml_eq(data, 'foobar', 'GPX serializes transformed line correctly'); } function test_Format_GPX_serialize_lines(t) { t.plan(1); From 114541d0b714767b560e8de34cfd1057ccfeb372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Tue, 15 May 2012 08:58:23 +0200 Subject: [PATCH 2/2] use t.geom_eq in GPX tests --- tests/Format/GPX.html | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/tests/Format/GPX.html b/tests/Format/GPX.html index 65e6677685..ee08de9556 100644 --- a/tests/Format/GPX.html +++ b/tests/Format/GPX.html @@ -20,20 +20,47 @@ } function test_Format_GPX_read(t) { t.plan(7); + var expected, + P = OpenLayers.Geometry.Point, + LS = OpenLayers.Geometry.LineString; var f = new OpenLayers.Format.GPX(); var features = f.read(gpx_data); t.eq(features.length, 3, "Number of features read is correct"); - t.eq(features[2].geometry.toString(), "POINT(-0.1853562259 51.3697845627)", "waypoint feature correctly created"); - t.eq(features[0].geometry.toString(), "LINESTRING(-0.1721292044 51.3768216433,-0.1649230916 51.370833767,-0.1736741378 51.3644368725,-0.166259525 51.3576354272)", "track feature correctly created"); - t.eq(features[1].geometry.toString(), "LINESTRING(-0.1829991904 51.3761803674,-0.1758887005 51.3697894659,-0.1833202965 51.3639790884,-0.1751119509 51.3567607069)", "route feature correctly created"); + expected = new P(-0.1853562259, 51.3697845627); + t.geom_eq(features[2].geometry, expected, "waypoint feature correctly created"); + expected = new LS([ + new P(-0.1721292044, 51.3768216433), + new P(-0.1649230916, 51.370833767), + new P(-0.1736741378, 51.3644368725), + new P(-0.166259525, 51.3576354272) + ]); + t.geom_eq(features[0].geometry, expected, "track feature correctly created"); + expected = new LS([ + new P(-0.1829991904, 51.3761803674), + new P(-0.1758887005, 51.3697894659), + new P(-0.1833202965, 51.3639790884), + new P(-0.1751119509, 51.3567607069) + ]); + t.geom_eq(features[1].geometry, expected, "route feature correctly created"); f.internalProjection = new OpenLayers.Projection("EPSG:3857"); features = f.read(gpx_data); - t.eq(features[2].geometry.toString(), "POINT(-20633.760679678744 6686966.841929403)", "transformed waypoint feature correctly created"); - features[0].geometry.components.pop();//hack to suppress rounding errors - t.eq(features[0].geometry.toString(), "LINESTRING(-19161.33538179203 6688221.743275255,-18359.1545744088 6687153.931130851,-19333.316581165607 6686013.33343931)", "transformed track feature correctly created"); - features[1].geometry.components.pop();//hack to suppress rounding errors - t.eq(features[1].geometry.toString(), "LINESTRING(-20371.3766880736 6688107.378491073,-19579.84057322507 6686967.716235109,-20407.12205561124 6685931.714395953)", "transformed route feature correctly created"); + expected = new P(-20633.760679678744, 6686966.841929403); + t.geom_eq(features[2].geometry, expected, "transformed waypoint feature correctly created"); + expected = new LS([ + new P(-19161.33538179203, 6688221.743275255), + new P(-18359.1545744088, 6687153.931130851), + new P(-19333.316581165607, 6686013.33343931), + new P(-18507.925659955214, 6684800.777090962) + ]); + t.geom_eq(features[0].geometry, expected, "transformed track feature correctly created"); + expected = new LS([ + new P(-20371.3766880736, 6688107.378491073), + new P(-19579.84057322507, 6686967.716235109), + new P(-20407.12205561124, 6685931.714395953), + new P(-19493.373203291227, 6684644.845706556) + ]); + t.geom_eq(features[1].geometry, expected, "transformed route feature correctly created"); } function test_format_GPX_read_attributes(t) { t.plan(2);