-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
360 lines (296 loc) · 13.9 KB
/
utils.js
File metadata and controls
360 lines (296 loc) · 13.9 KB
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
/**
* Утилиты для работы с Яндекс Картами
* Вспомогательные функции для геопространственных вычислений
*/
class MapUtils {
/**
* Константы для геодезических вычислений
*/
static EARTH_RADIUS = 6371000; // Радиус Земли в метрах
static DEG_TO_RAD = Math.PI / 180;
static RAD_TO_DEG = 180 / Math.PI;
/**
* Вычисление расстояния между двумя точками (формула гаверсинуса)
* @param {Array} point1 - [широта, долгота] первой точки
* @param {Array} point2 - [широта, долгота] второй точки
* @returns {number} Расстояние в метрах
*/
static calculateDistance(point1, point2) {
const [lat1, lon1] = point1;
const [lat2, lon2] = point2;
const dLat = (lat2 - lat1) * this.DEG_TO_RAD;
const dLon = (lon2 - lon1) * this.DEG_TO_RAD;
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * this.DEG_TO_RAD) * Math.cos(lat2 * this.DEG_TO_RAD) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return this.EARTH_RADIUS * c;
}
/**
* Вычисление азимута между двумя точками
* @param {Array} point1 - [широта, долгота] первой точки
* @param {Array} point2 - [широта, долгота] второй точки
* @returns {number} Азимут в градусах (0-360)
*/
static calculateBearing(point1, point2) {
const [lat1, lon1] = point1;
const [lat2, lon2] = point2;
const dLon = (lon2 - lon1) * this.DEG_TO_RAD;
const lat1Rad = lat1 * this.DEG_TO_RAD;
const lat2Rad = lat2 * this.DEG_TO_RAD;
const y = Math.sin(dLon) * Math.cos(lat2Rad);
const x = Math.cos(lat1Rad) * Math.sin(lat2Rad) -
Math.sin(lat1Rad) * Math.cos(lat2Rad) * Math.cos(dLon);
let bearing = Math.atan2(y, x) * this.RAD_TO_DEG;
bearing = (bearing + 360) % 360;
return bearing;
}
/**
* Вычисление точки на заданном расстоянии и азимуте
* @param {Array} startPoint - [широта, долгота] начальной точки
* @param {number} distance - Расстояние в метрах
* @param {number} bearing - Азимут в градусах
* @returns {Array} [широта, долгота] конечной точки
*/
static calculateDestinationPoint(startPoint, distance, bearing) {
const [lat1, lon1] = startPoint;
const bearingRad = bearing * this.DEG_TO_RAD;
const lat1Rad = lat1 * this.DEG_TO_RAD;
const lon1Rad = lon1 * this.DEG_TO_RAD;
const angularDistance = distance / this.EARTH_RADIUS;
const lat2Rad = Math.asin(
Math.sin(lat1Rad) * Math.cos(angularDistance) +
Math.cos(lat1Rad) * Math.sin(angularDistance) * Math.cos(bearingRad)
);
const lon2Rad = lon1Rad + Math.atan2(
Math.sin(bearingRad) * Math.sin(angularDistance) * Math.cos(lat1Rad),
Math.cos(angularDistance) - Math.sin(lat1Rad) * Math.sin(lat2Rad)
);
return [lat2Rad * this.RAD_TO_DEG, lon2Rad * this.RAD_TO_DEG];
}
/**
* Создание круга вокруг точки
* @param {Array} center - [широта, долгота] центра
* @param {number} radius - Радиус в метрах
* @param {number} segments - Количество сегментов (по умолчанию 64)
* @returns {Array} Массив точек круга
*/
static createCircle(center, radius, segments = 64) {
const points = [];
const [centerLat, centerLon] = center;
for (let i = 0; i <= segments; i++) {
const angle = (i / segments) * 2 * Math.PI;
const point = this.calculateDestinationPoint(center, radius, angle * this.RAD_TO_DEG);
points.push(point);
}
return points;
}
/**
* Создание прямоугольника вокруг точки
* @param {Array} center - [широта, долгота] центра
* @param {number} width - Ширина в метрах
* @param {number} height - Высота в метрах
* @param {number} rotation - Поворот в градусах (по умолчанию 0)
* @returns {Array} Массив точек прямоугольника
*/
static createRectangle(center, width, height, rotation = 0) {
const [centerLat, centerLon] = center;
const halfWidth = width / 2;
const halfHeight = height / 2;
// Создаем углы прямоугольника
const corners = [
[-halfHeight, -halfWidth],
[-halfHeight, halfWidth],
[halfHeight, halfWidth],
[halfHeight, -halfWidth]
];
const points = [];
corners.forEach(([dy, dx]) => {
// Поворачиваем точку
const cosRot = Math.cos(rotation * this.DEG_TO_RAD);
const sinRot = Math.sin(rotation * this.DEG_TO_RAD);
const rotatedX = dx * cosRot - dy * sinRot;
const rotatedY = dx * sinRot + dy * cosRot;
// Конвертируем в координаты
const lat = centerLat + (rotatedY / 111320);
const lon = centerLon + (rotatedX / (111320 * Math.cos(centerLat * this.DEG_TO_RAD)));
points.push([lat, lon]);
});
return points;
}
/**
* Проверка, находится ли точка внутри полигона
* @param {Array} point - [широта, долгота] проверяемой точки
* @param {Array} polygon - Массив точек полигона
* @returns {boolean} true, если точка внутри полигона
*/
static isPointInPolygon(point, polygon) {
const [lat, lon] = point;
let inside = false;
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const [latI, lonI] = polygon[i];
const [latJ, lonJ] = polygon[j];
if (((latI > lat) !== (latJ > lat)) &&
(lon < (lonJ - lonI) * (lat - latI) / (latJ - latI) + lonI)) {
inside = !inside;
}
}
return inside;
}
/**
* Вычисление площади полигона
* @param {Array} polygon - Массив точек полигона
* @returns {number} Площадь в квадратных метрах
*/
static calculatePolygonArea(polygon) {
if (polygon.length < 3) return 0;
let area = 0;
const n = polygon.length;
for (let i = 0; i < n; i++) {
const [latI, lonI] = polygon[i];
const [latJ, lonJ] = polygon[(i + 1) % n];
area += (lonJ - lonI) * (2 + Math.sin(latI * this.DEG_TO_RAD) + Math.sin(latJ * this.DEG_TO_RAD));
}
area = Math.abs(area) * this.EARTH_RADIUS * this.EARTH_RADIUS / 2;
return area;
}
/**
* Вычисление центра масс полигона
* @param {Array} polygon - Массив точек полигона
* @returns {Array} [широта, долгота] центра масс
*/
static calculatePolygonCentroid(polygon) {
if (polygon.length === 0) return null;
if (polygon.length === 1) return polygon[0];
let centerLat = 0;
let centerLon = 0;
polygon.forEach(([lat, lon]) => {
centerLat += lat;
centerLon += lon;
});
return [centerLat / polygon.length, centerLon / polygon.length];
}
/**
* Сглаживание полигона (алгоритм Рамера-Дугласа-Пекера)
* @param {Array} polygon - Массив точек полигона
* @param {number} tolerance - Допуск в метрах
* @returns {Array} Упрощенный полигон
*/
static simplifyPolygon(polygon, tolerance) {
if (polygon.length <= 2) return polygon;
const toleranceRad = tolerance / this.EARTH_RADIUS;
function perpendicularDistance(point, lineStart, lineEnd) {
const [lat, lon] = point;
const [lat1, lon1] = lineStart;
const [lat2, lon2] = lineEnd;
const lat1Rad = lat1 * this.DEG_TO_RAD;
const lon1Rad = lon1 * this.DEG_TO_RAD;
const lat2Rad = lat2 * this.DEG_TO_RAD;
const lon2Rad = lon2 * this.DEG_TO_RAD;
const latRad = lat * this.DEG_TO_RAD;
const lonRad = lon * this.DEG_TO_RAD;
const a = Math.sin(latRad - lat1Rad);
const b = Math.sin(lat2Rad - lat1Rad);
const c = Math.sin(lonRad - lon1Rad);
const d = Math.sin(lon2Rad - lon1Rad);
return Math.abs(a * d - b * c);
}
function douglasPeucker(points, start, end) {
if (end - start <= 1) return;
let maxDistance = 0;
let maxIndex = start;
for (let i = start + 1; i < end; i++) {
const distance = perpendicularDistance(points[i], points[start], points[end]);
if (distance > maxDistance) {
maxDistance = distance;
maxIndex = i;
}
}
if (maxDistance > toleranceRad) {
douglasPeucker(points, start, maxIndex);
douglasPeucker(points, maxIndex, end);
} else {
for (let i = start + 1; i < end; i++) {
points[i] = null;
}
}
}
const points = [...polygon];
douglasPeucker(points, 0, points.length - 1);
return points.filter(point => point !== null);
}
/**
* Конвертация координат в различные форматы
*/
static convertCoordinates(lat, lon, format = 'decimal') {
switch (format) {
case 'decimal':
return [lat, lon];
case 'dms': // градусы, минуты, секунды
const latDeg = Math.floor(Math.abs(lat));
const latMin = Math.floor((Math.abs(lat) - latDeg) * 60);
const latSec = ((Math.abs(lat) - latDeg - latMin / 60) * 3600).toFixed(2);
const lonDeg = Math.floor(Math.abs(lon));
const lonMin = Math.floor((Math.abs(lon) - lonDeg) * 60);
const lonSec = ((Math.abs(lon) - lonDeg - lonMin / 60) * 3600).toFixed(2);
return {
latitude: `${latDeg}° ${latMin}' ${latSec}" ${lat >= 0 ? 'N' : 'S'}`,
longitude: `${lonDeg}° ${lonMin}' ${lonSec}" ${lon >= 0 ? 'E' : 'W'}`
};
case 'dm': // градусы, минуты
const latDeg2 = Math.floor(Math.abs(lat));
const latMin2 = ((Math.abs(lat) - latDeg2) * 60).toFixed(4);
const lonDeg2 = Math.floor(Math.abs(lon));
const lonMin2 = ((Math.abs(lon) - lonDeg2) * 60).toFixed(4);
return {
latitude: `${latDeg2}° ${latMin2}' ${lat >= 0 ? 'N' : 'S'}`,
longitude: `${lonDeg2}° ${lonMin2}' ${lon >= 0 ? 'E' : 'W'}`
};
default:
return [lat, lon];
}
}
/**
* Форматирование расстояния в читаемом виде
* @param {number} distance - Расстояние в метрах
* @returns {string} Отформатированное расстояние
*/
static formatDistance(distance) {
if (distance < 1000) {
return `${Math.round(distance)} м`;
} else if (distance < 100000) {
return `${(distance / 1000).toFixed(1)} км`;
} else {
return `${Math.round(distance / 1000)} км`;
}
}
/**
* Форматирование площади в читаемом виде
* @param {number} area - Площадь в квадратных метрах
* @returns {string} Отформатированная площадь
*/
static formatArea(area) {
if (area < 10000) {
return `${Math.round(area)} м²`;
} else if (area < 1000000) {
return `${(area / 10000).toFixed(2)} га`;
} else {
return `${(area / 1000000).toFixed(2)} км²`;
}
}
}
// Экспорт утилит
if (typeof module !== 'undefined' && module.exports) {
module.exports = MapUtils;
} else {
window.MapUtils = MapUtils;
}
// Глобальные функции для удобства использования
window.calculateDistance = MapUtils.calculateDistance.bind(MapUtils);
window.calculateBearing = MapUtils.calculateBearing.bind(MapUtils);
window.createCircle = MapUtils.createCircle.bind(MapUtils);
window.createRectangle = MapUtils.createRectangle.bind(MapUtils);
window.isPointInPolygon = MapUtils.isPointInPolygon.bind(MapUtils);
window.calculatePolygonArea = MapUtils.calculatePolygonArea.bind(MapUtils);
window.formatDistance = MapUtils.formatDistance.bind(MapUtils);
window.formatArea = MapUtils.formatArea.bind(MapUtils);