-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
MultiPoint.js
203 lines (192 loc) · 5.13 KB
/
MultiPoint.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
/**
* @module ol/geom/MultiPoint
*/
import {extend} from '../array.js';
import {closestSquaredDistanceXY, containsXY} from '../extent.js';
import {squaredDistance as squaredDx} from '../math.js';
import Point from './Point.js';
import SimpleGeometry from './SimpleGeometry.js';
import {deflateCoordinates} from './flat/deflate.js';
import {inflateCoordinates} from './flat/inflate.js';
/**
* @classdesc
* Multi-point geometry.
*
* @api
*/
class MultiPoint 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();
if (layout && !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 point to this multipoint.
* @param {Point} point Point.
* @api
*/
appendPoint(point) {
extend(this.flatCoordinates, point.getFlatCoordinates());
this.changed();
}
/**
* Make a complete copy of the geometry.
* @return {!MultiPoint} Clone.
* @api
* @override
*/
clone() {
const multiPoint = new MultiPoint(
this.flatCoordinates.slice(),
this.layout,
);
multiPoint.applyProperties(this);
return multiPoint;
}
/**
* @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.
* @override
*/
closestPointXY(x, y, closestPoint, minSquaredDistance) {
if (minSquaredDistance < closestSquaredDistanceXY(this.getExtent(), x, y)) {
return minSquaredDistance;
}
const flatCoordinates = this.flatCoordinates;
const stride = this.stride;
for (let i = 0, ii = flatCoordinates.length; i < ii; i += stride) {
const squaredDistance = squaredDx(
x,
y,
flatCoordinates[i],
flatCoordinates[i + 1],
);
if (squaredDistance < minSquaredDistance) {
minSquaredDistance = squaredDistance;
for (let j = 0; j < stride; ++j) {
closestPoint[j] = flatCoordinates[i + j];
}
closestPoint.length = stride;
}
}
return minSquaredDistance;
}
/**
* Return the coordinates of the multipoint.
* @return {Array<import("../coordinate.js").Coordinate>} Coordinates.
* @api
* @override
*/
getCoordinates() {
return inflateCoordinates(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
);
}
/**
* Return the point at the specified index.
* @param {number} index Index.
* @return {Point} Point.
* @api
*/
getPoint(index) {
const n = this.flatCoordinates.length / this.stride;
if (index < 0 || n <= index) {
return null;
}
return new Point(
this.flatCoordinates.slice(
index * this.stride,
(index + 1) * this.stride,
),
this.layout,
);
}
/**
* Return the points of this multipoint.
* @return {Array<Point>} Points.
* @api
*/
getPoints() {
const flatCoordinates = this.flatCoordinates;
const layout = this.layout;
const stride = this.stride;
/** @type {Array<Point>} */
const points = [];
for (let i = 0, ii = flatCoordinates.length; i < ii; i += stride) {
const point = new Point(flatCoordinates.slice(i, i + stride), layout);
points.push(point);
}
return points;
}
/**
* Get the type of this geometry.
* @return {import("./Geometry.js").Type} Geometry type.
* @api
* @override
*/
getType() {
return 'MultiPoint';
}
/**
* 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
* @override
*/
intersectsExtent(extent) {
const flatCoordinates = this.flatCoordinates;
const stride = this.stride;
for (let i = 0, ii = flatCoordinates.length; i < ii; i += stride) {
const x = flatCoordinates[i];
const y = flatCoordinates[i + 1];
if (containsXY(extent, x, y)) {
return true;
}
}
return false;
}
/**
* Set the coordinates of the multipoint.
* @param {!Array<import("../coordinate.js").Coordinate>} coordinates Coordinates.
* @param {import("./Geometry.js").GeometryLayout} [layout] Layout.
* @api
* @override
*/
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 MultiPoint;