Files
openlayers/tests/Geometry/Collection.html

432 lines
16 KiB
HTML

<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
var coll;
function test_Collection_constructor (t) {
t.plan( 4 );
//null param
coll = new OpenLayers.Geometry.Collection();
t.ok( coll instanceof OpenLayers.Geometry.Collection, "new OpenLayers.Geometry.Collection returns coll object" );
t.eq( coll.CLASS_NAME, "OpenLayers.Geometry.Collection", "coll.CLASS_NAME is set correctly");
t.eq( coll.components.length, 0, "coll.components is set correctly");
OpenLayers.Geometry.Collection.prototype._addComponents =
OpenLayers.Geometry.Collection.prototype.addComponents;
OpenLayers.Geometry.Collection.prototype.addComponents =
function(comps) { g_addcomponents = comps; };
//valid param
g_addcomponents = null;
var components = {};
coll = new OpenLayers.Geometry.Collection(components);
t.ok(g_addcomponents, components, "addcomponents called on non-null param")
OpenLayers.Geometry.Collection.prototype.addComponents =
OpenLayers.Geometry.Collection.prototype._addComponents;
}
function test_Collection_addComponents (t) {
t.plan( 10 );
coll = new OpenLayers.Geometry.Collection();
//null
coll.addComponents(null);
t.ok(true, "doesn't break on add null components");
OpenLayers.Geometry.Collection.prototype._addComponent =
OpenLayers.Geometry.Collection.prototype.addComponent;
OpenLayers.Geometry.Collection.prototype.addComponent =
function(comp) { g_addComp = comp; g_added++};
//nonarray argument
var g_added = 0;
var g_addComp = 0;
var component = {};
coll.addComponents(component);
t.eq(g_added, 1, "added once");
t.eq(g_addComp, component, "added component");
//array arg
var g_added = 0;
var g_addComp = 0;
var component1 = {};
var component2 = {};
coll.addComponents([component1, component2]);
t.eq(g_added, 2, "added twice");
t.eq(g_addComp, component2, "added component");
OpenLayers.Geometry.Collection.prototype.addComponent =
OpenLayers.Geometry.Collection.prototype._addComponent;
coll.addComponents(new OpenLayers.Geometry.Point(0,0));
coll.addComponents(new OpenLayers.Geometry.Point(10,10));
t.eq( coll.components.length, 2, "added two components to collection" );
bounds = coll.getBounds();
t.eq( bounds.left, 0, "left bound is 0" );
t.eq( bounds.bottom, 0, "bottom bound is 0" );
t.eq( bounds.right, 10, "right bound is 10" );
t.eq( bounds.top, 10, "top bound is 10" );
}
function test_Collection_clone (t) {
t.plan( 3 );
coll = new OpenLayers.Geometry.Collection();
coll.addComponents(new OpenLayers.Geometry.Point(0,0));
coll.addComponents(new OpenLayers.Geometry.Point(10,10));
coll2 = coll.clone();
t.ok( coll2 instanceof OpenLayers.Geometry.Collection, "coll.clone() returns collection object" );
t.eq( coll2.components.length, 2, "coll2.components.length is set correctly");
t.ok( coll2.components[0] instanceof OpenLayers.Geometry.Point,
"coll2.components.length is set correctly");
}
function test_Collection_removeComponents (t) {
t.plan( 5 );
coll = new OpenLayers.Geometry.Collection();
point = new OpenLayers.Geometry.Point(0,0);
coll.addComponents(point);
coll.addComponents(new OpenLayers.Geometry.Point(10,10));
coll.removeComponents(coll.components[0]);
t.eq( coll.components.length, 1, "coll.components.length is smaller after removeComponent" );
t.ok( coll.bounds == null, "bounds are nullified after call to remove (to trigger recalc on getBounds()");
bounds = coll.getBounds();
t.eq( bounds.left, 10, "left bound is 10 after removeComponent" );
t.eq( bounds.bottom, 10, "bottom bound is 10 after removeComponent" );
coll = new OpenLayers.Geometry.Collection();
for(var i=0; i<5; ++i) {
coll.addComponents(
new OpenLayers.Geometry.Point(Math.random(), Math.random())
);
}
coll.removeComponents(coll.components);
t.eq(coll.components.length, 0,
"remove components even works with multiple components");
}
function test_Collection_calculateBounds(t) {
t.plan( 9 );
var coll = new OpenLayers.Geometry.Collection();
coll.calculateBounds();
t.eq(coll.bounds, null, "null components list gives null bounds on calculation()");
var p1 = new OpenLayers.Geometry.Point(10,20);
var p2 = new OpenLayers.Geometry.Point(30,40);
var components = [p1, p2];
coll = new OpenLayers.Geometry.Collection(components);
coll.calculateBounds();
t.eq(coll.bounds.left, 10, "good left bounds");
t.eq(coll.bounds.bottom, 20, "good bottom bounds");
t.eq(coll.bounds.right, 30, "good right bounds");
t.eq(coll.bounds.top, 40, "good top bounds");
var newPoint = new OpenLayers.Geometry.Point(60,70);
coll.addComponent(newPoint);
coll.calculateBounds();
t.eq(coll.bounds.left, 10, "good left bounds");
t.eq(coll.bounds.bottom, 20, "good bottom bounds");
t.eq(coll.bounds.right, 60, "good right bounds");
t.eq(coll.bounds.top, 70, "good top bounds");
}
function test_Collection_equals(t) {
t.plan(1);
var geom = new OpenLayers.Geometry.Collection();
t.ok(!geom.equals(), "collection.equals() returns false for undefined");
}
function test_Collection_addComponent(t) {
t.plan(10);
var coll = new OpenLayers.Geometry.Collection();
//null
coll.addComponent(null);
t.ok(!coll.addComponent(null),
"addComponent returns false for bad component")
//good component
var component = new OpenLayers.Geometry.Point(3,4);
t.ok(coll.addComponent(component),
"addComponent returns true for good component");
t.ok(coll.bounds == null, "bounds cache correctly cleared");
var foundComponent = false;
for(var i=0; i< coll.components.length; i++) {
if (coll.components[i].equals(component)) {
foundComponent = true;
}
}
t.ok(foundComponent, "component added to internal array");
// restricted components
coll.componentTypes = ["OpenLayers.Geometry.Point",
"OpenLayers.Geometry.LineString"];
var point1 = new OpenLayers.Geometry.Point(0,0);
var point2 = new OpenLayers.Geometry.Point(1,1);
var line = new OpenLayers.Geometry.LineString([point1, point2]);
var multipoint = new OpenLayers.Geometry.MultiPoint([point1, point2]);
t.ok(coll.addComponent(point1),
"addComponent returns true for 1st geometry type in componentTypes");
t.ok(OpenLayers.Util.indexOf(coll.components, point1) > -1,
"addComponent adds 1st restricted type to components array");
t.ok(coll.addComponent(line),
"addComponent returns true for 2nd geometry type in componentTypes");
t.ok(OpenLayers.Util.indexOf(coll.components, point1) > -1,
"addComponent adds 2nd restricted type to components array");
t.ok(!coll.addComponent(multipoint),
"addComponent returns false for geometry type not in componentTypes");
t.ok(OpenLayers.Util.indexOf(coll.components, multipoint) == -1,
"addComponent doesn't add restricted type to component array");
}
function test_collection_getLength(t) {
t.plan(2);
//null
var coll = new OpenLayers.Geometry.Collection();
t.eq( coll.getLength(), 0, "null coll has 0 getlength");
//valid
coll.components = [
{ 'getLength': function() { return 50; } },
{ 'getLength': function() { return 15; } }
];
t.eq( coll.getLength(), 65, "coll with valid components correctly sums getlength");
}
function test_collection_getArea(t) {
t.plan(2);
//null
var coll = new OpenLayers.Geometry.Collection();
t.eq( coll.getArea(), 0, "null coll has 0 getArea");
//valid
coll.components = [
{ 'getArea': function() { return 50; } },
{ 'getArea': function() { return 15; } }
];
t.eq( coll.getArea(), 65, "coll with valid components correctly sums getArea");
}
function test_transform(t) {
t.plan(5);
var p1 = new OpenLayers.Geometry.Point(0,0);
p1.bounds = "foo";
var p2 = new OpenLayers.Geometry.Point(1,1);
p2.bounds = "foo";
var line = new OpenLayers.Geometry.LineString([p1, p2]);
var multipoint = new OpenLayers.Geometry.MultiPoint([p1, p2]);
var coll = new OpenLayers.Geometry.Collection([
p1, p2, line, multipoint
]);
coll.bounds = "foo";
var wgs84 = new OpenLayers.Projection("EPSG:4326");
var sm = new OpenLayers.Projection("EPSG:900913");
coll.transform(wgs84, sm);
t.eq(coll.bounds, null, "coll bounds cleared");
t.eq(p1.bounds, null, "p1 component bounds cleared");
t.eq(p2.bounds, null, "p2 component bounds cleared");
t.eq(line.bounds, null, "line component bounds cleared");
t.eq(multipoint.bounds, null, "multipoint component bounds cleared");
}
function test_getCentroid_pts_only(t) {
t.plan(3);
coll = new OpenLayers.Geometry.Collection();
coll.addComponent(new OpenLayers.Geometry.Point(0,0));
coll.addComponent(new OpenLayers.Geometry.Point(1,1));
centroid = coll.getCentroid(true);
t.ok(centroid != null, 'The centroid is not null.');
t.eq(centroid.x, 0.5, 'The centroid x coordinate is good.');
t.eq(centroid.y, 0.5, 'The centroid y coordinate is good.');
coll.destroy();
}
function test_getCentroid_poly_nonrecursive(t) {
t.plan(3);
coll = new OpenLayers.Geometry.Collection();
coll.addComponent(
new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([
new OpenLayers.Geometry.Point(0,0),
new OpenLayers.Geometry.Point(0,1),
new OpenLayers.Geometry.Point(1,1),
new OpenLayers.Geometry.Point(1,0)
])
])
);
// performing non-recursive getCentroid means this next polygon
// is excluded from the centroid computation
coll.addComponent(
new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([
new OpenLayers.Geometry.Point(2,2),
new OpenLayers.Geometry.Point(2,3),
new OpenLayers.Geometry.Point(3,3),
new OpenLayers.Geometry.Point(3,2)
])
])
);
centroid = coll.getCentroid();
t.ok(centroid != null, 'The centroid is not null.');
t.eq(centroid.x, 0.5, 'The centroid x coordinate is good.');
t.eq(centroid.y, 0.5, 'The centroid y coordinate is good.');
coll.destroy();
}
function test_getCentroid_poly_only(t) {
t.plan(3);
coll = new OpenLayers.Geometry.Collection();
coll.addComponent(
new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([
new OpenLayers.Geometry.Point(0,0),
new OpenLayers.Geometry.Point(0,1),
new OpenLayers.Geometry.Point(1,1),
new OpenLayers.Geometry.Point(1,0)
])
])
);
centroid = coll.getCentroid(true);
t.ok(centroid != null, 'The centroid is not null.');
t.eq(centroid.x, 0.5, 'The centroid x coordinate is good.');
t.eq(centroid.y, 0.5, 'The centroid y coordinate is good.');
coll.destroy();
}
function test_getCentroid_poly_and_pts(t) {
t.plan(3);
coll = new OpenLayers.Geometry.Collection();
coll.addComponent(
new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([
new OpenLayers.Geometry.Point(0,0),
new OpenLayers.Geometry.Point(0,1),
new OpenLayers.Geometry.Point(1,1),
new OpenLayers.Geometry.Point(1,0)
])
])
);
// since the polygon above has an area of 1 and these
// points have an area of 0, they should not change the centroid
coll.addComponent( new OpenLayers.Geometry.Point(2,2) );
coll.addComponent( new OpenLayers.Geometry.Point(4,4) );
centroid = coll.getCentroid(true);
t.ok(centroid != null, 'The centroid is not null.');
t.eq(centroid.x, 0.5, 'The centroid x coordinate is good.');
t.eq(centroid.y, 0.5, 'The centroid y coordinate is good.');
coll.destroy();
}
function test_getCentroid_poly_big_and_small(t) {
t.plan(3);
coll = new OpenLayers.Geometry.Collection();
// polygon w/area=1, centroid=0.5,0.5
coll.addComponent(
new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([
new OpenLayers.Geometry.Point(0,0),
new OpenLayers.Geometry.Point(0,1),
new OpenLayers.Geometry.Point(1,1),
new OpenLayers.Geometry.Point(1,0)
])
])
);
// since the polygon above has an area of 1 and this
// polygon has an area of 4, the center is weighted 20% toward
// the first polygon
// polygon w/area=4, centroid=5.5,5.5
coll.addComponent(
new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([
new OpenLayers.Geometry.Point(4.5,-0.5),
new OpenLayers.Geometry.Point(4.5,1.5),
new OpenLayers.Geometry.Point(6.5,1.5),
new OpenLayers.Geometry.Point(6.5,-0.5)
])
])
);
centroid = coll.getCentroid(true);
t.ok(centroid != null, 'The centroid is not null.');
t.eq(centroid.x, 4.5, 'The centroid x coordinate is good.');
t.eq(centroid.y, 0.5, 'The centroid y coordinate is good.');
coll.destroy();
}
function test_avoid_infinite_recursion(t) {
t.plan(1);
var g = new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing(),
new OpenLayers.Geometry.LinearRing()
]);
var bounds;
try {
bounds = g.getBounds();
t.eq(bounds, null, "Polygon with empty linear ring has null bounds");
} catch (err) {
t.fail("Failed to get bounds of polygon with empty linear ring: " + err.message);
}
}
function test_Collection_destroy(t) {
t.plan( 3 );
coll = new OpenLayers.Geometry.Collection();
coll.addComponents(new OpenLayers.Geometry.Point(0,0));
coll.addComponents(new OpenLayers.Geometry.Point(10,10));
coll.getBounds();
coll.destroy();
t.ok(coll.components == null, "components array cleared");
t.ok(coll.getBounds() == null, "bounds is cleared");
t.ok(coll.id == null, "id is cleared");
}
</script>
</head>
<body>
</body>
</html>