3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 var components = [new OpenLayers.Geometry.Point(10,15),
7 new OpenLayers.Geometry.Point(0,0)];
9 function test_LineString_constructor (t) {
11 line = new OpenLayers.Geometry.LineString();
12 t.ok( line instanceof OpenLayers.Geometry.LineString, "new OpenLayers.Geometry.LineString returns line object" );
13 t.eq( line.CLASS_NAME, "OpenLayers.Geometry.LineString", "line.CLASS_NAME is set correctly");
14 t.eq( line.components, [], "line.components is set correctly");
17 function test_LineString_constructor (t) {
19 line = new OpenLayers.Geometry.LineString(components);
20 t.ok( line instanceof OpenLayers.Geometry.LineString, "new OpenLayers.Geometry.LineString returns line object" );
21 t.eq( line.CLASS_NAME, "OpenLayers.Geometry.LineString", "line.CLASS_NAME is set correctly");
22 // TBD FIXME, recursion
23 // t.eq( line.components, components, "line.components is set correctly");
24 t.eq( line.components.length, 2, "line.components.length is set correctly");
27 function test_LineString_toString(t) {
30 line = new OpenLayers.Geometry.LineString(components);
32 "LINESTRING(10 15,0 0)",
33 "toString() returns WKT");
36 function test_LineString_removeComponent(t) {
39 OpenLayers.Geometry.Collection.prototype._removeComponent =
40 OpenLayers.Geometry.Collection.prototype.removeComponent;
41 OpenLayers.Geometry.Collection.prototype.removeComponent =
42 function(point) { g_removeComponent = point; };
44 line = new OpenLayers.Geometry.LineString(components);
46 g_removeComponent = null;
47 line.removeComponent(components[0]);
48 t.ok(g_removeComponent == null, "point not removed if only 2 points in components");
50 line.components.push(new OpenLayers.Geometry.Point(4,4));
51 line.removeComponent(components[0]);
52 t.ok(g_removeComponent, components[0], "point removed if 3 points in components");
54 OpenLayers.Geometry.Collection.prototype.removeComponent =
55 OpenLayers.Geometry.Collection.prototype._removeComponent;
58 function test_LineString_move(t) {
61 var components = [new OpenLayers.Geometry.Point(10,15),
62 new OpenLayers.Geometry.Point(0,0)];
63 var line = new OpenLayers.Geometry.LineString(components);
65 var x0 = components[0].x;
66 var y0 = components[0].y;
67 var x1 = components[1].x;
68 var y1 = components[1].y;
70 var dx = 10 * Math.random();
71 var dy = 10 * Math.random();
74 t.eq(line.components[0].x, x0 + dx, "move() correctly modifies first x");
75 t.eq(line.components[0].y, y0 + dy, "move() correctly modifies first y");
76 t.eq(line.components[1].x, x1 + dx, "move() correctly modifies second x");
77 t.eq(line.components[1].y, y1 + dy, "move() correctly modifies second y");
80 function test_LineString_rotate(t) {
83 var components = [new OpenLayers.Geometry.Point(10,15),
84 new OpenLayers.Geometry.Point(0,0)];
85 var geometry = new OpenLayers.Geometry.LineString(components);
89 var angle = 2 * Math.PI * Math.random();
90 var origin = new OpenLayers.Geometry.Point(10 * Math.random(),
92 for(var i=0; i<geometry.components.length; ++i) {
93 comp = geometry.components[i];
94 originals[i] = comp.rotate;
95 comp.rotate = function(a, o) {
96 t.ok(true, "rotate called for component " + i);
97 t.ok(a == angle, "rotate called with correct angle");
98 t.ok(o == origin, "rotate called with correct origin");
101 geometry.rotate(angle, origin);
103 // restore the original rotate defs
104 for(var i=0; i<geometry.components.length; ++i) {
105 comp.rotate = originals[i];
109 function test_LineString_resize(t) {
112 var tolerance = 1e-10;
114 var components = [new OpenLayers.Geometry.Point(10 * Math.random(),
116 new OpenLayers.Geometry.Point(10 * Math.random(),
117 10 * Math.random())];
118 var geometry = new OpenLayers.Geometry.LineString(components);
120 var origin = new OpenLayers.Geometry.Point(10 * Math.random(),
123 var scale = 10 * Math.random();
125 var oldLength = geometry.getLength();
126 var ret = geometry.resize(scale, origin);
127 t.ok(ret === geometry, "resize returns geometry");
128 var newLength = geometry.getLength();
129 t.ok((((newLength / oldLength) - scale) / scale) < tolerance,
130 "resize correctly changes the length of a linestring")
134 for(var i=0; i<geometry.components.length; ++i) {
135 comp = geometry.components[i];
136 originals[i] = comp.resize;
137 comp.resize = function(s, o) {
138 t.ok(true, "resize called for component " + i);
139 t.ok(s == scale, "resize called with correct scale");
140 t.ok(o == origin, "resize called with correct origin");
143 geometry.resize(scale, origin);
145 // restore the original resize defs
146 for(var i=0; i<geometry.components.length; ++i) {
147 comp.resize = originals[i];
152 function test_split(t) {
153 var wkt = OpenLayers.Geometry.fromWKT;
156 msg: "no intersection",
157 g1: "LINESTRING(0 0, 0 1)",
158 g2: "LINESTRING(1 0, 1 1)",
161 msg: "intersection at midpoint",
162 g1: "LINESTRING(0 0, 1 1)",
163 g2: "LINESTRING(1 0, 0 1)",
164 exp: ["LINESTRING(1 0, 0.5 0.5)", "LINESTRING(0.5 0.5, 0 1)"]
166 msg: "intersection at midpoint (reverse source/target)",
167 g1: "LINESTRING(1 0, 0 1)",
168 g2: "LINESTRING(0 0, 1 1)",
169 exp: ["LINESTRING(0 0, 0.5 0.5)", "LINESTRING(0.5 0.5, 1 1)"]
171 msg: "intersection at endpoint",
172 g1: "LINESTRING(0 0, 1 1)",
173 g2: "LINESTRING(1 0, 1 1)",
176 msg: "midpoint intersection, no options",
177 g1: "LINESTRING(0 0, 2 2)",
178 g2: "LINESTRING(0 2, 2 0)",
179 exp: ["LINESTRING(0 2, 1 1)", "LINESTRING(1 1, 2 0)"]
181 msg: "midpoint intersection, edge false",
183 g1: "LINESTRING(0 0, 2 2)",
184 g2: "LINESTRING(0 2, 2 0)",
187 msg: "midpoint intersection, mutual",
189 g1: "LINESTRING(0 0, 2 2)",
190 g2: "LINESTRING(0 2, 2 0)",
191 exp: [["LINESTRING(0 0, 1 1)", "LINESTRING(1 1, 2 2)"], ["LINESTRING(0 2, 1 1)", "LINESTRING(1 1, 2 0)"]]
193 msg: "close intersection, no tolerance",
194 g1: "LINESTRING(0 0, 0.9 0.9)",
195 g2: "LINESTRING(0 2, 2 0)",
198 msg: "close intersection, within tolerance",
199 opt: {tolerance: 0.2},
200 g1: "LINESTRING(0 0, 0.9 0.9)",
201 g2: "LINESTRING(0 2, 2 0)",
202 exp: ["LINESTRING(0 2, 0.9 0.9)", "LINESTRING(0.9 0.9, 2 0)"]
205 t.plan(cases.length);
206 var c, parts, part, midparts;
207 for(var i=0; i<cases.length; ++i) {
211 var got = g1.split(g2, c.opt);
213 if(got instanceof Array) {
215 for(var j=0; j<got.length; ++j) {
217 if(part instanceof Array) {
219 for(var k=0; k<part.length; ++k) {
220 midparts.push(part[k].toString());
222 parts.push("[" + midparts.join(", ") + "]");
224 parts.push(got[j].toString());
227 got = parts.join(", ");
229 if(exp instanceof Array) {
231 for(var j=0; j<exp.length; ++j) {
233 if(part instanceof Array) {
235 for(var k=0; k<part.length; ++k) {
236 midparts.push(wkt(part[k]).toString());
238 parts.push("[" + midparts.join(", ") + "]");
240 parts.push(wkt(exp[j]).toString());
243 exp = parts.join(", ");
245 t.eq(got, exp, "case " + i + ": " + c.msg);
251 function test_distanceTo(t) {
252 var wkt = OpenLayers.Geometry.fromWKT;
255 wkt("LINESTRING(-2 0, 0 -2, 2 -1, 2 0)")
259 got: geoms[1].distanceTo(geoms[0]),
260 expected: Math.sqrt(2)
262 got: geoms[1].distanceTo(geoms[0], {details: true}),
264 distance: Math.sqrt(2),
270 t.plan(cases.length);
271 for(var i=0; i<cases.length; ++i) {
272 t.eq(cases[i].got, cases[i].expected, "case " + i);
277 function test_LineString_equals(t) {
280 var x0 = Math.random() * 100;
281 var y0 = Math.random() * 100;
282 var x1 = Math.random() * 100;
283 var y1 = Math.random() * 100;
284 var point0 = new OpenLayers.Geometry.Point(x0, y0);
285 var point1 = new OpenLayers.Geometry.Point(x1, y1);
286 var geometry = new OpenLayers.Geometry.LineString([point0, point1]);
287 var equal = new OpenLayers.Geometry.LineString([point0, point1]);
288 var offX = new OpenLayers.Geometry.LineString([
289 new OpenLayers.Geometry.Point(x0 + 1, y0),
290 new OpenLayers.Geometry.Point(x1 + 1, y1)]);
291 var offY = new OpenLayers.Geometry.LineString([
292 new OpenLayers.Geometry.Point(x0, y0 + 1),
293 new OpenLayers.Geometry.Point(x1, y1 + 1)]);
294 t.ok(geometry.equals(equal),
295 "equals() returns true for a geometry with equivalent coordinates");
296 t.ok(!geometry.equals(offX),
297 "equals() returns false for a geometry with offset x");
298 t.ok(!geometry.equals(offY),
299 "equals() returns false for a geometry with offset y");
303 function test_getVertices(t) {
307 new OpenLayers.Geometry.Point(10, 20),
308 new OpenLayers.Geometry.Point(20, 30),
309 new OpenLayers.Geometry.Point(30, 40),
310 new OpenLayers.Geometry.Point(40, 50)
312 var line = new OpenLayers.Geometry.LineString(points);
314 var verts = line.getVertices();
315 t.ok(verts instanceof Array, "got back an array");
316 t.eq(verts.length, points.length, "of correct length length");
317 t.geom_eq(verts[0], points[0], "0: correct geometry");
318 t.geom_eq(verts[1], points[1], "1: correct geometry");
319 t.geom_eq(verts[2], points[2], "2: correct geometry");
320 t.geom_eq(verts[3], points[3], "3: correct geometry");
323 var nodes = line.getVertices(true);
324 t.ok(nodes instanceof Array, "[nodes only] got back an array");
325 t.eq(nodes.length, 2, "[nodes only] of correct length length");
326 t.geom_eq(nodes[0], points[0], "[nodes only] first: correct geometry");
327 t.geom_eq(nodes[1], points[points.length-1], "[nodes only] last: correct geometry");
330 var nodes = line.getVertices(false);
331 t.ok(nodes instanceof Array, "[no nodes] got back an array");
332 t.eq(nodes.length, 2, "[no nodes] of correct length length");
333 t.geom_eq(nodes[0], points[1], "[no nodes] first: correct geometry");
334 t.geom_eq(nodes[1], points[2], "[no nodes] last: correct geometry");
339 function test_LineString_clone(t) {
342 var x0 = Math.random() * 100;
343 var y0 = Math.random() * 100;
344 var x1 = Math.random() * 100;
345 var y1 = Math.random() * 100;
346 var point0 = new OpenLayers.Geometry.Point(x0, y0);
347 var point1 = new OpenLayers.Geometry.Point(x1, y1);
348 var geometry = new OpenLayers.Geometry.LineString([point0, point1]);
349 var clone = geometry.clone();
350 t.ok(clone instanceof OpenLayers.Geometry.LineString,
351 "clone() creates an OpenLayers.Geometry.LineString");
352 t.ok(geometry.equals(clone), "clone has equivalent coordinates");
355 function test_getGeodesicLength(t) {
357 // expected values from http://www.movable-type.co.uk/scripts/latlong-vincenty.html
359 wkt: "LINESTRING(0 0, -10 45)",
362 wkt: "LINESTRING(-10 45, 0 0)",
365 wkt: "LINESTRING(0 0, -10 45, -20 50)",
366 exp: 5081689.690 + 935018.062
368 t.plan(cases.length);
371 for(var i=0; i<cases.length; ++i) {
372 geom = new OpenLayers.Geometry.fromWKT(cases[i].wkt);
373 got = geom.getGeodesicLength();
374 t.eq(Math.round(got), Math.round(cases[i].exp), "[case " + i + "] length calculated");