forked from openlayers/openlayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinate.js
438 lines (413 loc) · 12.3 KB
/
coordinate.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* @module ol/coordinate
*/
import {getWidth} from './extent.js';
import {modulo, toFixed} from './math.js';
import {padNumber} from './string.js';
/**
* An array of numbers representing an xy coordinate. Example: `[16, 48]`.
* @typedef {Array<number>} Coordinate
* @api
*/
/**
* A function that takes a {@link module:ol/coordinate~Coordinate} and
* transforms it into a `{string}`.
*
* @typedef {function((Coordinate|undefined)): string} CoordinateFormat
* @api
*/
/**
* Add `delta` to `coordinate`. `coordinate` is modified in place and returned
* by the function.
*
* Example:
*
* import {add} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* add(coord, [-2, 4]);
* // coord is now [5.85, 51.983333]
*
* @param {Coordinate} coordinate Coordinate.
* @param {Coordinate} delta Delta.
* @return {Coordinate} The input coordinate adjusted by
* the given delta.
* @api
*/
export function add(coordinate, delta) {
coordinate[0] += +delta[0];
coordinate[1] += +delta[1];
return coordinate;
}
/**
* Calculates the point closest to the passed coordinate on the passed circle.
*
* @param {Coordinate} coordinate The coordinate.
* @param {import("./geom/Circle.js").default} circle The circle.
* @return {Coordinate} Closest point on the circumference.
*/
export function closestOnCircle(coordinate, circle) {
const r = circle.getRadius();
const center = circle.getCenter();
const x0 = center[0];
const y0 = center[1];
const x1 = coordinate[0];
const y1 = coordinate[1];
let dx = x1 - x0;
const dy = y1 - y0;
if (dx === 0 && dy === 0) {
dx = 1;
}
const d = Math.sqrt(dx * dx + dy * dy);
const x = x0 + (r * dx) / d;
const y = y0 + (r * dy) / d;
return [x, y];
}
/**
* Calculates the point closest to the passed coordinate on the passed segment.
* This is the foot of the perpendicular of the coordinate to the segment when
* the foot is on the segment, or the closest segment coordinate when the foot
* is outside the segment.
*
* @param {Coordinate} coordinate The coordinate.
* @param {Array<Coordinate>} segment The two coordinates
* of the segment.
* @return {Coordinate} The foot of the perpendicular of
* the coordinate to the segment.
*/
export function closestOnSegment(coordinate, segment) {
const x0 = coordinate[0];
const y0 = coordinate[1];
const start = segment[0];
const end = segment[1];
const x1 = start[0];
const y1 = start[1];
const x2 = end[0];
const y2 = end[1];
const dx = x2 - x1;
const dy = y2 - y1;
const along =
dx === 0 && dy === 0
? 0
: (dx * (x0 - x1) + dy * (y0 - y1)) / (dx * dx + dy * dy || 0);
let x, y;
if (along <= 0) {
x = x1;
y = y1;
} else if (along >= 1) {
x = x2;
y = y2;
} else {
x = x1 + along * dx;
y = y1 + along * dy;
}
return [x, y];
}
/**
* Returns a {@link module:ol/coordinate~CoordinateFormat} function that can be
* used to format
* a {Coordinate} to a string.
*
* Example without specifying the fractional digits:
*
* import {createStringXY} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* const stringifyFunc = createStringXY();
* const out = stringifyFunc(coord);
* // out is now '8, 48'
*
* Example with explicitly specifying 2 fractional digits:
*
* import {createStringXY} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* const stringifyFunc = createStringXY(2);
* const out = stringifyFunc(coord);
* // out is now '7.85, 47.98'
*
* @param {number} [fractionDigits] The number of digits to include
* after the decimal point. Default is `0`.
* @return {CoordinateFormat} Coordinate format.
* @api
*/
export function createStringXY(fractionDigits) {
return (
/**
* @param {Coordinate} coordinate Coordinate.
* @return {string} String XY.
*/
function (coordinate) {
return toStringXY(coordinate, fractionDigits);
}
);
}
/**
* @param {string} hemispheres Hemispheres.
* @param {number} degrees Degrees.
* @param {number} [fractionDigits] The number of digits to include
* after the decimal point. Default is `0`.
* @return {string} String.
*/
export function degreesToStringHDMS(hemispheres, degrees, fractionDigits) {
const normalizedDegrees = modulo(degrees + 180, 360) - 180;
const x = Math.abs(3600 * normalizedDegrees);
const decimals = fractionDigits || 0;
let deg = Math.floor(x / 3600);
let min = Math.floor((x - deg * 3600) / 60);
let sec = toFixed(x - deg * 3600 - min * 60, decimals);
if (sec >= 60) {
sec = 0;
min += 1;
}
if (min >= 60) {
min = 0;
deg += 1;
}
let hdms = deg + '\u00b0';
if (min !== 0 || sec !== 0) {
hdms += ' ' + padNumber(min, 2) + '\u2032';
}
if (sec !== 0) {
hdms += ' ' + padNumber(sec, 2, decimals) + '\u2033';
}
if (normalizedDegrees !== 0) {
hdms += ' ' + hemispheres.charAt(normalizedDegrees < 0 ? 1 : 0);
}
return hdms;
}
/**
* Transforms the given {@link module:ol/coordinate~Coordinate} to a string
* using the given string template. The strings `{x}` and `{y}` in the template
* will be replaced with the first and second coordinate values respectively.
*
* Example without specifying the fractional digits:
*
* import {format} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* const template = 'Coordinate is ({x}|{y}).';
* const out = format(coord, template);
* // out is now 'Coordinate is (8|48).'
*
* Example explicitly specifying the fractional digits:
*
* import {format} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* const template = 'Coordinate is ({x}|{y}).';
* const out = format(coord, template, 2);
* // out is now 'Coordinate is (7.85|47.98).'
*
* @param {Coordinate} coordinate Coordinate.
* @param {string} template A template string with `{x}` and `{y}` placeholders
* that will be replaced by first and second coordinate values.
* @param {number} [fractionDigits] The number of digits to include
* after the decimal point. Default is `0`.
* @return {string} Formatted coordinate.
* @api
*/
export function format(coordinate, template, fractionDigits) {
if (coordinate) {
return template
.replace('{x}', coordinate[0].toFixed(fractionDigits))
.replace('{y}', coordinate[1].toFixed(fractionDigits));
}
return '';
}
/**
* @param {Coordinate} coordinate1 First coordinate.
* @param {Coordinate} coordinate2 Second coordinate.
* @return {boolean} The two coordinates are equal.
*/
export function equals(coordinate1, coordinate2) {
let equals = true;
for (let i = coordinate1.length - 1; i >= 0; --i) {
if (coordinate1[i] != coordinate2[i]) {
equals = false;
break;
}
}
return equals;
}
/**
* Rotate `coordinate` by `angle`. `coordinate` is modified in place and
* returned by the function.
*
* Example:
*
* import {rotate} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* const rotateRadians = Math.PI / 2; // 90 degrees
* rotate(coord, rotateRadians);
* // coord is now [-47.983333, 7.85]
*
* @param {Coordinate} coordinate Coordinate.
* @param {number} angle Angle in radian.
* @return {Coordinate} Coordinate.
* @api
*/
export function rotate(coordinate, angle) {
const cosAngle = Math.cos(angle);
const sinAngle = Math.sin(angle);
const x = coordinate[0] * cosAngle - coordinate[1] * sinAngle;
const y = coordinate[1] * cosAngle + coordinate[0] * sinAngle;
coordinate[0] = x;
coordinate[1] = y;
return coordinate;
}
/**
* Scale `coordinate` by `scale`. `coordinate` is modified in place and returned
* by the function.
*
* Example:
*
* import {scale as scaleCoordinate} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* const scale = 1.2;
* scaleCoordinate(coord, scale);
* // coord is now [9.42, 57.5799996]
*
* @param {Coordinate} coordinate Coordinate.
* @param {number} scale Scale factor.
* @return {Coordinate} Coordinate.
*/
export function scale(coordinate, scale) {
coordinate[0] *= scale;
coordinate[1] *= scale;
return coordinate;
}
/**
* @param {Coordinate} coord1 First coordinate.
* @param {Coordinate} coord2 Second coordinate.
* @return {number} Squared distance between coord1 and coord2.
*/
export function squaredDistance(coord1, coord2) {
const dx = coord1[0] - coord2[0];
const dy = coord1[1] - coord2[1];
return dx * dx + dy * dy;
}
/**
* @param {Coordinate} coord1 First coordinate.
* @param {Coordinate} coord2 Second coordinate.
* @return {number} Distance between coord1 and coord2.
*/
export function distance(coord1, coord2) {
return Math.sqrt(squaredDistance(coord1, coord2));
}
/**
* Calculate the squared distance from a coordinate to a line segment.
*
* @param {Coordinate} coordinate Coordinate of the point.
* @param {Array<Coordinate>} segment Line segment (2
* coordinates).
* @return {number} Squared distance from the point to the line segment.
*/
export function squaredDistanceToSegment(coordinate, segment) {
return squaredDistance(coordinate, closestOnSegment(coordinate, segment));
}
/**
* Format a geographic coordinate with the hemisphere, degrees, minutes, and
* seconds.
*
* Example without specifying fractional digits:
*
* import {toStringHDMS} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* const out = toStringHDMS(coord);
* // out is now '47° 58′ 60″ N 7° 50′ 60″ E'
*
* Example explicitly specifying 1 fractional digit:
*
* import {toStringHDMS} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* const out = toStringHDMS(coord, 1);
* // out is now '47° 58′ 60.0″ N 7° 50′ 60.0″ E'
*
* @param {Coordinate} coordinate Coordinate.
* @param {number} [fractionDigits] The number of digits to include
* after the decimal point. Default is `0`.
* @return {string} Hemisphere, degrees, minutes and seconds.
* @api
*/
export function toStringHDMS(coordinate, fractionDigits) {
if (coordinate) {
return (
degreesToStringHDMS('NS', coordinate[1], fractionDigits) +
' ' +
degreesToStringHDMS('EW', coordinate[0], fractionDigits)
);
}
return '';
}
/**
* Format a coordinate as a comma delimited string.
*
* Example without specifying fractional digits:
*
* import {toStringXY} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* const out = toStringXY(coord);
* // out is now '8, 48'
*
* Example explicitly specifying 1 fractional digit:
*
* import {toStringXY} from 'ol/coordinate.js';
*
* const coord = [7.85, 47.983333];
* const out = toStringXY(coord, 1);
* // out is now '7.8, 48.0'
*
* @param {Coordinate} coordinate Coordinate.
* @param {number} [fractionDigits] The number of digits to include
* after the decimal point. Default is `0`.
* @return {string} XY.
* @api
*/
export function toStringXY(coordinate, fractionDigits) {
return format(coordinate, '{x}, {y}', fractionDigits);
}
/**
* Modifies the provided coordinate in-place to be within the real world
* extent. The lower projection extent boundary is inclusive, the upper one
* exclusive.
*
* @param {Coordinate} coordinate Coordinate.
* @param {import("./proj/Projection.js").default} projection Projection.
* @return {Coordinate} The coordinate within the real world extent.
*/
export function wrapX(coordinate, projection) {
if (projection.canWrapX()) {
const worldWidth = getWidth(projection.getExtent());
const worldsAway = getWorldsAway(coordinate, projection, worldWidth);
if (worldsAway) {
coordinate[0] -= worldsAway * worldWidth;
}
}
return coordinate;
}
/**
* @param {Coordinate} coordinate Coordinate.
* @param {import("./proj/Projection.js").default} projection Projection.
* @param {number} [sourceExtentWidth] Width of the source extent.
* @return {number} Offset in world widths.
*/
export function getWorldsAway(coordinate, projection, sourceExtentWidth) {
const projectionExtent = projection.getExtent();
let worldsAway = 0;
if (
projection.canWrapX() &&
(coordinate[0] < projectionExtent[0] || coordinate[0] > projectionExtent[2])
) {
sourceExtentWidth = sourceExtentWidth || getWidth(projectionExtent);
worldsAway = Math.floor(
(coordinate[0] - projectionExtent[0]) / sourceExtentWidth
);
}
return worldsAway;
}