-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
LineString.js
311 lines (291 loc) · 8.13 KB
/
LineString.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
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
/**
* @module ol/geom/LineString
*/
import SimpleGeometry from './SimpleGeometry.js';
import {assignClosestPoint, maxSquaredDelta} from './flat/closest.js';
import {closestSquaredDistanceXY} from '../extent.js';
import {deflateCoordinates} from './flat/deflate.js';
import {douglasPeucker} from './flat/simplify.js';
import {extend} from '../array.js';
import {forEach as forEachSegment} from './flat/segments.js';
import {inflateCoordinates} from './flat/inflate.js';
import {interpolatePoint, lineStringCoordinateAtM} from './flat/interpolate.js';
import {intersectsLineString} from './flat/intersectsextent.js';
import {lineStringLength} from './flat/length.js';
/**
* @classdesc
* Linestring geometry.
*
* @api
*/
class LineString extends SimpleGeometry {
/**
* @param {Array<import("../coordinate.js").Coordinate>|Array<number>} coordinates Coordinates.
* For internal use, flat coordinates in combination with `layout` are also accepted.
* @param {import("./Geometry.js").GeometryLayout} [layout] Layout.
*/
constructor(coordinates, layout) {
super();
/**
* @private
* @type {import("../coordinate.js").Coordinate|null}
*/
this.flatMidpoint_ = null;
/**
* @private
* @type {number}
*/
this.flatMidpointRevision_ = -1;
/**
* @private
* @type {number}
*/
this.maxDelta_ = -1;
/**
* @private
* @type {number}
*/
this.maxDeltaRevision_ = -1;
if (layout !== undefined && !Array.isArray(coordinates[0])) {
this.setFlatCoordinates(
layout,
/** @type {Array<number>} */ (coordinates),
);
} else {
this.setCoordinates(
/** @type {Array<import("../coordinate.js").Coordinate>} */ (
coordinates
),
layout,
);
}
}
/**
* Append the passed coordinate to the coordinates of the linestring.
* @param {import("../coordinate.js").Coordinate} coordinate Coordinate.
* @api
*/
appendCoordinate(coordinate) {
extend(this.flatCoordinates, coordinate);
this.changed();
}
/**
* Make a complete copy of the geometry.
* @return {!LineString} Clone.
* @api
*/
clone() {
const lineString = new LineString(
this.flatCoordinates.slice(),
this.layout,
);
lineString.applyProperties(this);
return lineString;
}
/**
* @param {number} x X.
* @param {number} y Y.
* @param {import("../coordinate.js").Coordinate} closestPoint Closest point.
* @param {number} minSquaredDistance Minimum squared distance.
* @return {number} Minimum squared distance.
*/
closestPointXY(x, y, closestPoint, minSquaredDistance) {
if (minSquaredDistance < closestSquaredDistanceXY(this.getExtent(), x, y)) {
return minSquaredDistance;
}
if (this.maxDeltaRevision_ != this.getRevision()) {
this.maxDelta_ = Math.sqrt(
maxSquaredDelta(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
0,
),
);
this.maxDeltaRevision_ = this.getRevision();
}
return assignClosestPoint(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
this.maxDelta_,
false,
x,
y,
closestPoint,
minSquaredDistance,
);
}
/**
* Iterate over each segment, calling the provided callback.
* If the callback returns a truthy value the function returns that
* value immediately. Otherwise the function returns `false`.
*
* @param {function(this: S, import("../coordinate.js").Coordinate, import("../coordinate.js").Coordinate): T} callback Function
* called for each segment. The function will receive two arguments, the start and end coordinates of the segment.
* @return {T|boolean} Value.
* @template T,S
* @api
*/
forEachSegment(callback) {
return forEachSegment(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
callback,
);
}
/**
* Returns the coordinate at `m` using linear interpolation, or `null` if no
* such coordinate exists.
*
* `extrapolate` controls extrapolation beyond the range of Ms in the
* MultiLineString. If `extrapolate` is `true` then Ms less than the first
* M will return the first coordinate and Ms greater than the last M will
* return the last coordinate.
*
* @param {number} m M.
* @param {boolean} [extrapolate] Extrapolate. Default is `false`.
* @return {import("../coordinate.js").Coordinate|null} Coordinate.
* @api
*/
getCoordinateAtM(m, extrapolate) {
if (this.layout != 'XYM' && this.layout != 'XYZM') {
return null;
}
extrapolate = extrapolate !== undefined ? extrapolate : false;
return lineStringCoordinateAtM(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
m,
extrapolate,
);
}
/**
* Return the coordinates of the linestring.
* @return {Array<import("../coordinate.js").Coordinate>} Coordinates.
* @api
*/
getCoordinates() {
return inflateCoordinates(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
);
}
/**
* Return the coordinate at the provided fraction along the linestring.
* The `fraction` is a number between 0 and 1, where 0 is the start of the
* linestring and 1 is the end.
* @param {number} fraction Fraction.
* @param {import("../coordinate.js").Coordinate} [dest] Optional coordinate whose values will
* be modified. If not provided, a new coordinate will be returned.
* @return {import("../coordinate.js").Coordinate} Coordinate of the interpolated point.
* @api
*/
getCoordinateAt(fraction, dest) {
return interpolatePoint(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
fraction,
dest,
this.stride,
);
}
/**
* Return the length of the linestring on projected plane.
* @return {number} Length (on projected plane).
* @api
*/
getLength() {
return lineStringLength(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
);
}
/**
* @return {Array<number>} Flat midpoint.
*/
getFlatMidpoint() {
if (this.flatMidpointRevision_ != this.getRevision()) {
this.flatMidpoint_ = this.getCoordinateAt(
0.5,
this.flatMidpoint_ ?? undefined,
);
this.flatMidpointRevision_ = this.getRevision();
}
return /** @type {Array<number>} */ (this.flatMidpoint_);
}
/**
* @param {number} squaredTolerance Squared tolerance.
* @return {LineString} Simplified LineString.
* @protected
*/
getSimplifiedGeometryInternal(squaredTolerance) {
/** @type {Array<number>} */
const simplifiedFlatCoordinates = [];
simplifiedFlatCoordinates.length = douglasPeucker(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
squaredTolerance,
simplifiedFlatCoordinates,
0,
);
return new LineString(simplifiedFlatCoordinates, 'XY');
}
/**
* Get the type of this geometry.
* @return {import("./Geometry.js").Type} Geometry type.
* @api
*/
getType() {
return 'LineString';
}
/**
* Test if the geometry and the passed extent intersect.
* @param {import("../extent.js").Extent} extent Extent.
* @return {boolean} `true` if the geometry and the extent intersect.
* @api
*/
intersectsExtent(extent) {
return intersectsLineString(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
extent,
);
}
/**
* Set the coordinates of the linestring.
* @param {!Array<import("../coordinate.js").Coordinate>} coordinates Coordinates.
* @param {import("./Geometry.js").GeometryLayout} [layout] Layout.
* @api
*/
setCoordinates(coordinates, layout) {
this.setLayout(layout, coordinates, 1);
if (!this.flatCoordinates) {
this.flatCoordinates = [];
}
this.flatCoordinates.length = deflateCoordinates(
this.flatCoordinates,
0,
coordinates,
this.stride,
);
this.changed();
}
}
export default LineString;