summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Format/WKT.html
diff options
context:
space:
mode:
Diffstat (limited to 'misc/openlayers/tests/Format/WKT.html')
-rw-r--r--misc/openlayers/tests/Format/WKT.html297
1 files changed, 297 insertions, 0 deletions
diff --git a/misc/openlayers/tests/Format/WKT.html b/misc/openlayers/tests/Format/WKT.html
new file mode 100644
index 0000000..bdfc233
--- /dev/null
+++ b/misc/openlayers/tests/Format/WKT.html
@@ -0,0 +1,297 @@
+<html>
+<head>
+ <script src="../OLLoader.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 = new OpenLayers.Feature.Vector(
+ new OpenLayers.Geometry.Collection([
+ points[0].geometry,
+ linestrings[0].geometry
+ ])
+ );
+
+ var geom_array = [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(8);
+
+ 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 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");
+
+ // test writing an array of geometries
+ t.eq(format.write(geom_array),
+ "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 WKT for an array of Geometries");
+
+ }
+
+ function test_Format_WKT_read(t) {
+ t.plan(13);
+
+ 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 multipoint without separating parens
+ t.ok(multipoint.geometry.equals(format.read(
+ "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 + ")").geometry),
+ "format correctly reads MultiPoint WKT without parens");
+
+ // 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 collection
+ var wkt = format.write(collection);
+ var got = format.read(wkt);
+ t.ok(got instanceof Array, "by default, reading a collection returns an array");
+ t.eq(got.length, 2, "read two items");
+ t.ok(got[0] instanceof OpenLayers.Feature.Vector, "first item is a feature");
+ t.geom_eq(got[0].geometry, points[0].geometry, "first feature's geometry is the correct point");
+ t.ok(got[1] instanceof OpenLayers.Feature.Vector, "second item is a feature");
+ t.geom_eq(got[1].geometry, linestrings[0].geometry, "second feature's geometry is the correct linestring");
+
+ }
+
+ function test_whitespace(t) {
+ t.plan(3);
+ var wkt = "LINESTRING(7.120068\t43.583917,\n7.120154 43.583652,\n7.120385\t43.582716,\r\n7.12039 43.582568, 7.120712 43.581511,7.120873\n43.580718)";
+ var format = new OpenLayers.Format.WKT();
+ var got = format.read(wkt);
+ t.ok(got instanceof OpenLayers.Feature.Vector, "read a feature");
+ t.ok(got.geometry instanceof OpenLayers.Geometry.LineString, "read a linestring");
+ t.ok(got.geometry.components.length, 6, "read a geometry with 6 components");
+ }
+
+ function test_Format_WKT_read_projection(t) {
+ t.plan(1);
+
+ var projections = {
+ src: new OpenLayers.Projection("EPSG:4326"),
+ dest: new OpenLayers.Projection("EPSG:900913")
+ },
+ points = {
+ src: new OpenLayers.Feature.Vector(
+ new OpenLayers.Geometry.Point(-87.9, 41.9)
+ ),
+ dest: new OpenLayers.Feature.Vector(
+ new OpenLayers.Geometry.Point(-9784983.2393667, 5146011.6785665)
+ )
+ },
+ format = new OpenLayers.Format.WKT({
+ externalProjection: projections["src"],
+ internalProjection: projections["dest"]
+ }),
+ gc_wkt_parts = [
+ "GEOMETRYCOLLECTION(",
+ "POINT(",
+ points["src"].geometry.x,
+ " ",
+ points["src"].geometry.y,
+ ")",
+ ")"
+ ],
+ feature = format.read( gc_wkt_parts.join("") )[0],
+ gotGeom = feature.geometry,
+ expectGeom = points["dest"].geometry,
+ // we don't use geometry::toString because we might run into
+ // precision issues
+ precision = 7,
+ got = gotGeom.x.toFixed(precision) + ' ' + gotGeom.y.toFixed(precision),
+ expected = expectGeom.x.toFixed(precision) + ' ' + expectGeom.y.toFixed(precision);
+
+ t.eq(got, expected,
+ "Geometry collections aren't transformed twice when reprojection.");
+ }
+ </script>
+</head>
+<body>
+</body>
+</html>