mirror of
https://github.com/FoggedLens/iD.git
synced 2026-02-13 01:02:58 +00:00
Convert iD.format.GeoJSON to member functions
This commit is contained in:
@@ -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')
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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')
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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]]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user