@turf/great-circle 6.2.0-alpha.3 → 6.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/index.js +0 -0
- package/dist/js/index.js +1 -0
- package/package.json +11 -8
- package/dist/es/main.es.js +0 -287
package/dist/es/index.js
CHANGED
|
File without changes
|
package/dist/js/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/great-circle",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.5.0",
|
|
4
4
|
"description": "turf great-circle module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"type": "git",
|
|
18
18
|
"url": "git://github.com/Turfjs/turf.git"
|
|
19
19
|
},
|
|
20
|
+
"funding": "https://opencollective.com/turf",
|
|
20
21
|
"publishConfig": {
|
|
21
22
|
"access": "public"
|
|
22
23
|
},
|
|
@@ -30,8 +31,11 @@
|
|
|
30
31
|
"main": "dist/js/index.js",
|
|
31
32
|
"module": "dist/es/index.js",
|
|
32
33
|
"exports": {
|
|
33
|
-
"
|
|
34
|
-
"
|
|
34
|
+
"./package.json": "./package.json",
|
|
35
|
+
".": {
|
|
36
|
+
"import": "./dist/es/index.js",
|
|
37
|
+
"require": "./dist/js/index.js"
|
|
38
|
+
}
|
|
35
39
|
},
|
|
36
40
|
"types": "index.d.ts",
|
|
37
41
|
"sideEffects": false,
|
|
@@ -43,13 +47,12 @@
|
|
|
43
47
|
"bench": "node -r esm bench.js",
|
|
44
48
|
"build": "rollup -c ../../rollup.config.js && echo '{\"type\":\"module\"}' > dist/es/package.json",
|
|
45
49
|
"docs": "node ../../scripts/generate-readmes",
|
|
46
|
-
"posttest": "node -r esm ../../scripts/validate-es5-dependencies.js",
|
|
47
50
|
"test": "npm-run-all test:*",
|
|
48
51
|
"test:tape": "node -r esm test.js",
|
|
49
52
|
"test:types": "tsc --esModuleInterop --noEmit types.ts"
|
|
50
53
|
},
|
|
51
54
|
"devDependencies": {
|
|
52
|
-
"@turf/truncate": "^6.
|
|
55
|
+
"@turf/truncate": "^6.5.0",
|
|
53
56
|
"benchmark": "*",
|
|
54
57
|
"load-json-file": "*",
|
|
55
58
|
"npm-run-all": "*",
|
|
@@ -58,8 +61,8 @@
|
|
|
58
61
|
"write-json-file": "*"
|
|
59
62
|
},
|
|
60
63
|
"dependencies": {
|
|
61
|
-
"@turf/helpers": "^6.
|
|
62
|
-
"@turf/invariant": "^6.
|
|
64
|
+
"@turf/helpers": "^6.5.0",
|
|
65
|
+
"@turf/invariant": "^6.5.0"
|
|
63
66
|
},
|
|
64
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "5375941072b90d489389db22b43bfe809d5e451e"
|
|
65
68
|
}
|
package/dist/es/main.es.js
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
import { getCoord } from '@turf/invariant';
|
|
2
|
-
|
|
3
|
-
var D2R = Math.PI / 180;
|
|
4
|
-
var R2D = 180 / Math.PI;
|
|
5
|
-
|
|
6
|
-
var Coord = function (lon, lat) {
|
|
7
|
-
this.lon = lon;
|
|
8
|
-
this.lat = lat;
|
|
9
|
-
this.x = D2R * lon;
|
|
10
|
-
this.y = D2R * lat;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
Coord.prototype.view = function () {
|
|
14
|
-
return String(this.lon).slice(0, 4) + ',' + String(this.lat).slice(0, 4);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
Coord.prototype.antipode = function () {
|
|
18
|
-
var anti_lat = -1 * this.lat;
|
|
19
|
-
var anti_lon = (this.lon < 0) ? 180 + this.lon : (180 - this.lon) * -1;
|
|
20
|
-
return new Coord(anti_lon, anti_lat);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
var LineString = function () {
|
|
24
|
-
this.coords = [];
|
|
25
|
-
this.length = 0;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
LineString.prototype.move_to = function (coord) {
|
|
29
|
-
this.length++;
|
|
30
|
-
this.coords.push(coord);
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
var Arc = function (properties) {
|
|
34
|
-
this.properties = properties || {};
|
|
35
|
-
this.geometries = [];
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
Arc.prototype.json = function () {
|
|
39
|
-
if (this.geometries.length <= 0) {
|
|
40
|
-
return {'geometry': { 'type': 'LineString', 'coordinates': null },
|
|
41
|
-
'type': 'Feature', 'properties': this.properties
|
|
42
|
-
};
|
|
43
|
-
} else if (this.geometries.length === 1) {
|
|
44
|
-
return {'geometry': { 'type': 'LineString', 'coordinates': this.geometries[0].coords },
|
|
45
|
-
'type': 'Feature', 'properties': this.properties
|
|
46
|
-
};
|
|
47
|
-
} else {
|
|
48
|
-
var multiline = [];
|
|
49
|
-
for (var i = 0; i < this.geometries.length; i++) {
|
|
50
|
-
multiline.push(this.geometries[i].coords);
|
|
51
|
-
}
|
|
52
|
-
return {'geometry': { 'type': 'MultiLineString', 'coordinates': multiline },
|
|
53
|
-
'type': 'Feature', 'properties': this.properties
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// TODO - output proper multilinestring
|
|
59
|
-
Arc.prototype.wkt = function () {
|
|
60
|
-
var wkt_string = '';
|
|
61
|
-
var wkt = 'LINESTRING(';
|
|
62
|
-
var collect = function (c) { wkt += c[0] + ' ' + c[1] + ','; };
|
|
63
|
-
for (var i = 0; i < this.geometries.length; i++) {
|
|
64
|
-
if (this.geometries[i].coords.length === 0) {
|
|
65
|
-
return 'LINESTRING(empty)';
|
|
66
|
-
} else {
|
|
67
|
-
var coords = this.geometries[i].coords;
|
|
68
|
-
coords.forEach(collect);
|
|
69
|
-
wkt_string += wkt.substring(0, wkt.length - 1) + ')';
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return wkt_string;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/*
|
|
76
|
-
* http://en.wikipedia.org/wiki/Great-circle_distance
|
|
77
|
-
*
|
|
78
|
-
*/
|
|
79
|
-
var GreatCircle = function (start, end, properties) {
|
|
80
|
-
if (!start || start.x === undefined || start.y === undefined) {
|
|
81
|
-
throw new Error('GreatCircle constructor expects two args: start and end objects with x and y properties');
|
|
82
|
-
}
|
|
83
|
-
if (!end || end.x === undefined || end.y === undefined) {
|
|
84
|
-
throw new Error('GreatCircle constructor expects two args: start and end objects with x and y properties');
|
|
85
|
-
}
|
|
86
|
-
this.start = new Coord(start.x, start.y);
|
|
87
|
-
this.end = new Coord(end.x, end.y);
|
|
88
|
-
this.properties = properties || {};
|
|
89
|
-
|
|
90
|
-
var w = this.start.x - this.end.x;
|
|
91
|
-
var h = this.start.y - this.end.y;
|
|
92
|
-
var z = Math.pow(Math.sin(h / 2.0), 2) +
|
|
93
|
-
Math.cos(this.start.y) *
|
|
94
|
-
Math.cos(this.end.y) *
|
|
95
|
-
Math.pow(Math.sin(w / 2.0), 2);
|
|
96
|
-
this.g = 2.0 * Math.asin(Math.sqrt(z));
|
|
97
|
-
|
|
98
|
-
if (this.g === Math.PI) {
|
|
99
|
-
throw new Error('it appears ' + start.view() + ' and ' + end.view() + ' are \'antipodal\', e.g diametrically opposite, thus there is no single route but rather infinite');
|
|
100
|
-
} else if (isNaN(this.g)) {
|
|
101
|
-
throw new Error('could not calculate great circle between ' + start + ' and ' + end);
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
/*
|
|
106
|
-
* http://williams.best.vwh.net/avform.htm#Intermediate
|
|
107
|
-
*/
|
|
108
|
-
GreatCircle.prototype.interpolate = function (f) {
|
|
109
|
-
var A = Math.sin((1 - f) * this.g) / Math.sin(this.g);
|
|
110
|
-
var B = Math.sin(f * this.g) / Math.sin(this.g);
|
|
111
|
-
var x = A * Math.cos(this.start.y) * Math.cos(this.start.x) + B * Math.cos(this.end.y) * Math.cos(this.end.x);
|
|
112
|
-
var y = A * Math.cos(this.start.y) * Math.sin(this.start.x) + B * Math.cos(this.end.y) * Math.sin(this.end.x);
|
|
113
|
-
var z = A * Math.sin(this.start.y) + B * Math.sin(this.end.y);
|
|
114
|
-
var lat = R2D * Math.atan2(z, Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)));
|
|
115
|
-
var lon = R2D * Math.atan2(y, x);
|
|
116
|
-
return [lon, lat];
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
/*
|
|
122
|
-
* Generate points along the great circle
|
|
123
|
-
*/
|
|
124
|
-
GreatCircle.prototype.Arc = function (npoints, options) {
|
|
125
|
-
var first_pass = [];
|
|
126
|
-
if (!npoints || npoints <= 2) {
|
|
127
|
-
first_pass.push([this.start.lon, this.start.lat]);
|
|
128
|
-
first_pass.push([this.end.lon, this.end.lat]);
|
|
129
|
-
} else {
|
|
130
|
-
var delta = 1.0 / (npoints - 1);
|
|
131
|
-
for (var i = 0; i < npoints; ++i) {
|
|
132
|
-
var step = delta * i;
|
|
133
|
-
var pair = this.interpolate(step);
|
|
134
|
-
first_pass.push(pair);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
/* partial port of dateline handling from:
|
|
138
|
-
gdal/ogr/ogrgeometryfactory.cpp
|
|
139
|
-
|
|
140
|
-
TODO - does not handle all wrapping scenarios yet
|
|
141
|
-
*/
|
|
142
|
-
var bHasBigDiff = false;
|
|
143
|
-
var dfMaxSmallDiffLong = 0;
|
|
144
|
-
// from http://www.gdal.org/ogr2ogr.html
|
|
145
|
-
// -datelineoffset:
|
|
146
|
-
// (starting with GDAL 1.10) offset from dateline in degrees (default long. = +/- 10deg, geometries within 170deg to -170deg will be splited)
|
|
147
|
-
var dfDateLineOffset = options && options.offset ? options.offset : 10;
|
|
148
|
-
var dfLeftBorderX = 180 - dfDateLineOffset;
|
|
149
|
-
var dfRightBorderX = -180 + dfDateLineOffset;
|
|
150
|
-
var dfDiffSpace = 360 - dfDateLineOffset;
|
|
151
|
-
|
|
152
|
-
// https://github.com/OSGeo/gdal/blob/7bfb9c452a59aac958bff0c8386b891edf8154ca/gdal/ogr/ogrgeometryfactory.cpp#L2342
|
|
153
|
-
for (var j = 1; j < first_pass.length; ++j) {
|
|
154
|
-
var dfPrevX = first_pass[j - 1][0];
|
|
155
|
-
var dfX = first_pass[j][0];
|
|
156
|
-
var dfDiffLong = Math.abs(dfX - dfPrevX);
|
|
157
|
-
if (dfDiffLong > dfDiffSpace &&
|
|
158
|
-
((dfX > dfLeftBorderX && dfPrevX < dfRightBorderX) || (dfPrevX > dfLeftBorderX && dfX < dfRightBorderX))) {
|
|
159
|
-
bHasBigDiff = true;
|
|
160
|
-
} else if (dfDiffLong > dfMaxSmallDiffLong) {
|
|
161
|
-
dfMaxSmallDiffLong = dfDiffLong;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
var poMulti = [];
|
|
166
|
-
if (bHasBigDiff && dfMaxSmallDiffLong < dfDateLineOffset) {
|
|
167
|
-
var poNewLS = [];
|
|
168
|
-
poMulti.push(poNewLS);
|
|
169
|
-
for (var k = 0; k < first_pass.length; ++k) {
|
|
170
|
-
var dfX0 = parseFloat(first_pass[k][0]);
|
|
171
|
-
if (k > 0 && Math.abs(dfX0 - first_pass[k - 1][0]) > dfDiffSpace) {
|
|
172
|
-
var dfX1 = parseFloat(first_pass[k - 1][0]);
|
|
173
|
-
var dfY1 = parseFloat(first_pass[k - 1][1]);
|
|
174
|
-
var dfX2 = parseFloat(first_pass[k][0]);
|
|
175
|
-
var dfY2 = parseFloat(first_pass[k][1]);
|
|
176
|
-
if (dfX1 > -180 && dfX1 < dfRightBorderX && dfX2 === 180 &&
|
|
177
|
-
k + 1 < first_pass.length &&
|
|
178
|
-
first_pass[k - 1][0] > -180 && first_pass[k - 1][0] < dfRightBorderX) {
|
|
179
|
-
poNewLS.push([-180, first_pass[k][1]]);
|
|
180
|
-
k++;
|
|
181
|
-
poNewLS.push([first_pass[k][0], first_pass[k][1]]);
|
|
182
|
-
continue;
|
|
183
|
-
} else if (dfX1 > dfLeftBorderX && dfX1 < 180 && dfX2 === -180 &&
|
|
184
|
-
k + 1 < first_pass.length &&
|
|
185
|
-
first_pass[k - 1][0] > dfLeftBorderX && first_pass[k - 1][0] < 180) {
|
|
186
|
-
poNewLS.push([180, first_pass[k][1]]);
|
|
187
|
-
k++;
|
|
188
|
-
poNewLS.push([first_pass[k][0], first_pass[k][1]]);
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (dfX1 < dfRightBorderX && dfX2 > dfLeftBorderX) {
|
|
193
|
-
// swap dfX1, dfX2
|
|
194
|
-
var tmpX = dfX1;
|
|
195
|
-
dfX1 = dfX2;
|
|
196
|
-
dfX2 = tmpX;
|
|
197
|
-
// swap dfY1, dfY2
|
|
198
|
-
var tmpY = dfY1;
|
|
199
|
-
dfY1 = dfY2;
|
|
200
|
-
dfY2 = tmpY;
|
|
201
|
-
}
|
|
202
|
-
if (dfX1 > dfLeftBorderX && dfX2 < dfRightBorderX) {
|
|
203
|
-
dfX2 += 360;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (dfX1 <= 180 && dfX2 >= 180 && dfX1 < dfX2) {
|
|
207
|
-
var dfRatio = (180 - dfX1) / (dfX2 - dfX1);
|
|
208
|
-
var dfY = dfRatio * dfY2 + (1 - dfRatio) * dfY1;
|
|
209
|
-
poNewLS.push([first_pass[k - 1][0] > dfLeftBorderX ? 180 : -180, dfY]);
|
|
210
|
-
poNewLS = [];
|
|
211
|
-
poNewLS.push([first_pass[k - 1][0] > dfLeftBorderX ? -180 : 180, dfY]);
|
|
212
|
-
poMulti.push(poNewLS);
|
|
213
|
-
} else {
|
|
214
|
-
poNewLS = [];
|
|
215
|
-
poMulti.push(poNewLS);
|
|
216
|
-
}
|
|
217
|
-
poNewLS.push([dfX0, first_pass[k][1]]);
|
|
218
|
-
} else {
|
|
219
|
-
poNewLS.push([first_pass[k][0], first_pass[k][1]]);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
} else {
|
|
223
|
-
// add normally
|
|
224
|
-
var poNewLS0 = [];
|
|
225
|
-
poMulti.push(poNewLS0);
|
|
226
|
-
for (var l = 0; l < first_pass.length; ++l) {
|
|
227
|
-
poNewLS0.push([first_pass[l][0], first_pass[l][1]]);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
var arc = new Arc(this.properties);
|
|
232
|
-
for (var m = 0; m < poMulti.length; ++m) {
|
|
233
|
-
var line = new LineString();
|
|
234
|
-
arc.geometries.push(line);
|
|
235
|
-
var points = poMulti[m];
|
|
236
|
-
for (var j0 = 0; j0 < points.length; ++j0) {
|
|
237
|
-
line.move_to(points[j0]);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
return arc;
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Calculate great circles routes as {@link LineString}
|
|
245
|
-
*
|
|
246
|
-
* @name greatCircle
|
|
247
|
-
* @param {Coord} start source point feature
|
|
248
|
-
* @param {Coord} end destination point feature
|
|
249
|
-
* @param {Object} [options={}] Optional parameters
|
|
250
|
-
* @param {Object} [options.properties={}] line feature properties
|
|
251
|
-
* @param {number} [options.npoints=100] number of points
|
|
252
|
-
* @param {number} [options.offset=10] offset controls the likelyhood that lines will
|
|
253
|
-
* be split which cross the dateline. The higher the number the more likely.
|
|
254
|
-
* @returns {Feature<LineString>} great circle line feature
|
|
255
|
-
* @example
|
|
256
|
-
* var start = turf.point([-122, 48]);
|
|
257
|
-
* var end = turf.point([-77, 39]);
|
|
258
|
-
*
|
|
259
|
-
* var greatCircle = turf.greatCircle(start, end, {'name': 'Seattle to DC'});
|
|
260
|
-
*
|
|
261
|
-
* //addToMap
|
|
262
|
-
* var addToMap = [start, end, greatCircle]
|
|
263
|
-
*/
|
|
264
|
-
function greatCircle(start, end, options) {
|
|
265
|
-
// Optional parameters
|
|
266
|
-
options = options || {};
|
|
267
|
-
if (typeof options !== 'object') throw new Error('options is invalid');
|
|
268
|
-
var properties = options.properties;
|
|
269
|
-
var npoints = options.npoints;
|
|
270
|
-
var offset = options.offset;
|
|
271
|
-
|
|
272
|
-
start = getCoord(start);
|
|
273
|
-
end = getCoord(end);
|
|
274
|
-
properties = properties || {};
|
|
275
|
-
npoints = npoints || 100;
|
|
276
|
-
offset = offset || 10;
|
|
277
|
-
|
|
278
|
-
var generator = new GreatCircle({x: start[0], y: start[1]}, {x: end[0], y: end[1]}, properties);
|
|
279
|
-
|
|
280
|
-
/* eslint-disable */
|
|
281
|
-
var line = generator.Arc(npoints, {offset: offset});
|
|
282
|
-
/* eslint-enable */
|
|
283
|
-
|
|
284
|
-
return line.json();
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
export default greatCircle;
|