Relation#asGeoJSON

This commit is contained in:
John Firebaugh
2013-02-05 10:58:41 -08:00
parent 72cd6b91fa
commit 8735413974
2 changed files with 53 additions and 0 deletions
+25
View File
@@ -83,6 +83,31 @@ _.extend(iD.Relation.prototype, {
return r;
},
asGeoJSON: function(resolver) {
if (this.isMultipolygon()) {
return {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'MultiPolygon',
coordinates: this.multipolygon(resolver)
}
};
} else {
return {
type: 'FeatureCollection',
properties: this.tags,
features: this.members.map(function(member) {
return _.extend({role: member.role}, resolver.entity(member.id).asGeoJSON(resolver));
})
};
}
},
isMultipolygon: function() {
return this.tags.type === 'multipolygon';
},
isRestriction: function() {
return !!(this.tags.type && this.tags.type.match(/^restriction:?/));
},
+28
View File
@@ -146,6 +146,34 @@ describe('iD.Relation', function () {
});
});
describe("#asGeoJSON", function (){
it('converts a multipolygon to a GeoJSON MultiPolygon feature', function() {
var a = iD.Node({loc: [1, 1]}),
b = iD.Node({loc: [2, 2]}),
c = iD.Node({loc: [3, 3]}),
w = iD.Way({nodes: [a.id, b.id, c.id, a.id]}),
r = iD.Relation({tags: {type: 'multipolygon'}, members: [{id: w.id, type: 'way'}]}),
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([[[[1, 1], [2, 2], [3, 3], [1, 1]]]]);
});
it('converts a relation to a GeoJSON FeatureCollection', function() {
var a = iD.Node({loc: [1, 1]}),
r = iD.Relation({tags: {type: 'type'}, members: [{id: a.id, role: 'role'}]}),
g = iD.Graph([a, r]),
json = r.asGeoJSON(g);
expect(json.type).to.equal('FeatureCollection');
expect(json.properties).to.eql({type: 'type'});
expect(json.features).to.eql([_.extend({role: 'role'}, a.asGeoJSON(g))]);
});
});
describe("#multipolygon", function () {
specify("single polygon consisting of a single way", function () {
var a = iD.Node({loc: [1, 1]}),