From 13a784bea58b329207d9b08e8f81bada52f2ffbf Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 5 Feb 2013 14:05:43 -0800 Subject: [PATCH] Better Way#asGeoJSON --- js/id/graph/way.js | 27 +++++++++++++++++++-------- test/spec/graph/way.js | 16 +++++++++++++++- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/js/id/graph/way.js b/js/id/graph/way.js index aae89a6ce..1f7c46065 100644 --- a/js/id/graph/way.js +++ b/js/id/graph/way.js @@ -103,13 +103,24 @@ _.extend(iD.Way.prototype, { }, asGeoJSON: function(resolver) { - return { - type: 'Feature', - properties: this.tags, - geometry: { - type: 'LineString', - coordinates: _.pluck(resolver.childNodes(this), 'loc') - } - }; + if (this.isArea()) { + return { + type: 'Feature', + properties: this.tags, + geometry: { + type: 'Polygon', + coordinates: [_.pluck(resolver.childNodes(this), 'loc')] + } + }; + } else { + return { + type: 'Feature', + properties: this.tags, + geometry: { + type: 'LineString', + coordinates: _.pluck(resolver.childNodes(this), 'loc') + } + }; + } } }); diff --git a/test/spec/graph/way.js b/test/spec/graph/way.js index 6b6b1543f..39393ab16 100644 --- a/test/spec/graph/way.js +++ b/test/spec/graph/way.js @@ -207,7 +207,7 @@ describe('iD.Way', function() { }); describe("#asGeoJSON", function () { - it("converts to a GeoJSON LineString features", function () { + it("converts a line to a GeoJSON LineString features", 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]}), @@ -219,5 +219,19 @@ describe('iD.Way', function() { expect(json.geometry.type).to.equal('LineString'); expect(json.geometry.coordinates).to.eql([[1, 2], [3, 4]]); }); + + it("converts an area to a GeoJSON Polygon features", function () { + var a = iD.Node({loc: [1, 2]}), + b = iD.Node({loc: [3, 4]}), + c = iD.Node({loc: [5, 6]}), + w = iD.Way({tags: {area: 'yes'}, nodes: [a.id, b.id, c.id, a.id]}), + graph = iD.Graph([a, b, c, w]), + json = w.asGeoJSON(graph); + + 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([[[1, 2], [3, 4], [5, 6], [1, 2]]]); + }); }); });