Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@turf/random

Package Overview
Dependencies
Maintainers
4
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/random - npm Package Compare versions

Comparing version 5.1.5 to 6.0.1

index.ts

97

index.d.ts

@@ -1,41 +0,72 @@

import { BBox, FeatureCollection, Feature, Point, LineString, Polygon, Position } from '@turf/helpers';
import { BBox, FeatureCollection, LineString, Point, Polygon, Position } from "@turf/helpers";
/**
* http://turfjs.org/docs/#randomposition
* Returns a random position within a {@link bounding box}.
*
* @name randomPosition
* @param {Array<number>} [bbox=[-180, -90, 180, 90]] a bounding box inside of which positions are placed.
* @returns {Array<number>} Position [longitude, latitude]
* @example
* var position = turf.randomPosition([-180, -90, 180, 90])
* // => position
*/
export function randomPosition(bbox?: BBox | {bbox?: BBox}): Position
export declare function randomPosition(bbox?: BBox | {
bbox: BBox;
}): Position;
/**
* http://turfjs.org/docs/#randompoint
* Returns a random {@link point}.
*
* @name randomPoint
* @param {number} [count=1] how many geometries will be generated
* @param {Object} [options={}] Optional parameters
* @param {Array<number>} [options.bbox=[-180, -90, 180, 90]] a bounding box inside of which geometries are placed.
* @returns {FeatureCollection<Point>} GeoJSON FeatureCollection of points
* @example
* var points = turf.randomPoint(25, {bbox: [-180, -90, 180, 90]})
* // => points
*/
export function randomPoint(
count?: number,
options?: {
bbox?: BBox
}
): FeatureCollection<Point>
export declare function randomPoint(count?: number, options?: {
bbox?: BBox;
}): FeatureCollection<Point, any>;
/**
* http://turfjs.org/docs/#randomlinestring
* Returns a random {@link polygon}.
*
* @name randomPolygon
* @param {number} [count=1] how many geometries will be generated
* @param {Object} [options={}] Optional parameters
* @param {Array<number>} [options.bbox=[-180, -90, 180, 90]] a bounding box inside of which geometries are placed.
* @param {number} [options.num_vertices=10] is how many coordinates each LineString will contain.
* @param {number} [options.max_radial_length=10] is the maximum number of decimal degrees latitude or longitude that a
* vertex can reach out of the center of the Polygon.
* @returns {FeatureCollection<Polygon>} GeoJSON FeatureCollection of polygons
* @example
* var polygons = turf.randomPolygon(25, {bbox: [-180, -90, 180, 90]})
* // => polygons
*/
export function randomLineString(
count?: number,
options?: {
bbox?: BBox,
num_vertices?: number,
max_length?: number,
max_rotation?: number
}
): FeatureCollection<LineString>
export declare function randomPolygon(count?: number, options?: {
bbox?: BBox;
num_vertices?: number;
max_radial_length?: number;
}): FeatureCollection<Polygon, any>;
/**
* http://turfjs.org/docs/#randompolygon
* Returns a random {@link linestring}.
*
* @name randomLineString
* @param {number} [count=1] how many geometries will be generated
* @param {Object} [options={}] Optional parameters
* @param {Array<number>} [options.bbox=[-180, -90, 180, 90]] a bounding box inside of which geometries are placed.
* @param {number} [options.num_vertices=10] is how many coordinates each LineString will contain.
* @param {number} [options.max_length=0.0001] is the maximum number of decimal degrees that a
* vertex can be from its predecessor
* @param {number} [options.max_rotation=Math.PI / 8] is the maximum number of radians that a
* line segment can turn from the previous segment.
* @returns {FeatureCollection<LineString>} GeoJSON FeatureCollection of linestrings
* @example
* var lineStrings = turf.randomLineString(25, {bbox: [-180, -90, 180, 90]})
* // => lineStrings
*/
export function randomPolygon(
count?: number,
options?: {
bbox?: BBox,
num_vertices?: number,
max_radial_length?: number
}
): FeatureCollection<LineString>
export declare function randomLineString(count?: number, options?: {
bbox?: BBox;
num_vertices?: number;
max_length?: number;
max_rotation?: number;
}): FeatureCollection<LineString, any>;

@@ -1,10 +0,4 @@

import {
point,
lineString,
polygon,
featureCollection,
isObject,
isNumber
} from '@turf/helpers';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var helpers_1 = require("@turf/helpers");
/**

@@ -18,11 +12,14 @@ * Returns a random position within a {@link bounding box}.

* var position = turf.randomPosition([-180, -90, 180, 90])
* //=position
* // => position
*/
export function randomPosition(bbox) {
if (isObject(bbox)) bbox = bbox.bbox;
if (bbox && !Array.isArray(bbox)) throw new Error('bbox is invalid');
if (bbox) return coordInBBox(bbox);
else return [lon(), lat()];
function randomPosition(bbox) {
if (Array.isArray(bbox)) {
return coordInBBox(bbox);
}
if (bbox && bbox.bbox) {
return coordInBBox(bbox.bbox);
}
return [lon(), lat()];
}
exports.randomPosition = randomPosition;
/**

@@ -38,18 +35,16 @@ * Returns a random {@link point}.

* var points = turf.randomPoint(25, {bbox: [-180, -90, 180, 90]})
* //=points
* // => points
*/
export function randomPoint(count, options) {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
var bbox = options.bbox;
if (count === undefined || count === null) count = 1;
function randomPoint(count, options) {
if (options === void 0) { options = {}; }
if (count === undefined || count === null) {
count = 1;
}
var features = [];
for (var i = 0; i < count; i++) {
features.push(point(randomPosition(bbox)));
features.push(helpers_1.point(randomPosition(options.bbox)));
}
return featureCollection(features);
return helpers_1.featureCollection(features);
}
exports.randomPoint = randomPoint;
/**

@@ -63,52 +58,49 @@ * Returns a random {@link polygon}.

* @param {number} [options.num_vertices=10] is how many coordinates each LineString will contain.
* @param {number} [options.max_radial_length=10] is the maximum number of decimal degrees latitude or longitude that a vertex can reach out of the center of the Polygon.
* @returns {FeatureCollection<Point>} GeoJSON FeatureCollection of points
* @param {number} [options.max_radial_length=10] is the maximum number of decimal degrees latitude or longitude that a
* vertex can reach out of the center of the Polygon.
* @returns {FeatureCollection<Polygon>} GeoJSON FeatureCollection of polygons
* @example
* var polygons = turf.randomPolygon(25, {bbox: [-180, -90, 180, 90]})
* //=polygons
* // => polygons
*/
export function randomPolygon(count, options) {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
var bbox = options.bbox;
var num_vertices = options.num_vertices;
var max_radial_length = options.max_radial_length;
if (count === undefined || count === null) count = 1;
// Validation
if (!isNumber(num_vertices)) num_vertices = 10;
if (!isNumber(max_radial_length)) max_radial_length = 10;
function randomPolygon(count, options) {
if (options === void 0) { options = {}; }
// Default param
if (count === undefined || count === null) {
count = 1;
}
if (!helpers_1.isNumber(options.num_vertices) || options.num_vertices === undefined) {
options.num_vertices = 10;
}
if (!helpers_1.isNumber(options.max_radial_length) || options.max_radial_length === undefined) {
options.max_radial_length = 10;
}
var features = [];
for (var i = 0; i < count; i++) {
var vertices = [],
circle_offsets = Array.apply(null,
new Array(num_vertices + 1)).map(Math.random);
circle_offsets.forEach(sumOffsets);
circle_offsets.forEach(scaleOffsets);
var _loop_1 = function (i) {
var vertices = [];
var circleOffsets = Array.apply(null, new Array(options.num_vertices + 1)).map(Math.random);
// Sum Offsets
circleOffsets.forEach(function (cur, index, arr) {
arr[index] = (index > 0) ? cur + arr[index - 1] : cur;
});
// scaleOffsets
circleOffsets.forEach(function (cur) {
cur = cur * 2 * Math.PI / circleOffsets[circleOffsets.length - 1];
var radialScaler = Math.random();
vertices.push([
radialScaler * (options.max_radial_length || 10) * Math.sin(cur),
radialScaler * (options.max_radial_length || 10) * Math.cos(cur),
]);
});
vertices[vertices.length - 1] = vertices[0]; // close the ring
// center the polygon around something
vertices = vertices.map(vertexToCoordinate(randomPosition(bbox)));
features.push(polygon([vertices]));
vertices = vertices.map(vertexToCoordinate(randomPosition(options.bbox)));
features.push(helpers_1.polygon([vertices]));
};
for (var i = 0; i < count; i++) {
_loop_1(i);
}
function sumOffsets(cur, index, arr) {
arr[index] = (index > 0) ? cur + arr[index - 1] : cur;
}
function scaleOffsets(cur) {
cur = cur * 2 * Math.PI / circle_offsets[circle_offsets.length - 1];
var radial_scaler = Math.random();
vertices.push([
radial_scaler * max_radial_length * Math.sin(cur),
radial_scaler * max_radial_length * Math.cos(cur)
]);
}
return featureCollection(features);
return helpers_1.featureCollection(features);
}
exports.randomPolygon = randomPolygon;
/**

@@ -122,13 +114,18 @@ * Returns a random {@link linestring}.

* @param {number} [options.num_vertices=10] is how many coordinates each LineString will contain.
* @param {number} [options.max_length=0.0001] is the maximum number of decimal degrees that a vertex can be from its predecessor
* @param {number} [options.max_rotation=Math.PI / 8] is the maximum number of radians that a line segment can turn from the previous segment.
* @returns {FeatureCollection<Point>} GeoJSON FeatureCollection of points
* @param {number} [options.max_length=0.0001] is the maximum number of decimal degrees that a
* vertex can be from its predecessor
* @param {number} [options.max_rotation=Math.PI / 8] is the maximum number of radians that a
* line segment can turn from the previous segment.
* @returns {FeatureCollection<LineString>} GeoJSON FeatureCollection of linestrings
* @example
* var lineStrings = turf.randomLineString(25, {bbox: [-180, -90, 180, 90]})
* //=lineStrings
* // => lineStrings
*/
export function randomLineString(count, options) {
function randomLineString(count, options) {
if (options === void 0) { options = {}; }
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
if (!helpers_1.isObject(options)) {
throw new Error("options is invalid");
}
var bbox = options.bbox;

@@ -138,9 +135,15 @@ var num_vertices = options.num_vertices;

var max_rotation = options.max_rotation;
if (count === undefined || count === null) count = 1;
if (count === undefined || count === null) {
count = 1;
}
// Default parameters
if (!isNumber(num_vertices) || num_vertices < 2) num_vertices = 10;
if (!isNumber(max_length)) max_length = 0.0001;
if (!isNumber(max_rotation)) max_rotation = Math.PI / 8;
if (!helpers_1.isNumber(num_vertices) || num_vertices === undefined || num_vertices < 2) {
num_vertices = 10;
}
if (!helpers_1.isNumber(max_length) || max_length === undefined) {
max_length = 0.0001;
}
if (!helpers_1.isNumber(max_rotation) || max_rotation === undefined) {
max_rotation = Math.PI / 8;
}
var features = [];

@@ -153,6 +156,4 @@ for (var i = 0; i < count; i++) {

Math.random() * 2 * Math.PI :
Math.tan(
(vertices[j][1] - vertices[j - 1][1]) /
(vertices[j][0] - vertices[j - 1][0])
);
Math.tan((vertices[j][1] - vertices[j - 1][1]) /
(vertices[j][0] - vertices[j - 1][0]));
var angle = priorAngle + (Math.random() - 0.5) * max_rotation * 2;

@@ -162,23 +163,23 @@ var distance = Math.random() * max_length;

vertices[j][0] + distance * Math.cos(angle),
vertices[j][1] + distance * Math.sin(angle)
vertices[j][1] + distance * Math.sin(angle),
]);
}
features.push(lineString(vertices));
features.push(helpers_1.lineString(vertices));
}
return featureCollection(features);
return helpers_1.featureCollection(features);
}
exports.randomLineString = randomLineString;
function vertexToCoordinate(hub) {
return function (cur) { return [cur[0] + hub[0], cur[1] + hub[1]]; };
return function (cur) {
return [cur[0] + hub[0], cur[1] + hub[1]];
};
}
function rnd() { return Math.random() - 0.5; }
function lon() { return rnd() * 360; }
function lat() { return rnd() * 180; }
function coordInBBox(bbox) {
return [
(Math.random() * (bbox[2] - bbox[0])) + bbox[0],
(Math.random() * (bbox[3] - bbox[1])) + bbox[1]];
(Math.random() * (bbox[3] - bbox[1])) + bbox[1]
];
}
{
"name": "@turf/random",
"version": "5.1.5",
"version": "6.0.1",
"description": "turf random module",
"main": "main.js",
"module": "main.es.js",
"main": "index",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts",
"main.js",
"main.es.js"
"index.ts",
"index.d.ts"
],
"scripts": {
"pretest": "rollup -c ../../rollup.config.js",
"test": "node -r @std/esm test.js",
"posttest": "node -r @std/esm ../../scripts/validate-es5-dependencies.js",
"bench": "node -r @std/esm bench.js",
"prepare": "tsc",
"pretest": "tsc",
"test": "node test.js",
"bench": "node bench.js",
"docs": "node ../../scripts/generate-readmes"

@@ -36,15 +34,12 @@ },

"devDependencies": {
"@std/esm": "*",
"benchmark": "*",
"glob": "*",
"rollup": "*",
"tape": "*"
"typescript": "*",
"tape": "*",
"tslint": "*",
"@types/tape": "*"
},
"dependencies": {
"@turf/helpers": "^5.1.5"
},
"@std/esm": {
"esm": "js",
"cjs": true
"@turf/helpers": "6.x"
}
}

@@ -7,7 +7,7 @@ # @turf/random

Returns a random position within a [box](bounding).
Returns a random position within a [box][1].
**Parameters**
- `bbox` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** a bounding box inside of which positions are placed. (optional, default `[-180,-90,180,90]`)
- `bbox` **[Array][2]&lt;[number][3]>** a bounding box inside of which positions are placed. (optional, default `[-180,-90,180,90]`)

@@ -18,16 +18,16 @@ **Examples**

var position = turf.randomPosition([-180, -90, 180, 90])
//=position
// => position
```
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** Position [longitude, latitude]
Returns **[Array][2]&lt;[number][3]>** Position [longitude, latitude]
## randomPoint
Returns a random [point](point).
Returns a random [point][4].
**Parameters**
- `count` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** how many geometries will be generated (optional, default `1`)
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional parameters (optional, default `{}`)
- `options.bbox` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** a bounding box inside of which geometries are placed. (optional, default `[-180,-90,180,90]`)
- `count` **[number][3]** how many geometries will be generated (optional, default `1`)
- `options` **[Object][5]** Optional parameters (optional, default `{}`)
- `options.bbox` **[Array][2]&lt;[number][3]>** a bounding box inside of which geometries are placed. (optional, default `[-180,-90,180,90]`)

@@ -38,18 +38,18 @@ **Examples**

var points = turf.randomPoint(25, {bbox: [-180, -90, 180, 90]})
//=points
// => points
```
Returns **[FeatureCollection](https://tools.ietf.org/html/rfc7946#section-3.3)&lt;[Point](https://tools.ietf.org/html/rfc7946#section-3.1.2)>** GeoJSON FeatureCollection of points
Returns **[FeatureCollection][6]&lt;[Point][7]>** GeoJSON FeatureCollection of points
## randomPolygon
Returns a random [polygon](polygon).
Returns a random [polygon][8].
**Parameters**
- `count` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** how many geometries will be generated (optional, default `1`)
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional parameters (optional, default `{}`)
- `options.bbox` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** a bounding box inside of which geometries are placed. (optional, default `[-180,-90,180,90]`)
- `options.num_vertices` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** is how many coordinates each LineString will contain. (optional, default `10`)
- `options.max_radial_length` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** is the maximum number of decimal degrees latitude or longitude that a vertex can reach out of the center of the Polygon. (optional, default `10`)
- `count` **[number][3]** how many geometries will be generated (optional, default `1`)
- `options` **[Object][5]** Optional parameters (optional, default `{}`)
- `options.bbox` **[Array][2]&lt;[number][3]>** a bounding box inside of which geometries are placed. (optional, default `[-180,-90,180,90]`)
- `options.num_vertices` **[number][3]** is how many coordinates each LineString will contain. (optional, default `10`)
- `options.max_radial_length` **[number][3]** is the maximum number of decimal degrees latitude or longitude that a vertex can reach out of the center of the Polygon. (optional, default `10`)

@@ -60,19 +60,19 @@ **Examples**

var polygons = turf.randomPolygon(25, {bbox: [-180, -90, 180, 90]})
//=polygons
// => polygons
```
Returns **[FeatureCollection](https://tools.ietf.org/html/rfc7946#section-3.3)&lt;[Point](https://tools.ietf.org/html/rfc7946#section-3.1.2)>** GeoJSON FeatureCollection of points
Returns **[FeatureCollection][6]&lt;[Polygon][9]>** GeoJSON FeatureCollection of polygons
## randomLineString
Returns a random [linestring](linestring).
Returns a random [linestring][10].
**Parameters**
- `count` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** how many geometries will be generated (optional, default `1`)
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional parameters (optional, default `{}`)
- `options.bbox` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** a bounding box inside of which geometries are placed. (optional, default `[-180,-90,180,90]`)
- `options.num_vertices` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** is how many coordinates each LineString will contain. (optional, default `10`)
- `options.max_length` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** is the maximum number of decimal degrees that a vertex can be from its predecessor (optional, default `0.0001`)
- `options.max_rotation` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** is the maximum number of radians that a line segment can turn from the previous segment. (optional, default `Math.PI/8`)
- `count` **[number][3]** how many geometries will be generated (optional, default `1`)
- `options` **[Object][5]** Optional parameters (optional, default `{}`)
- `options.bbox` **[Array][2]&lt;[number][3]>** a bounding box inside of which geometries are placed. (optional, default `[-180,-90,180,90]`)
- `options.num_vertices` **[number][3]** is how many coordinates each LineString will contain. (optional, default `10`)
- `options.max_length` **[number][3]** is the maximum number of decimal degrees that a vertex can be from its predecessor (optional, default `0.0001`)
- `options.max_rotation` **[number][3]** is the maximum number of radians that a line segment can turn from the previous segment. (optional, default `Math.PI/8`)

@@ -83,7 +83,29 @@ **Examples**

var lineStrings = turf.randomLineString(25, {bbox: [-180, -90, 180, 90]})
//=lineStrings
// => lineStrings
```
Returns **[FeatureCollection](https://tools.ietf.org/html/rfc7946#section-3.3)&lt;[Point](https://tools.ietf.org/html/rfc7946#section-3.1.2)>** GeoJSON FeatureCollection of points
Returns **[FeatureCollection][6]&lt;[LineString][11]>** GeoJSON FeatureCollection of linestrings
[1]: bounding
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[4]: point
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[6]: https://tools.ietf.org/html/rfc7946#section-3.3
[7]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[8]: polygon
[9]: https://tools.ietf.org/html/rfc7946#section-3.1.6
[10]: linestring
[11]: https://tools.ietf.org/html/rfc7946#section-3.1.4
<!-- This file is automatically generated. Please don't edit it directly:

@@ -90,0 +112,0 @@ if you find an error, edit the source file (likely index.js), and re-run

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc