Work on road drawing, index ways on demand

This commit is contained in:
Tom MacWright
2012-11-28 13:59:28 -05:00
parent 51370330f2
commit c1a87ed8b6
5 changed files with 56 additions and 59 deletions
+36 -39
View File
@@ -63,9 +63,8 @@ iD.modes.AddRoad = {
.append('g').attr('id', 'addroad');
teaser.append('circle')
.attr('class', 'handle')
.style('pointer-events', 'none')
.attr('r', 3);
.attr({ 'class': 'handle', r: 3 })
.style('pointer-events', 'none');
surface.on('mousemove.addroad', function() {
teaser.attr('transform', function() {
@@ -75,9 +74,9 @@ iD.modes.AddRoad = {
});
surface.on('click.addroad', function() {
var t = d3.select(d3.event.target);
var node;
var way = this.way();
var t = d3.select(d3.event.target),
node,
way = this.way();
// connect a way to an existing way
if (t.data() && t.data()[0] && t.data()[0].type === 'node') {
@@ -87,10 +86,11 @@ iD.modes.AddRoad = {
d3.mouse(surface.node())));
}
this.map.perform(iD.actions.startWay(way));
way.nodes.push(node.id);
this.map.perform(iD.actions.changeWayNodes(way, node));
this.map.selectClick(way);
this.controller.enter(iD.modes.DrawRoad(way));
this.controller.enter(iD.modes.DrawRoad(way.id));
}.bind(this));
d3.select(document).on('keydown.addroad', function() {
@@ -105,48 +105,45 @@ iD.modes.AddRoad = {
}
};
iD.modes.DrawRoad = function(way) {
iD.modes.DrawRoad = function(way_id) {
return {
enter: function() {
var surface = this.map.surface;
var lastNode = this.map.history.graph().entity(way.nodes[way.nodes.length - 1]);
this.nextnode = iD.modes._node([lastNode.lon, lastNode.lat]);
way.nodes.push(this.nextnode.id);
this.map.perform(iD.actions.changeWayNodes(way, this.nextnode));
var surface = this.map.surface,
nextnode = iD.modes._node([NaN, NaN]);
var nextnode_id = nextnode.id;
var way = this.map.history.graph().entity(way_id);
way.nodes.push(nextnode_id);
this.map.perform(iD.actions.changeWayNodes(way, nextnode));
surface.on('mousemove.drawroad', function() {
var ll = this.map.projection.invert(d3.mouse(surface.node()));
this.map.history.replace(iD.actions.move(this.nextnode, ll));
this.map.update();
var way = this.map.history.graph().entity(way_id);
var node = iD.Entity(this.map.history.graph().entity(nextnode_id), {
lon: ll[0],
lat: ll[1]
});
this.map.history.replace(iD.actions.changeWayNodes(way, node));
var only = iD.Util.trueObj([way.id].concat(_.pluck(way.nodes, 'id')));
this.map.redraw(only);
}.bind(this));
surface.on('click.drawroad', function() {
if (this._doubleTime) {
window.clearTimeout(this._doubleTime);
this._doubleTime = null;
var t = d3.select(d3.event.target);
d3.event.stopPropagation();
// connect a way to an existing way
if (t.data() && t.data()[0] && t.data()[0].type === 'node') {
node = t.data()[0];
} else {
// forgive me
var that = this;
this._doubleTime = window.setTimeout(function(e) {
return function() {
d3.event = e;
var t = d3.select(d3.event.target);
d3.event.stopPropagation();
// connect a way to an existing way
if (t.data() && t.data()[0] && t.data()[0].type === 'node') {
node = t.data()[0];
} else {
node = iD.modes._node(that.map.projection.invert(
d3.mouse(surface.node())));
}
way.nodes.pop();
way.nodes.push(node.id);
that.map.perform(iD.actions.changeWayNodes(way, node));
way.nodes = way.nodes.slice();
that.controller.enter(iD.modes.DrawRoad(way));
};
}(_.clone(d3.event)), 150);
node = iD.modes._node(this.map.projection.invert(
d3.mouse(surface.node())));
}
way.nodes.pop();
way.nodes.push(node.id);
this.map.perform(iD.actions.changeWayNodes(way, node));
way.nodes = way.nodes.slice();
this.controller.enter(iD.modes.DrawRoad(way));
}.bind(this));
surface.on('dblclick.drawroad', function() {
+1 -1
View File
@@ -38,7 +38,7 @@ iD.Node = function (attrs) {
};
iD.Way = function (attrs) {
return iD.Entity(_.extend({}, attrs, {type: 'way'}));
return iD.Entity(_.extend({}, attrs, {type: 'way', nodes: []}));
};
iD.Relation = function (attrs) {
+17 -17
View File
@@ -4,22 +4,6 @@ iD.Graph = function(entities, annotation) {
this.entities = entities || {};
this.annotation = annotation;
for (var id in this.entities) {
// TODO: update extents that need it.
if (this.entities[id].type === 'way' && !this.entities[id]._extent) {
// top left, bottom right
var extent = [[-Infinity, Infinity], [Infinity, -Infinity]];
var w = this.fetch(id);
for (var j = 0, l = w.nodes.length; j < l; j++) {
if (w.nodes[j].lon > extent[0][0]) extent[0][0] = w.nodes[j].lon;
if (w.nodes[j].lon < extent[1][0]) extent[1][0] = w.nodes[j].lon;
if (w.nodes[j].lat < extent[0][1]) extent[0][1] = w.nodes[j].lat;
if (w.nodes[j].lat > extent[1][1]) extent[1][1] = w.nodes[j].lat;
}
this.entities[id]._extent = extent;
}
}
if (iD.debug) {
Object.freeze(this);
Object.freeze(this.entities);
@@ -72,6 +56,22 @@ iD.Graph.prototype = {
entity._extent[1][1] > extent[1][1];
},
indexWay: function(way) {
if (way.type === 'way' && !way._extent) {
// top left, bottom right
var extent = [[-Infinity, Infinity], [Infinity, -Infinity]];
var w = way;
for (var j = 0, l = w.nodes.length; j < l; j++) {
if (w.nodes[j].lon > extent[0][0]) extent[0][0] = w.nodes[j].lon;
if (w.nodes[j].lon < extent[1][0]) extent[1][0] = w.nodes[j].lon;
if (w.nodes[j].lat < extent[0][1]) extent[0][1] = w.nodes[j].lat;
if (w.nodes[j].lat > extent[1][1]) extent[1][1] = w.nodes[j].lat;
}
way._extent = extent;
}
return true;
},
// get all objects that intersect an extent.
intersects: function(extent) {
var items = [];
@@ -81,7 +81,7 @@ iD.Graph.prototype = {
items.push(entity);
} else if (entity.type === 'way') {
var w = this.fetch(entity.id);
if (this.wayIntersect(w, extent)) items.push(w);
if (this.indexWay(w) && this.wayIntersect(w, extent)) items.push(w);
}
}
return items;
+1 -1
View File
@@ -73,7 +73,7 @@ var iD = function(container) {
var l = iD.loading('committing changes to openstreetmap');
connection.putChangeset(map.history.changes(), e.comment, function() {
l.remove();
map.history = new iD.History();
map.history = iD.History();
map.flush();
map.update();
map.redraw();
+1 -1
View File
@@ -424,7 +424,7 @@ iD.Map = function(elem, connection) {
}
function update() {
map.update();
// map.update();
redraw();
}