index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. var __spreadArrays = (this && this.__spreadArrays) || function () {
  2. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  3. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  4. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  5. r[k] = a[j];
  6. return r;
  7. };
  8. import { featureCollection, isNumber, isObject, lineString, point, polygon, } from "@turf/helpers";
  9. /**
  10. * Returns a random position within a {@link bounding box}.
  11. *
  12. * @name randomPosition
  13. * @param {Array<number>} [bbox=[-180, -90, 180, 90]] a bounding box inside of which positions are placed.
  14. * @returns {Array<number>} Position [longitude, latitude]
  15. * @example
  16. * var position = turf.randomPosition([-180, -90, 180, 90])
  17. * // => position
  18. */
  19. export function randomPosition(bbox) {
  20. if (Array.isArray(bbox)) {
  21. return coordInBBox(bbox);
  22. }
  23. if (bbox && bbox.bbox) {
  24. return coordInBBox(bbox.bbox);
  25. }
  26. return [lon(), lat()];
  27. }
  28. /**
  29. * Returns a random {@link point}.
  30. *
  31. * @name randomPoint
  32. * @param {number} [count=1] how many geometries will be generated
  33. * @param {Object} [options={}] Optional parameters
  34. * @param {Array<number>} [options.bbox=[-180, -90, 180, 90]] a bounding box inside of which geometries are placed.
  35. * @returns {FeatureCollection<Point>} GeoJSON FeatureCollection of points
  36. * @example
  37. * var points = turf.randomPoint(25, {bbox: [-180, -90, 180, 90]})
  38. * // => points
  39. */
  40. export function randomPoint(count, options) {
  41. if (options === void 0) { options = {}; }
  42. if (count === undefined || count === null) {
  43. count = 1;
  44. }
  45. var features = [];
  46. for (var i = 0; i < count; i++) {
  47. features.push(point(randomPosition(options.bbox)));
  48. }
  49. return featureCollection(features);
  50. }
  51. /**
  52. * Returns a random {@link polygon}.
  53. *
  54. * @name randomPolygon
  55. * @param {number} [count=1] how many geometries will be generated
  56. * @param {Object} [options={}] Optional parameters
  57. * @param {Array<number>} [options.bbox=[-180, -90, 180, 90]] a bounding box inside of which geometries are placed.
  58. * @param {number} [options.num_vertices=10] is how many coordinates each LineString will contain.
  59. * @param {number} [options.max_radial_length=10] is the maximum number of decimal degrees latitude or longitude that a
  60. * vertex can reach out of the center of the Polygon.
  61. * @returns {FeatureCollection<Polygon>} GeoJSON FeatureCollection of polygons
  62. * @example
  63. * var polygons = turf.randomPolygon(25, {bbox: [-180, -90, 180, 90]})
  64. * // => polygons
  65. */
  66. export function randomPolygon(count, options) {
  67. if (options === void 0) { options = {}; }
  68. // Default param
  69. if (count === undefined || count === null) {
  70. count = 1;
  71. }
  72. if (!isNumber(options.num_vertices) || options.num_vertices === undefined) {
  73. options.num_vertices = 10;
  74. }
  75. if (!isNumber(options.max_radial_length) ||
  76. options.max_radial_length === undefined) {
  77. options.max_radial_length = 10;
  78. }
  79. var features = [];
  80. var _loop_1 = function (i) {
  81. var vertices = [];
  82. var circleOffsets = __spreadArrays(Array(options.num_vertices + 1)).map(Math.random);
  83. // Sum Offsets
  84. circleOffsets.forEach(function (cur, index, arr) {
  85. arr[index] = index > 0 ? cur + arr[index - 1] : cur;
  86. });
  87. // scaleOffsets
  88. circleOffsets.forEach(function (cur) {
  89. cur = (cur * 2 * Math.PI) / circleOffsets[circleOffsets.length - 1];
  90. var radialScaler = Math.random();
  91. vertices.push([
  92. radialScaler * (options.max_radial_length || 10) * Math.sin(cur),
  93. radialScaler * (options.max_radial_length || 10) * Math.cos(cur),
  94. ]);
  95. });
  96. vertices[vertices.length - 1] = vertices[0]; // close the ring
  97. // center the polygon around something
  98. vertices = vertices.map(vertexToCoordinate(randomPosition(options.bbox)));
  99. features.push(polygon([vertices]));
  100. };
  101. for (var i = 0; i < count; i++) {
  102. _loop_1(i);
  103. }
  104. return featureCollection(features);
  105. }
  106. /**
  107. * Returns a random {@link linestring}.
  108. *
  109. * @name randomLineString
  110. * @param {number} [count=1] how many geometries will be generated
  111. * @param {Object} [options={}] Optional parameters
  112. * @param {Array<number>} [options.bbox=[-180, -90, 180, 90]] a bounding box inside of which geometries are placed.
  113. * @param {number} [options.num_vertices=10] is how many coordinates each LineString will contain.
  114. * @param {number} [options.max_length=0.0001] is the maximum number of decimal degrees that a
  115. * vertex can be from its predecessor
  116. * @param {number} [options.max_rotation=Math.PI / 8] is the maximum number of radians that a
  117. * line segment can turn from the previous segment.
  118. * @returns {FeatureCollection<LineString>} GeoJSON FeatureCollection of linestrings
  119. * @example
  120. * var lineStrings = turf.randomLineString(25, {bbox: [-180, -90, 180, 90]})
  121. * // => lineStrings
  122. */
  123. export function randomLineString(count, options) {
  124. if (options === void 0) { options = {}; }
  125. // Optional parameters
  126. options = options || {};
  127. if (!isObject(options)) {
  128. throw new Error("options is invalid");
  129. }
  130. var bbox = options.bbox;
  131. var num_vertices = options.num_vertices;
  132. var max_length = options.max_length;
  133. var max_rotation = options.max_rotation;
  134. if (count === undefined || count === null) {
  135. count = 1;
  136. }
  137. // Default parameters
  138. if (!isNumber(num_vertices) ||
  139. num_vertices === undefined ||
  140. num_vertices < 2) {
  141. num_vertices = 10;
  142. }
  143. if (!isNumber(max_length) || max_length === undefined) {
  144. max_length = 0.0001;
  145. }
  146. if (!isNumber(max_rotation) || max_rotation === undefined) {
  147. max_rotation = Math.PI / 8;
  148. }
  149. var features = [];
  150. for (var i = 0; i < count; i++) {
  151. var startingPoint = randomPosition(bbox);
  152. var vertices = [startingPoint];
  153. for (var j = 0; j < num_vertices - 1; j++) {
  154. var priorAngle = j === 0
  155. ? Math.random() * 2 * Math.PI
  156. : Math.tan((vertices[j][1] - vertices[j - 1][1]) /
  157. (vertices[j][0] - vertices[j - 1][0]));
  158. var angle = priorAngle + (Math.random() - 0.5) * max_rotation * 2;
  159. var distance = Math.random() * max_length;
  160. vertices.push([
  161. vertices[j][0] + distance * Math.cos(angle),
  162. vertices[j][1] + distance * Math.sin(angle),
  163. ]);
  164. }
  165. features.push(lineString(vertices));
  166. }
  167. return featureCollection(features);
  168. }
  169. function vertexToCoordinate(hub) {
  170. return function (cur) {
  171. return [cur[0] + hub[0], cur[1] + hub[1]];
  172. };
  173. }
  174. function rnd() {
  175. return Math.random() - 0.5;
  176. }
  177. function lon() {
  178. return rnd() * 360;
  179. }
  180. function lat() {
  181. return rnd() * 180;
  182. }
  183. function coordInBBox(bbox) {
  184. return [
  185. Math.random() * (bbox[2] - bbox[0]) + bbox[0],
  186. Math.random() * (bbox[3] - bbox[1]) + bbox[1],
  187. ];
  188. }