From 26e38d7f8f8f662554b9cd15669e128caa2858d3 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 16 Oct 2013 09:40:01 -0400 Subject: [PATCH] Return geometries rather than features in asGeoJSON No code needs the tags, and it eliminates one level of function calls in the d3 stream pipeline. --- js/id/core/node.js | 8 ++------ js/id/core/relation.js | 8 ++------ js/id/core/way.js | 18 +++++------------- test/spec/core/node.js | 8 +++----- test/spec/core/relation.js | 10 ++++------ test/spec/core/way.js | 18 +++++++----------- 6 files changed, 23 insertions(+), 47 deletions(-) diff --git a/js/id/core/node.js b/js/id/core/node.js index 5fb261d5f..519f3bb54 100644 --- a/js/id/core/node.js +++ b/js/id/core/node.js @@ -55,12 +55,8 @@ _.extend(iD.Node.prototype, { asGeoJSON: function() { return { - type: 'Feature', - properties: this.tags, - geometry: { - type: 'Point', - coordinates: this.loc - } + type: 'Point', + coordinates: this.loc }; } }); diff --git a/js/id/core/relation.js b/js/id/core/relation.js index 9fabc3725..d8429e8a8 100644 --- a/js/id/core/relation.js +++ b/js/id/core/relation.js @@ -141,12 +141,8 @@ _.extend(iD.Relation.prototype, { return resolver.transient(this, 'GeoJSON', function () { if (this.isMultipolygon()) { return { - type: 'Feature', - properties: this.tags, - geometry: { - type: 'MultiPolygon', - coordinates: this.multipolygon(resolver) - } + type: 'MultiPolygon', + coordinates: this.multipolygon(resolver) }; } else { return { diff --git a/js/id/core/way.js b/js/id/core/way.js index 30b3cb1ea..d502b26c2 100644 --- a/js/id/core/way.js +++ b/js/id/core/way.js @@ -151,29 +151,21 @@ _.extend(iD.Way.prototype, { } var json = { - type: 'Feature', - properties: this.tags, - geometry: { - type: 'Polygon', - coordinates: [_.pluck(nodes, 'loc')] - } + type: 'Polygon', + coordinates: [_.pluck(nodes, 'loc')] }; // Heuristic for detecting counterclockwise winding order. Assumes // that OpenStreetMap polygons are not hemisphere-spanning. if (d3.geo.area(json) > 2 * Math.PI) { - json.geometry.coordinates[0] = json.geometry.coordinates[0].reverse(); + json.coordinates[0] = json.coordinates[0].reverse(); } return json; } else { return { - type: 'Feature', - properties: this.tags, - geometry: { - type: 'LineString', - coordinates: _.pluck(nodes, 'loc') - } + type: 'LineString', + coordinates: _.pluck(nodes, 'loc') }; } }); diff --git a/test/spec/core/node.js b/test/spec/core/node.js index d5ad90ec1..908b2543b 100644 --- a/test/spec/core/node.js +++ b/test/spec/core/node.js @@ -78,14 +78,12 @@ describe('iD.Node', function () { }); describe("#asGeoJSON", function () { - it("converts to a GeoJSON Point features", function () { + it("converts to a GeoJSON Point geometry", function () { var node = iD.Node({tags: {amenity: 'cafe'}, loc: [1, 2]}), json = node.asGeoJSON(); - expect(json.type).to.equal('Feature'); - expect(json.properties).to.eql({amenity: 'cafe'}); - expect(json.geometry.type).to.equal('Point'); - expect(json.geometry.coordinates).to.eql([1, 2]); + expect(json.type).to.equal('Point'); + expect(json.coordinates).to.eql([1, 2]); }); }); }); diff --git a/test/spec/core/relation.js b/test/spec/core/relation.js index e003d2565..103928e2b 100644 --- a/test/spec/core/relation.js +++ b/test/spec/core/relation.js @@ -204,7 +204,7 @@ describe('iD.Relation', function () { }); describe("#asGeoJSON", function (){ - it('converts a multipolygon to a GeoJSON MultiPolygon feature', function() { + it('converts a multipolygon to a GeoJSON MultiPolygon geometry', function() { var a = iD.Node({loc: [1, 1]}), b = iD.Node({loc: [3, 3]}), c = iD.Node({loc: [2, 2]}), @@ -213,10 +213,8 @@ describe('iD.Relation', function () { g = iD.Graph([a, b, c, w, r]), json = r.asGeoJSON(g); - expect(json.type).to.equal('Feature'); - expect(json.properties).to.eql({type: 'multipolygon'}); - expect(json.geometry.type).to.equal('MultiPolygon'); - expect(json.geometry.coordinates).to.eql([[[a.loc, b.loc, c.loc, a.loc]]]); + expect(json.type).to.equal('MultiPolygon'); + expect(json.coordinates).to.eql([[[a.loc, b.loc, c.loc, a.loc]]]); }); it('forces clockwise winding order for outer multipolygon ways', function() { @@ -228,7 +226,7 @@ describe('iD.Relation', function () { g = iD.Graph([a, b, c, w, r]), json = r.asGeoJSON(g); - expect(json.geometry.coordinates[0][0]).to.eql([a.loc, b.loc, c.loc, a.loc]); + expect(json.coordinates[0][0]).to.eql([a.loc, b.loc, c.loc, a.loc]); }); it('forces counterclockwise winding order for inner multipolygon ways', function() { diff --git a/test/spec/core/way.js b/test/spec/core/way.js index a4b68aeab..d01cdf99d 100644 --- a/test/spec/core/way.js +++ b/test/spec/core/way.js @@ -282,20 +282,18 @@ describe('iD.Way', function() { }); describe("#asGeoJSON", function () { - it("converts a line to a GeoJSON LineString feature", function () { + it("converts a line to a GeoJSON LineString geometry", function () { var a = iD.Node({loc: [1, 2]}), b = iD.Node({loc: [3, 4]}), w = iD.Way({tags: {highway: 'residential'}, nodes: [a.id, b.id]}), graph = iD.Graph([a, b, w]), json = w.asGeoJSON(graph); - expect(json.type).to.equal('Feature'); - expect(json.properties).to.eql({highway: 'residential'}); - expect(json.geometry.type).to.equal('LineString'); - expect(json.geometry.coordinates).to.eql([[1, 2], [3, 4]]); + expect(json.type).to.equal('LineString'); + expect(json.coordinates).to.eql([[1, 2], [3, 4]]); }); - it("converts an area to a GeoJSON Polygon feature", function () { + it("converts an area to a GeoJSON Polygon geometry", function () { var a = iD.Node({loc: [1, 2]}), b = iD.Node({loc: [5, 6]}), c = iD.Node({loc: [3, 4]}), @@ -303,10 +301,8 @@ describe('iD.Way', function() { graph = iD.Graph([a, b, c, w]), json = w.asGeoJSON(graph, true); - expect(json.type).to.equal('Feature'); - expect(json.properties).to.eql({area: 'yes'}); - expect(json.geometry.type).to.equal('Polygon'); - expect(json.geometry.coordinates).to.eql([[a.loc, b.loc, c.loc, a.loc]]); + expect(json.type).to.equal('Polygon'); + expect(json.coordinates).to.eql([[a.loc, b.loc, c.loc, a.loc]]); }); it("forces clockwise polygon winding order", function () { @@ -317,7 +313,7 @@ describe('iD.Way', function() { graph = iD.Graph([a, b, c, w]), json = w.asGeoJSON(graph, true); - expect(json.geometry.coordinates).to.eql([[a.loc, b.loc, c.loc, a.loc]]); + expect(json.coordinates).to.eql([[a.loc, b.loc, c.loc, a.loc]]); }); }); });