Better Way#asGeoJSON

This commit is contained in:
John Firebaugh
2013-02-05 14:05:43 -08:00
parent 8735413974
commit 13a784bea5
2 changed files with 34 additions and 9 deletions

View File

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

View File

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