Convert iD.format.GeoJSON to member functions

This commit is contained in:
John Firebaugh
2013-01-26 21:01:44 -05:00
parent 50b997599d
commit 646c746991
8 changed files with 49 additions and 48 deletions

View File

@@ -1,29 +0,0 @@
iD.format.GeoJSON = {
mapping: function(entity, graph) {
if (this.mappings[entity.type]) {
return this.mappings[entity.type](entity, graph);
}
},
mappings: {
node: function(entity) {
return {
type: 'Feature',
properties: entity.tags,
geometry: {
type: 'Point',
coordinates: entity.loc
}
};
},
way: function(entity, graph) {
return {
type: 'Feature',
properties: entity.tags,
geometry: {
'type': 'LineString',
'coordinates': _.pluck(graph.childNodes(entity), 'loc')
}
};
}
}
};

View File

@@ -21,5 +21,16 @@ _.extend(iD.Node.prototype, {
move: function(loc) {
return this.update({loc: loc});
},
asGeoJSON: function() {
return {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'Point',
coordinates: this.loc
}
}
}
});

View File

@@ -83,5 +83,16 @@ _.extend(iD.Way.prototype, {
}
return this.update({nodes: nodes});
},
asGeoJSON: function(resolver) {
return {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'LineString',
coordinates: _.pluck(resolver.childNodes(this), 'loc')
}
};
}
});

View File

@@ -103,7 +103,6 @@
<script src='../js/id/controller/controller.js'></script>
<script src='../js/id/format/format.js'></script>
<script src='../js/id/format/geojson.js'></script>
<script src='../js/id/format/xml.js'></script>
<script src='../js/id/graph/entity.js'></script>

View File

@@ -47,7 +47,6 @@
<script src="spec/behavior/hover.js"></script>
<script src="spec/format/geojson.js"></script>
<script src="spec/format/xml.js"></script>
<script src="spec/geo/extent.js"></script>

View File

@@ -1,17 +0,0 @@
describe('iD.format.GeoJSON', function() {
describe('#mapping', function() {
it('converts a node to GeoJSON', function() {
var node = iD.Node({loc: [-77, 38]}),
graph = iD.Graph([node]);
expect(iD.format.GeoJSON.mapping(node, graph).geometry.type).to.equal('Point');
});
it('converts a way to GeoJSON', function() {
var way = iD.Way(),
graph = iD.Graph([way]),
json = iD.format.GeoJSON.mapping(way, graph);
expect(json.type).to.equal('Feature');
expect(json.geometry.type).to.equal('LineString');
});
});
});

View File

@@ -51,4 +51,16 @@ describe('iD.Node', function () {
expect(node.geometry(graph)).to.equal('point');
});
});
describe("#asGeoJSON", function () {
it("converts to a GeoJSON Point features", function () {
var node = iD.Node({tags: {amenity: 'cafe'}, loc: [1, 2]}),
json = node.asGeoJSON();
expect(json.type).to.equal('Feature');
expect(json.properties).to.eql({amenity: 'cafe'});
expect(json.geometry.type).to.equal('Point');
expect(json.geometry.coordinates).to.eql([1, 2]);
});
});
});

View File

@@ -199,4 +199,19 @@ describe('iD.Way', function() {
expect(w.removeNode('a').nodes).to.eql(['b', 'c', 'd', 'b']);
});
});
describe("#asGeoJSON", function () {
it("converts 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]}),
graph = iD.Graph([a, b, w]),
json = w.asGeoJSON(graph);
expect(json.type).to.equal('Feature');
expect(json.properties).to.eql({highway: 'residential'});
expect(json.geometry.type).to.equal('LineString');
expect(json.geometry.coordinates).to.eql([[1, 2], [3, 4]]);
});
});
});