Modify away! This was a long time coming. Thanks all for contributions. The ModifyFeature control lets you select a feature for modification, drag around vertices (or the whole feature in the case of a point), delete vertices, and add new vertices (closes #941).

git-svn-id: http://svn.openlayers.org/trunk/openlayers@4272 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2007-09-13 23:22:06 +00:00
parent 9724bbaeee
commit dd8d895460
5 changed files with 1024 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Modify Feature</title>
<style type="text/css">
#map {
width: 512px;
height: 350px;
border: 1px solid gray;
}
#controls {
width: 512px;
}
#controlToggle {
padding-left: 1em;
}
#controlToggle li {
list-style: none;
}
</style>
<script src="../lib/Firebug/firebug.js"></script>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript">
var map, vectors, controls;
function init(){
map = new OpenLayers.Map('map');
var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'});
OpenLayers.Feature.Vector.style['default']['strokeWidth'] = '2';
vectors = new OpenLayers.Layer.Vector("Vector Layer");
map.addLayers([wms, vectors]);
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.addControl(new OpenLayers.Control.MousePosition());
var modifyOptions = {
onModificationStart: function(feature) {
OpenLayers.Console.log("start modifying", feature.id);
},
onModification: function(feature) {
OpenLayers.Console.log("modified", feature.id);
},
onModificationEnd: function(feature) {
OpenLayers.Console.log("end modifying", feature.id);
},
onDelete: function(feature) {
OpenLayers.Console.log("delete", feature.id);
}
};
controls = {
point: new OpenLayers.Control.DrawFeature(vectors,
OpenLayers.Handler.Point),
line: new OpenLayers.Control.DrawFeature(vectors,
OpenLayers.Handler.Path),
polygon: new OpenLayers.Control.DrawFeature(vectors,
OpenLayers.Handler.Polygon),
modify: new OpenLayers.Control.ModifyFeature(vectors,
modifyOptions)
};
for(var key in controls) {
map.addControl(controls[key]);
}
map.setCenter(new OpenLayers.LonLat(0, 0), 3);
document.getElementById('noneToggle').checked = true;
}
function toggleControl(element) {
for(key in controls) {
var control = controls[key];
if(element.value == key && element.checked) {
control.activate();
} else {
control.deactivate();
}
}
}
</script>
</head>
<body onload="init()">
<h3>OpenLayers Modify Feature Example</h3>
<div id="map"></div>
<div id="controls">
<ul id="controlToggle">
<li>
<input type="radio" name="type" value="none" id="noneToggle"
onclick="toggleControl(this);" checked="checked" />
<label for="noneToggle">navigate</label>
</li>
<li>
<input type="radio" name="type" value="point" id="pointToggle" onclick="toggleControl(this);" />
<label for="pointToggle">draw point</label>
</li>
<li>
<input type="radio" name="type" value="line" id="lineToggle" onclick="toggleControl(this);" />
<label for="lineToggle">draw line</label>
</li>
<li>
<input type="radio" name="type" value="polygon" id="polygonToggle" onclick="toggleControl(this);" />
<label for="polygonToggle">draw polygon</label>
</li>
<li>
<input type="radio" name="type" value="modify" id="modifyToggle"
onclick="toggleControl(this);" />
<label for="modifyToggle">modify feature</label>
</li>
</ul>
</div>
</body>
</html>

View File

@@ -142,6 +142,7 @@
"OpenLayers/Control/LayerSwitcher.js",
"OpenLayers/Control/DrawFeature.js",
"OpenLayers/Control/DragFeature.js",
"OpenLayers/Control/ModifyFeature.js",
"OpenLayers/Control/Panel.js",
"OpenLayers/Control/SelectFeature.js",
"OpenLayers/Geometry.js",

View File

@@ -0,0 +1,462 @@
/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
* See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
* for the full text of the license. */
/**
* @requires OpenLayers/Control/DragFeature.js
* @requires OpenLayers/Control/SelectFeature.js
* @requires OpenLayers/Handler/Keyboard.js
*
* Class: OpenLayers.Control.ModifyFeature
* Control to modify features. When activated, a click renders the vertices
* of a feature - these vertices can then be dragged. By default, the
* delete key will delete the vertex under the mouse. New features are
* added by dragging "virtual vertices" between vertices. Create a new
* control with the <OpenLayers.Control.ModifyFeature> constructor.
*
* Inherits From:
* - <OpenLayers.Control>
*/
OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
/**
* APIProperty: geometryTypes
* {Array(String)} To restrict modification to a limited set of geometry
* types, send a list of strings corresponding to the geometry class
* names.
*/
geometryTypes: null,
/**
* Property: layer
* {OpenLayers.Layer.Vector}
*/
layer: null,
/**
* Property: feature
* {<OpenLayers.Feature.Vector>} Feature currently available for modification.
*/
feature: null,
/**
* Property: vertices
* {Array(<OpenLayers.Feature.Vector>)} Verticies currently available
* for dragging.
*/
vertices: null,
/**
* Property: virtualVertices
* {Array(<OpenLayers.Feature.Vector>)} Virtual vertices in the middle
* of each edge.
*/
virtualVertices: null,
/**
* Property: selectControl
* {<OpenLayers.Control.Select>}
*/
selectControl: null,
/**
* Property: dragControl
* {<OpenLayers.Control.DragFeature>}
*/
dragControl: null,
/**
* Property: keyboardHandler
* {<OpenLayers.Handler.Keyboard>}
*/
keyboardHandler: null,
/**
* APIProperty: deleteCodes
* {Array(Integer)} Keycodes for deleting verticies. Set to null to disable
* vertex deltion by keypress. If non-null, keypresses with codes
* in this array will delete vertices under the mouse. Default
* is 46 and 100, the 'delete' and lowercase 'd' keys.
*/
deleteCodes: null,
/**
* APIProperty: virtualStyle
* {<OpenLayers.Feature.Vector.Style>}
*/
virtualStyle: null,
/**
* APIProperty: onModificationStart
* {Function} Optional function to be called when a feature is selected
* to be modified. The function should expect to be called with a
* feature. This could be used for example to allow to lock the
* feature on server-side.
*/
onModificationStart: function() {},
/**
* APIProperty: onModification
* {Function} Optional function to be called when a feature has been
* modified. The function should expect to be called with a feature.
*/
onModification: function() {},
/**
* APIProperty: onModificationEnd
* {Function} Optional function to be called when a feature is finished
* being modified. The function should expect to be called with a
* feature.
*/
onModificationEnd: function() {},
/**
* Constructor: OpenLayers.Control.ModifyFeature
* Create a new modify feature control.
*
* Parameters:
* layer - {OpenLayers.Layer.Vector} Layer that contains features that
* will be modified.
* options - {Object} Optional object whose properties will be set on the
* control.
*/
initialize: function(layer, options) {
this.layer = layer;
this.vertices = [];
this.virtualVertices = [];
this.styleVirtual = OpenLayers.Util.extend({}, this.layer.style);
this.styleVirtual.fillOpacity = 0.3;
this.styleVirtual.strokeOpacity = 0.3;
this.deleteCodes = [46, 100];
OpenLayers.Control.prototype.initialize.apply(this, [options]);
if(!(this.deleteCodes instanceof Array)) {
this.deleteCodes = [this.deleteCodes];
}
var control = this;
// configure the select control
var selectOptions = {
geometryTypes: this.geometryTypes,
onSelect: function(feature) {
control.selectFeature.apply(control, [feature]);
},
onUnselect: function(feature) {
control.unselectFeature.apply(control, [feature]);
}
};
this.selectControl = new OpenLayers.Control.SelectFeature(
layer, selectOptions
);
// configure the drag control
var dragOptions = {
geometryTypes: ["OpenLayers.Geometry.Point"],
snappingOptions: this.snappingOptions,
onStart: function(feature, pixel) {
control.dragStart.apply(control, [feature, pixel]);
},
onDrag: function(feature) {
control.dragVertex.apply(control, [feature]);
},
onComplete: function(feature) {
control.dragComplete.apply(control, [feature]);
}
};
this.dragControl = new OpenLayers.Control.DragFeature(
layer, dragOptions
);
// configure the keyboard handler
var keyboardOptions = {
keypress: this.handleKeypress
};
this.keyboardHandler = new OpenLayers.Handler.Keyboard(
this, keyboardOptions
);
},
/**
* APIMethod: destroy
* Take care of things that are not handled in superclass.
*/
destroy: function() {
this.layer = null;
this.selectControl.destroy();
this.dragControl.destroy();
this.keyboardHandler.destroy();
OpenLayers.Control.prototype.destroy.apply(this, []);
},
/**
* APIMethod: activate
* Activate the control and the feature handler.
*
* Returns:
* {Boolean} Successfully activated the control and feature handler.
*/
activate: function() {
return (this.selectControl.activate() &&
this.keyboardHandler.activate() &&
OpenLayers.Control.prototype.activate.apply(this, arguments));
},
/**
* APIMethod: deactivate
* Deactivate the controls.
*
* Returns:
* {Boolean} Successfully deactivated the control.
*/
deactivate: function() {
var deactivated = false;
// the return from the controls is unimportant in this case
if(OpenLayers.Control.prototype.deactivate.apply(this, arguments)) {
this.layer.removeFeatures(this.vertices);
this.layer.removeFeatures(this.virtualVertices);
this.vertices = [];
this.dragControl.deactivate();
if(this.feature) {
this.selectControl.unselect.apply(this.selectControl,
[this.feature]);
}
this.selectControl.deactivate();
this.keyboardHandler.deactivate();
deactivated = true;
}
return deactivated;
},
/**
* Method: selectFeature
* Called when the select feature control selects a feature.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The selected feature.
*/
selectFeature: function(feature) {
this.feature = feature;
this.resetVertices();
this.dragControl.activate();
this.onModificationStart(this.feature);
},
/**
* Method: unselectFeature
* Called when the select feature control unselects a feature.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The unselected feature.
*/
unselectFeature: function(feature) {
this.layer.removeFeatures(this.vertices);
this.layer.removeFeatures(this.virtualVertices);
this.vertices = [];
this.virtualVertices = [];
this.feature = null;
this.dragControl.deactivate();
this.onModificationEnd(feature);
},
/**
* Method: dragStart
* Called by the drag feature control with before a feature is dragged.
* This method is used to differentiate between points and vertices
* of higher order geometries. This respects the <geometryTypes>
* property and forces a select of points when the drag control is
* already active (and stops events from propagating to the select
* control).
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The point or vertex about to be
* dragged.
* pixel - {<OpenLayers.Pixel>} Pixel location of the mouse event.
*/
dragStart: function(feature, pixel) {
// only change behavior if the feature is not in the vertices array
if(feature != this.feature &&
OpenLayers.Util.indexOf(this.vertices, feature) == -1 &&
OpenLayers.Util.indexOf(this.virtualVertices, feature) == -1) {
if(this.feature) {
// unselect the currently selected feature
this.selectControl.clickFeature.apply(this.selectControl,
[this.feature]);
}
// check any constraints on the geometry type
if(this.geometryTypes == null ||
OpenLayers.Util.indexOf(this.geometryTypes,
feature.geometry.CLASS_NAME) != -1) {
// select the point
this.selectControl.clickFeature.apply(this.selectControl,
[feature]);
/**
* TBD: These lines improve workflow by letting the user
* immediately start dragging after the mouse down.
* However, it is very ugly to be messing with controls
* and their handlers in this way. I'd like a better
* solution if the workflow change is necessary.
*/
// prepare the point for dragging
this.dragControl.overFeature.apply(this.dragControl,
[feature]);
this.dragControl.lastPixel = pixel;
this.dragControl.dragHandler.started = true;
this.dragControl.dragHandler.start = pixel;
this.dragControl.dragHandler.last = pixel;
}
}
},
/**
* Method: dragVertex
* Called by the drag feature control with each drag move of a vertex.
*
* Parameters:
* vertex - {<OpenLayers.Feature.Vector>} The vertex being dragged.
*/
dragVertex: function(vertex) {
if(this.feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
if(this.feature != vertex) {
this.feature = vertex;
}
} else {
if(OpenLayers.Util.indexOf(this.virtualVertices, vertex) != -1) {
vertex.geometry.parent.addComponent(vertex.geometry,
vertex._index);
delete vertex._index;
OpenLayers.Util.removeItem(this.virtualVertices, vertex);
this.layer.removeFeatures(vertex);
}
}
this.layer.drawFeature(this.feature, this.selectControl.selectStyle);
this.layer.removeFeatures(this.virtualVertices);
// keep the vertex on top so it gets the mouseout after dragging
// this should be removed in favor of an option to draw under or
// maintain node z-index
this.layer.drawFeature(vertex);
},
/**
* Method: dragComplete
* Called by the drag feature control when the feature dragging is complete.
*
* Parameters:
* vertex - {<OpenLayers.Feature.Vector>} The vertex being dragged.
*/
dragComplete: function(vertex) {
this.resetVertices();
this.onModification(this.feature);
},
/**
* Method: resetVertices
*/
resetVertices: function() {
if(this.vertices.length > 0) {
this.layer.removeFeatures(this.vertices);
this.vertices = [];
}
if(this.virtualVertices.length > 0) {
this.layer.removeFeatures(this.virtualVertices);
this.virtualVertices = [];
}
if(this.feature &&
this.feature.geometry.CLASS_NAME != "OpenLayers.Geometry.Point") {
this.collectVertices(this.feature.geometry);
this.layer.addFeatures(this.vertices);
this.layer.addFeatures(this.virtualVertices);
}
},
/**
* Method: handleKeypress
* Called by the feature handler on keypress. This is used to delete
* vertices and point features. If the <deleteCode> property is set,
* vertices and points will be deleted when a feature is selected
* for modification and the mouse is over a vertex.
*
* Parameters:
* {Integer} Key code corresponding to the keypress event.
*/
handleKeypress: function(code) {
// check for delete key
if(this.feature &&
OpenLayers.Util.indexOf(this.deleteCodes, code) != -1) {
var vertex = this.dragControl.feature;
if(vertex &&
OpenLayers.Util.indexOf(this.vertices, vertex) != -1) {
// remove the vertex
vertex.geometry.parent.removeComponent(vertex.geometry);
this.layer.drawFeature(this.feature,
this.selectControl.selectStyle);
this.resetVertices();
this.onModification(this.feature);
}
}
},
/**
* Method: collectVertices
* Collect the vertices from the modifiable feature's geometry and push
* them on to the control's vertices array.
*/
collectVertices: function() {
this.vertices = [];
this.virtualVirtices = [];
var control = this;
function collectComponentVertices(geometry) {
var i, vertex, component;
if(geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
vertex = new OpenLayers.Feature.Vector(geometry);
control.vertices.push(vertex);
} else {
for(i=0; i<geometry.components.length; ++i) {
component = geometry.components[i];
if(component.CLASS_NAME == "OpenLayers.Geometry.Point") {
vertex = new OpenLayers.Feature.Vector(component);
control.vertices.push(vertex);
} else {
collectComponentVertices(component);
}
}
// add virtual vertices in the middle of each edge
if(geometry.CLASS_NAME != "OpenLayers.Geometry.MultiPoint") {
for(i=0; i<geometry.components.length-1; ++i) {
var prevVertex = geometry.components[i];
var nextVertex = geometry.components[i + 1];
if(prevVertex.CLASS_NAME == "OpenLayers.Geometry.Point" &&
nextVertex.CLASS_NAME == "OpenLayers.Geometry.Point") {
var x = (prevVertex.x + nextVertex.x) / 2;
var y = (prevVertex.y + nextVertex.y) / 2;
var point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(x, y),
null, control.styleVirtual
);
// set the virtual parent and intended index
point.geometry.parent = geometry;
point._index = i + 1;
control.virtualVertices.push(point);
}
}
}
}
}
collectComponentVertices(this.feature.geometry);
},
/**
* Method: setMap
* Set the map property for the control and all handlers.
*
* Parameters:
* map - {OpenLayers.Map} The control's map.
*/
setMap: function(map) {
this.selectControl.setMap(map);
this.dragControl.setMap(map);
OpenLayers.Control.prototype.setMap.apply(this, arguments);
},
CLASS_NAME: "OpenLayers.Control.ModifyFeature"
});

View File

@@ -0,0 +1,448 @@
<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_ModifyFeature_constructor(t) {
t.plan(2);
var layer = "foo";
var options = {
geometryTypes: "bar"
};
var control = new OpenLayers.Control.ModifyFeature(layer, options);
t.eq(control.layer, "foo",
"constructor sets layer correctly");
t.eq(control.selectControl.geometryTypes, "bar",
"constructor sets options correctly on feature handler");
}
function test_ModifyFeature_destroy(t) {
t.plan(2);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.ModifyFeature(layer);
control.selectControl.destroy = function() {
t.ok(true,
"control.destroy calls destroy on select control");
}
control.dragControl.destroy = function() {
t.ok(true,
"control.destroy calls destroy on feature handler");
}
control.destroy();
}
function test_ModifyFeature_activate(t) {
t.plan(2);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.ModifyFeature(layer);
map.addControl(control);
t.ok(!control.selectControl.active,
"select control is not active prior to activating control");
control.activate();
t.ok(control.selectControl.active,
"select control is active after activating control");
}
function test_ModifyFeature_initDeleteCodes(t) {
t.plan(3);
var layer = new OpenLayers.Layer.Vector();
var control = new OpenLayers.Control.ModifyFeature(layer, {'deleteCodes': 46});
t.eq(control.deleteCodes[0], 46, "Delete code properly turned into an array.");
var control = new OpenLayers.Control.ModifyFeature(layer);
t.eq(control.deleteCodes[0], 46, "Default deleteCodes include delete");
t.eq(control.deleteCodes[1], 100, "Default deleteCodes include 'd'");
}
function test_ModifyFeature_handleKeypress(t) {
t.plan(8);
/**
* There are two things that we want to test here
* 1) test that control.deleteCodes are respected
* 3) test that a vertex is properly deleted
*
* In the future, feature deletion may be added to the control.
*/
var control = new OpenLayers.Control.ModifyFeature({style: null});
var delKey = 46;
var dKey = 100;
control.deleteCodes = [delKey, dKey];
//// test that point is deleted for all delete codes
//var point = new OpenLayers.Feature.Vector(
// new OpenLayers.Geometry.Point()
//);
//// mock up deletion before dragging (but after selection)
//control.dragControl.feature = null;
//control.feature = point;
//var oldUnselect = control.unselectFeature;
//control.unselectFeature = function(feature) {
// t.eq(feature.id, point.id,
// "point deletion before drag: unselectFeature called with the correct feature");
//};
//control.layer = {
// removeFeatures: function(features) {
// t.ok(features.length == 1,
// "point deletion before drag: removeFeatures called with a single feature");
// t.eq(features[0].id, point.id,
// "point deletion before drag: removeFeatures called with the correct feature");
// }
//};
//control.onDelete = function(feature) {
// t.eq(feature.id, point.id,
// "point deletion before drag: onDelete called with the correct feature");
//};
//// run the above four tests twice
//control.handleKeypress(delKey);
//control.handleKeypress(dKey);
//// reset modified methods
//control.unselectFeatures = oldUnselect;
//control.onDelete = function() {};
//
//// mock up deletion during dragging - these repeat the above tests
//control.dragControl.feature = point;
//control.feature = point;
//var oldUnselect = control.unselectFeature;
//control.unselectFeature = function(feature) {
// t.eq(feature.id, point.id,
// "point deletion during drag: unselectFeature called with the correct feature");
//};
//control.layer = {
// removeFeatures: function(features) {
// t.ok(features.length == 1,
// "point deletion during drag: removeFeatures called with a single feature");
// t.eq(features[0].id, point.id,
// "point deletion during drag: removeFeatures called with the correct feature");
// }
//};
//control.onDelete = function(feature) {
// t.eq(feature.id, point.id,
// "point deletion during drag: onDelete called with the correct feature");
//};
//// run the above four tests twice
//control.handleKeypress(delKey);
//control.handleKeypress(dKey);
//// reset modified methods
//control.unselectFeatures = oldUnselect;
//control.onDelete = function() {};
// test that a polygon vertex is deleted for all delete codes
var point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point()
);
var poly = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon()
);
// mock up vertex deletion
control.dragControl.feature = point;
control.feature = poly;
control.vertices = [point];
point.geometry.parent = {
removeComponent: function(geometry) {
t.eq(geometry.id, point.geometry.id,
"vertex deletion: removeComponent called on parent with proper geometry");
}
};
control.layer = {
drawFeature: function(feature) {
t.eq(feature.id, poly.id,
"vertex deletion: drawFeature called with the proper feature");
}
};
var oldReset = control.resetVertices;
control.resetVertices = function() {
t.ok(true, "vertex deletion: resetVertices called");
};
control.onModification = function(feature) {
t.eq(feature.id, poly.id,
"vertex deletion: onModification called with the proper feature");
};
// run the above four tests twice
control.handleKeypress(delKey);
control.handleKeypress(dKey);
// reset modified methods
control.onModification = function() {};
}
function test_ModifyFeature_onUnSelect(t) {
t.plan(5);
var layer = new OpenLayers.Layer.Vector();
var control = new OpenLayers.Control.ModifyFeature(layer);
var fakeFeature = {'id':'myid'};
control.vertices = 'a';
control.virtualVertices = 'b';
control.features = true;
control.dragControl.deactivate = function() { t.ok(true, "Deactivate called on drag control"); }
control.onModificationEnd = function (feature) { t.eq(feature.id, fakeFeature.id, "onModificationEnd got feature.") }
layer.removeFeatures = function(verts) {
t.ok(verts == 'a' || verts == 'b', "Verts removed correctly")
}
control.unselectFeature(fakeFeature);
t.eq(control.feature, null, "feature is set to null");
}
function test_ModifyFeature_selectFeature(t) {
t.plan(12);
var layer = new OpenLayers.Layer.Vector();
var control = new OpenLayers.Control.ModifyFeature(layer);
control.vertices = [];
control.virtualVertices = [];
control.dragControl.activate = function() { t.ok(true, "drag Control activated"); }
control.onModificationStart = function(feature) { t.eq(feature.id, fakeFeature.id, "On Modification Start called with correct feature."); }
// Start of testing
control.collectVertices = function() { t.fail("Collect vertices called when geom is a point"); }
var fakeFeature = {'id':'myFakeFeature','geometry':{'CLASS_NAME':'OpenLayers.Geometry.Point'}};
// Points don't call collectVertices
control.selectFeature(fakeFeature);
control.collectVertices = function() {
this.vertices = 'a';
this.virtualVertices = 'd';
t.ok(true, "collectVertices called");
}
layer.addFeatures = function(features) {
t.ok(features == 'a' || features == 'd', "features passed correctly");
}
fakeFeature.geometry.CLASS_NAME='OpenLayers.Geometry.Polygon';
// OnSelect calls collectVertices and passes features to layer
control.selectFeature(fakeFeature);
control.vertices = ['a'];
control.virtualVertices = ['b'];
layer.addFeatures = function(features) {}
layer.removeFeatures = function(features) {
t.eq(features.length, 1, "Correct feature length passed in");
}
// Features are removed whenever they exist
control.selectFeature(fakeFeature);
}
function test_ModifyFeature_resetVertices(t) {
t.plan(9);
var layer = new OpenLayers.Layer.Vector();
var control = new OpenLayers.Control.ModifyFeature(layer);
var point = new OpenLayers.Geometry.Point(5,6);
var point2 = new OpenLayers.Geometry.Point(7,8);
var point3 = new OpenLayers.Geometry.Point(9,10);
control.feature = new OpenLayers.Feature.Vector(point);
control.resetVertices();
t.eq(control.vertices.length, 0, "Correct vertices length");
t.eq(control.virtualVertices.length, 0, "Correct virtual vertices length.");
var multiPoint = new OpenLayers.Geometry.MultiPoint([point, point2]);
control.feature = new OpenLayers.Feature.Vector(multiPoint);
control.resetVertices();
t.eq(control.vertices.length, 2, "Correct vertices length with multipoint");
t.eq(control.virtualVertices.length, 0, "Correct virtual vertices length (multipoint).");
var line = new OpenLayers.Geometry.LineString([point, point2]);
control.feature = new OpenLayers.Feature.Vector(line);
control.resetVertices();
t.eq(control.vertices.length, 2, "Correct vertices length with line");
t.eq(control.virtualVertices.length, 1, "Correct virtual vertices length (linestring).");
var polygon = new OpenLayers.Geometry.Polygon([new OpenLayers.Geometry.LinearRing([point, point2, point3])]);
control.feature = new OpenLayers.Feature.Vector(polygon);
control.resetVertices();
t.eq(control.vertices.length, 4, "Correct vertices length with polygon");
t.eq(control.vertices[0].geometry.id, control.vertices[3].geometry.id, "First and last vertices are the same");
t.eq(control.virtualVertices.length, 3, "Correct virtual vertices length (polygon).");
}
function test_ModifyFeature_onDrag(t) {
t.plan(1);
t.ok(true, "onDrag not tested yet.");
}
function test_ModifyFeature_dragComplete(t) {
t.plan(6);
var layer = new OpenLayers.Layer.Vector();
var control = new OpenLayers.Control.ModifyFeature(layer);
var fakeFeature = {
'geometry': { 'id':'myGeom'},
'id': 'fakeFeature'
};
control.collectVertices = function(geom) {
t.eq(geom.id, 'myGeom', "collect geom called");
this.vertices = 'normal';
this.virtualVertices = 'virtual';
}
layer.addFeatures = function (verts) {
t.ok(verts == 'virtual' || verts == 'normal', verts + " verts correct");
}
layer.removeFeatures = function (verts) {
t.ok(verts == 'previous virtual' || verts == 'previous normal', verts + " verts correct");
}
control.onModification = function(feat) {
t.eq(feat.id, fakeFeature.id, "onModification gets correct feat");
}
control.feature = fakeFeature;
control.vertices = 'previous normal';
control.virtualVertices = 'previous virtual';
control.dragComplete();
}
function test_ModifyFeature_deactivate(t) {
t.plan(2);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.ModifyFeature(layer);
map.addControl(control);
control.selectControl.deactivate = function() {
t.ok(true,
"control.deactivate calls deactivate on select control");
}
control.dragControl.deactivate = function() {
t.ok(true,
"control.deactivate calls deactivate on drag control");
}
control.active = true;
control.deactivate();
}
function test_ModifyFeature_onModificationStart(t) {
t.plan(1);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.ModifyFeature(layer);
map.addControl(control);
control.activate();
// make sure onModificationStart is called on feature selection
var testFeature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(Math.random(), Math.random())
);
control.onModificationStart = function(feature) {
t.eq(feature.id, testFeature.id,
"onModificationStart called with the right feature");
};
control.selectFeature(testFeature);
}
function test_ModifyFeature_onModification(t) {
t.plan(2);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.ModifyFeature(layer);
map.addControl(control);
control.activate();
// make sure onModification is called on drag complete
var point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(Math.random(), Math.random())
);
control.feature = point;
control.onModification = function(feature) {
t.eq(feature.id, point.id,
"onModification called with the right feature on drag complete");
};
control.dragComplete();
// make sure onModification is called on vertex deletion
var poly = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon()
);
var oldDraw = layer.drawFeature;
layer.drawFeature = function() {
return;
};
control.feature = poly;
control.vertices = [point];
control.onModification = function(feature) {
t.eq(feature.id, poly.id,
"onModification called with the right feature on vertex delete");
};
point.geometry.parent = poly.geometry;
control.dragControl.feature = point;
control.handleKeypress(46);
layer.drawFeature = oldDraw;
}
function test_ModifyFeature_onModificationEnd(t) {
t.plan(1);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.ModifyFeature(layer);
map.addControl(control);
control.activate();
// make sure onModificationEnd is called on unselect feature
var testFeature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(Math.random(), Math.random())
);
control.onModificationEnd = function(feature) {
t.eq(feature.id, testFeature.id,
"onModificationEnd called with the right feature");
};
control.unselectFeature(testFeature);
}
//function t//est_ModifyFeature_onDelete(t) {
// t.plan(2);
// var map = new OpenLayers.Map("map");
// var layer = new OpenLayers.Layer.Vector();
// map.addLayer(layer);
// var control = new OpenLayers.Control.ModifyFeature(layer);
// map.addControl(control);
// control.activate();
//
// // make sure onDelete is called on point deletion (before dragging)
// var point = new OpenLayers.Feature.Vector(
// new OpenLayers.Geometry.Point(Math.random(), Math.random())
// );
// control.feature = point;
// control.onDelete = function(feature) {
// t.eq(feature.id, point.id,
// "onDelete called with the right feature before drag");
// };
// control.handleKeypress(46);
//
// // make sure onDelete is called on point deletion (during dragging)
// var point = new OpenLayers.Feature.Vector(
// new OpenLayers.Geometry.Point(Math.random(), Math.random())
// );
// control.dragControl.feature = point;
// control.feature = point;
// control.onDelete = function(feature) {
// t.eq(feature.id, point.id,
// "onDelete called with the right feature during drag");
// };
// control.handleKeypress(46);
//
//}
</script>
</head>
<body>
<div id="map" style="width: 400px; height: 250px;"/>
</body>
</html>

View File

@@ -66,6 +66,7 @@
<li>Control/test_Attribution.html</li>
<li>Control/test_SelectFeature.html</li>
<li>Control/test_DragFeature.html</li>
<li>Control/test_ModifyFeature.html</li>
<li>Control/test_DragPan.html</li>
<li>Control/test_OverviewMap.html</li>
<li>Control/test_NavToolbar.html</li>