3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
7 for(var i=0; i<12; ++i) {
8 points.push(new OpenLayers.Feature.Vector(
9 new OpenLayers.Geometry.Point(Math.random() * 100,
13 var multipoint = new OpenLayers.Feature.Vector(
14 new OpenLayers.Geometry.MultiPoint([
22 new OpenLayers.Feature.Vector(
23 new OpenLayers.Geometry.LineString([
29 new OpenLayers.Feature.Vector(
30 new OpenLayers.Geometry.LineString([
38 var multilinestring = new OpenLayers.Feature.Vector(
39 new OpenLayers.Geometry.MultiLineString([
40 linestrings[0].geometry,
41 linestrings[1].geometry
46 new OpenLayers.Geometry.LinearRing([
51 new OpenLayers.Geometry.LinearRing([
56 new OpenLayers.Geometry.LinearRing([
61 new OpenLayers.Geometry.LinearRing([
69 new OpenLayers.Feature.Vector(
70 new OpenLayers.Geometry.Polygon([rings[0], rings[1]])
72 new OpenLayers.Feature.Vector(
73 new OpenLayers.Geometry.Polygon([rings[2], rings[3]])
77 var multipolygon = new OpenLayers.Feature.Vector(
78 new OpenLayers.Geometry.MultiPolygon([
84 var collection = [points[0], linestrings[0]];
86 function test_Format_WKT_constructor(t) {
89 var options = {'foo': 'bar'};
90 var format = new OpenLayers.Format.WKT(options);
91 t.ok(format instanceof OpenLayers.Format.WKT,
92 "new OpenLayers.Format.WKT returns object" );
93 t.eq(format.foo, "bar", "constructor sets options correctly");
94 t.eq(typeof format.read, "function", "format has a read function");
95 t.eq(typeof format.write, "function", "format has a write function");
98 function test_Format_WKT_write(t) {
101 var format = new OpenLayers.Format.WKT();
105 t.eq(format.write(points[0]),
106 "POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")",
107 "format correctly writes Point WKT");
110 t.eq(format.write(multipoint),
111 "MULTIPOINT(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
112 points[1].geometry.x + " " + points[1].geometry.y + "," +
113 points[2].geometry.x + " " + points[2].geometry.y + ")",
114 "format correctly writes MultiPoint WKT");
117 t.eq(format.write(linestrings[0]),
118 "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
119 points[1].geometry.x + " " + points[1].geometry.y + "," +
120 points[2].geometry.x + " " + points[2].geometry.y + ")",
121 "format correctly writes LineString WKT");
123 // test a multilinestring
124 t.eq(format.write(multilinestring),
125 "MULTILINESTRING((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
126 points[1].geometry.x + " " + points[1].geometry.y + "," +
127 points[2].geometry.x + " " + points[2].geometry.y + ")," +
128 "(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
129 points[4].geometry.x + " " + points[4].geometry.y + "," +
130 points[5].geometry.x + " " + points[5].geometry.y + "))",
131 "format correctly writes MultiLineString WKT");
134 t.eq(format.write(polygons[0]),
135 "POLYGON((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
136 points[1].geometry.x + " " + points[1].geometry.y + "," +
137 points[2].geometry.x + " " + points[2].geometry.y + "," +
138 points[0].geometry.x + " " + points[0].geometry.y + ")," +
139 "(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
140 points[4].geometry.x + " " + points[4].geometry.y + "," +
141 points[5].geometry.x + " " + points[5].geometry.y + "," +
142 points[3].geometry.x + " " + points[3].geometry.y + "))",
143 "format correctly writes Polygon WKT");
145 // test a multipolygon
146 t.eq(format.write(multipolygon),
147 "MULTIPOLYGON(((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
148 points[1].geometry.x + " " + points[1].geometry.y + "," +
149 points[2].geometry.x + " " + points[2].geometry.y + "," +
150 points[0].geometry.x + " " + points[0].geometry.y + ")," +
151 "(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
152 points[4].geometry.x + " " + points[4].geometry.y + "," +
153 points[5].geometry.x + " " + points[5].geometry.y + "," +
154 points[3].geometry.x + " " + points[3].geometry.y + "))," +
155 "((" + points[6].geometry.x + " " + points[6].geometry.y + "," +
156 points[7].geometry.x + " " + points[7].geometry.y + "," +
157 points[8].geometry.x + " " + points[8].geometry.y + "," +
158 points[6].geometry.x + " " + points[6].geometry.y + ")," +
159 "(" + points[9].geometry.x + " " + points[9].geometry.y + "," +
160 points[10].geometry.x + " " + points[10].geometry.y + "," +
161 points[11].geometry.x + " " + points[11].geometry.y + "," +
162 points[9].geometry.x + " " + points[9].geometry.y + ")))",
163 "format correctly writes MultiPolygon WKT");
165 // test a geometrycollection
166 t.eq(format.write(collection),
167 "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," +
168 "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
169 points[1].geometry.x + " " + points[1].geometry.y + "," +
170 points[2].geometry.x + " " + points[2].geometry.y + "))",
171 "format correctly writes GeometryCollection WKT");
175 function test_Format_WKT_read(t) {
178 var format = new OpenLayers.Format.WKT();
181 * Since we're explicitly testing calls to write, the read tests
182 * just make sure that geometry can make a round trip from read to write.
186 t.ok(points[0].geometry.equals(format.read(format.write(points[0])).geometry),
187 "format correctly reads Point WKT");
190 t.ok(multipoint.geometry.equals(format.read(format.write(multipoint)).geometry),
191 "format correctly reads MultiPoint WKT");
194 t.ok(linestrings[0].geometry.equals(format.read(format.write(linestrings[0])).geometry),
195 "format correctly reads LineString WKT");
197 // test a multilinestring
198 t.ok(multilinestring.geometry.equals(format.read(format.write(multilinestring)).geometry),
199 "format correctly reads MultiLineString WKT");
202 t.ok(polygons[0].geometry.equals(format.read(format.write(polygons[0])).geometry),
203 "format correctly reads Polygon WKT");
205 // test a multipolygon
206 t.ok(multipolygon.geometry.equals(format.read(format.write(multipolygon)).geometry),
207 "format correctly reads MultiPolygon WKT");
209 // test a geometrycollection
210 t.eq(format.write(collection),
211 "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," +
212 "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
213 points[1].geometry.x + " " + points[1].geometry.y + "," +
214 points[2].geometry.x + " " + points[2].geometry.y + "))",
215 "format correctly writes GeometryCollection WKT");
219 function test_Format_WKT_read_projection(t) {
223 src: new OpenLayers.Projection("EPSG:4326"),
224 dest: new OpenLayers.Projection("EPSG:900913")
228 src: new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-87.9, 41.9)),
229 dest: new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-9784983.2393667, 5146011.6785665))
232 var format = new OpenLayers.Format.WKT({
233 externalProjection: projections["src"],
234 internalProjection: projections["dest"]
236 var feature = format.read("GEOMETRYCOLLECTION(POINT(" + points["src"].geometry.x + " " + points["src"].geometry.y + "))")[0];
237 t.eq(feature.geometry.toString(), points["dest"].geometry.toString(),
238 "Geometry collections aren't transformed twice when reprojection.");