]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Geometry.html
initial commit
[syp.git] / openlayers / tests / Geometry.html
1 <html>
2 <head>
3   <script src="../lib/OpenLayers.js"></script>
4   <script src="data/geos_wkt_intersects.js"></script>
5   <script type="text/javascript">
6     var map; 
7
8     function test_Geometry_constructor (t) {
9         t.plan( 2 );
10         
11         var g = new OpenLayers.Geometry();
12         
13         t.eq(g.CLASS_NAME, "OpenLayers.Geometry", "correct CLASS_NAME")
14         t.ok(OpenLayers.String.startsWith(g.id, "OpenLayers.Geometry_"),
15              "id correctly set");
16     }
17
18
19     function test_Geometry_clone(t) {
20         t.plan(2);
21         var geometry = new OpenLayers.Geometry();
22         var clone = geometry.clone();
23
24         t.eq(clone.CLASS_NAME, "OpenLayers.Geometry", "correct CLASS_NAME")
25         t.ok(OpenLayers.String.startsWith(clone.id, "OpenLayers.Geometry_"),
26              "id correctly set");
27     }
28
29     function test_Geometry_setBounds(t) {
30         t.plan( 2 );
31         
32         var g = new OpenLayers.Geometry();
33         
34       //null object
35         g.setBounds(null);
36         t.ok(g.bounds == null, "setbounds with null value does not crash or set bounds");
37       
38       //no classname object
39         g_clone = {};
40         var object = {
41             'clone': function() { return g_clone; }
42         };
43         g.setBounds(object);
44         t.ok(g.bounds == g_clone, "setbounds with valid object sets bounds, calls clone");
45     }
46
47     function test_Geometry_extendBounds(t) {
48         t.plan(9);
49         
50         OpenLayers.Bounds.prototype._extend = 
51             OpenLayers.Bounds.prototype.extend;
52         OpenLayers.Bounds.prototype.extend = function(b) { 
53             g_extendBounds = b; 
54         };
55         
56         var g = new OpenLayers.Geometry();
57
58       //this.bounds null (calculateBounds(), setBounds() called)
59         g.setBounds = function(b) { g_setBounds = b; };
60         g.calculateBounds = function() { g_calculateBounds = {}; };
61         var object = {};
62         g_setBounds = null;
63         g_calculateBounds = null;
64         g_extendBounds = null;
65         g.extendBounds(object);
66         t.ok(g_calculateBounds != null, "calculateBounds() called when this.bounds is null");
67         t.ok(g_setBounds == object, "setBounds() called when this.bounds is null and calculateBounds() is null too");        
68         t.ok(g_extendBounds != object, "this.bounds.extend() not called when this.bounds is null and calculateBounds() is null too");
69         
70       //this.bounds null (calculateBounds() sets this.bounds:
71       //   -  setBounds() not called
72       //   -  this.bounds.extend() called
73         g_calcBounds = new OpenLayers.Bounds(1,2,3,4);
74         g.calculateBounds = function() { 
75             g_calculateBounds = {}; 
76             this.bounds = g_calcBounds; 
77         };
78         var object = {};
79
80         g_setBounds = null;
81         g_calculateBounds = null;
82         g_extendBounds = null;
83         g.extendBounds(object);
84         t.ok(g_calculateBounds != null, "calculateBounds() called when this.bounds is null");
85         t.ok(g_setBounds == null, "setBounds() not called when this.bounds is null and calculateBounds() sets this.bounds");        
86         t.ok(g_extendBounds == object, "this.bounds.extend() called when this.bounds is null and calculateBounds() sets this.bounds");        
87
88         
89       //this.bounds non-null thus extend()
90       //   -  setBounds() not called
91       //   -  this.bounds.extend() called
92         g_setBounds = null;
93         g_calculateBounds = null;
94         g_extendBounds = null;
95         g.extendBounds(object);
96         t.ok(g_calculateBounds == null, "calculateBounds() not called when this.bounds is non null");
97         t.ok(g_setBounds == null, "setBounds() not called when this.bounds is nonnull");        
98         t.ok(g_extendBounds == object, "this.bounds.extend() called when this.bounds is non-null");        
99
100         OpenLayers.Bounds.prototype.extend = 
101             OpenLayers.Bounds.prototype._extend;
102
103
104     }
105
106     function test_Geometry_getBounds(t) {
107         t.plan(1);
108         
109         var g = new OpenLayers.Geometry();
110         
111         var testBounds = new OpenLayers.Bounds(1,2,3,4);
112         g.bounds = testBounds.clone();
113         
114         t.ok(g.getBounds().equals(testBounds), "getBounds works");
115     }
116     
117     function test_Geometry_atPoint(t) {
118         t.plan(6);
119             
120         var g = new OpenLayers.Geometry();
121     
122         var lonlat = null;
123         var lon = 5;
124         var lat = 10;
125     
126       //null lonlat
127         g.bounds = new OpenLayers.Bounds();
128
129         var atPoint = g.atPoint(lonlat, lon, lat);
130         t.ok(!atPoint, "null lonlat")
131
132       //null this.bounds
133         g.bounds = null;
134         lonlat = new OpenLayers.LonLat(1,2);      
135       
136         atPoint = g.atPoint(lonlat, lon, lat);
137         t.ok(!atPoint, "null this.bounds")
138
139     //toleranceLon/toleranceLat
140
141       //default toleranceLon/toleranceLat
142         OpenLayers.Bounds.prototype._containsLonLat = OpenLayers.Bounds.prototype.containsLonLat;
143         g_Return = {};
144         OpenLayers.Bounds.prototype.containsLonLat = function(ll) {
145             g_bounds = this;
146             return g_Return;
147          }
148
149         var testBounds = new OpenLayers.Bounds(10,20,30,40);
150         g.bounds = testBounds.clone();
151         lonlat = new OpenLayers.LonLat(20,30);
152       
153         g_bounds = null;
154         atPoint = g.atPoint(lonlat);
155         t.ok(g_bounds.equals(testBounds), "default toleranceLon/Lat are 0");
156         t.ok(atPoint == g_Return, "default toleranceLon/Lat returns correctly");
157
158       //real toleranceLon/toleranceLat
159         var testBounds = new OpenLayers.Bounds(10,20,30,40);
160         g.bounds = testBounds.clone();
161         lonlat = new OpenLayers.LonLat(20,30);
162       
163         g_bounds = null;
164         atPoint = g.atPoint(lonlat, lon, lat);
165         testBounds.left -= lon;
166         testBounds.bottom -= lat;
167         testBounds.right += lon;
168         testBounds.top += lat;
169         t.ok(g_bounds.equals(testBounds), "real toleranceLon/Lat are 0");
170         t.ok(atPoint == g_Return, "real toleranceLon/Lat returns correctly");
171
172         OpenLayers.Bounds.prototype.containsLonLat = OpenLayers.Bounds.prototype._containsLonLat;
173     
174     }
175     
176     function test_Geometry_getLength(t) {
177         t.plan(1);
178         
179         var g = new OpenLayers.Geometry();
180         
181         t.eq(g.getLength(), 0, "getLength is 0");
182     }    
183
184     function test_Geometry_getArea(t) {
185         t.plan(1);
186         
187         var g = new OpenLayers.Geometry();
188         
189         t.eq(g.getArea(), 0, "getArea is 0");
190     }
191     
192     function test_Geometry_clearBounds(t) {
193         t.plan(2);
194         
195         var g = new OpenLayers.Geometry();
196         g.parent = new OpenLayers.Geometry();
197
198         g.bounds = "foo";
199         g.parent.bounds = "bar";
200         
201         g.clearBounds();
202         t.ok(g.bounds == null, "bounds is correctly cleared");
203         t.ok(g.parent.bounds == null, "parent geometry bounds is correctly cleared");
204     }
205
206     function test_Geometry_destroy(t) {
207         t.plan( 2 );
208
209         var g = new OpenLayers.Geometry();
210         g.bounds = new OpenLayers.Bounds();
211
212         g_style_destroy = null;
213         g.destroy();
214         
215         t.eq(g.id, null, "id nullified");
216
217         t.eq(g.bounds, null, "bounds nullified");
218
219     }
220
221     function test_Geometry_intersects_geos_wkt(t) {
222         var wkt = new OpenLayers.Format.WKT();
223         var failures = [];
224         var intersect12, intersect21, msg;
225         for (var i = 0; i < geos_test_data.length; i++) {
226             var testcase = geos_test_data[i];
227             f1 = wkt.read(testcase['wkt1']);
228             f2 = wkt.read(testcase['wkt2']);
229             intersect12 = f1.geometry.intersects(f2.geometry);
230             intersect21 = f2.geometry.intersects(f1.geometry);
231             if(intersect12 != testcase.result) {
232                 msg = "f1 should " + (testcase.result ? "" : "not ") +
233                       "intersect f2: f1 = '" + testcase['wkt1'] + "' " +
234                       "f2 = '" + testcase['wkt2'] + "'";
235                 failures.push(msg);
236             }
237             if(intersect21 != testcase.result) {
238                 msg = "f2 should " + (testcase.result ? "" : "not ") +
239                       "intersect f1: f1 = '" + testcase['wkt1'] + "' " +
240                       "f2 = '" + testcase['wkt2'] + "'";
241                 failures.push(msg);
242             }
243         }
244         if(failures.length == 0) {
245             t.plan(1);
246             t.ok(true, "all " + geos_test_data.length + " geos tests pass");
247         } else {
248             t.plan(failures.length);
249             for(var f=0; f<failures.length; ++f) {
250                 t.fail(failures[f]);
251             }
252         }
253     }
254     
255     function test_distanceToSegment(t) {
256         var dist = OpenLayers.Geometry.distanceToSegment;
257
258         var cases = [{
259             got: dist({x: 0, y: 0}, {x1: 0, y1: 1, x2: 1, y2: 1}),
260             expected: {distance: 1, x: 0, y: 1}
261         }, {
262             got: dist({x: 0, y: 0}, {x1: -1, y1: -1, x2: 0, y2: -1}),
263             expected: {distance: 1, x: 0, y: -1}
264         }, {
265             got: dist({x: 0, y: 0}, {x1: -1, y1: -1, x2: 1, y2: 1}),
266             expected: {distance: 0, x: 0, y: 0}
267         }, {
268             got: dist({x: 1, y: 1}, {x1: 2, y1: 0, x2: 2, y2: 3}),
269             expected: {distance: 1, x: 2, y: 1}
270         }, {
271             got: dist({x: -1, y: -1}, {x1: -2, y1: -2, x2: -1, y2: -3}),
272             expected: {distance: Math.sqrt(2), x: -2, y: -2}
273         }, {
274             got: dist({x: -1, y: 1}, {x1: -3, y1: 1, x2: -1, y2: 3}),
275             expected: {distance: Math.sqrt(2), x: -2, y: 2}
276         }];
277         
278         t.plan(cases.length);
279         for(var i=0; i<cases.length; ++i) {
280             t.eq(cases[i].got, cases[i].expected, "case " + i);
281         }
282         
283     }
284
285     function test_fromWKT(t) {
286         
287         var cases = [{
288             wkt: "POINT(1 2)",
289             geom: new OpenLayers.Geometry.Point(1, 2)
290         }, {
291             wkt: "MULTIPOINT(3.5 5.6,4.8 10.5)",
292             geom: new OpenLayers.Geometry.MultiPoint([
293                 new OpenLayers.Geometry.Point(3.5, 5.6),
294                 new OpenLayers.Geometry.Point(4.8, 10.5)
295             ])
296         }, {
297             wkt: "LINESTRING(1 2, 3 4)",
298             geom: new OpenLayers.Geometry.LineString([
299                 new OpenLayers.Geometry.Point(1, 2),
300                 new OpenLayers.Geometry.Point(3, 4)
301             ])
302         }, {
303             wkt: "POLYGON((0 0, 0 4, 4 4, 4 0, 0 0),(1 1, 1 3, 3 3, 3 1, 1 1))",
304             geom: new OpenLayers.Geometry.Polygon([
305                 new OpenLayers.Geometry.LinearRing([
306                     new OpenLayers.Geometry.Point(0, 0),
307                     new OpenLayers.Geometry.Point(0, 4),
308                     new OpenLayers.Geometry.Point(4, 4),
309                     new OpenLayers.Geometry.Point(4, 0),
310                     new OpenLayers.Geometry.Point(0, 0)
311                 ]),
312                 new OpenLayers.Geometry.LinearRing([
313                     new OpenLayers.Geometry.Point(1, 1),
314                     new OpenLayers.Geometry.Point(1, 3),
315                     new OpenLayers.Geometry.Point(3, 3),
316                     new OpenLayers.Geometry.Point(3, 1),
317                     new OpenLayers.Geometry.Point(1, 1)
318                 ])
319             ])
320         }, {
321             wkt: "GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))",
322             geom: new OpenLayers.Geometry.Collection([
323                 new OpenLayers.Geometry.Point(4, 6),
324                 new OpenLayers.Geometry.LineString([
325                     new OpenLayers.Geometry.Point(4, 6),
326                     new OpenLayers.Geometry.Point(7, 10)
327                 ])
328             ])
329         }];
330         
331         t.plan(cases.length);
332         var wkt = OpenLayers.Geometry.fromWKT;
333         for(var i=0; i<cases.length; ++i) {
334             t.geom_eq(wkt(cases[i].wkt), cases[i].geom, "case " + i);
335         }
336     }
337
338
339   </script>
340 </head>
341 <body>
342     <div id="map" style="width: 1024px; height: 512px;"/>
343 </body>
344 </html>