-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathapi_polygon.test.js
200 lines (192 loc) · 7.02 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
197
198
199
200
const assert = require('chai').assert
const gdal = require('../lib/gdal.js')
describe('gdal.Polygon', () => {
afterEach(gc)
it('should be instantiable', () => {
new gdal.Polygon()
})
it('should inherit from Geometry', () => {
assert.instanceOf(new gdal.Polygon(), gdal.Polygon)
assert.instanceOf(new gdal.Polygon(), gdal.Geometry)
})
describe('instance', () => {
describe('"rings" property', () => {
describe('get()', () => {
it("should return null if ring doesn't exist", () => {
const polygon = new gdal.Polygon()
assert.isNull(polygon.rings.get(2))
})
it('should return LinearRing instance', () => {
const polygon = new gdal.Polygon()
const 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()', () => {
it('should return ring count', () => {
const polygon = new gdal.Polygon()
assert.equal(polygon.rings.count(), 0)
const 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()', () => {
it('should add a ring', () => {
const polygon = new gdal.Polygon()
const 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)
const 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', () => {
const 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)
const 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)
const polygon = new gdal.Polygon()
polygon.rings.add([ ring1, ring2 ])
assert.equal(polygon.rings.count(), 2)
})
})
describe('forEach()', () => {
let polygon
before(() => {
const 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)
const 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)
const 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', () => {
let count = 0
polygon.rings.forEach((pt, i) => {
count++
assert.isNumber(i)
if (i === 1) return false
})
assert.equal(count, 2)
})
it('should iterate through all points', () => {
const result = []
polygon.rings.forEach((ring) => {
result.push(ring.points.toArray().map((pt) => 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()', () => {
it('should operate normally', () => {
const polygon = new gdal.Polygon()
const 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)
const result = polygon.rings.map((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()', () => {
it('should return array of LinearRing instances', () => {
const polygon = new gdal.Polygon()
const 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)
const 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()', () => {
it('should return area', () => {
const polygon = new gdal.Polygon()
const 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)
})
})
})
})