@turf/projection 7.0.0-alpha.2 → 7.1.0-alpha.7
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/README.md +14 -17
- package/dist/cjs/index.cjs +61 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +41 -0
- package/dist/{js → esm}/index.d.ts +7 -4
- package/dist/esm/index.js +61 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +36 -31
- package/dist/es/index.js +0 -134
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -139
package/README.md
CHANGED
|
@@ -8,10 +8,10 @@ Converts a WGS84 GeoJSON object into Mercator (EPSG:900913) projection
|
|
|
8
8
|
|
|
9
9
|
### Parameters
|
|
10
10
|
|
|
11
|
-
* `geojson` **([GeoJSON][1] | Position)** WGS84 GeoJSON object
|
|
12
|
-
* `options` **[Object][
|
|
11
|
+
* `geojson` **([GeoJSON][1] | [Position][2])** WGS84 GeoJSON object
|
|
12
|
+
* `options` **[Object][3]?** Optional parameters
|
|
13
13
|
|
|
14
|
-
* `options.mutate` **[boolean][
|
|
14
|
+
* `options.mutate` **[boolean][4]** allows GeoJSON input to be mutated (significant performance increase if true) (optional, default `false`)
|
|
15
15
|
|
|
16
16
|
### Examples
|
|
17
17
|
|
|
@@ -31,10 +31,10 @@ Converts a Mercator (EPSG:900913) GeoJSON object into WGS84 projection
|
|
|
31
31
|
|
|
32
32
|
### Parameters
|
|
33
33
|
|
|
34
|
-
* `geojson` **([GeoJSON][1] | Position)** Mercator GeoJSON object
|
|
35
|
-
* `options` **[Object][
|
|
34
|
+
* `geojson` **([GeoJSON][1] | [Position][2])** Mercator GeoJSON object
|
|
35
|
+
* `options` **[Object][3]?** Optional parameters
|
|
36
36
|
|
|
37
|
-
* `options.mutate` **[boolean][
|
|
37
|
+
* `options.mutate` **[boolean][4]** allows GeoJSON input to be mutated (significant performance increase if true) (optional, default `false`)
|
|
38
38
|
|
|
39
39
|
### Examples
|
|
40
40
|
|
|
@@ -50,30 +50,27 @@ Returns **[GeoJSON][1]** Projected GeoJSON
|
|
|
50
50
|
|
|
51
51
|
[1]: https://tools.ietf.org/html/rfc7946#section-3
|
|
52
52
|
|
|
53
|
-
[2]: https://developer.mozilla.org/docs/Web/
|
|
53
|
+
[2]: https://developer.mozilla.org/docs/Web/API/Position
|
|
54
54
|
|
|
55
|
-
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/
|
|
55
|
+
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
58
|
+
|
|
59
|
+
<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
|
|
60
60
|
|
|
61
61
|
---
|
|
62
62
|
|
|
63
|
-
This module is part of the [Turfjs project](
|
|
64
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
65
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
66
|
-
PRs and issues.
|
|
63
|
+
This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
|
|
67
64
|
|
|
68
65
|
### Installation
|
|
69
66
|
|
|
70
|
-
Install this module individually:
|
|
67
|
+
Install this single module individually:
|
|
71
68
|
|
|
72
69
|
```sh
|
|
73
70
|
$ npm install @turf/projection
|
|
74
71
|
```
|
|
75
72
|
|
|
76
|
-
Or install the
|
|
73
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
77
74
|
|
|
78
75
|
```sh
|
|
79
76
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
|
|
2
|
+
var _meta = require('@turf/meta');
|
|
3
|
+
var _helpers = require('@turf/helpers');
|
|
4
|
+
var _clone = require('@turf/clone');
|
|
5
|
+
function toMercator(geojson, options = {}) {
|
|
6
|
+
return convert(geojson, "mercator", options);
|
|
7
|
+
}
|
|
8
|
+
function toWgs84(geojson, options = {}) {
|
|
9
|
+
return convert(geojson, "wgs84", options);
|
|
10
|
+
}
|
|
11
|
+
function convert(geojson, projection, options = {}) {
|
|
12
|
+
options = options || {};
|
|
13
|
+
var mutate = options.mutate;
|
|
14
|
+
if (!geojson)
|
|
15
|
+
throw new Error("geojson is required");
|
|
16
|
+
if (Array.isArray(geojson) && _helpers.isNumber.call(void 0, geojson[0]))
|
|
17
|
+
geojson = projection === "mercator" ? convertToMercator(geojson) : convertToWgs84(geojson);
|
|
18
|
+
else {
|
|
19
|
+
if (mutate !== true)
|
|
20
|
+
geojson = _clone.clone.call(void 0, geojson);
|
|
21
|
+
_meta.coordEach.call(void 0, geojson, function(coord) {
|
|
22
|
+
var newCoord = projection === "mercator" ? convertToMercator(coord) : convertToWgs84(coord);
|
|
23
|
+
coord[0] = newCoord[0];
|
|
24
|
+
coord[1] = newCoord[1];
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return geojson;
|
|
28
|
+
}
|
|
29
|
+
function convertToMercator(lonLat) {
|
|
30
|
+
var D2R = Math.PI / 180, A = 6378137, MAXEXTENT = 20037508342789244e-9;
|
|
31
|
+
var adjusted = Math.abs(lonLat[0]) <= 180 ? lonLat[0] : lonLat[0] - sign(lonLat[0]) * 360;
|
|
32
|
+
var xy = [
|
|
33
|
+
A * adjusted * D2R,
|
|
34
|
+
A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R))
|
|
35
|
+
];
|
|
36
|
+
if (xy[0] > MAXEXTENT)
|
|
37
|
+
xy[0] = MAXEXTENT;
|
|
38
|
+
if (xy[0] < -MAXEXTENT)
|
|
39
|
+
xy[0] = -MAXEXTENT;
|
|
40
|
+
if (xy[1] > MAXEXTENT)
|
|
41
|
+
xy[1] = MAXEXTENT;
|
|
42
|
+
if (xy[1] < -MAXEXTENT)
|
|
43
|
+
xy[1] = -MAXEXTENT;
|
|
44
|
+
return xy;
|
|
45
|
+
}
|
|
46
|
+
function convertToWgs84(xy) {
|
|
47
|
+
var R2D = 180 / Math.PI;
|
|
48
|
+
var A = 6378137;
|
|
49
|
+
return [
|
|
50
|
+
xy[0] * R2D / A,
|
|
51
|
+
(Math.PI * 0.5 - 2 * Math.atan(Math.exp(-xy[1] / A))) * R2D
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
function sign(x) {
|
|
55
|
+
return x < 0 ? -1 : x > 0 ? 1 : 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
exports.toMercator = toMercator; exports.toWgs84 = toWgs84;
|
|
61
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";AACA,SAAS,iBAAiB;AAC1B,SAAqB,gBAAgB;AACrC,SAAS,aAAa;AAiBtB,SAAS,WACP,SACA,UAAgC,CAAC,GAC9B;AACH,SAAO,QAAQ,SAAS,YAAY,OAAO;AAC7C;AAiBA,SAAS,QACP,SACA,UAAgC,CAAC,GAC9B;AACH,SAAO,QAAQ,SAAS,SAAS,OAAO;AAC1C;AAYA,SAAS,QACP,SACA,YACA,UAAgC,CAAC,GAC5B;AAEL,YAAU,WAAW,CAAC;AACtB,MAAI,SAAS,QAAQ;AAGrB,MAAI,CAAC;AAAS,UAAM,IAAI,MAAM,qBAAqB;AAGnD,MAAI,MAAM,QAAQ,OAAO,KAAK,SAAS,QAAQ,CAAC,CAAC;AAC/C,cACE,eAAe,aACX,kBAAkB,OAAO,IACzB,eAAe,OAAO;AAAA,OAEzB;AAEH,QAAI,WAAW;AAAM,gBAAU,MAAM,OAAO;AAE5C,cAAU,SAAS,SAAU,OAAO;AAClC,UAAI,WACF,eAAe,aACX,kBAAkB,KAAK,IACvB,eAAe,KAAK;AAC1B,YAAM,CAAC,IAAI,SAAS,CAAC;AACrB,YAAM,CAAC,IAAI,SAAS,CAAC;AAAA,IACvB,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAUA,SAAS,kBAAkB,QAAkB;AAC3C,MAAI,MAAM,KAAK,KAAK,KAElB,IAAI,SACJ,YAAY;AAId,MAAI,WACF,KAAK,IAAI,OAAO,CAAC,CAAC,KAAK,MAAM,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,IAAI;AACzE,MAAI,KAAK;AAAA,IACP,IAAI,WAAW;AAAA,IACf,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,KAAK,OAAO,MAAM,OAAO,CAAC,IAAI,GAAG,CAAC;AAAA,EAC/D;AAGA,MAAI,GAAG,CAAC,IAAI;AAAW,OAAG,CAAC,IAAI;AAC/B,MAAI,GAAG,CAAC,IAAI,CAAC;AAAW,OAAG,CAAC,IAAI,CAAC;AACjC,MAAI,GAAG,CAAC,IAAI;AAAW,OAAG,CAAC,IAAI;AAC/B,MAAI,GAAG,CAAC,IAAI,CAAC;AAAW,OAAG,CAAC,IAAI,CAAC;AAEjC,SAAO;AACT;AAUA,SAAS,eAAe,IAAc;AAEpC,MAAI,MAAM,MAAM,KAAK;AACrB,MAAI,IAAI;AAER,SAAO;AAAA,IACJ,GAAG,CAAC,IAAI,MAAO;AAAA,KACf,KAAK,KAAK,MAAM,IAAM,KAAK,KAAK,KAAK,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK;AAAA,EAC5D;AACF;AASA,SAAS,KAAK,GAAW;AACvB,SAAO,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI;AAClC","sourcesContent":["import { Position } from \"geojson\";\nimport { coordEach } from \"@turf/meta\";\nimport { AllGeoJSON, isNumber } from \"@turf/helpers\";\nimport { clone } from \"@turf/clone\";\n\n/**\n * Converts a WGS84 GeoJSON object into Mercator (EPSG:900913) projection\n *\n * @name toMercator\n * @param {GeoJSON|Position} geojson WGS84 GeoJSON object\n * @param {Object} [options] Optional parameters\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {GeoJSON} Projected GeoJSON\n * @example\n * var pt = turf.point([-71,41]);\n * var converted = turf.toMercator(pt);\n *\n * //addToMap\n * var addToMap = [pt, converted];\n */\nfunction toMercator<G = AllGeoJSON | Position>(\n geojson: G,\n options: { mutate?: boolean } = {}\n): G {\n return convert(geojson, \"mercator\", options);\n}\n\n/**\n * Converts a Mercator (EPSG:900913) GeoJSON object into WGS84 projection\n *\n * @name toWgs84\n * @param {GeoJSON|Position} geojson Mercator GeoJSON object\n * @param {Object} [options] Optional parameters\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {GeoJSON} Projected GeoJSON\n * @example\n * var pt = turf.point([-7903683.846322424, 5012341.663847514]);\n * var converted = turf.toWgs84(pt);\n *\n * //addToMap\n * var addToMap = [pt, converted];\n */\nfunction toWgs84<G = AllGeoJSON | Position>(\n geojson: G,\n options: { mutate?: boolean } = {}\n): G {\n return convert(geojson, \"wgs84\", options);\n}\n\n/**\n * Converts a GeoJSON coordinates to the defined `projection`\n *\n * @private\n * @param {GeoJSON} geojson GeoJSON Feature or Geometry\n * @param {string} projection defines the projection system to convert the coordinates to\n * @param {Object} [options] Optional parameters\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {GeoJSON} Converted GeoJSON\n */\nfunction convert(\n geojson: any,\n projection: string,\n options: { mutate?: boolean } = {}\n): any {\n // Optional parameters\n options = options || {};\n var mutate = options.mutate;\n\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n\n // Handle Position\n if (Array.isArray(geojson) && isNumber(geojson[0]))\n geojson =\n projection === \"mercator\"\n ? convertToMercator(geojson)\n : convertToWgs84(geojson);\n // Handle GeoJSON\n else {\n // Handle possible data mutation\n if (mutate !== true) geojson = clone(geojson);\n\n coordEach(geojson, function (coord) {\n var newCoord =\n projection === \"mercator\"\n ? convertToMercator(coord)\n : convertToWgs84(coord);\n coord[0] = newCoord[0];\n coord[1] = newCoord[1];\n });\n }\n return geojson;\n}\n\n/**\n * Convert lon/lat values to 900913 x/y.\n * (from https://github.com/mapbox/sphericalmercator)\n *\n * @private\n * @param {Array<number>} lonLat WGS84 point\n * @returns {Array<number>} Mercator [x, y] point\n */\nfunction convertToMercator(lonLat: number[]) {\n var D2R = Math.PI / 180,\n // 900913 properties\n A = 6378137.0,\n MAXEXTENT = 20037508.342789244;\n\n // compensate longitudes passing the 180th meridian\n // from https://github.com/proj4js/proj4js/blob/master/lib/common/adjust_lon.js\n var adjusted =\n Math.abs(lonLat[0]) <= 180 ? lonLat[0] : lonLat[0] - sign(lonLat[0]) * 360;\n var xy = [\n A * adjusted * D2R,\n A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R)),\n ];\n\n // if xy value is beyond maxextent (e.g. poles), return maxextent\n if (xy[0] > MAXEXTENT) xy[0] = MAXEXTENT;\n if (xy[0] < -MAXEXTENT) xy[0] = -MAXEXTENT;\n if (xy[1] > MAXEXTENT) xy[1] = MAXEXTENT;\n if (xy[1] < -MAXEXTENT) xy[1] = -MAXEXTENT;\n\n return xy;\n}\n\n/**\n * Convert 900913 x/y values to lon/lat.\n * (from https://github.com/mapbox/sphericalmercator)\n *\n * @private\n * @param {Array<number>} xy Mercator [x, y] point\n * @returns {Array<number>} WGS84 [lon, lat] point\n */\nfunction convertToWgs84(xy: number[]) {\n // 900913 properties.\n var R2D = 180 / Math.PI;\n var A = 6378137.0;\n\n return [\n (xy[0] * R2D) / A,\n (Math.PI * 0.5 - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D,\n ];\n}\n\n/**\n * Returns the sign of the input, or zero\n *\n * @private\n * @param {number} x input\n * @returns {number} -1|0|1 output\n */\nfunction sign(x: number) {\n return x < 0 ? -1 : x > 0 ? 1 : 0;\n}\n\nexport { toMercator, toWgs84 };\n"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Position } from 'geojson';
|
|
2
|
+
import { AllGeoJSON } from '@turf/helpers';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts a WGS84 GeoJSON object into Mercator (EPSG:900913) projection
|
|
6
|
+
*
|
|
7
|
+
* @name toMercator
|
|
8
|
+
* @param {GeoJSON|Position} geojson WGS84 GeoJSON object
|
|
9
|
+
* @param {Object} [options] Optional parameters
|
|
10
|
+
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
11
|
+
* @returns {GeoJSON} Projected GeoJSON
|
|
12
|
+
* @example
|
|
13
|
+
* var pt = turf.point([-71,41]);
|
|
14
|
+
* var converted = turf.toMercator(pt);
|
|
15
|
+
*
|
|
16
|
+
* //addToMap
|
|
17
|
+
* var addToMap = [pt, converted];
|
|
18
|
+
*/
|
|
19
|
+
declare function toMercator<G = AllGeoJSON | Position>(geojson: G, options?: {
|
|
20
|
+
mutate?: boolean;
|
|
21
|
+
}): G;
|
|
22
|
+
/**
|
|
23
|
+
* Converts a Mercator (EPSG:900913) GeoJSON object into WGS84 projection
|
|
24
|
+
*
|
|
25
|
+
* @name toWgs84
|
|
26
|
+
* @param {GeoJSON|Position} geojson Mercator GeoJSON object
|
|
27
|
+
* @param {Object} [options] Optional parameters
|
|
28
|
+
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
29
|
+
* @returns {GeoJSON} Projected GeoJSON
|
|
30
|
+
* @example
|
|
31
|
+
* var pt = turf.point([-7903683.846322424, 5012341.663847514]);
|
|
32
|
+
* var converted = turf.toWgs84(pt);
|
|
33
|
+
*
|
|
34
|
+
* //addToMap
|
|
35
|
+
* var addToMap = [pt, converted];
|
|
36
|
+
*/
|
|
37
|
+
declare function toWgs84<G = AllGeoJSON | Position>(geojson: G, options?: {
|
|
38
|
+
mutate?: boolean;
|
|
39
|
+
}): G;
|
|
40
|
+
|
|
41
|
+
export { toMercator, toWgs84 };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Position } from
|
|
2
|
-
import { AllGeoJSON } from
|
|
1
|
+
import { Position } from 'geojson';
|
|
2
|
+
import { AllGeoJSON } from '@turf/helpers';
|
|
3
|
+
|
|
3
4
|
/**
|
|
4
5
|
* Converts a WGS84 GeoJSON object into Mercator (EPSG:900913) projection
|
|
5
6
|
*
|
|
@@ -15,7 +16,7 @@ import { AllGeoJSON } from "@turf/helpers";
|
|
|
15
16
|
* //addToMap
|
|
16
17
|
* var addToMap = [pt, converted];
|
|
17
18
|
*/
|
|
18
|
-
|
|
19
|
+
declare function toMercator<G = AllGeoJSON | Position>(geojson: G, options?: {
|
|
19
20
|
mutate?: boolean;
|
|
20
21
|
}): G;
|
|
21
22
|
/**
|
|
@@ -33,6 +34,8 @@ export declare function toMercator<G = AllGeoJSON | Position>(geojson: G, option
|
|
|
33
34
|
* //addToMap
|
|
34
35
|
* var addToMap = [pt, converted];
|
|
35
36
|
*/
|
|
36
|
-
|
|
37
|
+
declare function toWgs84<G = AllGeoJSON | Position>(geojson: G, options?: {
|
|
37
38
|
mutate?: boolean;
|
|
38
39
|
}): G;
|
|
40
|
+
|
|
41
|
+
export { toMercator, toWgs84 };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { coordEach } from "@turf/meta";
|
|
3
|
+
import { isNumber } from "@turf/helpers";
|
|
4
|
+
import { clone } from "@turf/clone";
|
|
5
|
+
function toMercator(geojson, options = {}) {
|
|
6
|
+
return convert(geojson, "mercator", options);
|
|
7
|
+
}
|
|
8
|
+
function toWgs84(geojson, options = {}) {
|
|
9
|
+
return convert(geojson, "wgs84", options);
|
|
10
|
+
}
|
|
11
|
+
function convert(geojson, projection, options = {}) {
|
|
12
|
+
options = options || {};
|
|
13
|
+
var mutate = options.mutate;
|
|
14
|
+
if (!geojson)
|
|
15
|
+
throw new Error("geojson is required");
|
|
16
|
+
if (Array.isArray(geojson) && isNumber(geojson[0]))
|
|
17
|
+
geojson = projection === "mercator" ? convertToMercator(geojson) : convertToWgs84(geojson);
|
|
18
|
+
else {
|
|
19
|
+
if (mutate !== true)
|
|
20
|
+
geojson = clone(geojson);
|
|
21
|
+
coordEach(geojson, function(coord) {
|
|
22
|
+
var newCoord = projection === "mercator" ? convertToMercator(coord) : convertToWgs84(coord);
|
|
23
|
+
coord[0] = newCoord[0];
|
|
24
|
+
coord[1] = newCoord[1];
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return geojson;
|
|
28
|
+
}
|
|
29
|
+
function convertToMercator(lonLat) {
|
|
30
|
+
var D2R = Math.PI / 180, A = 6378137, MAXEXTENT = 20037508342789244e-9;
|
|
31
|
+
var adjusted = Math.abs(lonLat[0]) <= 180 ? lonLat[0] : lonLat[0] - sign(lonLat[0]) * 360;
|
|
32
|
+
var xy = [
|
|
33
|
+
A * adjusted * D2R,
|
|
34
|
+
A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R))
|
|
35
|
+
];
|
|
36
|
+
if (xy[0] > MAXEXTENT)
|
|
37
|
+
xy[0] = MAXEXTENT;
|
|
38
|
+
if (xy[0] < -MAXEXTENT)
|
|
39
|
+
xy[0] = -MAXEXTENT;
|
|
40
|
+
if (xy[1] > MAXEXTENT)
|
|
41
|
+
xy[1] = MAXEXTENT;
|
|
42
|
+
if (xy[1] < -MAXEXTENT)
|
|
43
|
+
xy[1] = -MAXEXTENT;
|
|
44
|
+
return xy;
|
|
45
|
+
}
|
|
46
|
+
function convertToWgs84(xy) {
|
|
47
|
+
var R2D = 180 / Math.PI;
|
|
48
|
+
var A = 6378137;
|
|
49
|
+
return [
|
|
50
|
+
xy[0] * R2D / A,
|
|
51
|
+
(Math.PI * 0.5 - 2 * Math.atan(Math.exp(-xy[1] / A))) * R2D
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
function sign(x) {
|
|
55
|
+
return x < 0 ? -1 : x > 0 ? 1 : 0;
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
toMercator,
|
|
59
|
+
toWgs84
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { Position } from \"geojson\";\nimport { coordEach } from \"@turf/meta\";\nimport { AllGeoJSON, isNumber } from \"@turf/helpers\";\nimport { clone } from \"@turf/clone\";\n\n/**\n * Converts a WGS84 GeoJSON object into Mercator (EPSG:900913) projection\n *\n * @name toMercator\n * @param {GeoJSON|Position} geojson WGS84 GeoJSON object\n * @param {Object} [options] Optional parameters\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {GeoJSON} Projected GeoJSON\n * @example\n * var pt = turf.point([-71,41]);\n * var converted = turf.toMercator(pt);\n *\n * //addToMap\n * var addToMap = [pt, converted];\n */\nfunction toMercator<G = AllGeoJSON | Position>(\n geojson: G,\n options: { mutate?: boolean } = {}\n): G {\n return convert(geojson, \"mercator\", options);\n}\n\n/**\n * Converts a Mercator (EPSG:900913) GeoJSON object into WGS84 projection\n *\n * @name toWgs84\n * @param {GeoJSON|Position} geojson Mercator GeoJSON object\n * @param {Object} [options] Optional parameters\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {GeoJSON} Projected GeoJSON\n * @example\n * var pt = turf.point([-7903683.846322424, 5012341.663847514]);\n * var converted = turf.toWgs84(pt);\n *\n * //addToMap\n * var addToMap = [pt, converted];\n */\nfunction toWgs84<G = AllGeoJSON | Position>(\n geojson: G,\n options: { mutate?: boolean } = {}\n): G {\n return convert(geojson, \"wgs84\", options);\n}\n\n/**\n * Converts a GeoJSON coordinates to the defined `projection`\n *\n * @private\n * @param {GeoJSON} geojson GeoJSON Feature or Geometry\n * @param {string} projection defines the projection system to convert the coordinates to\n * @param {Object} [options] Optional parameters\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {GeoJSON} Converted GeoJSON\n */\nfunction convert(\n geojson: any,\n projection: string,\n options: { mutate?: boolean } = {}\n): any {\n // Optional parameters\n options = options || {};\n var mutate = options.mutate;\n\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n\n // Handle Position\n if (Array.isArray(geojson) && isNumber(geojson[0]))\n geojson =\n projection === \"mercator\"\n ? convertToMercator(geojson)\n : convertToWgs84(geojson);\n // Handle GeoJSON\n else {\n // Handle possible data mutation\n if (mutate !== true) geojson = clone(geojson);\n\n coordEach(geojson, function (coord) {\n var newCoord =\n projection === \"mercator\"\n ? convertToMercator(coord)\n : convertToWgs84(coord);\n coord[0] = newCoord[0];\n coord[1] = newCoord[1];\n });\n }\n return geojson;\n}\n\n/**\n * Convert lon/lat values to 900913 x/y.\n * (from https://github.com/mapbox/sphericalmercator)\n *\n * @private\n * @param {Array<number>} lonLat WGS84 point\n * @returns {Array<number>} Mercator [x, y] point\n */\nfunction convertToMercator(lonLat: number[]) {\n var D2R = Math.PI / 180,\n // 900913 properties\n A = 6378137.0,\n MAXEXTENT = 20037508.342789244;\n\n // compensate longitudes passing the 180th meridian\n // from https://github.com/proj4js/proj4js/blob/master/lib/common/adjust_lon.js\n var adjusted =\n Math.abs(lonLat[0]) <= 180 ? lonLat[0] : lonLat[0] - sign(lonLat[0]) * 360;\n var xy = [\n A * adjusted * D2R,\n A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R)),\n ];\n\n // if xy value is beyond maxextent (e.g. poles), return maxextent\n if (xy[0] > MAXEXTENT) xy[0] = MAXEXTENT;\n if (xy[0] < -MAXEXTENT) xy[0] = -MAXEXTENT;\n if (xy[1] > MAXEXTENT) xy[1] = MAXEXTENT;\n if (xy[1] < -MAXEXTENT) xy[1] = -MAXEXTENT;\n\n return xy;\n}\n\n/**\n * Convert 900913 x/y values to lon/lat.\n * (from https://github.com/mapbox/sphericalmercator)\n *\n * @private\n * @param {Array<number>} xy Mercator [x, y] point\n * @returns {Array<number>} WGS84 [lon, lat] point\n */\nfunction convertToWgs84(xy: number[]) {\n // 900913 properties.\n var R2D = 180 / Math.PI;\n var A = 6378137.0;\n\n return [\n (xy[0] * R2D) / A,\n (Math.PI * 0.5 - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D,\n ];\n}\n\n/**\n * Returns the sign of the input, or zero\n *\n * @private\n * @param {number} x input\n * @returns {number} -1|0|1 output\n */\nfunction sign(x: number) {\n return x < 0 ? -1 : x > 0 ? 1 : 0;\n}\n\nexport { toMercator, toWgs84 };\n"],"mappings":";AACA,SAAS,iBAAiB;AAC1B,SAAqB,gBAAgB;AACrC,SAAS,aAAa;AAiBtB,SAAS,WACP,SACA,UAAgC,CAAC,GAC9B;AACH,SAAO,QAAQ,SAAS,YAAY,OAAO;AAC7C;AAiBA,SAAS,QACP,SACA,UAAgC,CAAC,GAC9B;AACH,SAAO,QAAQ,SAAS,SAAS,OAAO;AAC1C;AAYA,SAAS,QACP,SACA,YACA,UAAgC,CAAC,GAC5B;AAEL,YAAU,WAAW,CAAC;AACtB,MAAI,SAAS,QAAQ;AAGrB,MAAI,CAAC;AAAS,UAAM,IAAI,MAAM,qBAAqB;AAGnD,MAAI,MAAM,QAAQ,OAAO,KAAK,SAAS,QAAQ,CAAC,CAAC;AAC/C,cACE,eAAe,aACX,kBAAkB,OAAO,IACzB,eAAe,OAAO;AAAA,OAEzB;AAEH,QAAI,WAAW;AAAM,gBAAU,MAAM,OAAO;AAE5C,cAAU,SAAS,SAAU,OAAO;AAClC,UAAI,WACF,eAAe,aACX,kBAAkB,KAAK,IACvB,eAAe,KAAK;AAC1B,YAAM,CAAC,IAAI,SAAS,CAAC;AACrB,YAAM,CAAC,IAAI,SAAS,CAAC;AAAA,IACvB,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAUA,SAAS,kBAAkB,QAAkB;AAC3C,MAAI,MAAM,KAAK,KAAK,KAElB,IAAI,SACJ,YAAY;AAId,MAAI,WACF,KAAK,IAAI,OAAO,CAAC,CAAC,KAAK,MAAM,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,IAAI;AACzE,MAAI,KAAK;AAAA,IACP,IAAI,WAAW;AAAA,IACf,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,KAAK,OAAO,MAAM,OAAO,CAAC,IAAI,GAAG,CAAC;AAAA,EAC/D;AAGA,MAAI,GAAG,CAAC,IAAI;AAAW,OAAG,CAAC,IAAI;AAC/B,MAAI,GAAG,CAAC,IAAI,CAAC;AAAW,OAAG,CAAC,IAAI,CAAC;AACjC,MAAI,GAAG,CAAC,IAAI;AAAW,OAAG,CAAC,IAAI;AAC/B,MAAI,GAAG,CAAC,IAAI,CAAC;AAAW,OAAG,CAAC,IAAI,CAAC;AAEjC,SAAO;AACT;AAUA,SAAS,eAAe,IAAc;AAEpC,MAAI,MAAM,MAAM,KAAK;AACrB,MAAI,IAAI;AAER,SAAO;AAAA,IACJ,GAAG,CAAC,IAAI,MAAO;AAAA,KACf,KAAK,KAAK,MAAM,IAAM,KAAK,KAAK,KAAK,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK;AAAA,EAC5D;AACF;AASA,SAAS,KAAK,GAAW;AACvB,SAAO,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI;AAClC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/projection",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.0-alpha.7+0ce6ecca0",
|
|
4
4
|
"description": "turf projection module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -34,49 +34,54 @@
|
|
|
34
34
|
"EPSG:900913",
|
|
35
35
|
"EPSG:102113"
|
|
36
36
|
],
|
|
37
|
-
"
|
|
38
|
-
"
|
|
37
|
+
"type": "module",
|
|
38
|
+
"main": "dist/cjs/index.cjs",
|
|
39
|
+
"module": "dist/esm/index.js",
|
|
40
|
+
"types": "dist/esm/index.d.ts",
|
|
39
41
|
"exports": {
|
|
40
42
|
"./package.json": "./package.json",
|
|
41
43
|
".": {
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
"import": {
|
|
45
|
+
"types": "./dist/esm/index.d.ts",
|
|
46
|
+
"default": "./dist/esm/index.js"
|
|
47
|
+
},
|
|
48
|
+
"require": {
|
|
49
|
+
"types": "./dist/cjs/index.d.cts",
|
|
50
|
+
"default": "./dist/cjs/index.cjs"
|
|
51
|
+
}
|
|
45
52
|
}
|
|
46
53
|
},
|
|
47
|
-
"types": "dist/js/index.d.ts",
|
|
48
54
|
"sideEffects": false,
|
|
49
55
|
"files": [
|
|
50
56
|
"dist"
|
|
51
57
|
],
|
|
52
58
|
"scripts": {
|
|
53
|
-
"bench": "tsx bench.
|
|
54
|
-
"build": "
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"test": "
|
|
59
|
-
"test:tape": "tsx test.js",
|
|
60
|
-
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
|
|
59
|
+
"bench": "tsx bench.ts",
|
|
60
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
61
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
62
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
63
|
+
"test:tape": "tsx test.ts",
|
|
64
|
+
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
|
|
61
65
|
},
|
|
62
66
|
"devDependencies": {
|
|
63
|
-
"@turf/truncate": "^7.
|
|
64
|
-
"@types/
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
67
|
+
"@turf/truncate": "^7.1.0-alpha.7+0ce6ecca0",
|
|
68
|
+
"@types/benchmark": "^2.1.5",
|
|
69
|
+
"@types/tape": "^4.2.32",
|
|
70
|
+
"benchmark": "^2.1.4",
|
|
71
|
+
"load-json-file": "^7.0.1",
|
|
72
|
+
"npm-run-all": "^4.1.5",
|
|
73
|
+
"proj4": "^2.9.2",
|
|
74
|
+
"tape": "^5.7.2",
|
|
75
|
+
"tsup": "^8.0.1",
|
|
76
|
+
"tsx": "^4.6.2",
|
|
77
|
+
"typescript": "^5.2.2",
|
|
78
|
+
"write-json-file": "^5.0.0"
|
|
74
79
|
},
|
|
75
80
|
"dependencies": {
|
|
76
|
-
"@turf/clone": "^7.
|
|
77
|
-
"@turf/helpers": "^7.
|
|
78
|
-
"@turf/meta": "^7.
|
|
79
|
-
"tslib": "^2.
|
|
81
|
+
"@turf/clone": "^7.1.0-alpha.7+0ce6ecca0",
|
|
82
|
+
"@turf/helpers": "^7.1.0-alpha.7+0ce6ecca0",
|
|
83
|
+
"@turf/meta": "^7.1.0-alpha.7+0ce6ecca0",
|
|
84
|
+
"tslib": "^2.6.2"
|
|
80
85
|
},
|
|
81
|
-
"gitHead": "
|
|
86
|
+
"gitHead": "0ce6ecca05829690270fec6d6bed2003495fe0ea"
|
|
82
87
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { coordEach } from "@turf/meta";
|
|
2
|
-
import { isNumber } from "@turf/helpers";
|
|
3
|
-
import clone from "@turf/clone";
|
|
4
|
-
/**
|
|
5
|
-
* Converts a WGS84 GeoJSON object into Mercator (EPSG:900913) projection
|
|
6
|
-
*
|
|
7
|
-
* @name toMercator
|
|
8
|
-
* @param {GeoJSON|Position} geojson WGS84 GeoJSON object
|
|
9
|
-
* @param {Object} [options] Optional parameters
|
|
10
|
-
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
11
|
-
* @returns {GeoJSON} Projected GeoJSON
|
|
12
|
-
* @example
|
|
13
|
-
* var pt = turf.point([-71,41]);
|
|
14
|
-
* var converted = turf.toMercator(pt);
|
|
15
|
-
*
|
|
16
|
-
* //addToMap
|
|
17
|
-
* var addToMap = [pt, converted];
|
|
18
|
-
*/
|
|
19
|
-
export function toMercator(geojson, options = {}) {
|
|
20
|
-
return convert(geojson, "mercator", options);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Converts a Mercator (EPSG:900913) GeoJSON object into WGS84 projection
|
|
24
|
-
*
|
|
25
|
-
* @name toWgs84
|
|
26
|
-
* @param {GeoJSON|Position} geojson Mercator GeoJSON object
|
|
27
|
-
* @param {Object} [options] Optional parameters
|
|
28
|
-
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
29
|
-
* @returns {GeoJSON} Projected GeoJSON
|
|
30
|
-
* @example
|
|
31
|
-
* var pt = turf.point([-7903683.846322424, 5012341.663847514]);
|
|
32
|
-
* var converted = turf.toWgs84(pt);
|
|
33
|
-
*
|
|
34
|
-
* //addToMap
|
|
35
|
-
* var addToMap = [pt, converted];
|
|
36
|
-
*/
|
|
37
|
-
export function toWgs84(geojson, options = {}) {
|
|
38
|
-
return convert(geojson, "wgs84", options);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Converts a GeoJSON coordinates to the defined `projection`
|
|
42
|
-
*
|
|
43
|
-
* @private
|
|
44
|
-
* @param {GeoJSON} geojson GeoJSON Feature or Geometry
|
|
45
|
-
* @param {string} projection defines the projection system to convert the coordinates to
|
|
46
|
-
* @param {Object} [options] Optional parameters
|
|
47
|
-
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
48
|
-
* @returns {GeoJSON} Converted GeoJSON
|
|
49
|
-
*/
|
|
50
|
-
function convert(geojson, projection, options = {}) {
|
|
51
|
-
// Optional parameters
|
|
52
|
-
options = options || {};
|
|
53
|
-
var mutate = options.mutate;
|
|
54
|
-
// Validation
|
|
55
|
-
if (!geojson)
|
|
56
|
-
throw new Error("geojson is required");
|
|
57
|
-
// Handle Position
|
|
58
|
-
if (Array.isArray(geojson) && isNumber(geojson[0]))
|
|
59
|
-
geojson =
|
|
60
|
-
projection === "mercator"
|
|
61
|
-
? convertToMercator(geojson)
|
|
62
|
-
: convertToWgs84(geojson);
|
|
63
|
-
// Handle GeoJSON
|
|
64
|
-
else {
|
|
65
|
-
// Handle possible data mutation
|
|
66
|
-
if (mutate !== true)
|
|
67
|
-
geojson = clone(geojson);
|
|
68
|
-
coordEach(geojson, function (coord) {
|
|
69
|
-
var newCoord = projection === "mercator"
|
|
70
|
-
? convertToMercator(coord)
|
|
71
|
-
: convertToWgs84(coord);
|
|
72
|
-
coord[0] = newCoord[0];
|
|
73
|
-
coord[1] = newCoord[1];
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
return geojson;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Convert lon/lat values to 900913 x/y.
|
|
80
|
-
* (from https://github.com/mapbox/sphericalmercator)
|
|
81
|
-
*
|
|
82
|
-
* @private
|
|
83
|
-
* @param {Array<number>} lonLat WGS84 point
|
|
84
|
-
* @returns {Array<number>} Mercator [x, y] point
|
|
85
|
-
*/
|
|
86
|
-
function convertToMercator(lonLat) {
|
|
87
|
-
var D2R = Math.PI / 180,
|
|
88
|
-
// 900913 properties
|
|
89
|
-
A = 6378137.0, MAXEXTENT = 20037508.342789244;
|
|
90
|
-
// compensate longitudes passing the 180th meridian
|
|
91
|
-
// from https://github.com/proj4js/proj4js/blob/master/lib/common/adjust_lon.js
|
|
92
|
-
var adjusted = Math.abs(lonLat[0]) <= 180 ? lonLat[0] : lonLat[0] - sign(lonLat[0]) * 360;
|
|
93
|
-
var xy = [
|
|
94
|
-
A * adjusted * D2R,
|
|
95
|
-
A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R)),
|
|
96
|
-
];
|
|
97
|
-
// if xy value is beyond maxextent (e.g. poles), return maxextent
|
|
98
|
-
if (xy[0] > MAXEXTENT)
|
|
99
|
-
xy[0] = MAXEXTENT;
|
|
100
|
-
if (xy[0] < -MAXEXTENT)
|
|
101
|
-
xy[0] = -MAXEXTENT;
|
|
102
|
-
if (xy[1] > MAXEXTENT)
|
|
103
|
-
xy[1] = MAXEXTENT;
|
|
104
|
-
if (xy[1] < -MAXEXTENT)
|
|
105
|
-
xy[1] = -MAXEXTENT;
|
|
106
|
-
return xy;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Convert 900913 x/y values to lon/lat.
|
|
110
|
-
* (from https://github.com/mapbox/sphericalmercator)
|
|
111
|
-
*
|
|
112
|
-
* @private
|
|
113
|
-
* @param {Array<number>} xy Mercator [x, y] point
|
|
114
|
-
* @returns {Array<number>} WGS84 [lon, lat] point
|
|
115
|
-
*/
|
|
116
|
-
function convertToWgs84(xy) {
|
|
117
|
-
// 900913 properties.
|
|
118
|
-
var R2D = 180 / Math.PI;
|
|
119
|
-
var A = 6378137.0;
|
|
120
|
-
return [
|
|
121
|
-
(xy[0] * R2D) / A,
|
|
122
|
-
(Math.PI * 0.5 - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D,
|
|
123
|
-
];
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Returns the sign of the input, or zero
|
|
127
|
-
*
|
|
128
|
-
* @private
|
|
129
|
-
* @param {number} x input
|
|
130
|
-
* @returns {number} -1|0|1 output
|
|
131
|
-
*/
|
|
132
|
-
function sign(x) {
|
|
133
|
-
return x < 0 ? -1 : x > 0 ? 1 : 0;
|
|
134
|
-
}
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const meta_1 = require("@turf/meta");
|
|
5
|
-
const helpers_1 = require("@turf/helpers");
|
|
6
|
-
const clone_1 = tslib_1.__importDefault(require("@turf/clone"));
|
|
7
|
-
/**
|
|
8
|
-
* Converts a WGS84 GeoJSON object into Mercator (EPSG:900913) projection
|
|
9
|
-
*
|
|
10
|
-
* @name toMercator
|
|
11
|
-
* @param {GeoJSON|Position} geojson WGS84 GeoJSON object
|
|
12
|
-
* @param {Object} [options] Optional parameters
|
|
13
|
-
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
14
|
-
* @returns {GeoJSON} Projected GeoJSON
|
|
15
|
-
* @example
|
|
16
|
-
* var pt = turf.point([-71,41]);
|
|
17
|
-
* var converted = turf.toMercator(pt);
|
|
18
|
-
*
|
|
19
|
-
* //addToMap
|
|
20
|
-
* var addToMap = [pt, converted];
|
|
21
|
-
*/
|
|
22
|
-
function toMercator(geojson, options = {}) {
|
|
23
|
-
return convert(geojson, "mercator", options);
|
|
24
|
-
}
|
|
25
|
-
exports.toMercator = toMercator;
|
|
26
|
-
/**
|
|
27
|
-
* Converts a Mercator (EPSG:900913) GeoJSON object into WGS84 projection
|
|
28
|
-
*
|
|
29
|
-
* @name toWgs84
|
|
30
|
-
* @param {GeoJSON|Position} geojson Mercator GeoJSON object
|
|
31
|
-
* @param {Object} [options] Optional parameters
|
|
32
|
-
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
33
|
-
* @returns {GeoJSON} Projected GeoJSON
|
|
34
|
-
* @example
|
|
35
|
-
* var pt = turf.point([-7903683.846322424, 5012341.663847514]);
|
|
36
|
-
* var converted = turf.toWgs84(pt);
|
|
37
|
-
*
|
|
38
|
-
* //addToMap
|
|
39
|
-
* var addToMap = [pt, converted];
|
|
40
|
-
*/
|
|
41
|
-
function toWgs84(geojson, options = {}) {
|
|
42
|
-
return convert(geojson, "wgs84", options);
|
|
43
|
-
}
|
|
44
|
-
exports.toWgs84 = toWgs84;
|
|
45
|
-
/**
|
|
46
|
-
* Converts a GeoJSON coordinates to the defined `projection`
|
|
47
|
-
*
|
|
48
|
-
* @private
|
|
49
|
-
* @param {GeoJSON} geojson GeoJSON Feature or Geometry
|
|
50
|
-
* @param {string} projection defines the projection system to convert the coordinates to
|
|
51
|
-
* @param {Object} [options] Optional parameters
|
|
52
|
-
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
53
|
-
* @returns {GeoJSON} Converted GeoJSON
|
|
54
|
-
*/
|
|
55
|
-
function convert(geojson, projection, options = {}) {
|
|
56
|
-
// Optional parameters
|
|
57
|
-
options = options || {};
|
|
58
|
-
var mutate = options.mutate;
|
|
59
|
-
// Validation
|
|
60
|
-
if (!geojson)
|
|
61
|
-
throw new Error("geojson is required");
|
|
62
|
-
// Handle Position
|
|
63
|
-
if (Array.isArray(geojson) && helpers_1.isNumber(geojson[0]))
|
|
64
|
-
geojson =
|
|
65
|
-
projection === "mercator"
|
|
66
|
-
? convertToMercator(geojson)
|
|
67
|
-
: convertToWgs84(geojson);
|
|
68
|
-
// Handle GeoJSON
|
|
69
|
-
else {
|
|
70
|
-
// Handle possible data mutation
|
|
71
|
-
if (mutate !== true)
|
|
72
|
-
geojson = clone_1.default(geojson);
|
|
73
|
-
meta_1.coordEach(geojson, function (coord) {
|
|
74
|
-
var newCoord = projection === "mercator"
|
|
75
|
-
? convertToMercator(coord)
|
|
76
|
-
: convertToWgs84(coord);
|
|
77
|
-
coord[0] = newCoord[0];
|
|
78
|
-
coord[1] = newCoord[1];
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
return geojson;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Convert lon/lat values to 900913 x/y.
|
|
85
|
-
* (from https://github.com/mapbox/sphericalmercator)
|
|
86
|
-
*
|
|
87
|
-
* @private
|
|
88
|
-
* @param {Array<number>} lonLat WGS84 point
|
|
89
|
-
* @returns {Array<number>} Mercator [x, y] point
|
|
90
|
-
*/
|
|
91
|
-
function convertToMercator(lonLat) {
|
|
92
|
-
var D2R = Math.PI / 180,
|
|
93
|
-
// 900913 properties
|
|
94
|
-
A = 6378137.0, MAXEXTENT = 20037508.342789244;
|
|
95
|
-
// compensate longitudes passing the 180th meridian
|
|
96
|
-
// from https://github.com/proj4js/proj4js/blob/master/lib/common/adjust_lon.js
|
|
97
|
-
var adjusted = Math.abs(lonLat[0]) <= 180 ? lonLat[0] : lonLat[0] - sign(lonLat[0]) * 360;
|
|
98
|
-
var xy = [
|
|
99
|
-
A * adjusted * D2R,
|
|
100
|
-
A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R)),
|
|
101
|
-
];
|
|
102
|
-
// if xy value is beyond maxextent (e.g. poles), return maxextent
|
|
103
|
-
if (xy[0] > MAXEXTENT)
|
|
104
|
-
xy[0] = MAXEXTENT;
|
|
105
|
-
if (xy[0] < -MAXEXTENT)
|
|
106
|
-
xy[0] = -MAXEXTENT;
|
|
107
|
-
if (xy[1] > MAXEXTENT)
|
|
108
|
-
xy[1] = MAXEXTENT;
|
|
109
|
-
if (xy[1] < -MAXEXTENT)
|
|
110
|
-
xy[1] = -MAXEXTENT;
|
|
111
|
-
return xy;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Convert 900913 x/y values to lon/lat.
|
|
115
|
-
* (from https://github.com/mapbox/sphericalmercator)
|
|
116
|
-
*
|
|
117
|
-
* @private
|
|
118
|
-
* @param {Array<number>} xy Mercator [x, y] point
|
|
119
|
-
* @returns {Array<number>} WGS84 [lon, lat] point
|
|
120
|
-
*/
|
|
121
|
-
function convertToWgs84(xy) {
|
|
122
|
-
// 900913 properties.
|
|
123
|
-
var R2D = 180 / Math.PI;
|
|
124
|
-
var A = 6378137.0;
|
|
125
|
-
return [
|
|
126
|
-
(xy[0] * R2D) / A,
|
|
127
|
-
(Math.PI * 0.5 - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D,
|
|
128
|
-
];
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Returns the sign of the input, or zero
|
|
132
|
-
*
|
|
133
|
-
* @private
|
|
134
|
-
* @param {number} x input
|
|
135
|
-
* @returns {number} -1|0|1 output
|
|
136
|
-
*/
|
|
137
|
-
function sign(x) {
|
|
138
|
-
return x < 0 ? -1 : x > 0 ? 1 : 0;
|
|
139
|
-
}
|