index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var meta_1 = require("@turf/meta");
  7. var helpers_1 = require("@turf/helpers");
  8. var clone_1 = __importDefault(require("@turf/clone"));
  9. /**
  10. * Converts a WGS84 GeoJSON object into Mercator (EPSG:900913) projection
  11. *
  12. * @name toMercator
  13. * @param {GeoJSON|Position} geojson WGS84 GeoJSON object
  14. * @param {Object} [options] Optional parameters
  15. * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
  16. * @returns {GeoJSON} Projected GeoJSON
  17. * @example
  18. * var pt = turf.point([-71,41]);
  19. * var converted = turf.toMercator(pt);
  20. *
  21. * //addToMap
  22. * var addToMap = [pt, converted];
  23. */
  24. function toMercator(geojson, options) {
  25. if (options === void 0) { options = {}; }
  26. return convert(geojson, "mercator", options);
  27. }
  28. exports.toMercator = toMercator;
  29. /**
  30. * Converts a Mercator (EPSG:900913) GeoJSON object into WGS84 projection
  31. *
  32. * @name toWgs84
  33. * @param {GeoJSON|Position} geojson Mercator GeoJSON object
  34. * @param {Object} [options] Optional parameters
  35. * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
  36. * @returns {GeoJSON} Projected GeoJSON
  37. * @example
  38. * var pt = turf.point([-7903683.846322424, 5012341.663847514]);
  39. * var converted = turf.toWgs84(pt);
  40. *
  41. * //addToMap
  42. * var addToMap = [pt, converted];
  43. */
  44. function toWgs84(geojson, options) {
  45. if (options === void 0) { options = {}; }
  46. return convert(geojson, "wgs84", options);
  47. }
  48. exports.toWgs84 = toWgs84;
  49. /**
  50. * Converts a GeoJSON coordinates to the defined `projection`
  51. *
  52. * @private
  53. * @param {GeoJSON} geojson GeoJSON Feature or Geometry
  54. * @param {string} projection defines the projection system to convert the coordinates to
  55. * @param {Object} [options] Optional parameters
  56. * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
  57. * @returns {GeoJSON} Converted GeoJSON
  58. */
  59. function convert(geojson, projection, options) {
  60. if (options === void 0) { options = {}; }
  61. // Optional parameters
  62. options = options || {};
  63. var mutate = options.mutate;
  64. // Validation
  65. if (!geojson)
  66. throw new Error("geojson is required");
  67. // Handle Position
  68. if (Array.isArray(geojson) && helpers_1.isNumber(geojson[0]))
  69. geojson =
  70. projection === "mercator"
  71. ? convertToMercator(geojson)
  72. : convertToWgs84(geojson);
  73. // Handle GeoJSON
  74. else {
  75. // Handle possible data mutation
  76. if (mutate !== true)
  77. geojson = clone_1.default(geojson);
  78. meta_1.coordEach(geojson, function (coord) {
  79. var newCoord = projection === "mercator"
  80. ? convertToMercator(coord)
  81. : convertToWgs84(coord);
  82. coord[0] = newCoord[0];
  83. coord[1] = newCoord[1];
  84. });
  85. }
  86. return geojson;
  87. }
  88. /**
  89. * Convert lon/lat values to 900913 x/y.
  90. * (from https://github.com/mapbox/sphericalmercator)
  91. *
  92. * @private
  93. * @param {Array<number>} lonLat WGS84 point
  94. * @returns {Array<number>} Mercator [x, y] point
  95. */
  96. function convertToMercator(lonLat) {
  97. var D2R = Math.PI / 180,
  98. // 900913 properties
  99. A = 6378137.0, MAXEXTENT = 20037508.342789244;
  100. // compensate longitudes passing the 180th meridian
  101. // from https://github.com/proj4js/proj4js/blob/master/lib/common/adjust_lon.js
  102. var adjusted = Math.abs(lonLat[0]) <= 180 ? lonLat[0] : lonLat[0] - sign(lonLat[0]) * 360;
  103. var xy = [
  104. A * adjusted * D2R,
  105. A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R)),
  106. ];
  107. // if xy value is beyond maxextent (e.g. poles), return maxextent
  108. if (xy[0] > MAXEXTENT)
  109. xy[0] = MAXEXTENT;
  110. if (xy[0] < -MAXEXTENT)
  111. xy[0] = -MAXEXTENT;
  112. if (xy[1] > MAXEXTENT)
  113. xy[1] = MAXEXTENT;
  114. if (xy[1] < -MAXEXTENT)
  115. xy[1] = -MAXEXTENT;
  116. return xy;
  117. }
  118. /**
  119. * Convert 900913 x/y values to lon/lat.
  120. * (from https://github.com/mapbox/sphericalmercator)
  121. *
  122. * @private
  123. * @param {Array<number>} xy Mercator [x, y] point
  124. * @returns {Array<number>} WGS84 [lon, lat] point
  125. */
  126. function convertToWgs84(xy) {
  127. // 900913 properties.
  128. var R2D = 180 / Math.PI;
  129. var A = 6378137.0;
  130. return [
  131. (xy[0] * R2D) / A,
  132. (Math.PI * 0.5 - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D,
  133. ];
  134. }
  135. /**
  136. * Returns the sign of the input, or zero
  137. *
  138. * @private
  139. * @param {number} x input
  140. * @returns {number} -1|0|1 output
  141. */
  142. function sign(x) {
  143. return x < 0 ? -1 : x > 0 ? 1 : 0;
  144. }