-
Notifications
You must be signed in to change notification settings - Fork 1
/
GridUtils.ts
280 lines (256 loc) · 7.77 KB
/
GridUtils.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
import { GeometryConstants, GeometryUtils, Point as MilPoint } from '@ngageoint/simple-features-js';
import { Bounds } from './features/Bounds';
import { Line } from './features/Line';
import { Point } from './features/Point';
import { Unit } from './features/Unit';
import { GridConstants } from './GridConstants';
import { Pixel } from './tile/Pixel';
/**
* Grid utilities
*/
export class GridUtils {
/**
* Get the pixel where the point fits into the bounds
*
* @param width
* width
* @param height
* height
* @param bounds
* bounds
* @param point
* point
* @return pixel
*/
public static getPixel(width: number, height: number, bounds: Bounds, point: Point): Pixel {
point = point.toMeters();
bounds = bounds.toMeters();
const x = this.getXPixel(width, bounds, point.getLongitude());
const y = this.getYPixel(height, bounds, point.getLatitude());
return new Pixel(x, y);
}
/**
* Get the X pixel for where the longitude in meters fits into the bounds
*
* @param width
* width
* @param bounds
* bounds
* @param longitude
* longitude in meters
* @return x pixel
*/
public static getXPixel(width: number, bounds: Bounds, longitude: number): number {
bounds = bounds.toMeters();
const boxWidth = bounds.getMaxLongitude() - bounds.getMinLongitude();
const offset = longitude - bounds.getMinLongitude();
const percentage = offset / boxWidth;
const pixel = percentage * width;
return pixel;
}
/**
* Get the Y pixel for where the latitude in meters fits into the bounds
*
* @param height
* height
* @param bounds
* bounds
* @param latitude
* latitude
* @return y pixel
*/
public static getYPixel(height: number, bounds: Bounds, latitude: number): number {
bounds = bounds.toMeters();
const boxHeight = bounds.getMaxLatitude() - bounds.getMinLatitude();
const offset = bounds.getMaxLatitude() - latitude;
const percentage = offset / boxHeight;
const pixel = percentage * height;
return pixel;
}
/**
* Get the tile bounds from the XYZ tile coordinates and zoom level
*
* @param x
* x coordinate
* @param y
* y coordinate
* @param zoom
* zoom level
* @return bounds
*/
public static getBounds(x: number, y: number, zoom: number): Bounds {
const tilesPerSide = this.tilesPerSide(zoom);
const tileSize = this.tileSize(tilesPerSide);
const minLon = -1 * GeometryConstants.WEB_MERCATOR_HALF_WORLD_WIDTH + x * tileSize;
const minLat = GeometryConstants.WEB_MERCATOR_HALF_WORLD_WIDTH - (y + 1) * tileSize;
const maxLon = -1 * GeometryConstants.WEB_MERCATOR_HALF_WORLD_WIDTH + (x + 1) * tileSize;
const maxLat = GeometryConstants.WEB_MERCATOR_HALF_WORLD_WIDTH - y * tileSize;
return Bounds.meters(minLon, minLat, maxLon, maxLat);
}
/**
* Get the tiles per side, width and height, at the zoom level
*
* @param zoom
* zoom level
* @return tiles per side
*/
public static tilesPerSide(zoom: number): number {
return Math.pow(2, zoom);
}
/**
* Get the tile size in meters
*
* @param tilesPerSide
* tiles per side
* @return tile size
*/
public static tileSize(tilesPerSide: number): number {
return (2 * GeometryConstants.WEB_MERCATOR_HALF_WORLD_WIDTH) / tilesPerSide;
}
/**
* Get the zoom level of the bounds using the shortest bounds side length
*
* @param bounds
* bounds
* @return zoom level
*/
public static getZoomLevel(bounds: Bounds): number {
bounds = bounds.toMeters();
const tileSize = Math.min(bounds.getWidth(), bounds.getHeight());
const tilesPerSide = (2 * GeometryConstants.WEB_MERCATOR_HALF_WORLD_WIDTH) / tileSize;
return Math.log(tilesPerSide) / Math.log(2);
}
/**
* Convert a coordinate from 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 {
let point = null;
if (fromUnit === toUnit) {
point = Point.point(longitude, latitude, toUnit);
} else {
point = this.toUnitOpposite(longitude, latitude, toUnit);
}
return point;
}
/**
* Convert a coordinate to the unit, assumes the coordinate is in the
* opposite unit
*
* @param longitude
* longitude
* @param latitude
* latitude
* @param unit
* desired unit
* @return point in unit
*/
public static toUnitOpposite(longitude: number, latitude: number, unit: Unit): Point {
let point: MilPoint;
switch (unit) {
case Unit.DEGREE:
point = GeometryUtils.metersToDegreesCoord(longitude, latitude);
break;
case Unit.METER:
point = GeometryUtils.degreesToMetersCoord(longitude, latitude);
break;
default:
throw new Error('Unsupported unit: ' + unit);
}
return Point.pointFromPoint(point, unit);
}
/**
* Is the band letter an omitted letter
* {@link GridConstants#BAND_LETTER_OMIT_I} or
* {@link GridConstants#BAND_LETTER_OMIT_O}
*
* @param letter
* band letter
* @return true if omitted
*/
public static isOmittedBandLetter(letter: string): boolean {
return letter === GridConstants.BAND_LETTER_OMIT_I || letter === GridConstants.BAND_LETTER_OMIT_O;
}
/**
* Get the precision value before the value
*
* @param value
* value
* @param precision
* precision
* @return precision value
*/
public static precisionBefore(value: number, precision: number): number {
let before = 0.0;
if (Math.abs(value) >= precision) {
before = value - (((value % precision) + precision) % precision);
} else if (value < 0.0) {
before = -precision;
}
return before;
}
/**
* Get the precision value after the value
*
* @param value
* value
* @param precision
* precision
* @return precision value
*/
public static precisionAfter(value: number, precision: number): number {
return this.precisionBefore(value + precision, precision);
}
/**
* Get the point intersection between two lines
*
* @param line1
* first line
* @param line2
* second line
* @return intersection point or null if no intersection
*/
public static lineIntersection(line1: Line, line2: Line): Point | undefined {
return this.intersection(line1.getPoint1(), line1.getPoint2(), line2.getPoint1(), line2.getPoint2());
}
/**
* Get the point intersection between end points of two lines
*
* @param line1Point1
* first point of the first line
* @param line1Point2
* second point of the first line
* @param line2Point1
* first point of the second line
* @param line2Point2
* second point of the second line
* @return intersection point or null if no intersection
*/
public static intersection(
line1Point1: Point,
line1Point2: Point,
line2Point1: Point,
line2Point2: Point,
): Point | undefined {
let intersection: Point | undefined;
const point: MilPoint = GeometryUtils.intersection(
line1Point1.toMeters(),
line1Point2.toMeters(),
line2Point1.toMeters(),
line2Point2.toMeters(),
);
if (point !== null && point !== undefined) {
intersection = Point.pointFromPoint(point, Unit.METER).toUnit(line1Point1.getUnit()!);
}
return intersection;
}
}