1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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>
|