Adding getVertices method to all geometries. Call with nodesOnly true if you only want endpoints (of lines and multilines). r=crschmidt (closes #1192)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8842 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-02-05 22:49:03 +00:00
parent dd70dceef9
commit df920a88d5
9 changed files with 190 additions and 0 deletions

View File

@@ -150,6 +150,20 @@ OpenLayers.Geometry = OpenLayers.Class({
distanceTo: function(geometry, options) {
},
/**
* APIMethod: getVertices
* Return a list of all points in this geometry.
*
* Parameters:
* nodesOnly - {Boolean} For lines, only return vertices that are
* endpoints.
*
* Returns:
* {Array} A list of all vertices in the geometry.
*/
getVertices: function(nodesOnly) {
},
/**
* Method: atPoint
* Note - This is only an approximation based on the bounds of the

View File

@@ -401,5 +401,27 @@ OpenLayers.Geometry.Collection = OpenLayers.Class(OpenLayers.Geometry, {
return intersect;
},
/**
* APIMethod: getVertices
* Return a list of all points in this geometry.
*
* Parameters:
* nodesOnly - {Boolean} For lines, only return vertices that are
* endpoints.
*
* Returns:
* {Array} A list of all vertices in the geometry.
*/
getVertices: function(nodesOnly) {
var vertices = [];
for(var i=0, len=this.components.length; i<len; ++i) {
Array.prototype.push.apply(
vertices, this.components[i].getVertices(nodesOnly)
);
}
return vertices;
},
CLASS_NAME: "OpenLayers.Geometry.Collection"
});

View File

@@ -154,6 +154,30 @@ OpenLayers.Geometry.LineString = OpenLayers.Class(OpenLayers.Geometry.Curve, {
return segments.sort(byX1);
},
/**
* APIMethod: getVertices
* Return a list of all points in this geometry.
*
* Parameters:
* nodesOnly - {Boolean} For lines, only return vertices that are
* endpoints.
*
* Returns:
* {Array} A list of all vertices in the geometry.
*/
getVertices: function(nodesOnly) {
var vertices;
if(nodesOnly) {
vertices = [
this.components[0],
this.components[this.components.length-1]
];
} else {
vertices = this.components;
}
return vertices;
},
/**
* APIMethod: distanceTo
* Calculate the closest distance between two geometries.

View File

@@ -324,5 +324,20 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
return intersect;
},
/**
* APIMethod: getVertices
* Return a list of all points in this geometry.
*
* Parameters:
* nodesOnly - {Boolean} For lines, only return vertices that are
* endpoints.
*
* Returns:
* {Array} A list of all vertices in the geometry.
*/
getVertices: function(nodesOnly) {
return nodesOnly ? [] : this.components.slice(0, this.components.length-1);
},
CLASS_NAME: "OpenLayers.Geometry.LinearRing"
});

View File

@@ -250,5 +250,20 @@ OpenLayers.Geometry.Point = OpenLayers.Class(OpenLayers.Geometry, {
return this;
},
/**
* APIMethod: getVertices
* Return a list of all points in this geometry.
*
* Parameters:
* nodesOnly - {Boolean} For lines, only return vertices that are
* endpoints.
*
* Returns:
* {Array} A list of all vertices in the geometry.
*/
getVertices: function(nodesOnly) {
return [this];
},
CLASS_NAME: "OpenLayers.Geometry.Point"
});

View File

@@ -199,6 +199,35 @@
t.ok(!geometry.equals(offY),
"equals() returns false for a geometry with offset y");
}
function test_getVertices(t) {
t.plan(10);
var points = [
new OpenLayers.Geometry.Point(10, 20),
new OpenLayers.Geometry.Point(20, 30),
new OpenLayers.Geometry.Point(30, 40),
new OpenLayers.Geometry.Point(40, 50)
];
var line = new OpenLayers.Geometry.LineString(points);
var verts = line.getVertices();
t.ok(verts instanceof Array, "got back an array");
t.eq(verts.length, points.length, "of correct length length");
t.geom_eq(verts[0], points[0], "0: correct geometry");
t.geom_eq(verts[1], points[1], "1: correct geometry");
t.geom_eq(verts[2], points[2], "2: correct geometry");
t.geom_eq(verts[3], points[3], "3: correct geometry");
// get nodes only
var nodes = line.getVertices(true);
t.ok(nodes instanceof Array, "[nodes only] got back an array");
t.eq(nodes.length, 2, "[nodes only] of correct length length");
t.geom_eq(nodes[0], points[0], "[nodes only] first: correct geometry");
t.geom_eq(nodes[1], points[points.length-1], "[nodes only] last: correct geometry");
}
function test_LineString_clone(t) {
t.plan(2);

View File

@@ -21,6 +21,44 @@
t.eq( mline.components.length, 1, "mline.components.length is set correctly");
}
function test_getVertices(t) {
t.plan(16);
var points = [
new OpenLayers.Geometry.Point(10, 20),
new OpenLayers.Geometry.Point(20, 30),
new OpenLayers.Geometry.Point(30, 40),
new OpenLayers.Geometry.Point(40, 50)
];
var multi = new OpenLayers.Geometry.MultiLineString([
new OpenLayers.Geometry.LineString(points),
new OpenLayers.Geometry.LineString(points)
]);
var verts = multi.getVertices();
t.ok(verts instanceof Array, "got back an array");
t.eq(verts.length, 2 * points.length, "of correct length length");
t.geom_eq(verts[0], points[0], "0: correct geometry");
t.geom_eq(verts[1], points[1], "1: correct geometry");
t.geom_eq(verts[2], points[2], "2: correct geometry");
t.geom_eq(verts[3], points[3], "3: correct geometry");
t.geom_eq(verts[4], points[0], "4: correct geometry");
t.geom_eq(verts[5], points[1], "5: correct geometry");
t.geom_eq(verts[6], points[2], "6: correct geometry");
t.geom_eq(verts[7], points[3], "7: correct geometry");
// nodes only
var nodes = multi.getVertices(true);
t.ok(nodes instanceof Array, "got back an array");
t.eq(nodes.length, 4, "of correct length length");
t.geom_eq(nodes[0], points[0], "0: correct geometry");
t.geom_eq(nodes[1], points[3], "1: correct geometry");
t.geom_eq(nodes[2], points[0], "2: correct geometry");
t.geom_eq(nodes[3], points[3], "3: correct geometry");
}
</script>
</head>

View File

@@ -184,6 +184,16 @@
"equals() returns false for a geometry with offset y");
}
function test_getVertices(t) {
t.plan(3);
var point = new OpenLayers.Geometry.Point(10, 20);
var verts = point.getVertices();
t.ok(verts instanceof Array, "got back an array");
t.eq(verts.length, 1, "of length 1");
t.geom_eq(verts[0], point, "with correct geometry");
}
function test_Point_clone(t) {
t.plan(2);

View File

@@ -307,6 +307,29 @@
}
function test_getVertices(t) {
t.plan(6);
var points = [
new OpenLayers.Geometry.Point(10, 20),
new OpenLayers.Geometry.Point(20, 30),
new OpenLayers.Geometry.Point(30, 40),
new OpenLayers.Geometry.Point(40, 50)
];
var polygon = new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing(points)
]);
var verts = polygon.getVertices();
t.ok(verts instanceof Array, "got back an array");
t.eq(verts.length, points.length, "of correct length length");
t.geom_eq(verts[0], points[0], "0: correct geometry");
t.geom_eq(verts[1], points[1], "1: correct geometry");
t.geom_eq(verts[2], points[2], "2: correct geometry");
t.geom_eq(verts[3], points[3], "3: correct geometry");
}
function test_Polygon_clone(t) {
t.plan(2);