index.cjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _helpers = require('@turf/helpers');
  3. function randomPosition(bbox) {
  4. checkBBox(bbox);
  5. return randomPositionUnchecked(bbox);
  6. }
  7. function randomPositionUnchecked(bbox) {
  8. if (Array.isArray(bbox)) {
  9. return coordInBBox(bbox);
  10. }
  11. if (bbox && bbox.bbox) {
  12. return coordInBBox(bbox.bbox);
  13. }
  14. return [lon(), lat()];
  15. }
  16. function checkBBox(bbox) {
  17. if (bbox == null) {
  18. return;
  19. } else if (Array.isArray(bbox)) {
  20. _helpers.validateBBox.call(void 0, bbox);
  21. } else if (bbox.bbox != null) {
  22. _helpers.validateBBox.call(void 0, bbox.bbox);
  23. }
  24. }
  25. function randomPoint(count, options = {}) {
  26. checkBBox(options.bbox);
  27. if (count === void 0 || count === null) {
  28. count = 1;
  29. }
  30. const features = [];
  31. for (let i = 0; i < count; i++) {
  32. features.push(_helpers.point.call(void 0, randomPositionUnchecked(options.bbox)));
  33. }
  34. return _helpers.featureCollection.call(void 0, features);
  35. }
  36. function randomPolygon(count, options = {}) {
  37. checkBBox(options.bbox);
  38. if (count === void 0 || count === null) {
  39. count = 1;
  40. }
  41. if (options.bbox === void 0 || options.bbox === null) {
  42. options.bbox = [-180, -90, 180, 90];
  43. }
  44. if (!_helpers.isNumber.call(void 0, options.num_vertices) || options.num_vertices === void 0) {
  45. options.num_vertices = 10;
  46. }
  47. if (!_helpers.isNumber.call(void 0, options.max_radial_length) || options.max_radial_length === void 0) {
  48. options.max_radial_length = 10;
  49. }
  50. const bboxWidth = Math.abs(options.bbox[0] - options.bbox[2]);
  51. const bboxHeight = Math.abs(options.bbox[1] - options.bbox[3]);
  52. const maxRadius = Math.min(bboxWidth / 2, bboxHeight / 2);
  53. if (options.max_radial_length > maxRadius) {
  54. throw new Error("max_radial_length is greater than the radius of the bbox");
  55. }
  56. const paddedBbox = [
  57. options.bbox[0] + options.max_radial_length,
  58. options.bbox[1] + options.max_radial_length,
  59. options.bbox[2] - options.max_radial_length,
  60. options.bbox[3] - options.max_radial_length
  61. ];
  62. const features = [];
  63. for (let i = 0; i < count; i++) {
  64. let vertices = [];
  65. const circleOffsets = [...Array(options.num_vertices + 1)].map(Math.random);
  66. circleOffsets.forEach((cur, index, arr) => {
  67. arr[index] = index > 0 ? cur + arr[index - 1] : cur;
  68. });
  69. circleOffsets.forEach((cur) => {
  70. cur = cur * 2 * Math.PI / circleOffsets[circleOffsets.length - 1];
  71. const radialScaler = Math.random();
  72. vertices.push([
  73. radialScaler * (options.max_radial_length || 10) * Math.sin(cur),
  74. radialScaler * (options.max_radial_length || 10) * Math.cos(cur)
  75. ]);
  76. });
  77. vertices[vertices.length - 1] = vertices[0];
  78. vertices = vertices.reverse().map(vertexToCoordinate(randomPositionUnchecked(paddedBbox)));
  79. features.push(_helpers.polygon.call(void 0, [vertices]));
  80. }
  81. return _helpers.featureCollection.call(void 0, features);
  82. }
  83. function randomLineString(count, options = {}) {
  84. options = options || {};
  85. if (!_helpers.isObject.call(void 0, options)) {
  86. throw new Error("options is invalid");
  87. }
  88. const bbox = options.bbox;
  89. checkBBox(bbox);
  90. let num_vertices = options.num_vertices;
  91. let max_length = options.max_length;
  92. let max_rotation = options.max_rotation;
  93. if (count === void 0 || count === null) {
  94. count = 1;
  95. }
  96. if (!_helpers.isNumber.call(void 0, num_vertices) || num_vertices === void 0 || num_vertices < 2) {
  97. num_vertices = 10;
  98. }
  99. if (!_helpers.isNumber.call(void 0, max_length) || max_length === void 0) {
  100. max_length = 1e-4;
  101. }
  102. if (!_helpers.isNumber.call(void 0, max_rotation) || max_rotation === void 0) {
  103. max_rotation = Math.PI / 8;
  104. }
  105. const features = [];
  106. for (let i = 0; i < count; i++) {
  107. const startingPoint = randomPositionUnchecked(bbox);
  108. const vertices = [startingPoint];
  109. for (let j = 0; j < num_vertices - 1; j++) {
  110. const priorAngle = j === 0 ? Math.random() * 2 * Math.PI : Math.tan(
  111. (vertices[j][1] - vertices[j - 1][1]) / (vertices[j][0] - vertices[j - 1][0])
  112. );
  113. const angle = priorAngle + (Math.random() - 0.5) * max_rotation * 2;
  114. const distance = Math.random() * max_length;
  115. vertices.push([
  116. vertices[j][0] + distance * Math.cos(angle),
  117. vertices[j][1] + distance * Math.sin(angle)
  118. ]);
  119. }
  120. features.push(_helpers.lineString.call(void 0, vertices));
  121. }
  122. return _helpers.featureCollection.call(void 0, features);
  123. }
  124. function vertexToCoordinate(hub) {
  125. return (cur) => {
  126. return [cur[0] + hub[0], cur[1] + hub[1]];
  127. };
  128. }
  129. function rnd() {
  130. return Math.random() - 0.5;
  131. }
  132. function lon() {
  133. return rnd() * 360;
  134. }
  135. function lat() {
  136. return rnd() * 180;
  137. }
  138. function coordInBBox(bbox) {
  139. return [
  140. Math.random() * (bbox[2] - bbox[0]) + bbox[0],
  141. Math.random() * (bbox[3] - bbox[1]) + bbox[1]
  142. ];
  143. }
  144. exports.randomLineString = randomLineString; exports.randomPoint = randomPoint; exports.randomPolygon = randomPolygon; exports.randomPosition = randomPosition;
  145. //# sourceMappingURL=index.cjs.map