From 26e0a1f56c438300fa4b9205adbd416a84d84fb4 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Wed, 17 Oct 2012 16:49:43 -0400 Subject: [PATCH] Add toGeoJSON for nodes and ways --- js/iD/Node.js | 12 ++++++++++++ js/iD/Way.js | 19 ++++++++++++++++--- test/spec/Node.js | 6 ++++++ test/spec/Way.js | 6 ++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/js/iD/Node.js b/js/iD/Node.js index 7c285639c..7f89c78cf 100644 --- a/js/iD/Node.js +++ b/js/iD/Node.js @@ -21,6 +21,18 @@ iD.Node.prototype = { this.latp = 180/Math.PI * Math.log(Math.tan(Math.PI/4+this.lat*(Math.PI/180)/2)); }, + + toGeoJSON: function() { + return { + type: 'Feature', + properties: this.tags, + geometry: { + type: 'Point', + coordinates: [this.lon, this.lat] + } + }; + }, + latp2lat: function(a) { // summary: Get a latitude from a projected latitude. // returns: Latitude. diff --git a/js/iD/Way.js b/js/iD/Way.js index 268e6de0e..7cba274af 100644 --- a/js/iD/Way.js +++ b/js/iD/Way.js @@ -6,12 +6,12 @@ iD.Way = function(conn, id, nodes, tags, loaded) { this.entityType = 'way'; this.id = id; this._id = iD.Util.id(); - this.nodes = nodes || []; this.deleted = false; this.entity = new iD.Entity(); this.tags = tags || {}; this.loaded = (loaded === undefined) ? true : loaded; this.modified = this.id < 0; + this.nodes = nodes || []; _.each(nodes, _.bind(function(node) { node.entity.addParent(this); }, this)); @@ -19,12 +19,12 @@ iD.Way = function(conn, id, nodes, tags, loaded) { }; iD.Way.prototype = { - isClosed:function() { + isClosed: function() { // summary: Is this a closed way (first and last nodes the same)? return this.nodes[this.nodes.length - 1] === this.nodes[0]; // Boolean }, - isType:function(type) { + isType: function(type) { // summary: Is this a 'way' (always true), an 'area' (closed) or a 'line' (unclosed)? if (type === 'way') return true; if (type === 'area') return this.isClosed(); @@ -32,6 +32,19 @@ iD.Way.prototype = { return false; // Boolean }, + toGeoJSON: function() { + return { + type: 'Feature', + properties: this.tags, + geometry: { + 'type': 'LineString', + 'coordinates': _.map(this.nodes, function(node) { + return [node.lon, node.lat]; + }) + } + }; + }, + // --------------------- // Bounding-box handling within:function(left,right,top,bottom) { diff --git a/test/spec/Node.js b/test/spec/Node.js index 8a68f4092..90f539ef3 100644 --- a/test/spec/Node.js +++ b/test/spec/Node.js @@ -28,4 +28,10 @@ describe('Node', function() { it('knows if it is without a bounding box', function() { expect(node.within(-90, -85, 90, -90)).toBeFalsy(); }); + + it('can provide geojson', function() { + var gj = node.toGeoJSON(); + expect(gj.type).toEqual('Feature'); + expect(gj.geometry.type).toEqual('Point'); + }); }); diff --git a/test/spec/Way.js b/test/spec/Way.js index 1365a4d93..7fc577714 100644 --- a/test/spec/Way.js +++ b/test/spec/Way.js @@ -24,4 +24,10 @@ describe('Way', function() { it('is also an area when it has no nodes', function() { expect(way.isType('area')).toEqual(true); }); + + it('can provide geojson', function() { + var gj = way.toGeoJSON(); + expect(gj.type).toEqual('Feature'); + expect(gj.geometry.type).toEqual('LineString'); + }); });