forked from yocontra/node-gdal-next
-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathapi_polygon.test.ts
More file actions
315 lines (299 loc) · 10.7 KB
/
Copy pathapi_polygon.test.ts
File metadata and controls
315 lines (299 loc) · 10.7 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { assert } from 'chai'
import * as path from 'path'
import * as gdal from 'gdal-async'
describe('gdal.Polygon', () => {
afterEach(() => void global.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 throw if ring doesn't exist", () => {
const polygon = new gdal.Polygon()
assert.throws(() => {
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()', () => {
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)
it('should add a ring', () => {
const polygon = new gdal.Polygon()
polygon.rings.add(ring1)
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 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)
})
it('should reject invalid geometries', () => {
const polygon = new gdal.Polygon()
polygon.rings.add(ring1)
const ring2 = new gdal.LineString()
assert.throws(() => {
// @ts-expect-error voluntary error
polygon.rings.add(ring2)
}, /must be a LinearRing/)
})
})
let polygon: gdal.Polygon
const createRings = () => {
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 ])
}
const original = [
[
'{ "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('forEach()', () => {
before(createRings)
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 = [] as string[][]
polygon.rings.forEach((ring) => {
result.push(ring.points.toArray().map((pt) => pt.toJSON()))
})
assert.deepEqual(result, original)
})
})
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('@@iterator()', () => {
before(createRings)
it('should iterate through all points', () => {
const result = []
for (const ring of polygon.rings) {
result.push(ring.points.toArray().map((pt) => pt.toJSON()))
}
assert.deepEqual(result, original)
})
})
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)
})
})
})
})
describe('gdal.MultiPolygon', () => {
afterEach(() => void global.gc!())
let multiPolygon: gdal.MultiPolygon
beforeEach(() => {
multiPolygon = gdal.open(path.resolve(__dirname, 'data', 'park.geo.json'))
.layers.get(0).features.get(0).getGeometry() as gdal.MultiPolygon
})
it('unionCascaded() should return a Geometry', () => {
const geom = gdal.Geometry.fromWKT(
'MULTIPOLYGON(((0 0,0 1,1 1,1 0,0 0)),((0.5 0.5,0.5 1.5,1.5 1.5,1.5 0.5,0.5 0.5)))') as gdal.MultiPolygon
const union = geom.unionCascaded()
assert.instanceOf(union, gdal.Geometry)
})
it('unionCascaded() should throw on Error', () => {
assert.throws(() => {
multiPolygon.unionCascaded()
})
})
it('getArea() should return a number', () => {
const area = multiPolygon.getArea()
assert.isNumber(area)
assert.closeTo(area, 2.04665, 1e-3)
})
describe('"children" property', () => {
it('should be an instance of GeometryCollectionChildren', () => {
assert.instanceOf(multiPolygon.children, gdal.GeometryCollectionChildren)
})
describe('get()', () => {
it('should return a Geometry', () => {
const geom = multiPolygon.children.get(0)
assert.instanceOf(geom, gdal.Geometry)
assert.instanceOf(geom, gdal.Polygon)
})
it('should throw if the geometry does not exist', () => {
assert.throws(() => {
multiPolygon.children.get(112)
})
})
})
it('count() should return a number', () => {
const n = multiPolygon.children.count()
assert.isNumber(n)
assert.equal(n, 1)
})
describe('add()', () => {
it('should add a new element', () => {
const geom = new gdal.Polygon()
const count = multiPolygon.children.count()
multiPolygon.children.add(geom)
assert.equal(multiPolygon.children.count(), count + 1)
assert.deepEqual(multiPolygon.children.get(count), geom)
})
it('should add an array of new elements', () => {
const geom = new gdal.Polygon()
const count = multiPolygon.children.count()
multiPolygon.children.add([ geom ])
assert.equal(multiPolygon.children.count(), count + 1)
assert.deepEqual(multiPolygon.children.get(count), geom)
})
it('should throw on no arguments', () => {
assert.throws(() => {
(multiPolygon.children.add as () => void)()
}, /must be given/)
})
it('should throw on invalid elements', () => {
assert.throws(() => {
multiPolygon.children.add({} as gdal.Geometry)
}, /child must be a geometry/)
})
it('should throw on invalid array elements', () => {
assert.throws(() => {
multiPolygon.children.add([ {} as gdal.Geometry ])
}, /All array elements must be geometry objects/)
})
})
describe('remove()', () => {
it('should remove an element', () => {
const count = multiPolygon.children.count()
multiPolygon.children.remove(count - 1)
assert.equal(multiPolygon.children.count(), count - 1)
})
it('should throw on error', () => {
assert.throws(() => {
multiPolygon.children.remove(-2)
})
})
})
})
})