index.js 2.8 KB

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