index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var meta_1 = require("@turf/meta");
  4. /**
  5. * Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry.
  6. *
  7. * @name truncate
  8. * @param {GeoJSON} geojson any GeoJSON Feature, FeatureCollection, Geometry or GeometryCollection.
  9. * @param {Object} [options={}] Optional parameters
  10. * @param {number} [options.precision=6] coordinate decimal precision
  11. * @param {number} [options.coordinates=3] maximum number of coordinates (primarly used to remove z coordinates)
  12. * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
  13. * @returns {GeoJSON} layer with truncated geometry
  14. * @example
  15. * var point = turf.point([
  16. * 70.46923055566859,
  17. * 58.11088890802906,
  18. * 1508
  19. * ]);
  20. * var options = {precision: 3, coordinates: 2};
  21. * var truncated = turf.truncate(point, options);
  22. * //=truncated.geometry.coordinates => [70.469, 58.111]
  23. *
  24. * //addToMap
  25. * var addToMap = [truncated];
  26. */
  27. function truncate(geojson, options) {
  28. if (options === void 0) { options = {}; }
  29. // Optional parameters
  30. var precision = options.precision;
  31. var coordinates = options.coordinates;
  32. var mutate = options.mutate;
  33. // default params
  34. precision =
  35. precision === undefined || precision === null || isNaN(precision)
  36. ? 6
  37. : precision;
  38. coordinates =
  39. coordinates === undefined || coordinates === null || isNaN(coordinates)
  40. ? 3
  41. : coordinates;
  42. // validation
  43. if (!geojson)
  44. throw new Error("<geojson> is required");
  45. if (typeof precision !== "number")
  46. throw new Error("<precision> must be a number");
  47. if (typeof coordinates !== "number")
  48. throw new Error("<coordinates> must be a number");
  49. // prevent input mutation
  50. if (mutate === false || mutate === undefined)
  51. geojson = JSON.parse(JSON.stringify(geojson));
  52. var factor = Math.pow(10, precision);
  53. // Truncate Coordinates
  54. meta_1.coordEach(geojson, function (coords) {
  55. truncateCoords(coords, factor, coordinates);
  56. });
  57. return geojson;
  58. }
  59. /**
  60. * Truncate Coordinates - Mutates coordinates in place
  61. *
  62. * @private
  63. * @param {Array<any>} coords Geometry Coordinates
  64. * @param {number} factor rounding factor for coordinate decimal precision
  65. * @param {number} coordinates maximum number of coordinates (primarly used to remove z coordinates)
  66. * @returns {Array<any>} mutated coordinates
  67. */
  68. function truncateCoords(coords, factor, coordinates) {
  69. // Remove extra coordinates (usually elevation coordinates and more)
  70. if (coords.length > coordinates)
  71. coords.splice(coordinates, coords.length);
  72. // Truncate coordinate decimals
  73. for (var i = 0; i < coords.length; i++) {
  74. coords[i] = Math.round(coords[i] * factor) / factor;
  75. }
  76. return coords;
  77. }
  78. exports.default = truncate;