forked from yocontra/node-gdal-next
-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
api_linestring.test.ts
296 lines (293 loc) · 10.4 KB
/
api_linestring.test.ts
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
import * as gdal from 'gdal-async'
import { assert } from 'chai'
describe('gdal.LineString', () => {
afterEach(() => void global.gc!())
it('should be instantiable', () => {
new gdal.LineString()
})
it('should inherit from Geometry', () => {
assert.instanceOf(new gdal.LineString(), gdal.LineString)
assert.instanceOf(new gdal.LineString(), gdal.SimpleCurve)
assert.instanceOf(new gdal.LineString(), gdal.Geometry)
})
describe('instance', () => {
describe('getLength()', () => {
it('should return length', () => {
const line = new gdal.LineString()
line.points.add(0, 0, 0)
line.points.add(10, 10, 0)
line.points.add(10, 20, 0)
assert.closeTo(
line.getLength(),
Math.sqrt(10 * 10 + 10 * 10) + 10,
0.001
)
})
})
describe('addSubLineString()', () => {
it('should append to current linestring', () => {
const a = gdal.Geometry.fromWKT('LINESTRING(0 0, 0 1, 0 2)') as gdal.LineString
const b = gdal.Geometry.fromWKT('LINESTRING(0 2, 1 2, 2 2)') as gdal.LineString
const c = gdal.Geometry.fromWKT(
'LINESTRING(0 0, 0 1, 0 2, 0 2, 1 2, 2 2)'
)
assert.instanceOf(a, gdal.LineString)
assert.instanceOf(b, gdal.LineString)
a.addSubLineString(b)
assert.isTrue(a.equals(c))
})
it('should use start option', () => {
const a = gdal.Geometry.fromWKT('LINESTRING(0 0, 0 1, 0 2)') as gdal.LineString
const b = gdal.Geometry.fromWKT('LINESTRING(0 2, 1 2, 2 2)') as gdal.LineString
const c = gdal.Geometry.fromWKT('LINESTRING(0 0, 0 1, 0 2, 1 2, 2 2)')
assert.instanceOf(a, gdal.LineString)
assert.instanceOf(b, gdal.LineString)
a.addSubLineString(b, 1)
assert.isTrue(a.equals(c))
})
it('should use end option', () => {
const a = gdal.Geometry.fromWKT('LINESTRING(0 0, 0 1, 0 2)') as gdal.LineString
const b = gdal.Geometry.fromWKT('LINESTRING(0 2, 1 2, 2 2)') as gdal.LineString
const c = gdal.Geometry.fromWKT('LINESTRING(0 0, 0 1, 0 2, 1 2)')
assert.instanceOf(a, gdal.LineString)
assert.instanceOf(b, gdal.LineString)
a.addSubLineString(b, 1, 1)
assert.isTrue(a.equals(c))
})
it('should throw if given a non-linestring', () => {
const a = gdal.Geometry.fromWKT('LINESTRING(0 0, 0 1, 0 2)') as gdal.LineString
const b = gdal.Geometry.fromWKT('POINT(0 2)')
assert.instanceOf(a, gdal.LineString)
assert.instanceOf(b, gdal.Point)
assert.throws(() => {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
a.addSubLineString(b as any)
})
})
it('should throw if given invalid indexes', () => {
const a = gdal.Geometry.fromWKT('LINESTRING(0 0, 0 1, 0 2)') as gdal.LineString
const b = gdal.Geometry.fromWKT('LINESTRING(1 2)') as gdal.LineString
assert.instanceOf(a, gdal.LineString)
assert.instanceOf(b, gdal.LineString)
assert.throws(() => {
a.addSubLineString(b, -1)
})
assert.throws(() => {
a.addSubLineString(b, 1)
})
assert.throws(() => {
a.addSubLineString(b, 0, 1)
})
assert.throws(() => {
a.addSubLineString(b, 0, -2)
})
})
})
describe('"points" property', () => {
describe('count()', () => {
it('should return number of points', () => {
const line = new gdal.LineString()
line.points.add(0, 0, 0)
line.points.add(10, 10, 0)
line.points.add(10, 10, 2)
assert.equal(line.points.count(), 3)
})
})
describe('add()', () => {
it('should accept gdal.Point instance', () => {
const line = new gdal.LineString()
const pt = new gdal.Point(2, 3)
line.points.add(pt)
const pt_result = line.points.get(0)
assert.equal(pt_result.x, 2)
assert.equal(pt_result.y, 3)
})
it('should accept gdal.Point array', () => {
const line = new gdal.LineString()
line.points.add([ new gdal.Point(2, 3), new gdal.Point(4, 5) ])
const pt_result1 = line.points.get(0)
assert.equal(pt_result1.x, 2)
assert.equal(pt_result1.y, 3)
const pt_result2 = line.points.get(1)
assert.equal(pt_result2.x, 4)
assert.equal(pt_result2.y, 5)
})
it('should accept object', () => {
const line = new gdal.LineString()
line.points.add({ x: 2, y: 3 })
const pt_result = line.points.get(0)
assert.equal(pt_result.x, 2)
assert.equal(pt_result.y, 3)
})
it('should accept object array', () => {
const line = new gdal.LineString()
line.points.add([
{ x: 2, y: 3 },
{ x: 4, y: 5 }
])
const pt_result1 = line.points.get(0)
assert.equal(pt_result1.x, 2)
assert.equal(pt_result1.y, 3)
const pt_result2 = line.points.get(1)
assert.equal(pt_result2.x, 4)
assert.equal(pt_result2.y, 5)
})
it('should accept x,y,z arguments', () => {
const line = new gdal.LineString()
line.points.add(1, 2, 3)
const pt_result = line.points.get(0)
assert.equal(pt_result.x, 1)
assert.equal(pt_result.y, 2)
assert.equal(pt_result.z, 3)
})
})
describe('set()', () => {
it("should throw if point doesn't exist", () => {
assert.throws(() => {
const line = new gdal.LineString()
line.points.set(1, new gdal.Point(1, 1))
line.points.set(1, { x: 1, y: 1 })
line.points.set(1, 1, 1, 1)
})
})
it('should accept object', () => {
const line = new gdal.LineString()
line.points.add(0, 0)
line.points.set(0, { x: 1, y: 2 })
const pt = line.points.get(0)
assert.equal(pt.x, 1)
assert.equal(pt.y, 2)
})
it('should accept Point instance', () => {
const line = new gdal.LineString()
line.points.add(0, 0)
line.points.set(0, new gdal.Point(1, 2))
const pt = line.points.get(0)
assert.equal(pt.x, 1)
assert.equal(pt.y, 2)
})
it('should accept x,y,z arguments', () => {
const line = new gdal.LineString()
line.points.add(0, 0)
line.points.set(0, 1, 2, 3)
const pt = line.points.get(0)
assert.equal(pt.x, 1)
assert.equal(pt.y, 2)
assert.equal(pt.z, 3)
})
})
describe('get()', () => {
it("should throw if point doesn't exist", () => {
const line = new gdal.LineString()
assert.throws(() => {
line.points.get(2)
})
})
it('should Point instance', () => {
const line = new gdal.LineString()
line.points.add(1, 2, 3)
const pt = line.points.get(0)
assert.instanceOf(pt, gdal.Point)
assert.equal(pt.x, 1)
assert.equal(pt.y, 2)
assert.equal(pt.z, 3)
})
})
describe('resize()', () => {
it('should adjust the number of points', () => {
const line = new gdal.LineString()
line.points.add(1, 2, 3)
line.points.add(2, 3, 4)
line.points.add(3, 4, 5)
line.points.resize(2)
assert.equal(line.points.count(), 2)
})
})
describe('reverse()', () => {
it('should flip order of points', () => {
const line = new gdal.LineString()
line.points.add(1, 2, 3)
line.points.add(2, 3, 4)
line.points.add(3, 4, 5)
line.points.reverse()
const p1 = line.points.get(0)
const p2 = line.points.get(1)
const p3 = line.points.get(2)
assert.equal(p1.x, 3)
assert.equal(p2.x, 2)
assert.equal(p3.x, 1)
})
})
describe('forEach()', () => {
it('should stop if callback returns false', () => {
const line = new gdal.LineString()
line.points.add(1, 2, 3)
line.points.add(2, 3, 4)
line.points.add(3, 4, 5)
let count = 0
line.points.forEach((pt, i) => {
count++
assert.isNumber(i)
if (i === 1) return false
})
assert.equal(count, 2)
})
it('should iterate through all points', () => {
const x_expected = [ 1, 2, 3 ]
const x_actual = [] as number[]
const line = new gdal.LineString()
line.points.add(1, 2, 3)
line.points.add(2, 3, 4)
line.points.add(3, 4, 5)
line.points.forEach((pt, i) => {
assert.isNumber(i)
x_actual.push(pt.x)
})
assert.deepEqual(x_actual, x_expected)
})
})
describe('@@iterator()', () => {
it('should iterate through all points', () => {
const x_expected = [ 1, 2, 3 ]
const x_actual = []
const line = new gdal.LineString()
line.points.add(1, 2, 3)
line.points.add(2, 3, 4)
line.points.add(3, 4, 5)
for (const pt of line.points) {
x_actual.push(pt.x)
}
assert.deepEqual(x_actual, x_expected)
})
})
describe('map()', () => {
it('should operate normally', () => {
const line = new gdal.LineString()
line.points.add(1, 2, 3)
line.points.add(2, 3, 4)
line.points.add(3, 4, 5)
const result = line.points.map((pt, i) =>
[ pt.x, pt.y, pt.z, i ].join('/')
)
assert.deepEqual(result, [ '1/2/3/0', '2/3/4/1', '3/4/5/2' ])
})
})
describe('toArray()', () => {
it('should return array of Point instances', () => {
const line = new gdal.LineString()
line.points.add(1, 2, 3)
line.points.add(2, 3, 4)
line.points.add(3, 4, 5)
const points = line.points.toArray()
assert.lengthOf(points, 3)
assert.instanceOf(points[0], gdal.Point)
assert.instanceOf(points[1], gdal.Point)
assert.instanceOf(points[2], gdal.Point)
assert.equal(points[0].x, 1)
assert.equal(points[1].x, 2)
assert.equal(points[2].x, 3)
})
})
})
})
})