From df920a88d5a2d2fa524e909010431dd3315dc2fa Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Thu, 5 Feb 2009 22:49:03 +0000 Subject: [PATCH] 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 --- lib/OpenLayers/Geometry.js | 14 ++++++++++ lib/OpenLayers/Geometry/Collection.js | 22 ++++++++++++++++ lib/OpenLayers/Geometry/LineString.js | 24 +++++++++++++++++ lib/OpenLayers/Geometry/LinearRing.js | 15 +++++++++++ lib/OpenLayers/Geometry/Point.js | 15 +++++++++++ tests/Geometry/LineString.html | 29 ++++++++++++++++++++ tests/Geometry/MultiLineString.html | 38 +++++++++++++++++++++++++++ tests/Geometry/Point.html | 10 +++++++ tests/Geometry/Polygon.html | 23 ++++++++++++++++ 9 files changed, 190 insertions(+) diff --git a/lib/OpenLayers/Geometry.js b/lib/OpenLayers/Geometry.js index 61db5dab6a..881fffa4a7 100644 --- a/lib/OpenLayers/Geometry.js +++ b/lib/OpenLayers/Geometry.js @@ -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 diff --git a/lib/OpenLayers/Geometry/Collection.js b/lib/OpenLayers/Geometry/Collection.js index b5e784187b..b61f68dbba 100644 --- a/lib/OpenLayers/Geometry/Collection.js +++ b/lib/OpenLayers/Geometry/Collection.js @@ -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 diff --git a/tests/Geometry/Point.html b/tests/Geometry/Point.html index ed22966289..8932ee2901 100644 --- a/tests/Geometry/Point.html +++ b/tests/Geometry/Point.html @@ -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); diff --git a/tests/Geometry/Polygon.html b/tests/Geometry/Polygon.html index b24930594e..8eccbc5503 100644 --- a/tests/Geometry/Polygon.html +++ b/tests/Geometry/Polygon.html @@ -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);