index.js 7.2 KB

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