Methods for programmatically manipulating sketches while digitizing features. r=bartvde (closes #3343)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@12103 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2011-06-17 18:59:16 +00:00
parent abdb336354
commit 2cf3f62d1b
12 changed files with 842 additions and 5 deletions

View File

@@ -121,6 +121,106 @@ OpenLayers.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control, {
this.events.triggerEvent("featureadded",{feature : feature});
}
},
/**
* APIMethod: insertXY
* Insert a point in the current sketch given x & y coordinates.
*
* Parameters:
* x - {Number} The x-coordinate of the point.
* y - {Number} The y-coordinate of the point.
*/
insertXY: function(x, y) {
if (this.handler && this.handler.line) {
this.handler.insertXY(x, y);
}
},
/**
* APIMethod: insertDeltaXY
* Insert a point given offsets from the previously inserted point.
*
* Parameters:
* dx - {Number} The x-coordinate offset of the point.
* dy - {Number} The y-coordinate offset of the point.
*/
insertDeltaXY: function(dx, dy) {
if (this.handler && this.handler.line) {
this.handler.insertDeltaXY(dx, dy);
}
},
/**
* APIMethod: insertDirectionLength
* Insert a point in the current sketch given a direction and a length.
*
* Parameters:
* direction - {Number} Degrees clockwise from the positive x-axis.
* length - {Number} Distance from the previously drawn point.
*/
insertDirectionLength: function(direction, length) {
if (this.handler && this.handler.line) {
this.handler.insertDirectionLength(direction, length);
}
},
/**
* APIMethod: insertDeflectionLength
* Insert a point in the current sketch given a deflection and a length.
* The deflection should be degrees clockwise from the previously
* digitized segment.
*
* Parameters:
* deflection - {Number} Degrees clockwise from the previous segment.
* length - {Number} Distance from the previously drawn point.
*/
insertDeflectionLength: function(deflection, length) {
if (this.handler && this.handler.line) {
this.handler.insertDeflectionLength(deflection, length);
}
},
/**
* APIMethod: undo
* Remove the most recently added point in the current sketch geometry.
*
* Returns:
* {Boolean} An edit was undone.
*/
undo: function() {
return this.handler.undo && this.handler.undo();
},
/**
* APIMethod: redo
* Reinsert the most recently removed point resulting from an <undo> call.
* The undo stack is deleted whenever a point is added by other means.
*
* Returns:
* {Boolean} An edit was redone.
*/
redo: function() {
return this.handler.redo && this.handler.redo();
},
/**
* APIMethod: finishSketch
* Finishes the sketch without including the currently drawn point.
* This method can be called to terminate drawing programmatically
* instead of waiting for the user to end the sketch.
*/
finishSketch: function() {
this.handler.finishGeometry();
},
/**
* APIMethod: cancel
* Cancel the current sketch. This removes the current sketch and keeps
* the drawing control active.
*/
cancel: function() {
this.handler.cancel();
},
CLASS_NAME: "OpenLayers.Control.DrawFeature"
});

View File

@@ -183,14 +183,20 @@ OpenLayers.Geometry.Collection = OpenLayers.Class(OpenLayers.Geometry, {
*
* Parameters:
* components - {Array(<OpenLayers.Geometry>)} The components to be removed
*
* Returns:
* {Boolean} A component was removed.
*/
removeComponents: function(components) {
var removed = false;
if(!(OpenLayers.Util.isArray(components))) {
components = [components];
}
for(var i=components.length-1; i>=0; --i) {
this.removeComponent(components[i]);
removed = this.removeComponent(components[i]) || removed;
}
return removed;
},
/**
@@ -199,6 +205,9 @@ OpenLayers.Geometry.Collection = OpenLayers.Class(OpenLayers.Geometry, {
*
* Parameters:
* component - {<OpenLayers.Geometry>}
*
* Returns:
* {Boolean} The component was removed.
*/
removeComponent: function(component) {
@@ -207,6 +216,7 @@ OpenLayers.Geometry.Collection = OpenLayers.Class(OpenLayers.Geometry, {
// clearBounds() so that it gets recalculated on the next call
// to this.getBounds();
this.clearBounds();
return true;
},
/**

View File

@@ -37,12 +37,17 @@ OpenLayers.Geometry.LineString = OpenLayers.Class(OpenLayers.Geometry.Curve, {
*
* Parameters:
* point - {<OpenLayers.Geometry.Point>} The point to be removed
*
* Returns:
* {Boolean} The component was removed.
*/
removeComponent: function(point) {
if ( this.components && (this.components.length > 2)) {
var removed = this.components && (this.components.length > 2);
if (removed) {
OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,
arguments);
}
return removed;
},
/**

View File

@@ -91,10 +91,13 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
*
* Parameters:
* point - {<OpenLayers.Geometry.Point>}
*
* Returns:
* {Boolean} The component was removed.
*/
removeComponent: function(point) {
if (this.components.length > 3) {
var removed = this.components && (this.components.length > 3);
if (removed) {
//remove last point
this.components.pop();
@@ -106,6 +109,7 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
[firstPoint]);
}
return removed;
},
/**

View File

@@ -67,6 +67,12 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
*/
timerId: null,
/**
* Property: redoStack
* {Array} Stack containing points removed with <undo>.
*/
redoStack: null,
/**
* Constructor: OpenLayers.Handler.Path
* Create a new path hander
@@ -170,6 +176,129 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
this.callback("point", [this.point.geometry, this.getGeometry()]);
this.callback("modify", [this.point.geometry, this.getSketch()]);
this.drawFeature();
delete this.redoStack;
},
/**
* Method: insertXY
* Insert a point in the current sketch given x & y coordinates. The new
* point is inserted immediately before the most recently drawn point.
*
* Parameters:
* x - {Number} The x-coordinate of the point.
* y - {Number} The y-coordinate of the point.
*/
insertXY: function(x, y) {
this.line.geometry.addComponent(
new OpenLayers.Geometry.Point(x, y),
this.getCurrentPointIndex()
);
this.drawFeature();
delete this.redoStack;
},
/**
* Method: insertDeltaXY
* Insert a point given offsets from the previously inserted point.
*
* Parameters:
* dx - {Number} The x-coordinate offset of the point.
* dy - {Number} The y-coordinate offset of the point.
*/
insertDeltaXY: function(dx, dy) {
var previousIndex = this.getCurrentPointIndex() - 1;
var p0 = this.line.geometry.components[previousIndex];
if (p0 && !isNaN(p0.x) && !isNaN(p0.y)) {
this.insertXY(p0.x + dx, p0.y + dy);
}
},
/**
* Method: insertDirectionLength
* Insert a point in the current sketch given a direction and a length.
*
* Parameters:
* direction - {Number} Degrees clockwise from the positive x-axis.
* length - {Number} Distance from the previously drawn point.
*/
insertDirectionLength: function(direction, length) {
direction *= Math.PI / 180;
var dx = length * Math.cos(direction);
var dy = length * Math.sin(direction);
this.insertDeltaXY(dx, dy);
},
/**
* Method: insertDeflectionLength
* Insert a point in the current sketch given a deflection and a length.
* The deflection should be degrees clockwise from the previously
* digitized segment.
*
* Parameters:
* deflection - {Number} Degrees clockwise from the previous segment.
* length - {Number} Distance from the previously drawn point.
*/
insertDeflectionLength: function(deflection, length) {
var previousIndex = this.getCurrentPointIndex() - 1;
if (previousIndex > 0) {
var p1 = this.line.geometry.components[previousIndex];
var p0 = this.line.geometry.components[previousIndex-1];
var theta = Math.atan2(p1.y - p0.y, p1.x - p0.x);
this.insertDirectionLength(
(theta * 180 / Math.PI) + deflection, length
);
}
},
/**
* Method: getCurrentPointIndex
*
* Returns:
* {Number} The index of the most recently drawn point.
*/
getCurrentPointIndex: function() {
return this.line.geometry.components.length - 1;
},
/**
* Method: undo
* Remove the most recently added point in the sketch geometry.
*
* Returns:
* {Boolean} A point was removed.
*/
undo: function() {
var geometry = this.line.geometry;
var components = geometry.components;
var index = this.getCurrentPointIndex() - 1;
var target = components[index];
var undone = geometry.removeComponent(target);
if (undone) {
if (!this.redoStack) {
this.redoStack = [];
}
this.redoStack.push(target);
this.drawFeature();
}
return undone;
},
/**
* Method: redo
* Reinsert the most recently removed point resulting from an <undo> call.
* The undo stack is deleted whenever a point is added by other means.
*
* Returns:
* {Boolean} A point was added.
*/
redo: function() {
var target = this.redoStack && this.redoStack.pop();
if (target) {
this.line.geometry.addComponent(target, this.getCurrentPointIndex());
this.drawFeature();
}
return !!target;
},
/**

View File

@@ -127,7 +127,17 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
}
OpenLayers.Handler.Path.prototype.addPoint.apply(this, arguments);
},
/**
* Method: getCurrentPointIndex
*
* Returns:
* {Number} The index of the most recently drawn point.
*/
getCurrentPointIndex: function() {
return this.line.geometry.components.length - 2;
},
/**
* Method: enforceTopology
* Simple topology enforcement for drawing interior rings. Ensures vertices