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
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
var line;
function test_MultiLineString_constructor (t) {
t.plan( 3 );
mline = new OpenLayers.Geometry.MultiLineString();
t.ok( mline instanceof OpenLayers.Geometry.MultiLineString, "new OpenLayers.Geometry.MultiLineString returns mline object" );
t.eq( mline.CLASS_NAME, "OpenLayers.Geometry.MultiLineString", "mline.CLASS_NAME is set correctly");
t.eq( mline.components, [], "line.components is set correctly");
}
function test_MultiLineString_constructor (t) {
t.plan( 3 );
line = new OpenLayers.Geometry.LineString();
mline = new OpenLayers.Geometry.MultiLineString(line);
t.ok( mline instanceof OpenLayers.Geometry.MultiLineString, "new OpenLayers.Geometry.MultiLineString returns mline object" );
t.eq( mline.CLASS_NAME, "OpenLayers.Geometry.MultiLineString", "mline.CLASS_NAME is set correctly");
t.eq( mline.components.length, 1, "mline.components.length is set correctly");
}
function test_split(t) {
var wkt = OpenLayers.Geometry.fromWKT;
var cases = [{
msg: "no intersection",
g1: "MULTILINESTRING((0 0, 0 1), (2 2, 3 3))",
g2: "MULTILINESTRING((1 0, 1 1), (2 2, 3 2))",
exp: null
} , {
msg: "intersection at midpoint",
g1: "MULTILINESTRING((0 0, 1 1))",
g2: "MULTILINESTRING((1 0, 0 1))",
exp: ["MULTILINESTRING((1 0, 0.5 0.5))", "MULTILINESTRING((0.5 0.5, 0 1))"]
}, {
msg: "intersection at midpoint (reverse source/target)",
g1: "MULTILINESTRING((1 0, 0 1))",
g2: "MULTILINESTRING((0 0, 1 1))",
exp: ["MULTILINESTRING((0 0, 0.5 0.5))", "MULTILINESTRING((0.5 0.5, 1 1))"]
}, {
msg: "intersection at endpoint",
g1: "MULTILINESTRING((0 0, 1 1))",
g2: "MULTILINESTRING((1 0, 1 1))",
exp: null
}, {
msg: "midpoint intersection, no options",
g1: "MULTILINESTRING((0 0, 2 2))",
g2: "MULTILINESTRING((0 2, 2 0))",
exp: ["MULTILINESTRING((0 2, 1 1))", "MULTILINESTRING((1 1, 2 0))"]
}, {
msg: "midpoint intersection, edge false",
opt: {edge: false},
g1: "MULTILINESTRING((0 0, 2 2))",
g2: "MULTILINESTRING((0 2, 2 0))",
exp: null
}, {
msg: "midpoint intersection, mutual",
opt: {mutual: true},
g1: "MULTILINESTRING((0 0, 2 2))",
g2: "MULTILINESTRING((0 2, 2 0))",
exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2))"], ["MULTILINESTRING((0 2, 1 1))", "MULTILINESTRING((1 1, 2 0))"]]
}, {
msg: "close intersection, no tolerance",
g1: "MULTILINESTRING((0 0, 0.9 0.9))",
g2: "MULTILINESTRING((0 2, 2 0))",
exp: null
}, {
msg: "close intersection, within tolerance",
opt: {tolerance: 0.2},
g1: "MULTILINESTRING((0 0, 0.9 0.9))",
g2: "MULTILINESTRING((0 2, 2 0))",
exp: ["MULTILINESTRING((0 2, 0.9 0.9))", "MULTILINESTRING((0.9 0.9, 2 0))"]
}, {
msg: "multi source, single target",
g1: "MULTILINESTRING((0 0, 2 2))",
g2: "LINESTRING(0 2, 2 0)",
exp: ["LINESTRING(0 2, 1 1)", "LINESTRING(1 1, 2 0)"]
}, {
msg: "multi source, single target, mutual split",
opt: {mutual: true},
g1: "MULTILINESTRING((0 0, 2 2))",
g2: "LINESTRING(0 2, 2 0)",
exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2))"], ["LINESTRING(0 2, 1 1)", "LINESTRING(1 1, 2 0)"]]
}, {
msg: "single source, multi target",
g1: "LINESTRING(0 2, 2 0)",
g2: "MULTILINESTRING((0 0, 2 2))",
exp: ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2))"]
}, {
msg: "partial target split",
g1: "MULTILINESTRING((2 0, 0 2))",
g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
exp: ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]
}, {
msg: "partial target split, mutual true",
opt: {mutual: true},
g1: "MULTILINESTRING((2 0, 0 2))",
g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
exp: [["MULTILINESTRING((2 0, 1 1))", "MULTILINESTRING((1 1, 0 2))"], ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]]
}, {
msg: "partial source split, mutual true",
opt: {mutual: true},
g1: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
g2: "MULTILINESTRING((2 0, 0 2))",
exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"], ["MULTILINESTRING((2 0, 1 1))", "MULTILINESTRING((1 1, 0 2))"]]
}, {
msg: "partial target split with source endpoint",
g1: "MULTILINESTRING((1 0, 1 1))",
g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
exp: ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]
}, {
msg: "partial target split with source endpoint, mutual true",
opt: {mutual: true},
g1: "MULTILINESTRING((5 5, 6 6), (1 0, 1 1))",
g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
exp: [[], ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]]
}, {
msg: "partial source split with target endpoint",
g1: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
g2: "MULTILINESTRING((1 0, 1 1))",
exp: null
}, {
msg: "partial source split with target endpoint, mutual true",
opt: {mutual: true},
g1: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4), (5 5, 6 6))",
g2: "MULTILINESTRING((1 0, 1 1))",
exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4), (5 5, 6 6))"], []]
}, {
msg: "partial target and source split",
g1: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
g2: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
exp: ["MULTILINESTRING((5 0, 5 2), (5 4, 5 5))", "MULTILINESTRING((5 5, 5 6), (5 8, 5 10))"]
}, {
msg: "partial target and source split, mutual true",
opt: {mutual: true},
g1: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
g2: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
exp: [["MULTILINESTRING((0 5, 2 5), (4 5, 5 5))", "MULTILINESTRING((5 5, 6 5), (8 5, 10 5))"],
["MULTILINESTRING((5 0, 5 2), (5 4, 5 5))", "MULTILINESTRING((5 5, 5 6), (5 8, 5 10))"]]
}, {
msg: "partial target and source split with source endpoint, mutual true",
opt: {mutual: true},
g1: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
g2: "MULTILINESTRING((4 0, 4 2), (4 4, 4 6), (4 8, 4 10))",
exp: [[], ["MULTILINESTRING((4 0, 4 2), (4 4, 4 5))", "MULTILINESTRING((4 5, 4 6), (4 8, 4 10))"]]
}, {
msg: "partial target and source split with target endpoint, mutual true",
opt: {mutual: true},
g1: "MULTILINESTRING((4 0, 4 2), (4 4, 4 6), (4 8, 4 10))",
g2: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
exp: [["MULTILINESTRING((4 0, 4 2), (4 4, 4 5))", "MULTILINESTRING((4 5, 4 6), (4 8, 4 10))"], []]
}, {
msg: "partial target and source split with source vertex, mutual true",
opt: {mutual: true},
g1: "MULTILINESTRING((0 5, 2 5), (4 5, 5 5, 6 5), (8 5, 10 5))",
g2: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
exp: [["MULTILINESTRING((0 5, 2 5), (4 5, 5 5))", "MULTILINESTRING((5 5, 6 5), (8 5, 10 5))"], ["MULTILINESTRING((5 0, 5 2), (5 4, 5 5))", "MULTILINESTRING((5 5, 5 6), (5 8, 5 10))"]]
}, {
msg: "partial target and source split with target vertex, mutual true",
opt: {mutual: true},
g1: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
g2: "MULTILINESTRING((0 5, 2 5), (4 5, 5 5, 6 5), (8 5, 10 5))",
exp: [["MULTILINESTRING((5 0, 5 2), (5 4, 5 5))", "MULTILINESTRING((5 5, 5 6), (5 8, 5 10))"], ["MULTILINESTRING((0 5, 2 5), (4 5, 5 5))", "MULTILINESTRING((5 5, 6 5), (8 5, 10 5))"]]
}];
t.plan(cases.length);
var c, parts, part, midparts;
for(var i=0; i<cases.length; ++i) {
c = cases[i];
var g1 = wkt(c.g1);
var g2 = wkt(c.g2);
var got = g1.split(g2, c.opt);
var exp = c.exp;
if(got instanceof Array) {
parts = [];
for(var j=0; j<got.length; ++j) {
part = got[j];
if(part instanceof Array) {
midparts = [];
for(var k=0; k<part.length; ++k) {
midparts.push(part[k].toString());
}
parts.push("[" + midparts.join(", ") + "]");
} else {
parts.push(got[j].toString());
}
}
got = parts.join(", ");
}
if(exp instanceof Array) {
parts = [];
for(var j=0; j<exp.length; ++j) {
part = exp[j];
if(part instanceof Array) {
midparts = [];
for(var k=0; k<part.length; ++k) {
midparts.push(wkt(part[k]).toString());
}
parts.push("[" + midparts.join(", ") + "]");
} else {
parts.push(wkt(exp[j]).toString());
}
}
exp = parts.join(", ");
}
t.eq(got, exp, "case " + i + ": " + c.msg);
}
}
function test_getVertices(t) {
t.plan(22);
var points = [
new OpenLayers.Geometry.Point(10, 20),
new OpenLayers.Geometry.Point(20, 30),
new OpenLayers.Geometry.Point(30, 40),
new OpenLayers.Geometry.Point(40, 50)
];
var multi = new OpenLayers.Geometry.MultiLineString([
new OpenLayers.Geometry.LineString(points),
new OpenLayers.Geometry.LineString(points)
]);
var verts = multi.getVertices();
t.ok(verts instanceof Array, "got back an array");
t.eq(verts.length, 2 * points.length, "of correct length length");
t.geom_eq(verts[0], points[0], "0: correct geometry");
t.geom_eq(verts[1], points[1], "1: correct geometry");
t.geom_eq(verts[2], points[2], "2: correct geometry");
t.geom_eq(verts[3], points[3], "3: correct geometry");
t.geom_eq(verts[4], points[0], "4: correct geometry");
t.geom_eq(verts[5], points[1], "5: correct geometry");
t.geom_eq(verts[6], points[2], "6: correct geometry");
t.geom_eq(verts[7], points[3], "7: correct geometry");
// nodes only
var nodes = multi.getVertices(true);
t.ok(nodes instanceof Array, "[nodes only] got back an array");
t.eq(nodes.length, 4, "[nodes only] of correct length length");
t.geom_eq(nodes[0], points[0], "[nodes only] 0: correct geometry");
t.geom_eq(nodes[1], points[3], "[nodes only] 1: correct geometry");
t.geom_eq(nodes[2], points[0], "[nodes only] 2: correct geometry");
t.geom_eq(nodes[3], points[3], "[nodes only] 3: correct geometry");
// no nodes
var nodes = multi.getVertices(false);
t.ok(nodes instanceof Array, "[no nodes] got back an array");
t.eq(nodes.length, 4, "[no nodes] of correct length length");
t.geom_eq(nodes[0], points[1], "[no nodes] 0: correct geometry");
t.geom_eq(nodes[1], points[2], "[no nodes] 1: correct geometry");
t.geom_eq(nodes[2], points[1], "[no nodes] 2: correct geometry");
t.geom_eq(nodes[3], points[2], "[no nodes] 3: correct geometry");
}
</script>
</head>
<body>
</body>
</html>
|