-
Notifications
You must be signed in to change notification settings - Fork 1
/
Point.ts
307 lines (281 loc) · 6.48 KB
/
Point.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
297
298
299
300
301
302
303
304
305
306
307
import { Point as MilPoint } from '@ngageoint/simple-features-js';
import { GridUtils } from '../GridUtils';
import { GridTile } from '../tile/GridTile';
import { Pixel } from '../tile/Pixel';
import { Bounds } from './Bounds';
import { Unit } from './Unit';
/**
* Point
*/
export class Point extends MilPoint {
/**
* Unit
*/
private unit?: Unit = Unit.DEGREE;
/**
* Create a point
*
* @param longitude
* longitude
* @param latitude
* latitude
* @param unit
* unit; if null, it will use the default degree unit
* @return point
*/
public static point(longitude: number, latitude: number, unit?: Unit): Point {
let point: Point;
if (unit !== undefined) {
point = new Point(longitude, latitude);
point.unit = unit;
} else {
point = this.degrees(longitude, latitude);
}
return point;
}
/**
* Create a point in degrees
*
* @param longitude
* longitude in degrees
* @param latitude
* latitude in degrees
* @return point in degrees
*/
public static degrees(longitude: number, latitude: number): Point {
return Point.point(longitude, latitude, Unit.DEGREE);
}
/**
* Create a point in meters
*
* @param longitude
* longitude in meters
* @param latitude
* latitude in meters
* @return point in meters
*/
public static meters(longitude: number, latitude: number): Point {
return Point.point(longitude, latitude, Unit.METER);
}
/**
* Create a point from a coordinate in a unit to another unit
*
* @param fromUnit
* unit of provided coordinate
* @param longitude
* longitude
* @param latitude
* latitude
* @param toUnit
* desired unit
* @return point in unit
*/
public static toUnit(fromUnit: Unit, longitude: number, latitude: number, toUnit: Unit): Point {
return GridUtils.toUnit(fromUnit, longitude, latitude, toUnit);
}
/**
* Create a point from a coordinate in an opposite unit to another unit
*
* @param longitude
* longitude
* @param latitude
* latitude
* @param unit
* desired unit
* @return point in unit
*/
public static toUnitInverse(longitude: number, latitude: number, unit: Unit): Point {
return GridUtils.toUnitOpposite(longitude, latitude, unit);
}
/**
* Create a point converting the degrees coordinate to meters
*
* @param longitude
* longitude in degrees
* @param latitude
* latitude in degrees
* @return point in meters
*/
public static degreesToMeters(longitude: number, latitude: number): Point {
return Point.toUnit(Unit.DEGREE, longitude, latitude, Unit.METER);
}
/**
* Create a point converting the meters coordinate to degrees
*
* @param longitude
* longitude in meters
* @param latitude
* latitude in meters
* @return point in degrees
*/
public static metersToDegrees(longitude: number, latitude: number): Point {
return Point.toUnit(Unit.METER, longitude, latitude, Unit.DEGREE);
}
/**
* Create a point
*
* @param point
* point to copy
* @param unit
* unit
* @return point
*/
public static pointFromPoint(point: MilPoint, unit?: Unit): Point {
const newPoint = new Point(point);
newPoint.unit = unit;
return newPoint;
}
/**
* Get the longitude
*
* @return longitude
*/
public getLongitude(): number {
return this.x;
}
/**
* Set the longitude
*
* @param longitude
* longitude
*/
public setLongitude(longitude: number): void {
this.x = longitude;
}
/**
* Get the latitude
*
* @return latitude
*/
public getLatitude(): number {
return this.y;
}
/**
* Set the latitude
*
* @param latitude
* latitude
*/
public setLatitude(latitude: number): void {
this.y = latitude;
}
/**
* Get the unit
*
* @return unit
*/
public getUnit(): Unit | undefined {
return this.unit;
}
/**
* Set the unit
*
* @param unit
* unit
*/
public setUnit(unit?: Unit): void {
this.unit = unit;
}
/**
* Is in the provided unit type
*
* @param unit
* unit
* @return true if in the unit
*/
public isUnit(unit?: Unit): boolean {
return this.unit === unit;
}
/**
* Is this point in degrees
*
* @return true if degrees
*/
public isDegrees(): boolean {
return this.isUnit(Unit.DEGREE);
}
/**
* Is this point in meters
*
* @return true if meters
*/
public isMeters(): boolean {
return this.isUnit(Unit.METER);
}
/**
* Convert to the unit
*
* @param unit
* unit
* @return point in units, same point if equal units
*/
public toUnit(unit: Unit): Point {
let newPoint: Point;
if (this.isUnit(unit)) {
newPoint = this;
} else {
newPoint = GridUtils.toUnit(this.unit!, this.getLongitude(), this.getLatitude(), unit);
}
return newPoint;
}
/**
* Convert to degrees
*
* @return point in degrees, same point if already in degrees
*/
public toDegrees(): Point {
return this.toUnit(Unit.DEGREE);
}
/**
* Convert to meters
*
* @return point in meters, same point if already in meters
*/
public toMeters(): Point {
return this.toUnit(Unit.METER);
}
/**
* Get the pixel where the point fits into tile
*
* @param tile
* tile
* @return pixel
*/
public getPixelFromTile(tile: GridTile): Pixel {
return this.getPixel(tile.getWidth(), tile.getHeight(), tile.getBounds()!);
}
/**
* Get the pixel where the point fits into the bounds
*
* @param width
* width
* @param height
* height
* @param bounds
* bounds
* @return pixel
*/
public getPixel(width: number, height: number, bounds: Bounds): Pixel {
return GridUtils.getPixel(width, height, bounds, this);
}
/**
* Copy the point
*
* @return point copy
*/
public copy(): Point {
const copy = new Point(this);
copy.unit = this.unit;
return copy;
}
/**
* {@inheritDoc}
*/
public equals(other: Point): boolean {
if (this === other) return true;
if (!super.equals(other)) return false;
if (!(other instanceof Point)) return false;
if (this.unit !== other.getUnit()) return false;
return true;
}
}