Files
openlayers/tests/Format/test_WKT.html

190 lines
9.2 KiB
HTML

<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
var points = [];
for(var i=0; i<12; ++i) {
points.push(new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(Math.random() * 100,
Math.random() * 100))
);
}
var multipoint = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.MultiPoint([
points[0].geometry, points[1].geometry, points[2].geometry
])
);
var linestrings = [
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString([points[0].geometry, points[1].geometry, points[2].geometry])
),
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString([points[3].geometry, points[4].geometry, points[5].geometry])
)
];
var multilinestring = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.MultiLineString([
linestrings[0].geometry, linestrings[1].geometry
])
);
var rings = [
new OpenLayers.Geometry.LinearRing([points[0].geometry, points[1].geometry, points[2].geometry]),
new OpenLayers.Geometry.LinearRing([points[3].geometry, points[4].geometry, points[5].geometry]),
new OpenLayers.Geometry.LinearRing([points[6].geometry, points[7].geometry, points[8].geometry]),
new OpenLayers.Geometry.LinearRing([points[9].geometry, points[10].geometry, points[11].geometry])
];
var polygons = [
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([rings[0], rings[1]])
),
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([rings[2], rings[3]])
)
];
var multipolygon = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.MultiPolygon([
polygons[0].geometry, polygons[1].geometry
])
);
var collection = [points[0], linestrings[0]];
function test_Format_WKT_constructor(t) {
t.plan(4);
var options = {'foo': 'bar'};
var format = new OpenLayers.Format.WKT(options);
t.ok(format instanceof OpenLayers.Format.WKT,
"new OpenLayers.Format.WKT returns object" );
t.eq(format.foo, "bar", "constructor sets options correctly");
t.eq(typeof format.read, "function", "format has a read function");
t.eq(typeof format.write, "function", "format has a write function");
}
function test_Format_WKT_write(t) {
t.plan(7);
var format = new OpenLayers.Format.WKT();
// test a point
t.eq(format.write(points[0]),
"POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")",
"format correctly writes Point WKT");
// test a multipoint
t.eq(format.write(multipoint),
"MULTIPOINT(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].geometry.x + " " + points[2].geometry.y + ")",
"format correctly writes MultiPoint WKT");
// test a linestring
t.eq(format.write(linestrings[0]),
"LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].geometry.x + " " + points[2].geometry.y + ")",
"format correctly writes LineString WKT");
// test a multilinestring
t.eq(format.write(multilinestring),
"MULTILINESTRING((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].geometry.x + " " + points[2].geometry.y + ")," +
"(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
points[4].geometry.x + " " + points[4].geometry.y + "," +
points[5].geometry.x + " " + points[5].geometry.y + "))",
"format correctly writes MultiLineString WKT");
// test a polygon
t.eq(format.write(polygons[0]),
"POLYGON((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].geometry.x + " " + points[2].geometry.y + "," +
points[0].geometry.x + " " + points[0].geometry.y + ")," +
"(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
points[4].geometry.x + " " + points[4].geometry.y + "," +
points[5].geometry.x + " " + points[5].geometry.y + "," +
points[3].geometry.x + " " + points[3].geometry.y + "))",
"format correctly writes Polygon WKT");
// test a multipolygon
t.eq(format.write(multipolygon),
"MULTIPOLYGON(((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].geometry.x + " " + points[2].geometry.y + "," +
points[0].geometry.x + " " + points[0].geometry.y + ")," +
"(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
points[4].geometry.x + " " + points[4].geometry.y + "," +
points[5].geometry.x + " " + points[5].geometry.y + "," +
points[3].geometry.x + " " + points[3].geometry.y + "))," +
"((" + points[6].geometry.x + " " + points[6].geometry.y + "," +
points[7].geometry.x + " " + points[7].geometry.y + "," +
points[8].geometry.x + " " + points[8].geometry.y + "," +
points[6].geometry.x + " " + points[6].geometry.y + ")," +
"(" + points[9].geometry.x + " " + points[9].geometry.y + "," +
points[10].geometry.x + " " + points[10].geometry.y + "," +
points[11].geometry.x + " " + points[11].geometry.y + "," +
points[9].geometry.x + " " + points[9].geometry.y + ")))",
"format correctly writes MultiPolygon WKT");
// test a geometrycollection
t.eq(format.write(collection),
"GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," +
"LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].geometry.x + " " + points[2].geometry.y + "))",
"format correctly writes GeometryCollection WKT");
}
function test_Format_WKT_read(t) {
t.plan(7);
var format = new OpenLayers.Format.WKT();
/**
* Since we're explicitly testing calls to write, the read tests
* just make sure that geometry can make a round trip from read to write.
*/
// test a point
t.ok(points[0].geometry.equals(format.read(format.write(points[0])).geometry),
"format correctly reads Point WKT");
// test a multipoint
t.ok(multipoint.geometry.equals(format.read(format.write(multipoint)).geometry),
"format correctly reads MultiPoint WKT");
// test a linestring
t.ok(linestrings[0].geometry.equals(format.read(format.write(linestrings[0])).geometry),
"format correctly reads LineString WKT");
// test a multilinestring
t.ok(multilinestring.geometry.equals(format.read(format.write(multilinestring)).geometry),
"format correctly reads MultiLineString WKT");
// test a polygon
t.ok(polygons[0].geometry.equals(format.read(format.write(polygons[0])).geometry),
"format correctly reads Polygon WKT");
// test a multipolygon
t.ok(multipolygon.geometry.equals(format.read(format.write(multipolygon)).geometry),
"format correctly reads MultiPolygon WKT");
// test a geometrycollection
t.eq(format.write(collection),
"GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," +
"LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].geometry.x + " " + points[2].geometry.y + "))",
"format correctly writes GeometryCollection WKT");
}
</script>
</head>
<body>
</body>
</html>