Split more functionality into util.geo and test it.

This commit is contained in:
Tom MacWright
2012-12-04 16:52:59 -05:00
parent 851efdf01f
commit 340b97c0e9
3 changed files with 42 additions and 22 deletions

View File

@@ -8,18 +8,6 @@ iD.modes._node = function(ll) {
});
};
iD.modes.chooseIndex = function(way, point, map) {
var dist = iD.util.geo.dist;
var projNodes = way.nodes.map(function(n) {
return map.projection([n.lon, n.lat]);
});
for (var i = 0, changes = []; i < projNodes.length - 1; i++) {
changes[i] =
(dist(projNodes[i], point) + dist(point, projNodes[i + 1])) /
dist(projNodes[i], projNodes[i + 1]);
}
return _.indexOf(changes, _.min(changes)) + 1;
};
iD.modes.AddPlace = {
id: 'add-place',
@@ -114,7 +102,7 @@ iD.modes.AddRoad = {
node = t.data()[0];
// snap into an existing way
} else if (t.data() && t.data()[0] && t.data()[0].type === 'way') {
var index = iD.modes.chooseIndex(t.data()[0], d3.mouse(surface.node()), this.map);
var index = iD.util.geo.chooseIndex(t.data()[0], d3.mouse(surface.node()), this.map);
node = iD.modes._node(this.map.projection.invert(
d3.mouse(surface.node())));
var connectedWay = this.history.graph().entity(t.data()[0].id);

View File

@@ -88,3 +88,16 @@ iD.util.geo.nodeIntersect = function(entity, extent) {
entity.lat < extent[0][1] &&
entity.lat > extent[1][1];
};
iD.util.geo.chooseIndex = function(way, point, map) {
var dist = iD.util.geo.dist;
var projNodes = way.nodes.map(function(n) {
return map.projection([n.lon, n.lat]);
});
for (var i = 0, changes = []; i < projNodes.length - 1; i++) {
changes[i] =
(dist(projNodes[i], point) + dist(point, projNodes[i + 1])) /
dist(projNodes[i], projNodes[i + 1]);
}
return _.indexOf(changes, _.min(changes)) + 1;
};

View File

@@ -22,16 +22,35 @@ describe('Util', function() {
expect(iD.util.friendlyName({ tags: { name: 'hi', highway: 'Route 5' }})).to.equal('hi');
});
describe('#interp', function() {
it('interpolates halfway', function() {
var a = { lat: 0, lon: 0 },
b = { lat: 10, lon: 10 };
expect(iD.util.geo.interp(a, b, 0.5)).to.eql({ lat: 5, lon: 5});
describe('geo', function() {
describe('#interp', function() {
it('interpolates halfway', function() {
var a = { lat: 0, lon: 0 },
b = { lat: 10, lon: 10 };
expect(iD.util.geo.interp(a, b, 0.5)).to.eql({ lat: 5, lon: 5});
});
it('interpolates to one side', function() {
var a = { lat: 0, lon: 0 },
b = { lat: 10, lon: 10 };
expect(iD.util.geo.interp(a, b, 0)).to.eql({ lat: 0, lon: 0});
});
});
it('interpolates to one side', function() {
var a = { lat: 0, lon: 0 },
b = { lat: 10, lon: 10 };
expect(iD.util.geo.interp(a, b, 0)).to.eql({ lat: 0, lon: 0});
describe('#dist', function() {
it('distance between two same points is zero', function() {
var a = [0, 0],
b = [0, 0];
expect(iD.util.geo.dist(a, b)).to.eql(0);
});
it('a straight 10 unit line is 10', function() {
var a = [0, 0],
b = [10, 0];
expect(iD.util.geo.dist(a, b)).to.eql(10);
});
it('a pythagorean triangle is right', function() {
var a = [0, 0],
b = [4, 3];
expect(iD.util.geo.dist(a, b)).to.eql(5);
});
});
});
});