3 <script src="../lib/OpenLayers.js"></script>
4 <script src="data/geos_wkt_intersects.js"></script>
5 <script type="text/javascript">
8 function test_Geometry_constructor (t) {
11 var g = new OpenLayers.Geometry();
13 t.eq(g.CLASS_NAME, "OpenLayers.Geometry", "correct CLASS_NAME")
14 t.ok(OpenLayers.String.startsWith(g.id, "OpenLayers.Geometry_"),
19 function test_Geometry_clone(t) {
21 var geometry = new OpenLayers.Geometry();
22 var clone = geometry.clone();
24 t.eq(clone.CLASS_NAME, "OpenLayers.Geometry", "correct CLASS_NAME")
25 t.ok(OpenLayers.String.startsWith(clone.id, "OpenLayers.Geometry_"),
29 function test_Geometry_setBounds(t) {
32 var g = new OpenLayers.Geometry();
36 t.ok(g.bounds == null, "setbounds with null value does not crash or set bounds");
41 'clone': function() { return g_clone; }
44 t.ok(g.bounds == g_clone, "setbounds with valid object sets bounds, calls clone");
47 function test_Geometry_extendBounds(t) {
50 OpenLayers.Bounds.prototype._extend =
51 OpenLayers.Bounds.prototype.extend;
52 OpenLayers.Bounds.prototype.extend = function(b) {
56 var g = new OpenLayers.Geometry();
58 //this.bounds null (calculateBounds(), setBounds() called)
59 g.setBounds = function(b) { g_setBounds = b; };
60 g.calculateBounds = function() { g_calculateBounds = {}; };
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");
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;
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");
89 //this.bounds non-null thus extend()
90 // - setBounds() not called
91 // - this.bounds.extend() called
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");
100 OpenLayers.Bounds.prototype.extend =
101 OpenLayers.Bounds.prototype._extend;
106 function test_Geometry_getBounds(t) {
109 var g = new OpenLayers.Geometry();
111 var testBounds = new OpenLayers.Bounds(1,2,3,4);
112 g.bounds = testBounds.clone();
114 t.ok(g.getBounds().equals(testBounds), "getBounds works");
117 function test_Geometry_atPoint(t) {
120 var g = new OpenLayers.Geometry();
127 g.bounds = new OpenLayers.Bounds();
129 var atPoint = g.atPoint(lonlat, lon, lat);
130 t.ok(!atPoint, "null lonlat")
134 lonlat = new OpenLayers.LonLat(1,2);
136 atPoint = g.atPoint(lonlat, lon, lat);
137 t.ok(!atPoint, "null this.bounds")
139 //toleranceLon/toleranceLat
141 //default toleranceLon/toleranceLat
142 OpenLayers.Bounds.prototype._containsLonLat = OpenLayers.Bounds.prototype.containsLonLat;
144 OpenLayers.Bounds.prototype.containsLonLat = function(ll) {
149 var testBounds = new OpenLayers.Bounds(10,20,30,40);
150 g.bounds = testBounds.clone();
151 lonlat = new OpenLayers.LonLat(20,30);
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");
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);
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");
172 OpenLayers.Bounds.prototype.containsLonLat = OpenLayers.Bounds.prototype._containsLonLat;
176 function test_Geometry_getLength(t) {
179 var g = new OpenLayers.Geometry();
181 t.eq(g.getLength(), 0, "getLength is 0");
184 function test_Geometry_getArea(t) {
187 var g = new OpenLayers.Geometry();
189 t.eq(g.getArea(), 0, "getArea is 0");
192 function test_Geometry_clearBounds(t) {
195 var g = new OpenLayers.Geometry();
196 g.parent = new OpenLayers.Geometry();
199 g.parent.bounds = "bar";
202 t.ok(g.bounds == null, "bounds is correctly cleared");
203 t.ok(g.parent.bounds == null, "parent geometry bounds is correctly cleared");
206 function test_Geometry_destroy(t) {
209 var g = new OpenLayers.Geometry();
210 g.bounds = new OpenLayers.Bounds();
212 g_style_destroy = null;
215 t.eq(g.id, null, "id nullified");
217 t.eq(g.bounds, null, "bounds nullified");
221 function test_Geometry_intersects_geos_wkt(t) {
222 var wkt = new OpenLayers.Format.WKT();
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'] + "'";
237 if(intersect21 != testcase.result) {
238 msg = "f2 should " + (testcase.result ? "" : "not ") +
239 "intersect f1: f1 = '" + testcase['wkt1'] + "' " +
240 "f2 = '" + testcase['wkt2'] + "'";
244 if(failures.length == 0) {
246 t.ok(true, "all " + geos_test_data.length + " geos tests pass");
248 t.plan(failures.length);
249 for(var f=0; f<failures.length; ++f) {
255 function test_distanceToSegment(t) {
256 var dist = OpenLayers.Geometry.distanceToSegment;
259 got: dist({x: 0, y: 0}, {x1: 0, y1: 1, x2: 1, y2: 1}),
260 expected: {distance: 1, x: 0, y: 1}
262 got: dist({x: 0, y: 0}, {x1: -1, y1: -1, x2: 0, y2: -1}),
263 expected: {distance: 1, x: 0, y: -1}
265 got: dist({x: 0, y: 0}, {x1: -1, y1: -1, x2: 1, y2: 1}),
266 expected: {distance: 0, x: 0, y: 0}
268 got: dist({x: 1, y: 1}, {x1: 2, y1: 0, x2: 2, y2: 3}),
269 expected: {distance: 1, x: 2, y: 1}
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}
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}
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);
285 function test_fromWKT(t) {
289 geom: new OpenLayers.Geometry.Point(1, 2)
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)
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)
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)
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)
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)
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);
342 <div id="map" style="width: 1024px; height: 512px;"/>