index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { flattenEach } from '@turf/meta';
  2. import { getType, getCoords } from '@turf/invariant';
  3. import { isObject, lineString, multiLineString, lengthToDegrees } from '@turf/helpers';
  4. /**
  5. * https://github.com/rook2pawn/node-intersection
  6. *
  7. * Author @rook2pawn
  8. */
  9. /**
  10. * AB
  11. *
  12. * @private
  13. * @param {Array<Array<number>>} segment - 2 vertex line segment
  14. * @returns {Array<number>} coordinates [x, y]
  15. */
  16. function ab(segment) {
  17. var start = segment[0];
  18. var end = segment[1];
  19. return [end[0] - start[0], end[1] - start[1]];
  20. }
  21. /**
  22. * Cross Product
  23. *
  24. * @private
  25. * @param {Array<number>} v1 coordinates [x, y]
  26. * @param {Array<number>} v2 coordinates [x, y]
  27. * @returns {Array<number>} Cross Product
  28. */
  29. function crossProduct(v1, v2) {
  30. return v1[0] * v2[1] - v2[0] * v1[1];
  31. }
  32. /**
  33. * Add
  34. *
  35. * @private
  36. * @param {Array<number>} v1 coordinates [x, y]
  37. * @param {Array<number>} v2 coordinates [x, y]
  38. * @returns {Array<number>} Add
  39. */
  40. function add(v1, v2) {
  41. return [v1[0] + v2[0], v1[1] + v2[1]];
  42. }
  43. /**
  44. * Sub
  45. *
  46. * @private
  47. * @param {Array<number>} v1 coordinates [x, y]
  48. * @param {Array<number>} v2 coordinates [x, y]
  49. * @returns {Array<number>} Sub
  50. */
  51. function sub(v1, v2) {
  52. return [v1[0] - v2[0], v1[1] - v2[1]];
  53. }
  54. /**
  55. * scalarMult
  56. *
  57. * @private
  58. * @param {number} s scalar
  59. * @param {Array<number>} v coordinates [x, y]
  60. * @returns {Array<number>} scalarMult
  61. */
  62. function scalarMult(s, v) {
  63. return [s * v[0], s * v[1]];
  64. }
  65. /**
  66. * Intersect Segments
  67. *
  68. * @private
  69. * @param {Array<number>} a coordinates [x, y]
  70. * @param {Array<number>} b coordinates [x, y]
  71. * @returns {Array<number>} intersection
  72. */
  73. function intersectSegments(a, b) {
  74. var p = a[0];
  75. var r = ab(a);
  76. var q = b[0];
  77. var s = ab(b);
  78. var cross = crossProduct(r, s);
  79. var qmp = sub(q, p);
  80. var numerator = crossProduct(qmp, s);
  81. var t = numerator / cross;
  82. var intersection = add(p, scalarMult(t, r));
  83. return intersection;
  84. }
  85. /**
  86. * Is Parallel
  87. *
  88. * @private
  89. * @param {Array<number>} a coordinates [x, y]
  90. * @param {Array<number>} b coordinates [x, y]
  91. * @returns {boolean} true if a and b are parallel (or co-linear)
  92. */
  93. function isParallel(a, b) {
  94. var r = ab(a);
  95. var s = ab(b);
  96. return crossProduct(r, s) === 0;
  97. }
  98. /**
  99. * Intersection
  100. *
  101. * @private
  102. * @param {Array<number>} a coordinates [x, y]
  103. * @param {Array<number>} b coordinates [x, y]
  104. * @returns {Array<number>|boolean} true if a and b are parallel (or co-linear)
  105. */
  106. function intersection(a, b) {
  107. if (isParallel(a, b)) return false;
  108. return intersectSegments(a, b);
  109. }
  110. /**
  111. * Takes a {@link LineString|line} and returns a {@link LineString|line} at offset by the specified distance.
  112. *
  113. * @name lineOffset
  114. * @param {Geometry|Feature<LineString|MultiLineString>} geojson input GeoJSON
  115. * @param {number} distance distance to offset the line (can be of negative value)
  116. * @param {Object} [options={}] Optional parameters
  117. * @param {string} [options.units='kilometers'] can be degrees, radians, miles, kilometers, inches, yards, meters
  118. * @returns {Feature<LineString|MultiLineString>} Line offset from the input line
  119. * @example
  120. * var line = turf.lineString([[-83, 30], [-84, 36], [-78, 41]], { "stroke": "#F00" });
  121. *
  122. * var offsetLine = turf.lineOffset(line, 2, {units: 'miles'});
  123. *
  124. * //addToMap
  125. * var addToMap = [offsetLine, line]
  126. * offsetLine.properties.stroke = "#00F"
  127. */
  128. function lineOffset(geojson, distance, options) {
  129. // Optional parameters
  130. options = options || {};
  131. if (!isObject(options)) throw new Error("options is invalid");
  132. var units = options.units;
  133. // Valdiation
  134. if (!geojson) throw new Error("geojson is required");
  135. if (distance === undefined || distance === null || isNaN(distance))
  136. throw new Error("distance is required");
  137. var type = getType(geojson);
  138. var properties = geojson.properties;
  139. switch (type) {
  140. case "LineString":
  141. return lineOffsetFeature(geojson, distance, units);
  142. case "MultiLineString":
  143. var coords = [];
  144. flattenEach(geojson, function (feature) {
  145. coords.push(
  146. lineOffsetFeature(feature, distance, units).geometry.coordinates
  147. );
  148. });
  149. return multiLineString(coords, properties);
  150. default:
  151. throw new Error("geometry " + type + " is not supported");
  152. }
  153. }
  154. /**
  155. * Line Offset
  156. *
  157. * @private
  158. * @param {Geometry|Feature<LineString>} line input line
  159. * @param {number} distance distance to offset the line (can be of negative value)
  160. * @param {string} [units=kilometers] units
  161. * @returns {Feature<LineString>} Line offset from the input line
  162. */
  163. function lineOffsetFeature(line, distance, units) {
  164. var segments = [];
  165. var offsetDegrees = lengthToDegrees(distance, units);
  166. var coords = getCoords(line);
  167. var finalCoords = [];
  168. coords.forEach(function (currentCoords, index) {
  169. if (index !== coords.length - 1) {
  170. var segment = processSegment(
  171. currentCoords,
  172. coords[index + 1],
  173. offsetDegrees
  174. );
  175. segments.push(segment);
  176. if (index > 0) {
  177. var seg2Coords = segments[index - 1];
  178. var intersects = intersection(segment, seg2Coords);
  179. // Handling for line segments that aren't straight
  180. if (intersects !== false) {
  181. seg2Coords[1] = intersects;
  182. segment[0] = intersects;
  183. }
  184. finalCoords.push(seg2Coords[0]);
  185. if (index === coords.length - 2) {
  186. finalCoords.push(segment[0]);
  187. finalCoords.push(segment[1]);
  188. }
  189. }
  190. // Handling for lines that only have 1 segment
  191. if (coords.length === 2) {
  192. finalCoords.push(segment[0]);
  193. finalCoords.push(segment[1]);
  194. }
  195. }
  196. });
  197. return lineString(finalCoords, line.properties);
  198. }
  199. /**
  200. * Process Segment
  201. * Inspiration taken from http://stackoverflow.com/questions/2825412/draw-a-parallel-line
  202. *
  203. * @private
  204. * @param {Array<number>} point1 Point coordinates
  205. * @param {Array<number>} point2 Point coordinates
  206. * @param {number} offset Offset
  207. * @returns {Array<Array<number>>} offset points
  208. */
  209. function processSegment(point1, point2, offset) {
  210. var L = Math.sqrt(
  211. (point1[0] - point2[0]) * (point1[0] - point2[0]) +
  212. (point1[1] - point2[1]) * (point1[1] - point2[1])
  213. );
  214. var out1x = point1[0] + (offset * (point2[1] - point1[1])) / L;
  215. var out2x = point2[0] + (offset * (point2[1] - point1[1])) / L;
  216. var out1y = point1[1] + (offset * (point1[0] - point2[0])) / L;
  217. var out2y = point2[1] + (offset * (point1[0] - point2[0])) / L;
  218. return [
  219. [out1x, out1y],
  220. [out2x, out2y],
  221. ];
  222. }
  223. export default lineOffset;