]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Geometry/MultiLineString.html
initial commit
[syp.git] / openlayers / tests / Geometry / MultiLineString.html
1 <html>
2 <head>
3   <script src="../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5     var line;
6         
7     function test_MultiLineString_constructor (t) {
8         t.plan( 3 );
9         mline = new OpenLayers.Geometry.MultiLineString();
10         t.ok( mline instanceof OpenLayers.Geometry.MultiLineString, "new OpenLayers.Geometry.MultiLineString returns mline object" );
11         t.eq( mline.CLASS_NAME, "OpenLayers.Geometry.MultiLineString", "mline.CLASS_NAME is set correctly");
12         t.eq( mline.components, [], "line.components is set correctly");
13     }
14
15     function test_MultiLineString_constructor (t) {
16         t.plan( 3 );
17         line = new OpenLayers.Geometry.LineString();
18         mline = new OpenLayers.Geometry.MultiLineString(line);
19         t.ok( mline instanceof OpenLayers.Geometry.MultiLineString, "new OpenLayers.Geometry.MultiLineString returns mline object" );
20         t.eq( mline.CLASS_NAME, "OpenLayers.Geometry.MultiLineString", "mline.CLASS_NAME is set correctly");
21         t.eq( mline.components.length, 1, "mline.components.length is set correctly");
22     }
23
24     function test_split(t) {
25         var wkt = OpenLayers.Geometry.fromWKT;
26         
27         var cases = [{
28             msg: "no intersection",
29             g1: "MULTILINESTRING((0 0, 0 1), (2 2, 3 3))",
30             g2: "MULTILINESTRING((1 0, 1 1), (2 2, 3 2))",
31             exp: null
32         } , {
33             msg: "intersection at midpoint",
34             g1: "MULTILINESTRING((0 0, 1 1))",
35             g2: "MULTILINESTRING((1 0, 0 1))",
36             exp: ["MULTILINESTRING((1 0, 0.5 0.5))", "MULTILINESTRING((0.5 0.5, 0 1))"]
37         }, {
38             msg: "intersection at midpoint (reverse source/target)",
39             g1: "MULTILINESTRING((1 0, 0 1))",
40             g2: "MULTILINESTRING((0 0, 1 1))",
41             exp: ["MULTILINESTRING((0 0, 0.5 0.5))", "MULTILINESTRING((0.5 0.5, 1 1))"]
42         }, {
43             msg: "intersection at endpoint",
44             g1: "MULTILINESTRING((0 0, 1 1))",
45             g2: "MULTILINESTRING((1 0, 1 1))",
46             exp: null
47         }, {
48             msg: "midpoint intersection, no options",
49             g1: "MULTILINESTRING((0 0, 2 2))",
50             g2: "MULTILINESTRING((0 2, 2 0))",
51             exp: ["MULTILINESTRING((0 2, 1 1))", "MULTILINESTRING((1 1, 2 0))"]
52         }, {
53             msg: "midpoint intersection, edge false",
54             opt: {edge: false},
55             g1: "MULTILINESTRING((0 0, 2 2))",
56             g2: "MULTILINESTRING((0 2, 2 0))",
57             exp: null
58         }, {
59             msg: "midpoint intersection, mutual",
60             opt: {mutual: true},
61             g1: "MULTILINESTRING((0 0, 2 2))",
62             g2: "MULTILINESTRING((0 2, 2 0))",
63             exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2))"], ["MULTILINESTRING((0 2, 1 1))", "MULTILINESTRING((1 1, 2 0))"]]
64         }, {
65             msg: "close intersection, no tolerance",
66             g1: "MULTILINESTRING((0 0, 0.9 0.9))",
67             g2: "MULTILINESTRING((0 2, 2 0))",
68             exp: null
69         }, {
70             msg: "close intersection, within tolerance",
71             opt: {tolerance: 0.2},
72             g1: "MULTILINESTRING((0 0, 0.9 0.9))",
73             g2: "MULTILINESTRING((0 2, 2 0))",
74             exp: ["MULTILINESTRING((0 2, 0.9 0.9))", "MULTILINESTRING((0.9 0.9, 2 0))"]
75         }, {
76             msg: "multi source, single target",
77             g1: "MULTILINESTRING((0 0, 2 2))",
78             g2: "LINESTRING(0 2, 2 0)",
79             exp: ["LINESTRING(0 2, 1 1)", "LINESTRING(1 1, 2 0)"]
80         }, {
81             msg: "multi source, single target, mutual split",
82             opt: {mutual: true},
83             g1: "MULTILINESTRING((0 0, 2 2))",
84             g2: "LINESTRING(0 2, 2 0)",
85             exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2))"], ["LINESTRING(0 2, 1 1)", "LINESTRING(1 1, 2 0)"]]
86         }, {
87             msg: "single source, multi target",
88             g1: "LINESTRING(0 2, 2 0)",
89             g2: "MULTILINESTRING((0 0, 2 2))",
90             exp: ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2))"]
91         }, {
92             msg: "partial target split",
93             g1: "MULTILINESTRING((2 0, 0 2))",
94             g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
95             exp: ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]
96         }, {
97             msg: "partial target split, mutual true",
98             opt: {mutual: true},
99             g1: "MULTILINESTRING((2 0, 0 2))",
100             g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
101             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))"]]
102         }, {
103             msg: "partial source split, mutual true",
104             opt: {mutual: true},
105             g1: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
106             g2: "MULTILINESTRING((2 0, 0 2))",
107             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))"]]
108         }, {
109             msg: "partial target split with source endpoint",
110             g1: "MULTILINESTRING((1 0, 1 1))",
111             g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
112             exp: ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]
113         }, {
114             msg: "partial target split with source endpoint, mutual true",
115             opt: {mutual: true},
116             g1: "MULTILINESTRING((5 5, 6 6), (1 0, 1 1))",
117             g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
118             exp: [[], ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]]
119         }, {
120             msg: "partial source split with target endpoint",
121             g1: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
122             g2: "MULTILINESTRING((1 0, 1 1))",
123             exp: null
124         }, {
125             msg: "partial source split with target endpoint, mutual true",
126             opt: {mutual: true},
127             g1: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4), (5 5, 6 6))",
128             g2: "MULTILINESTRING((1 0, 1 1))",
129             exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4), (5 5, 6 6))"], []]
130         }, {
131             msg: "partial target and source split",
132             g1: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
133             g2: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
134             exp: ["MULTILINESTRING((5 0, 5 2), (5 4, 5 5))", "MULTILINESTRING((5 5, 5 6), (5 8, 5 10))"]
135         }, {
136             msg: "partial target and source split, mutual true",
137             opt: {mutual: true},
138             g1: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
139             g2: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
140             exp: [["MULTILINESTRING((0 5, 2 5), (4 5, 5 5))", "MULTILINESTRING((5 5, 6 5), (8 5, 10 5))"],
141                   ["MULTILINESTRING((5 0, 5 2), (5 4, 5 5))", "MULTILINESTRING((5 5, 5 6), (5 8, 5 10))"]]
142         }, {
143             msg: "partial target and source split with source endpoint, mutual true",
144             opt: {mutual: true},
145             g1: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
146             g2: "MULTILINESTRING((4 0, 4 2), (4 4, 4 6), (4 8, 4 10))",
147             exp: [[], ["MULTILINESTRING((4 0, 4 2), (4 4, 4 5))", "MULTILINESTRING((4 5, 4 6), (4 8, 4 10))"]]
148         }, {
149             msg: "partial target and source split with target endpoint, mutual true",
150             opt: {mutual: true},
151             g1: "MULTILINESTRING((4 0, 4 2), (4 4, 4 6), (4 8, 4 10))",
152             g2: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
153             exp: [["MULTILINESTRING((4 0, 4 2), (4 4, 4 5))", "MULTILINESTRING((4 5, 4 6), (4 8, 4 10))"], []]
154         }, {
155             msg: "partial target and source split with source vertex, mutual true",
156             opt: {mutual: true},
157             g1: "MULTILINESTRING((0 5, 2 5), (4 5, 5 5, 6 5), (8 5, 10 5))",
158             g2: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
159             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))"]]
160         }, {
161             msg: "partial target and source split with target vertex, mutual true",
162             opt: {mutual: true},
163             g1: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
164             g2: "MULTILINESTRING((0 5, 2 5), (4 5, 5 5, 6 5), (8 5, 10 5))",
165             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))"]]
166         }];
167
168
169         t.plan(cases.length);
170         var c, parts, part, midparts;
171         for(var i=0; i<cases.length; ++i) {
172             c = cases[i];
173             var g1 = wkt(c.g1);
174             var g2 = wkt(c.g2);
175             var got = g1.split(g2, c.opt);
176             var exp = c.exp;
177             if(got instanceof Array) {
178                 parts = [];
179                 for(var j=0; j<got.length; ++j) {
180                     part = got[j];
181                     if(part instanceof Array) {
182                         midparts = [];
183                         for(var k=0; k<part.length; ++k) {
184                             midparts.push(part[k].toString());
185                         }
186                         parts.push("[" + midparts.join(", ") + "]");
187                     } else {
188                         parts.push(got[j].toString());
189                     }
190                 }
191                 got = parts.join(", ");
192             }
193             if(exp instanceof Array) {
194                 parts = [];
195                 for(var j=0; j<exp.length; ++j) {
196                     part = exp[j];
197                     if(part instanceof Array) {
198                         midparts = [];
199                         for(var k=0; k<part.length; ++k) {
200                             midparts.push(wkt(part[k]).toString());
201                         }
202                         parts.push("[" + midparts.join(", ") + "]");
203                     } else {
204                         parts.push(wkt(exp[j]).toString());
205                     }
206                 }
207                 exp = parts.join(", ");
208             }
209             t.eq(got, exp, "case " + i + ": " +  c.msg);
210         }
211         
212     }
213
214     function test_getVertices(t) {
215         t.plan(22);
216         
217         var points = [
218             new OpenLayers.Geometry.Point(10, 20),
219             new OpenLayers.Geometry.Point(20, 30),
220             new OpenLayers.Geometry.Point(30, 40),
221             new OpenLayers.Geometry.Point(40, 50)
222         ];
223         
224         var multi = new OpenLayers.Geometry.MultiLineString([
225             new OpenLayers.Geometry.LineString(points),
226             new OpenLayers.Geometry.LineString(points)
227         ]);
228         
229         var verts = multi.getVertices();
230         t.ok(verts instanceof Array, "got back an array");
231         t.eq(verts.length, 2 * points.length, "of correct length length");
232         t.geom_eq(verts[0], points[0], "0: correct geometry");
233         t.geom_eq(verts[1], points[1], "1: correct geometry");
234         t.geom_eq(verts[2], points[2], "2: correct geometry");
235         t.geom_eq(verts[3], points[3], "3: correct geometry");
236         t.geom_eq(verts[4], points[0], "4: correct geometry");
237         t.geom_eq(verts[5], points[1], "5: correct geometry");
238         t.geom_eq(verts[6], points[2], "6: correct geometry");
239         t.geom_eq(verts[7], points[3], "7: correct geometry");
240         
241         // nodes only
242         var nodes = multi.getVertices(true);
243         t.ok(nodes instanceof Array, "[nodes only] got back an array");
244         t.eq(nodes.length, 4, "[nodes only] of correct length length");
245         t.geom_eq(nodes[0], points[0], "[nodes only] 0: correct geometry");
246         t.geom_eq(nodes[1], points[3], "[nodes only] 1: correct geometry");
247         t.geom_eq(nodes[2], points[0], "[nodes only] 2: correct geometry");
248         t.geom_eq(nodes[3], points[3], "[nodes only] 3: correct geometry");
249         
250         // no nodes
251         var nodes = multi.getVertices(false);
252         t.ok(nodes instanceof Array, "[no nodes] got back an array");
253         t.eq(nodes.length, 4, "[no nodes] of correct length length");
254         t.geom_eq(nodes[0], points[1], "[no nodes] 0: correct geometry");
255         t.geom_eq(nodes[1], points[2], "[no nodes] 1: correct geometry");
256         t.geom_eq(nodes[2], points[1], "[no nodes] 2: correct geometry");
257         t.geom_eq(nodes[3], points[2], "[no nodes] 3: correct geometry");
258
259
260     }
261
262
263   </script>
264 </head>
265 <body>
266 </body>
267 </html>