@turf/projection 7.0.0-alpha.1 → 7.0.0-alpha.110

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 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][2]?** Optional parameters
11
+ * `geojson` **([GeoJSON][1] | [Position][2])** WGS84 GeoJSON object
12
+ * `options` **[Object][3]?** Optional parameters
13
13
 
14
- * `options.mutate` **[boolean][3]** allows GeoJSON input to be mutated (significant performance increase if true) (optional, default `false`)
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][2]?** Optional parameters
34
+ * `geojson` **([GeoJSON][1] | [Position][2])** Mercator GeoJSON object
35
+ * `options` **[Object][3]?** Optional parameters
36
36
 
37
- * `options.mutate` **[boolean][3]** allows GeoJSON input to be mutated (significant performance increase if true) (optional, default `false`)
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/JavaScript/Reference/Global_Objects/Object
53
+ [2]: https://developer.mozilla.org/docs/Web/API/Position
54
54
 
55
- [3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
55
+ [3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
56
56
 
57
- <!-- This file is automatically generated. Please don't edit it directly:
58
- if you find an error, edit the source file (likely index.js), and re-run
59
- ./scripts/generate-readmes in the turf project. -->
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](http://turfjs.org/), an open source
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 Turf module that includes it as a function:
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,70 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ var _meta = require('@turf/meta');
6
+ var _helpers = require('@turf/helpers');
7
+ var _clone = require('@turf/clone');
8
+ function toMercator(geojson, options = {}) {
9
+ return convert(geojson, "mercator", options);
10
+ }
11
+ __name(toMercator, "toMercator");
12
+ function toWgs84(geojson, options = {}) {
13
+ return convert(geojson, "wgs84", options);
14
+ }
15
+ __name(toWgs84, "toWgs84");
16
+ function convert(geojson, projection, options = {}) {
17
+ options = options || {};
18
+ var mutate = options.mutate;
19
+ if (!geojson)
20
+ throw new Error("geojson is required");
21
+ if (Array.isArray(geojson) && _helpers.isNumber.call(void 0, geojson[0]))
22
+ geojson = projection === "mercator" ? convertToMercator(geojson) : convertToWgs84(geojson);
23
+ else {
24
+ if (mutate !== true)
25
+ geojson = _clone.clone.call(void 0, geojson);
26
+ _meta.coordEach.call(void 0, geojson, function(coord) {
27
+ var newCoord = projection === "mercator" ? convertToMercator(coord) : convertToWgs84(coord);
28
+ coord[0] = newCoord[0];
29
+ coord[1] = newCoord[1];
30
+ });
31
+ }
32
+ return geojson;
33
+ }
34
+ __name(convert, "convert");
35
+ function convertToMercator(lonLat) {
36
+ var D2R = Math.PI / 180, A = 6378137, MAXEXTENT = 20037508342789244e-9;
37
+ var adjusted = Math.abs(lonLat[0]) <= 180 ? lonLat[0] : lonLat[0] - sign(lonLat[0]) * 360;
38
+ var xy = [
39
+ A * adjusted * D2R,
40
+ A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R))
41
+ ];
42
+ if (xy[0] > MAXEXTENT)
43
+ xy[0] = MAXEXTENT;
44
+ if (xy[0] < -MAXEXTENT)
45
+ xy[0] = -MAXEXTENT;
46
+ if (xy[1] > MAXEXTENT)
47
+ xy[1] = MAXEXTENT;
48
+ if (xy[1] < -MAXEXTENT)
49
+ xy[1] = -MAXEXTENT;
50
+ return xy;
51
+ }
52
+ __name(convertToMercator, "convertToMercator");
53
+ function convertToWgs84(xy) {
54
+ var R2D = 180 / Math.PI;
55
+ var A = 6378137;
56
+ return [
57
+ xy[0] * R2D / A,
58
+ (Math.PI * 0.5 - 2 * Math.atan(Math.exp(-xy[1] / A))) * R2D
59
+ ];
60
+ }
61
+ __name(convertToWgs84, "convertToWgs84");
62
+ function sign(x) {
63
+ return x < 0 ? -1 : x > 0 ? 1 : 0;
64
+ }
65
+ __name(sign, "sign");
66
+
67
+
68
+
69
+ exports.toMercator = toMercator; exports.toWgs84 = toWgs84;
70
+ //# 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;AALS;AAsBT,SAAS,QACP,SACA,UAAgC,CAAC,GAC9B;AACH,SAAO,QAAQ,SAAS,SAAS,OAAO;AAC1C;AALS;AAiBT,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;AAjCS;AA2CT,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;AAtBS;AAgCT,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;AATS;AAkBT,SAAS,KAAK,GAAW;AACvB,SAAO,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI;AAClC;AAFS","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"]}
@@ -1,5 +1,6 @@
1
- import { Position } from "geojson";
2
- import { AllGeoJSON } from "@turf/helpers";
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
- export declare function toMercator<G = AllGeoJSON | Position>(geojson: G, options?: {
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
- export declare function toWgs84<G = AllGeoJSON | Position>(geojson: G, options?: {
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,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 };
@@ -0,0 +1,70 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ import { coordEach } from "@turf/meta";
6
+ import { isNumber } from "@turf/helpers";
7
+ import { clone } from "@turf/clone";
8
+ function toMercator(geojson, options = {}) {
9
+ return convert(geojson, "mercator", options);
10
+ }
11
+ __name(toMercator, "toMercator");
12
+ function toWgs84(geojson, options = {}) {
13
+ return convert(geojson, "wgs84", options);
14
+ }
15
+ __name(toWgs84, "toWgs84");
16
+ function convert(geojson, projection, options = {}) {
17
+ options = options || {};
18
+ var mutate = options.mutate;
19
+ if (!geojson)
20
+ throw new Error("geojson is required");
21
+ if (Array.isArray(geojson) && isNumber(geojson[0]))
22
+ geojson = projection === "mercator" ? convertToMercator(geojson) : convertToWgs84(geojson);
23
+ else {
24
+ if (mutate !== true)
25
+ geojson = clone(geojson);
26
+ coordEach(geojson, function(coord) {
27
+ var newCoord = projection === "mercator" ? convertToMercator(coord) : convertToWgs84(coord);
28
+ coord[0] = newCoord[0];
29
+ coord[1] = newCoord[1];
30
+ });
31
+ }
32
+ return geojson;
33
+ }
34
+ __name(convert, "convert");
35
+ function convertToMercator(lonLat) {
36
+ var D2R = Math.PI / 180, A = 6378137, MAXEXTENT = 20037508342789244e-9;
37
+ var adjusted = Math.abs(lonLat[0]) <= 180 ? lonLat[0] : lonLat[0] - sign(lonLat[0]) * 360;
38
+ var xy = [
39
+ A * adjusted * D2R,
40
+ A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R))
41
+ ];
42
+ if (xy[0] > MAXEXTENT)
43
+ xy[0] = MAXEXTENT;
44
+ if (xy[0] < -MAXEXTENT)
45
+ xy[0] = -MAXEXTENT;
46
+ if (xy[1] > MAXEXTENT)
47
+ xy[1] = MAXEXTENT;
48
+ if (xy[1] < -MAXEXTENT)
49
+ xy[1] = -MAXEXTENT;
50
+ return xy;
51
+ }
52
+ __name(convertToMercator, "convertToMercator");
53
+ function convertToWgs84(xy) {
54
+ var R2D = 180 / Math.PI;
55
+ var A = 6378137;
56
+ return [
57
+ xy[0] * R2D / A,
58
+ (Math.PI * 0.5 - 2 * Math.atan(Math.exp(-xy[1] / A))) * R2D
59
+ ];
60
+ }
61
+ __name(convertToWgs84, "convertToWgs84");
62
+ function sign(x) {
63
+ return x < 0 ? -1 : x > 0 ? 1 : 0;
64
+ }
65
+ __name(sign, "sign");
66
+ export {
67
+ toMercator,
68
+ toWgs84
69
+ };
70
+ //# sourceMappingURL=index.mjs.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;AALS;AAsBT,SAAS,QACP,SACA,UAAgC,CAAC,GAC9B;AACH,SAAO,QAAQ,SAAS,SAAS,OAAO;AAC1C;AALS;AAiBT,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;AAjCS;AA2CT,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;AAtBS;AAgCT,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;AATS;AAkBT,SAAS,KAAK,GAAW;AACvB,SAAO,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI;AAClC;AAFS;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/projection",
3
- "version": "7.0.0-alpha.1",
3
+ "version": "7.0.0-alpha.110+1411d63a7",
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
- "main": "dist/js/index.js",
38
- "module": "dist/es/index.js",
37
+ "type": "commonjs",
38
+ "main": "dist/cjs/index.cjs",
39
+ "module": "dist/esm/index.mjs",
40
+ "types": "dist/cjs/index.d.ts",
39
41
  "exports": {
40
42
  "./package.json": "./package.json",
41
43
  ".": {
42
- "types": "./dist/js/index.d.ts",
43
- "import": "./dist/es/index.js",
44
- "require": "./dist/js/index.js"
44
+ "import": {
45
+ "types": "./dist/esm/index.d.mts",
46
+ "default": "./dist/esm/index.mjs"
47
+ },
48
+ "require": {
49
+ "types": "./dist/cjs/index.d.ts",
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.js",
54
- "build": "npm-run-all build:*",
55
- "build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
56
- "build:js": "tsc",
57
- "docs": "tsx ../../scripts/generate-readmes",
58
- "test": "npm-run-all test:*",
59
- "test:tape": "tsx test.js",
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",
60
64
  "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
61
65
  },
62
66
  "devDependencies": {
63
- "@turf/truncate": "^7.0.0-alpha.1",
64
- "@types/tape": "*",
65
- "benchmark": "*",
66
- "load-json-file": "*",
67
- "npm-run-all": "*",
68
- "proj4": "*",
69
- "tape": "*",
70
- "tslint": "*",
71
- "tsx": "*",
72
- "typescript": "*",
73
- "write-json-file": "*"
67
+ "@turf/truncate": "^7.0.0-alpha.110+1411d63a7",
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.0.0-alpha.1",
77
- "@turf/helpers": "^7.0.0-alpha.1",
78
- "@turf/meta": "^7.0.0-alpha.1",
79
- "tslib": "^2.3.0"
81
+ "@turf/clone": "^7.0.0-alpha.110+1411d63a7",
82
+ "@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
83
+ "@turf/meta": "^7.0.0-alpha.110+1411d63a7",
84
+ "tslib": "^2.6.2"
80
85
  },
81
- "gitHead": "cf7a0c507b017ca066acffd0ce23bda5b393fb5a"
86
+ "gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
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
- }
@@ -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
- }