Files
openlayers/tests/Geometry/Point.html

215 lines
7.5 KiB
HTML

<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
var point;
function test_Point_constructor (t) {
t.plan( 8 );
//empty
point = new OpenLayers.Geometry.Point();
t.ok( point instanceof OpenLayers.Geometry.Point, "new OpenLayers.Geometry.Point returns point object" );
t.eq( point.CLASS_NAME, "OpenLayers.Geometry.Point", "point.CLASS_NAME is set correctly");
//valid
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
t.ok( point instanceof OpenLayers.Geometry.Point, "new OpenLayers.Geometry.Point returns point object" );
t.eq( point.CLASS_NAME, "OpenLayers.Geometry.Point", "point.CLASS_NAME is set correctly");
t.eq( point.x, x, "point.x is set correctly");
t.eq( point.y, y, "point.y is set correctly");
t.eq( point.lon, null, "point.lon is not set");
t.eq( point.lat, null, "point.lat is not set");
}
function test_Point_calculateBounds (t) {
t.plan(4);
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
point.calculateBounds();
t.eq( point.bounds.left, x, "bounds.left is 10" );
t.eq( point.bounds.right, x, "bounds.right is 10" );
t.eq( point.bounds.top, y, "bounds.top is 20" );
t.eq( point.bounds.bottom, y, "bounds.bottom is 20" );
}
function test_Point_transform_getBounds (t) {
t.plan(2);
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
point.calculateBounds();
t.ok( point.bounds != null, "bounds calculated by calcBounds" );
point.transform(new OpenLayers.Projection("EPSG:4326"),
new OpenLayers.Projection("EPSG:900913"));
t.eq(point.bounds, null, "Point bounds cleared after transform");
}
function test_Point_distanceTo(t) {
t.plan(7);
var x1 = 10;
var y1 = 20;
point1 = new OpenLayers.Geometry.Point(x1, y1);
var x2 = 100;
var y2 = 200;
point2 = new OpenLayers.Geometry.Point(x2, y2);
var dist = point1.distanceTo(point2)
t.eq( dist, 201.24611797498107267682563018581, "distances calculating correctly");
t.eq( dist, Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)), "distance calculation correct");
// test that details are returned (though trivial in this case)
var result = point1.distanceTo(point2, {details: true});
t.eq(result.distance, point1.distanceTo(point2), "[details] distance property is same as return without details");
t.eq(result.x0, x1, "[details] x0 property is correct");
t.eq(result.y0, y1, "[details] y0 property is correct");
t.eq(result.x1, x2, "[details] x1 property is correct");
t.eq(result.y1, y2, "[details] y1 property is correct");
}
function test_Point_toString(t) {
t.plan(1);
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
t.eq(point.toString(), "POINT(" + x + " " + y + ")",
"toString() returns WKT" );
}
function test_Point_move(t) {
t.plan(3);
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
var dx = 10 * Math.random();
var dy = 10 * Math.random();
point.bounds = "foo";
point.move(dx, dy);
t.eq(point.x, x + dx, "move() correctly modifies x");
t.eq(point.y, y + dy, "move() correctly modifies y");
t.ok(point.bounds == null, "bounds is cleared after a move()");
}
function test_Point_rotate(t) {
t.plan(5);
var tolerance = 1e-10;
var x = 10;
var y = 20;
var point = new OpenLayers.Geometry.Point(x, y);
var origin = new OpenLayers.Geometry.Point(5, 10);
// rotate a full revolution
point.bounds = "foo";
point.rotate(360, origin);
t.ok(((point.x - x) / x) < tolerance,
"rotate by 360 returns to the same y");
t.ok(((point.y - y) / y) < tolerance,
"rotate by 360 returns to the same y");
t.ok(point.bounds == null, "bounds is cleared after a rotate()");
// rotate an 1/8 turn
point.rotate(45, origin);
t.ok(((point.x - 1.4644660940672636) / 1.4644660940672636) < tolerance,
"rotate 1/8 turn correctly");
t.ok(((point.y - 20.606601717798213) / 20.606601717798213) < tolerance,
"rotate 1/8 turn correctly");
}
function test_Point_resize(t) {
t.plan(6);
var tolerance = 1e-10;
var x = 100 * Math.random();
var y = 100 * Math.random();
var point = new OpenLayers.Geometry.Point(x, y);
point.bounds = "foo";
var i = 100 * Math.random();
var j = 100 * Math.random();
var origin = new OpenLayers.Geometry.Point(i, j);
var scale = 10 * Math.random();
var oldDistance = origin.distanceTo(point);
var ret = point.resize(scale, origin);
var newDistance = origin.distanceTo(point);
t.ok(ret === point, "resize returns geometry");
t.ok((origin.x == i) && (origin.y == j),
"resize leaves the origin untouched");
t.ok((((newDistance / oldDistance) - scale) / scale) < tolerance,
"resize moves points the correct distance from the origin");
t.ok(point.bounds == null, "bounds is correctly cleared after a resize()");
// resize with non uniform scaling (ratio != 1)
point = new OpenLayers.Geometry.Point(10, 10);
origin = new OpenLayers.Geometry.Point(0, 0);
point.resize(2, origin, 4);
t.eq(point.x, 80, "non-uniform scaling correctly applied in x dim");
t.eq(point.y, 20, "non-uniform scaling correctly applied in y dim");
}
function test_Point_equals(t) {
t.plan(3);
var x = Math.random() * 100;
var y = Math.random() * 100;
var geometry = new OpenLayers.Geometry.Point(x, y);
var equal = new OpenLayers.Geometry.Point(x, y);
var offX = new OpenLayers.Geometry.Point(x + 1, y);
var offY = new OpenLayers.Geometry.Point(x, y + 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(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);
var x = Math.random() * 100;
var y = Math.random() * 100;
var geometry = new OpenLayers.Geometry.Point(x, y);
var clone = geometry.clone();
t.ok(clone instanceof OpenLayers.Geometry.Point,
"clone() creates an OpenLayers.Geometry.Point");
t.ok(geometry.equals(clone), "clone has equivalent coordinates");
}
</script>
</head>
<body>
</body>
</html>