summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Geometry/LineString.html
diff options
context:
space:
mode:
authorChris Schlaeger <chris@linux.com>2014-08-12 21:56:44 +0200
committerChris Schlaeger <chris@linux.com>2014-08-12 21:56:44 +0200
commitea346a785dc1b3f7c156f6fc33da634e1f1a627b (patch)
treeaf67530553d20b6e82ad60fd79593e9c4abf5565 /misc/openlayers/tests/Geometry/LineString.html
parent59741cd535c47f25971bf8c32b25da25ceadc6d5 (diff)
downloadpostrunner-ea346a785dc1b3f7c156f6fc33da634e1f1a627b.zip
Adding jquery, flot and openlayers to be included with the GEM.v0.0.4
Diffstat (limited to 'misc/openlayers/tests/Geometry/LineString.html')
-rw-r--r--misc/openlayers/tests/Geometry/LineString.html443
1 files changed, 443 insertions, 0 deletions
diff --git a/misc/openlayers/tests/Geometry/LineString.html b/misc/openlayers/tests/Geometry/LineString.html
new file mode 100644
index 0000000..4b2ec0e
--- /dev/null
+++ b/misc/openlayers/tests/Geometry/LineString.html
@@ -0,0 +1,443 @@
+<html>
+<head>
+ <script src="../OLLoader.js"></script>
+ <script type="text/javascript">
+ var line;
+ var components = [new OpenLayers.Geometry.Point(10,15),
+ new OpenLayers.Geometry.Point(0,0)];
+
+ function test_LineString_constructor (t) {
+ t.plan( 3 );
+ line = new OpenLayers.Geometry.LineString();
+ t.ok( line instanceof OpenLayers.Geometry.LineString, "new OpenLayers.Geometry.LineString returns line object" );
+ t.eq( line.CLASS_NAME, "OpenLayers.Geometry.LineString", "line.CLASS_NAME is set correctly");
+ t.eq( line.components, [], "line.components is set correctly");
+ }
+
+ function test_LineString_constructor (t) {
+ t.plan( 3 );
+ line = new OpenLayers.Geometry.LineString(components);
+ t.ok( line instanceof OpenLayers.Geometry.LineString, "new OpenLayers.Geometry.LineString returns line object" );
+ t.eq( line.CLASS_NAME, "OpenLayers.Geometry.LineString", "line.CLASS_NAME is set correctly");
+ // TBD FIXME, recursion
+ // t.eq( line.components, components, "line.components is set correctly");
+ t.eq( line.components.length, 2, "line.components.length is set correctly");
+ }
+
+ function test_LineString_toString(t) {
+ t.plan(1);
+
+ line = new OpenLayers.Geometry.LineString(components);
+ t.eq(line.toString(),
+ "LINESTRING(10 15,0 0)",
+ "toString() returns WKT");
+ }
+
+ function test_LineString_removeComponent(t) {
+ t.plan(2);
+
+ OpenLayers.Geometry.Collection.prototype._removeComponent =
+ OpenLayers.Geometry.Collection.prototype.removeComponent;
+ OpenLayers.Geometry.Collection.prototype.removeComponent =
+ function(point) { g_removeComponent = point; };
+
+ line = new OpenLayers.Geometry.LineString(components);
+
+ g_removeComponent = null;
+ line.removeComponent(components[0]);
+ t.ok(g_removeComponent == null, "point not removed if only 2 points in components");
+
+ line.components.push(new OpenLayers.Geometry.Point(4,4));
+ line.removeComponent(components[0]);
+ t.ok(g_removeComponent, components[0], "point removed if 3 points in components");
+
+ OpenLayers.Geometry.Collection.prototype.removeComponent =
+ OpenLayers.Geometry.Collection.prototype._removeComponent;
+ }
+
+ function test_LineString_move(t) {
+ t.plan(4);
+
+ var components = [new OpenLayers.Geometry.Point(10,15),
+ new OpenLayers.Geometry.Point(0,0)];
+ var line = new OpenLayers.Geometry.LineString(components);
+
+ var x0 = components[0].x;
+ var y0 = components[0].y;
+ var x1 = components[1].x;
+ var y1 = components[1].y;
+
+ var dx = 10 * Math.random();
+ var dy = 10 * Math.random();
+ line.move(dx, dy);
+
+ t.eq(line.components[0].x, x0 + dx, "move() correctly modifies first x");
+ t.eq(line.components[0].y, y0 + dy, "move() correctly modifies first y");
+ t.eq(line.components[1].x, x1 + dx, "move() correctly modifies second x");
+ t.eq(line.components[1].y, y1 + dy, "move() correctly modifies second y");
+ }
+
+ function test_LineString_rotate(t) {
+ t.plan(6);
+
+ var components = [new OpenLayers.Geometry.Point(10,15),
+ new OpenLayers.Geometry.Point(0,0)];
+ var geometry = new OpenLayers.Geometry.LineString(components);
+
+ var originals = [];
+ var comp;
+ var angle = 2 * Math.PI * Math.random();
+ var origin = new OpenLayers.Geometry.Point(10 * Math.random(),
+ 10 * Math.random());
+ for(var i=0; i<geometry.components.length; ++i) {
+ comp = geometry.components[i];
+ originals[i] = comp.rotate;
+ comp.rotate = function(a, o) {
+ t.ok(true, "rotate called for component " + i);
+ t.ok(a == angle, "rotate called with correct angle");
+ t.ok(o == origin, "rotate called with correct origin");
+ }
+ }
+ geometry.rotate(angle, origin);
+
+ // restore the original rotate defs
+ for(var i=0; i<geometry.components.length; ++i) {
+ comp.rotate = originals[i];
+ }
+ }
+
+ function test_LineString_resize(t) {
+ t.plan(8);
+
+ var tolerance = 1e-10;
+
+ var components = [new OpenLayers.Geometry.Point(10 * Math.random(),
+ 10 * Math.random()),
+ new OpenLayers.Geometry.Point(10 * Math.random(),
+ 10 * Math.random())];
+ var geometry = new OpenLayers.Geometry.LineString(components);
+
+ var origin = new OpenLayers.Geometry.Point(10 * Math.random(),
+ 10 * Math.random());
+
+ var scale = 10 * Math.random();
+
+ var oldLength = geometry.getLength();
+ var ret = geometry.resize(scale, origin);
+ t.ok(ret === geometry, "resize returns geometry");
+ var newLength = geometry.getLength();
+ t.ok((((newLength / oldLength) - scale) / scale) < tolerance,
+ "resize correctly changes the length of a linestring")
+
+ var originals = [];
+ var comp;
+ for(var i=0; i<geometry.components.length; ++i) {
+ comp = geometry.components[i];
+ originals[i] = comp.resize;
+ comp.resize = function(s, o) {
+ t.ok(true, "resize called for component " + i);
+ t.ok(s == scale, "resize called with correct scale");
+ t.ok(o == origin, "resize called with correct origin");
+ }
+ }
+ geometry.resize(scale, origin);
+
+ // restore the original resize defs
+ for(var i=0; i<geometry.components.length; ++i) {
+ comp.resize = originals[i];
+ }
+
+ }
+
+ function test_split(t) {
+ var wkt = OpenLayers.Geometry.fromWKT;
+
+ var cases = [{
+ msg: "no intersection",
+ g1: "LINESTRING(0 0, 0 1)",
+ g2: "LINESTRING(1 0, 1 1)",
+ exp: null
+ } , {
+ msg: "intersection at midpoint",
+ g1: "LINESTRING(0 0, 1 1)",
+ g2: "LINESTRING(1 0, 0 1)",
+ exp: ["LINESTRING(1 0, 0.5 0.5)", "LINESTRING(0.5 0.5, 0 1)"]
+ }, {
+ msg: "intersection at midpoint (reverse source/target)",
+ g1: "LINESTRING(1 0, 0 1)",
+ g2: "LINESTRING(0 0, 1 1)",
+ exp: ["LINESTRING(0 0, 0.5 0.5)", "LINESTRING(0.5 0.5, 1 1)"]
+ }, {
+ msg: "intersection at endpoint",
+ g1: "LINESTRING(0 0, 1 1)",
+ g2: "LINESTRING(1 0, 1 1)",
+ exp: null
+ }, {
+ msg: "midpoint intersection, no options",
+ g1: "LINESTRING(0 0, 2 2)",
+ g2: "LINESTRING(0 2, 2 0)",
+ exp: ["LINESTRING(0 2, 1 1)", "LINESTRING(1 1, 2 0)"]
+ }, {
+ msg: "midpoint intersection, edge false",
+ opt: {edge: false},
+ g1: "LINESTRING(0 0, 2 2)",
+ g2: "LINESTRING(0 2, 2 0)",
+ exp: null
+ }, {
+ msg: "midpoint intersection, mutual",
+ opt: {mutual: true},
+ g1: "LINESTRING(0 0, 2 2)",
+ g2: "LINESTRING(0 2, 2 0)",
+ exp: [["LINESTRING(0 0, 1 1)", "LINESTRING(1 1, 2 2)"], ["LINESTRING(0 2, 1 1)", "LINESTRING(1 1, 2 0)"]]
+ }, {
+ msg: "close intersection, no tolerance",
+ g1: "LINESTRING(0 0, 0.9 0.9)",
+ g2: "LINESTRING(0 2, 2 0)",
+ exp: null
+ }, {
+ msg: "close intersection, within tolerance",
+ opt: {tolerance: 0.2},
+ g1: "LINESTRING(0 0, 0.9 0.9)",
+ g2: "LINESTRING(0 2, 2 0)",
+ exp: ["LINESTRING(0 2, 0.9 0.9)", "LINESTRING(0.9 0.9, 2 0)"]
+ }];
+
+ t.plan(cases.length);
+ var c, parts, part, midparts;
+ for(var i=0; i<cases.length; ++i) {
+ c = cases[i];
+ var g1 = wkt(c.g1);
+ var g2 = wkt(c.g2);
+ var got = g1.split(g2, c.opt);
+ var exp = c.exp;
+ if(got instanceof Array) {
+ parts = [];
+ for(var j=0; j<got.length; ++j) {
+ part = got[j];
+ if(part instanceof Array) {
+ midparts = [];
+ for(var k=0; k<part.length; ++k) {
+ midparts.push(part[k].toString());
+ }
+ parts.push("[" + midparts.join(", ") + "]");
+ } else {
+ parts.push(got[j].toString());
+ }
+ }
+ got = parts.join(", ");
+ }
+ if(exp instanceof Array) {
+ parts = [];
+ for(var j=0; j<exp.length; ++j) {
+ part = exp[j];
+ if(part instanceof Array) {
+ midparts = [];
+ for(var k=0; k<part.length; ++k) {
+ midparts.push(wkt(part[k]).toString());
+ }
+ parts.push("[" + midparts.join(", ") + "]");
+ } else {
+ parts.push(wkt(exp[j]).toString());
+ }
+ }
+ exp = parts.join(", ");
+ }
+ t.eq(got, exp, "case " + i + ": " + c.msg);
+ }
+
+ }
+
+
+ function test_distanceTo(t) {
+ var wkt = OpenLayers.Geometry.fromWKT;
+ var geoms = [
+ wkt("POINT(0 0)"),
+ wkt("LINESTRING(-2 0, 0 -2, 2 -1, 2 0)")
+ ];
+
+ var cases = [{
+ got: geoms[1].distanceTo(geoms[0]),
+ expected: Math.sqrt(2)
+ }, {
+ got: geoms[1].distanceTo(geoms[0], {details: true}),
+ expected: {
+ distance: Math.sqrt(2),
+ x0: -1, y0: -1,
+ x1: 0, y1: 0
+ }
+ }];
+
+ t.plan(cases.length);
+ for(var i=0; i<cases.length; ++i) {
+ t.eq(cases[i].got, cases[i].expected, "case " + i);
+ }
+
+ }
+
+ function test_LineString_equals(t) {
+ t.plan(3);
+
+ var x0 = Math.random() * 100;
+ var y0 = Math.random() * 100;
+ var x1 = Math.random() * 100;
+ var y1 = Math.random() * 100;
+ var point0 = new OpenLayers.Geometry.Point(x0, y0);
+ var point1 = new OpenLayers.Geometry.Point(x1, y1);
+ var geometry = new OpenLayers.Geometry.LineString([point0, point1]);
+ var equal = new OpenLayers.Geometry.LineString([point0, point1]);
+ var offX = new OpenLayers.Geometry.LineString([
+ new OpenLayers.Geometry.Point(x0 + 1, y0),
+ new OpenLayers.Geometry.Point(x1 + 1, y1)]);
+ var offY = new OpenLayers.Geometry.LineString([
+ new OpenLayers.Geometry.Point(x0, y0 + 1),
+ new OpenLayers.Geometry.Point(x1, y1 + 1)]);
+ t.ok(geometry.equals(equal),
+ "equals() returns true for a geometry with equivalent coordinates");
+ t.ok(!geometry.equals(offX),
+ "equals() returns false for a geometry with offset x");
+ t.ok(!geometry.equals(offY),
+ "equals() returns false for a geometry with offset y");
+ }
+
+
+ function test_getVertices(t) {
+ t.plan(14);
+
+ 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");
+
+ // no nodes
+ var nodes = line.getVertices(false);
+ t.ok(nodes instanceof Array, "[no nodes] got back an array");
+ t.eq(nodes.length, 2, "[no nodes] of correct length length");
+ t.geom_eq(nodes[0], points[1], "[no nodes] first: correct geometry");
+ t.geom_eq(nodes[1], points[2], "[no nodes] last: correct geometry");
+
+ }
+
+
+ function test_LineString_clone(t) {
+ t.plan(2);
+
+ var x0 = Math.random() * 100;
+ var y0 = Math.random() * 100;
+ var x1 = Math.random() * 100;
+ var y1 = Math.random() * 100;
+ var point0 = new OpenLayers.Geometry.Point(x0, y0);
+ var point1 = new OpenLayers.Geometry.Point(x1, y1);
+ var geometry = new OpenLayers.Geometry.LineString([point0, point1]);
+ var clone = geometry.clone();
+ t.ok(clone instanceof OpenLayers.Geometry.LineString,
+ "clone() creates an OpenLayers.Geometry.LineString");
+ t.ok(geometry.equals(clone), "clone has equivalent coordinates");
+ }
+
+ function test_getGeodesicLength(t) {
+
+ // expected values from http://www.movable-type.co.uk/scripts/latlong-vincenty.html
+ var cases = [{
+ wkt: "LINESTRING(0 0, -10 45)",
+ exp: 5081689.690
+ }, {
+ wkt: "LINESTRING(-10 45, 0 0)",
+ exp: 5081689.690
+ }, {
+ wkt: "LINESTRING(0 0, -10 45, -20 50)",
+ exp: 5081689.690 + 935018.062
+ }];
+ t.plan(cases.length);
+
+ var geom, got;
+ for(var i=0; i<cases.length; ++i) {
+ geom = new OpenLayers.Geometry.fromWKT(cases[i].wkt);
+ got = geom.getGeodesicLength();
+ t.eq(Math.round(got), Math.round(cases[i].exp), "[case " + i + "] length calculated");
+ }
+
+ }
+
+ function test_LineString_simplify(t){
+ t.plan(8);
+ var ls1 = new OpenLayers.Geometry.LineString([
+ new OpenLayers.Geometry.Point(0,0),
+ new OpenLayers.Geometry.Point(1,2.1),
+ new OpenLayers.Geometry.Point(1.8,3.8),
+ new OpenLayers.Geometry.Point(2,4),
+ new OpenLayers.Geometry.Point(3,4),
+ new OpenLayers.Geometry.Point(4,4.5),
+ new OpenLayers.Geometry.Point(5,5)
+
+ ]);
+ var ls2 = new OpenLayers.Geometry.LineString([
+ new OpenLayers.Geometry.Point(0,0),
+ new OpenLayers.Geometry.Point(1,2.1),
+ new OpenLayers.Geometry.Point(1.8,3.8),
+ new OpenLayers.Geometry.Point(2,4),
+ new OpenLayers.Geometry.Point(3,4),
+ new OpenLayers.Geometry.Point(4,4.5),
+ new OpenLayers.Geometry.Point(5,5),
+ new OpenLayers.Geometry.Point(0,0)
+
+ ]);
+ var ls3 = new OpenLayers.Geometry.LineString([
+ new OpenLayers.Geometry.Point(0,0),
+ new OpenLayers.Geometry.Point(1,1)
+ ]);
+ var ls5 = new OpenLayers.Geometry.LineString([
+ new OpenLayers.Geometry.Point(0,0),
+ new OpenLayers.Geometry.Point(1,1),
+ new OpenLayers.Geometry.Point(2,2),
+ new OpenLayers.Geometry.Point(3,3),
+ new OpenLayers.Geometry.Point(4,4),
+ new OpenLayers.Geometry.Point(5,5)
+
+ ]);
+ var ls6 = new OpenLayers.Geometry.LineString([
+ new OpenLayers.Geometry.Point(0,0),
+ new OpenLayers.Geometry.Point(1,1),
+ new OpenLayers.Geometry.Point(1,1),
+ new OpenLayers.Geometry.Point(3,2)
+ ]);
+
+ t.ok(ls1 instanceof OpenLayers.Geometry.LineString, 'LineString is instance of OpenLayers.Geometry.LineString');
+ var simplified1 = ls1.simplify(0.5);
+ t.ok(simplified1 instanceof OpenLayers.Geometry.LineString, 'Simplified LineString is instance of OpenLayers.Geometry.LineString');
+ t.ok(simplified1.getVertices().length <= ls1.getVertices().length, 'Simplified LineString has less or equal number of vertices');
+ // The simplified version is derived from PostGIS function ST_SIMPLIFY()
+ t.ok(simplified1.toString() === 'LINESTRING(0 0,1.8 3.8,5 5)', 'LineString 1 was simplified correctly');
+ var simplified2 = ls2.simplify(0.5);
+ // The simplified version is derived from PostGIS function ST_SIMPLIFY()
+ t.ok(simplified2.toString() === 'LINESTRING(0 0,1.8 3.8,5 5,0 0)', 'LineString 2 was simplified correctly');
+ var simplified3 = ls3.simplify(0.5);
+ t.ok(simplified3.toString() === ls3.toString(), 'LineString with 2 vertices is left untouched');
+ var simplified5 = ls5.simplify(0.0);
+ t.ok(simplified5.toString() === 'LINESTRING(0 0,5 5)', 'A tolerance of 0 returns the optimized version needless vertices');
+ var simplified6 = ls6.simplify(0.0);
+ t.ok(simplified6.toString() === 'LINESTRING(0 0,1 1,3 2)', 'A tolerance of 0 returns the optimized version without doubled vertices');
+ }
+
+ </script>
+</head>
+<body>
+</body>
+</html>