summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Geometry/Point.html
blob: e688250b63c26be3863648700ffc27b5406099c9 (plain)
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
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var point; 
        
    function test_Point_constructor (t) {
        t.plan( 8 );

      //empty
        point = new OpenLayers.Geometry.Point();
        t.ok( point instanceof OpenLayers.Geometry.Point, "new OpenLayers.Geometry.Point returns point object" );
        t.eq( point.CLASS_NAME, "OpenLayers.Geometry.Point", "point.CLASS_NAME is set correctly");

      //valid
        var x = 10;
        var y = 20;
        point = new OpenLayers.Geometry.Point(x, y);
        t.ok( point instanceof OpenLayers.Geometry.Point, "new OpenLayers.Geometry.Point returns point object" );
        t.eq( point.CLASS_NAME, "OpenLayers.Geometry.Point", "point.CLASS_NAME is set correctly");
        t.eq( point.x, x, "point.x is set correctly");
        t.eq( point.y, y, "point.y is set correctly");
        t.eq( point.lon, null, "point.lon is not set");
        t.eq( point.lat, null, "point.lat is not set");
    }

    function test_Point_calculateBounds (t) {
        t.plan(4);

        var x = 10;
        var y = 20;
        point = new OpenLayers.Geometry.Point(x, y);
        point.calculateBounds();
        t.eq( point.bounds.left, x, "bounds.left is 10" );
        t.eq( point.bounds.right, x, "bounds.right is 10" );
        t.eq( point.bounds.top, y, "bounds.top is 20" );
        t.eq( point.bounds.bottom, y, "bounds.bottom is 20" );
    }
    

    function test_Point_transform_getBounds (t) {
        t.plan(2);

        var x = 10;
        var y = 20;
        point = new OpenLayers.Geometry.Point(x, y);
        point.calculateBounds();
        t.ok( point.bounds != null, "bounds calculated by calcBounds" );
        point.transform(new OpenLayers.Projection("EPSG:4326"), 
                        new OpenLayers.Projection("EPSG:900913"));
        t.eq(point.bounds, null, "Point bounds cleared after transform");
    }

    function test_Point_transform_string(t) {
        t.plan(4);

        var x = 10;
        var y = 20;
        point = new OpenLayers.Geometry.Point(x, y);
        point.calculateBounds();
        t.ok( point.bounds != null, "bounds calculated by calcBounds" );
        point.transform("EPSG:4326", "EPSG:900913");
        t.eq(point.bounds, null, "Point bounds cleared after transform");
        t.eq(point.x.toFixed(2), "1113194.91", "transformed x");
        t.eq(point.y.toFixed(2), "2273030.93", "transformed y");
        
    }
    
    function test_Point_distanceTo(t) {
        t.plan(7);

        var x1 = 10;
        var y1 = 20;
        point1 = new OpenLayers.Geometry.Point(x1, y1);

        var x2 = 100;
        var y2 = 200;
        point2 = new OpenLayers.Geometry.Point(x2, y2);

        var dist = point1.distanceTo(point2)
        t.eq( dist, 201.24611797498107267682563018581, "distances calculating correctly");
        t.eq( dist, Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)), "distance calculation correct");
        
        // test that details are returned (though trivial in this case)
        var result = point1.distanceTo(point2, {details: true});
        t.eq(result.distance, point1.distanceTo(point2), "[details] distance property is same as return without details");
        t.eq(result.x0, x1, "[details] x0 property is correct");
        t.eq(result.y0, y1, "[details] y0 property is correct");
        t.eq(result.x1, x2, "[details] x1 property is correct");
        t.eq(result.y1, y2, "[details] y1 property is correct");
        
    }
    
    function test_Point_toString(t) {
        t.plan(1);

        var x = 10;
        var y = 20;
        point = new OpenLayers.Geometry.Point(x, y);
        t.eq(point.toString(), "POINT(" + x + " " + y + ")",
             "toString() returns WKT" );
        
    }

    function test_Point_toString_no_wkt(t) {
        t.plan(1);
        
        var WKT = OpenLayers.Format.WKT;
        OpenLayers.Format.WKT = null;

        var x = 10;
        var y = 20;
        point = new OpenLayers.Geometry.Point(x, y);
        t.eq(point.toString(), "[object Object]", "default string representation");
        
        OpenLayers.Format.WKT = WKT;
        
    }
    
    function test_Point_move(t) {
        t.plan(3);
        
        var x = 10;
        var y = 20;
        point = new OpenLayers.Geometry.Point(x, y);
        
        var dx = 10 * Math.random();
        var dy = 10 * Math.random();
        point.bounds = "foo";
        point.move(dx, dy);
        t.eq(point.x, x + dx, "move() correctly modifies x");
        t.eq(point.y, y + dy, "move() correctly modifies y");
        
        t.ok(point.bounds == null, "bounds is cleared after a move()");
    }

    function test_Point_rotate(t) {
        t.plan(5);
        
        var tolerance = 1e-10;
        var x = 10;
        var y = 20;
        var point = new OpenLayers.Geometry.Point(x, y);
        var origin = new OpenLayers.Geometry.Point(5, 10);
        
        // rotate a full revolution
        point.bounds = "foo";
        point.rotate(360, origin);
        t.ok(((point.x - x) / x) < tolerance,
             "rotate by 360 returns to the same y");
        t.ok(((point.y - y) / y) < tolerance,
             "rotate by 360 returns to the same y");
        
        t.ok(point.bounds == null, "bounds is cleared after a rotate()");
        
        // rotate an 1/8 turn
        point.rotate(45, origin);
        t.ok(((point.x - 1.4644660940672636) / 1.4644660940672636) < tolerance,
             "rotate 1/8 turn correctly");
        t.ok(((point.y - 20.606601717798213) / 20.606601717798213) < tolerance,
             "rotate 1/8 turn correctly");
    }

    function test_Point_resize(t) {
        t.plan(6);
        
        var tolerance = 1e-10;
        var x = 100 * Math.random();
        var y = 100 * Math.random();
        var point = new OpenLayers.Geometry.Point(x, y);
        point.bounds = "foo";
        
        var i = 100 * Math.random();
        var j = 100 * Math.random();
        var origin = new OpenLayers.Geometry.Point(i, j);
        
        var scale = 10 * Math.random();
        var oldDistance = origin.distanceTo(point);
        
        var ret = point.resize(scale, origin);
        var newDistance = origin.distanceTo(point);
        
        t.ok(ret === point, "resize returns geometry");
        t.ok((origin.x == i) && (origin.y == j),
             "resize leaves the origin untouched");
        t.ok((((newDistance / oldDistance) - scale) / scale) < tolerance,
             "resize moves points the correct distance from the origin");
        
        t.ok(point.bounds == null, "bounds is correctly cleared after a resize()");
        
        // resize with non uniform scaling (ratio != 1)
        point = new OpenLayers.Geometry.Point(10, 10);
        origin = new OpenLayers.Geometry.Point(0, 0);
        point.resize(2, origin, 4);
        t.eq(point.x, 80, "non-uniform scaling correctly applied in x dim");
        t.eq(point.y, 20, "non-uniform scaling correctly applied in y dim");
        
    }

    function test_Point_equals(t) {
        t.plan(3);
        
        var x = Math.random() * 100;
        var y = Math.random() * 100;
        var geometry = new OpenLayers.Geometry.Point(x, y);
        var equal = new OpenLayers.Geometry.Point(x, y);
        var offX = new OpenLayers.Geometry.Point(x + 1, y);
        var offY = new OpenLayers.Geometry.Point(x, y + 1);
        t.ok(geometry.equals(equal),
             "equals() returns true for a geometry with equivalent coordinates");
        t.ok(!geometry.equals(offX),
             "equals() returns false for a geometry with offset x");
        t.ok(!geometry.equals(offY),
             "equals() returns false for a geometry with offset y");
    }
    
    function test_getVertices(t) {
        t.plan(3);
        
        var point = new OpenLayers.Geometry.Point(10, 20);
        var verts = point.getVertices();
        t.ok(verts instanceof Array, "got back an array");
        t.eq(verts.length, 1, "of length 1");
        t.geom_eq(verts[0], point, "with correct geometry");
    }
    
    function test_Point_clone(t) {
        t.plan(2);
        
        var x = Math.random() * 100;
        var y = Math.random() * 100;
        var geometry = new OpenLayers.Geometry.Point(x, y);
        var clone = geometry.clone();
        t.ok(clone instanceof OpenLayers.Geometry.Point,
             "clone() creates an OpenLayers.Geometry.Point");
        t.ok(geometry.equals(clone), "clone has equivalent coordinates");
    }
    

  </script>
</head>
<body>
</body>
</html>