index.cjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.js
  2. var _meta = require('@turf/meta');
  3. var _invariant = require('@turf/invariant');
  4. var _helpers = require('@turf/helpers');
  5. // lib/intersection.js
  6. function ab(segment) {
  7. var start = segment[0];
  8. var end = segment[1];
  9. return [end[0] - start[0], end[1] - start[1]];
  10. }
  11. function crossProduct(v1, v2) {
  12. return v1[0] * v2[1] - v2[0] * v1[1];
  13. }
  14. function add(v1, v2) {
  15. return [v1[0] + v2[0], v1[1] + v2[1]];
  16. }
  17. function sub(v1, v2) {
  18. return [v1[0] - v2[0], v1[1] - v2[1]];
  19. }
  20. function scalarMult(s, v) {
  21. return [s * v[0], s * v[1]];
  22. }
  23. function intersectSegments(a, b) {
  24. var p = a[0];
  25. var r = ab(a);
  26. var q = b[0];
  27. var s = ab(b);
  28. var cross = crossProduct(r, s);
  29. var qmp = sub(q, p);
  30. var numerator = crossProduct(qmp, s);
  31. var t = numerator / cross;
  32. var intersection2 = add(p, scalarMult(t, r));
  33. return intersection2;
  34. }
  35. function isParallel(a, b) {
  36. var r = ab(a);
  37. var s = ab(b);
  38. return crossProduct(r, s) === 0;
  39. }
  40. function intersection(a, b) {
  41. if (isParallel(a, b)) return false;
  42. return intersectSegments(a, b);
  43. }
  44. // index.js
  45. function lineOffset(geojson, distance, options) {
  46. options = options || {};
  47. if (!_helpers.isObject.call(void 0, options)) throw new Error("options is invalid");
  48. var units = options.units;
  49. if (!geojson) throw new Error("geojson is required");
  50. if (distance === void 0 || distance === null || isNaN(distance))
  51. throw new Error("distance is required");
  52. var type = _invariant.getType.call(void 0, geojson);
  53. var properties = geojson.properties;
  54. switch (type) {
  55. case "LineString":
  56. return lineOffsetFeature(geojson, distance, units);
  57. case "MultiLineString":
  58. var coords = [];
  59. _meta.flattenEach.call(void 0, geojson, function(feature) {
  60. coords.push(
  61. lineOffsetFeature(feature, distance, units).geometry.coordinates
  62. );
  63. });
  64. return _helpers.multiLineString.call(void 0, coords, properties);
  65. default:
  66. throw new Error("geometry " + type + " is not supported");
  67. }
  68. }
  69. function lineOffsetFeature(line, distance, units) {
  70. var segments = [];
  71. var offsetDegrees = _helpers.lengthToDegrees.call(void 0, distance, units);
  72. var coords = _invariant.getCoords.call(void 0, line);
  73. var finalCoords = [];
  74. coords.forEach(function(currentCoords, index) {
  75. if (index !== coords.length - 1) {
  76. var segment = processSegment(
  77. currentCoords,
  78. coords[index + 1],
  79. offsetDegrees
  80. );
  81. segments.push(segment);
  82. if (index > 0) {
  83. var seg2Coords = segments[index - 1];
  84. var intersects = intersection(segment, seg2Coords);
  85. if (intersects !== false) {
  86. seg2Coords[1] = intersects;
  87. segment[0] = intersects;
  88. }
  89. finalCoords.push(seg2Coords[0]);
  90. if (index === coords.length - 2) {
  91. finalCoords.push(segment[0]);
  92. finalCoords.push(segment[1]);
  93. }
  94. }
  95. if (coords.length === 2) {
  96. finalCoords.push(segment[0]);
  97. finalCoords.push(segment[1]);
  98. }
  99. }
  100. });
  101. return _helpers.lineString.call(void 0, finalCoords, line.properties);
  102. }
  103. function processSegment(point1, point2, offset) {
  104. var L = Math.sqrt(
  105. (point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1])
  106. );
  107. var out1x = point1[0] + offset * (point2[1] - point1[1]) / L;
  108. var out2x = point2[0] + offset * (point2[1] - point1[1]) / L;
  109. var out1y = point1[1] + offset * (point1[0] - point2[0]) / L;
  110. var out2y = point2[1] + offset * (point1[0] - point2[0]) / L;
  111. return [
  112. [out1x, out1y],
  113. [out2x, out2y]
  114. ];
  115. }
  116. var turf_line_offset_default = lineOffset;
  117. exports.default = turf_line_offset_default; exports.lineOffset = lineOffset;
  118. //# sourceMappingURL=index.cjs.map