-
Notifications
You must be signed in to change notification settings - Fork 124
/
api_polygon.test.js
196 lines (188 loc) · 6.52 KB
/
api_polygon.test.js
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
var assert = require('chai').assert;
var gdal = require('../lib/gdal.js');
describe('gdal.Polygon', function() {
afterEach(gc);
it('should be instantiable', function() {
new gdal.Polygon();
});
it('should inherit from Geometry', function() {
assert.instanceOf(new gdal.Polygon(), gdal.Polygon);
assert.instanceOf(new gdal.Polygon(), gdal.Geometry);
});
describe('instance', function() {
describe('"rings" property', function() {
describe('get()', function() {
it('should return null if ring doesn\'t exist', function() {
var polygon = new gdal.Polygon();
assert.isNull(polygon.rings.get(2));
});
it('should return LinearRing instance', function() {
var polygon = new gdal.Polygon();
var ring = new gdal.LinearRing();
ring.points.add(0, 0, 0);
ring.points.add(10, 0, 0);
ring.points.add(10, 10, 0);
ring.points.add(0, 10, 0);
ring.points.add(0, 0, 0);
polygon.rings.add(ring);
assert.instanceOf(polygon.rings.get(0), gdal.LinearRing);
});
});
describe('count()', function() {
it('should return ring count', function() {
var polygon = new gdal.Polygon();
assert.equal(polygon.rings.count(), 0);
var ring = new gdal.LinearRing();
ring.points.add(0, 0, 0);
ring.points.add(10, 0, 0);
ring.points.add(10, 10, 0);
ring.points.add(0, 10, 0);
ring.points.add(0, 0, 0);
polygon.rings.add(ring);
assert.equal(polygon.rings.count(), 1);
});
});
describe('add()', function() {
it('should add a ring', function() {
var polygon = new gdal.Polygon();
var ring = new gdal.LinearRing();
ring.points.add(0, 0, 0);
ring.points.add(10, 0, 0);
ring.points.add(10, 11, 0);
ring.points.add(0, 10, 0);
ring.points.add(0, 0, 0);
polygon.rings.add(ring);
var ring_result = polygon.rings.get(0);
assert.instanceOf(ring_result, gdal.LinearRing);
assert.equal(ring_result.points.get(2).y, 11);
});
it('should accept multiple LinearRing instances', function() {
var ring1 = new gdal.LinearRing();
ring1.points.add(0, 0, 0);
ring1.points.add(10, 0, 0);
ring1.points.add(10, 11, 0);
ring1.points.add(0, 10, 0);
ring1.points.add(0, 0, 0);
var ring2 = new gdal.LinearRing();
ring2.points.add(1, 0, 0);
ring2.points.add(11, 0, 0);
ring2.points.add(11, 11, 0);
ring2.points.add(1, 10, 0);
ring2.points.add(1, 0, 0);
var polygon = new gdal.Polygon();
polygon.rings.add([ring1, ring2]);
assert.equal(polygon.rings.count(), 2);
});
});
describe('forEach()', function() {
var polygon;
before(function() {
var ring1 = new gdal.LinearRing();
ring1.points.add(0, 0, 0);
ring1.points.add(10, 0, 0);
ring1.points.add(10, 11, 0);
ring1.points.add(0, 10, 0);
ring1.points.add(0, 0, 0);
var ring2 = new gdal.LinearRing();
ring2.points.add(1, 0, 0);
ring2.points.add(11, 0, 0);
ring2.points.add(11, 11, 0);
ring2.points.add(1, 10, 0);
ring2.points.add(1, 0, 0);
var ring3 = new gdal.LinearRing();
ring3.points.add(2, 0, 0);
ring3.points.add(20, 0, 0);
ring3.points.add(20, 11, 0);
ring3.points.add(3, 10, 0);
ring3.points.add(3, 0, 0);
polygon = new gdal.Polygon();
polygon.rings.add([ring1, ring2, ring3]);
});
it('should stop if callback returns false', function() {
var count = 0;
polygon.rings.forEach(function(pt, i) {
count++;
assert.isNumber(i);
if (i === 1) return false;
});
assert.equal(count, 2);
});
it('should iterate through all points', function() {
var result = [];
polygon.rings.forEach(function(ring) {
result.push(ring.points.toArray().map(function(pt) {
return pt.toJSON();
}));
});
assert.deepEqual(result, [
['{ "type": "Point", "coordinates": [ 0.0, 0.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 10.0, 0.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 10.0, 11.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 0.0, 10.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 0.0, 0.0, 0.0 ] }' ],
[ '{ "type": "Point", "coordinates": [ 1.0, 0.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 11.0, 0.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 11.0, 11.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 1.0, 10.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 1.0, 0.0, 0.0 ] }' ],
[ '{ "type": "Point", "coordinates": [ 2.0, 0.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 20.0, 0.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 20.0, 11.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 3.0, 10.0, 0.0 ] }',
'{ "type": "Point", "coordinates": [ 3.0, 0.0, 0.0 ] }' ]
]);
});
});
describe('map()', function() {
it('should operate normally', function() {
var polygon = new gdal.Polygon();
var ring = new gdal.LinearRing();
ring.points.add(0, 0, 0);
ring.points.add(10, 0, 0);
ring.points.add(10, 10, 0);
ring.points.add(0, 11, 0);
ring.points.add(0, 0, 0);
polygon.rings.add(ring);
var result = polygon.rings.map(function(ring, i) {
assert.isNumber(i);
assert.instanceOf(ring, gdal.LinearRing);
return 'a';
});
assert.isArray(result);
assert.lengthOf(result, 1);
assert.equal(result[0], 'a');
});
});
describe('toArray()', function() {
it('should return array of LinearRing instances', function() {
var polygon = new gdal.Polygon();
var ring = new gdal.LinearRing();
ring.points.add(0, 0, 0);
ring.points.add(10, 0, 0);
ring.points.add(10, 10, 0);
ring.points.add(0, 11, 0);
ring.points.add(0, 0, 0);
polygon.rings.add(ring);
var array = polygon.rings.toArray();
assert.isArray(array);
assert.lengthOf(array, 1);
assert.instanceOf(array[0], gdal.LinearRing);
assert.equal(array[0].points.get(3).y, 11);
});
});
});
describe('getArea()', function() {
it('should return area', function() {
var polygon = new gdal.Polygon();
var ring = new gdal.LinearRing();
ring.points.add(0, 0, 0);
ring.points.add(10, 0, 0);
ring.points.add(10, 10, 0);
ring.points.add(0, 10, 0);
ring.points.add(0, 0, 0);
polygon.rings.add(ring);
assert.closeTo(ring.getArea(), 100, 0.001);
});
});
});
});