Add toGeoJSON for nodes and ways

This commit is contained in:
Tom MacWright
2012-10-17 16:49:43 -04:00
parent a3515e16ee
commit 26e0a1f56c
4 changed files with 40 additions and 3 deletions

View File

@@ -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.

View File

@@ -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) {

View File

@@ -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');
});
});

View File

@@ -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');
});
});