@turf/random
Advanced tools
Comparing version 6.5.0 to 7.0.0-alpha.0
@@ -1,9 +0,2 @@ | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
import { featureCollection, isNumber, isObject, lineString, point, polygon, } from "@turf/helpers"; | ||
import { featureCollection, isNumber, isObject, lineString, point, polygon, validateBBox, } from "@turf/helpers"; | ||
/** | ||
@@ -15,2 +8,3 @@ * Returns a random position within a {@link bounding box}. | ||
* @returns {Array<number>} Position [longitude, latitude] | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -21,2 +15,7 @@ * var position = turf.randomPosition([-180, -90, 180, 90]) | ||
export function randomPosition(bbox) { | ||
checkBBox(bbox); | ||
return randomPositionUnchecked(bbox); | ||
} | ||
// does not check bbox for validity, that is handled by the exported functions | ||
function randomPositionUnchecked(bbox) { | ||
if (Array.isArray(bbox)) { | ||
@@ -30,2 +29,13 @@ return coordInBBox(bbox); | ||
} | ||
function checkBBox(bbox) { | ||
if (bbox == null) { | ||
return; | ||
} | ||
else if (Array.isArray(bbox)) { | ||
validateBBox(bbox); | ||
} | ||
else if (bbox.bbox != null) { | ||
validateBBox(bbox.bbox); | ||
} | ||
} | ||
/** | ||
@@ -39,2 +49,3 @@ * Returns a random {@link point}. | ||
* @returns {FeatureCollection<Point>} GeoJSON FeatureCollection of points | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -44,10 +55,10 @@ * var points = turf.randomPoint(25, {bbox: [-180, -90, 180, 90]}) | ||
*/ | ||
export function randomPoint(count, options) { | ||
if (options === void 0) { options = {}; } | ||
export function randomPoint(count, options = {}) { | ||
checkBBox(options.bbox); | ||
if (count === undefined || count === null) { | ||
count = 1; | ||
} | ||
var features = []; | ||
for (var i = 0; i < count; i++) { | ||
features.push(point(randomPosition(options.bbox))); | ||
const features = []; | ||
for (let i = 0; i < count; i++) { | ||
features.push(point(randomPositionUnchecked(options.bbox))); | ||
} | ||
@@ -67,2 +78,3 @@ return featureCollection(features); | ||
* @returns {FeatureCollection<Polygon>} GeoJSON FeatureCollection of polygons | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -72,4 +84,4 @@ * var polygons = turf.randomPolygon(25, {bbox: [-180, -90, 180, 90]}) | ||
*/ | ||
export function randomPolygon(count, options) { | ||
if (options === void 0) { options = {}; } | ||
export function randomPolygon(count, options = {}) { | ||
checkBBox(options.bbox); | ||
// Default param | ||
@@ -86,14 +98,14 @@ if (count === undefined || count === null) { | ||
} | ||
var features = []; | ||
var _loop_1 = function (i) { | ||
var vertices = []; | ||
var circleOffsets = __spreadArrays(Array(options.num_vertices + 1)).map(Math.random); | ||
const features = []; | ||
for (let i = 0; i < count; i++) { | ||
let vertices = []; | ||
const circleOffsets = [...Array(options.num_vertices + 1)].map(Math.random); | ||
// Sum Offsets | ||
circleOffsets.forEach(function (cur, index, arr) { | ||
circleOffsets.forEach((cur, index, arr) => { | ||
arr[index] = index > 0 ? cur + arr[index - 1] : cur; | ||
}); | ||
// scaleOffsets | ||
circleOffsets.forEach(function (cur) { | ||
circleOffsets.forEach((cur) => { | ||
cur = (cur * 2 * Math.PI) / circleOffsets[circleOffsets.length - 1]; | ||
var radialScaler = Math.random(); | ||
const radialScaler = Math.random(); | ||
vertices.push([ | ||
@@ -106,7 +118,4 @@ radialScaler * (options.max_radial_length || 10) * Math.sin(cur), | ||
// center the polygon around something | ||
vertices = vertices.map(vertexToCoordinate(randomPosition(options.bbox))); | ||
vertices = vertices.map(vertexToCoordinate(randomPositionUnchecked(options.bbox))); | ||
features.push(polygon([vertices])); | ||
}; | ||
for (var i = 0; i < count; i++) { | ||
_loop_1(i); | ||
} | ||
@@ -128,2 +137,3 @@ return featureCollection(features); | ||
* @returns {FeatureCollection<LineString>} GeoJSON FeatureCollection of linestrings | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -133,4 +143,3 @@ * var lineStrings = turf.randomLineString(25, {bbox: [-180, -90, 180, 90]}) | ||
*/ | ||
export function randomLineString(count, options) { | ||
if (options === void 0) { options = {}; } | ||
export function randomLineString(count, options = {}) { | ||
// Optional parameters | ||
@@ -141,6 +150,7 @@ options = options || {}; | ||
} | ||
var bbox = options.bbox; | ||
var num_vertices = options.num_vertices; | ||
var max_length = options.max_length; | ||
var max_rotation = options.max_rotation; | ||
const bbox = options.bbox; | ||
checkBBox(bbox); | ||
let num_vertices = options.num_vertices; | ||
let max_length = options.max_length; | ||
let max_rotation = options.max_rotation; | ||
if (count === undefined || count === null) { | ||
@@ -161,13 +171,13 @@ count = 1; | ||
} | ||
var features = []; | ||
for (var i = 0; i < count; i++) { | ||
var startingPoint = randomPosition(bbox); | ||
var vertices = [startingPoint]; | ||
for (var j = 0; j < num_vertices - 1; j++) { | ||
var priorAngle = j === 0 | ||
const features = []; | ||
for (let i = 0; i < count; i++) { | ||
const startingPoint = randomPositionUnchecked(bbox); | ||
const vertices = [startingPoint]; | ||
for (let j = 0; j < num_vertices - 1; j++) { | ||
const priorAngle = j === 0 | ||
? Math.random() * 2 * Math.PI | ||
: 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; | ||
var distance = Math.random() * max_length; | ||
const angle = priorAngle + (Math.random() - 0.5) * max_rotation * 2; | ||
const distance = Math.random() * max_length; | ||
vertices.push([ | ||
@@ -183,3 +193,3 @@ vertices[j][0] + distance * Math.cos(angle), | ||
function vertexToCoordinate(hub) { | ||
return function (cur) { | ||
return (cur) => { | ||
return [cur[0] + hub[0], cur[1] + hub[1]]; | ||
@@ -186,0 +196,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import { BBox, FeatureCollection, LineString, Point, Polygon, Position } from "@turf/helpers"; | ||
import { BBox, FeatureCollection, LineString, Point, Polygon, Position } from "geojson"; | ||
/** | ||
@@ -8,2 +8,3 @@ * Returns a random position within a {@link bounding box}. | ||
* @returns {Array<number>} Position [longitude, latitude] | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -24,2 +25,3 @@ * var position = turf.randomPosition([-180, -90, 180, 90]) | ||
* @returns {FeatureCollection<Point>} GeoJSON FeatureCollection of points | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -43,2 +45,3 @@ * var points = turf.randomPoint(25, {bbox: [-180, -90, 180, 90]}) | ||
* @returns {FeatureCollection<Polygon>} GeoJSON FeatureCollection of polygons | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -66,2 +69,3 @@ * var polygons = turf.randomPolygon(25, {bbox: [-180, -90, 180, 90]}) | ||
* @returns {FeatureCollection<LineString>} GeoJSON FeatureCollection of linestrings | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -68,0 +72,0 @@ * var lineStrings = turf.randomLineString(25, {bbox: [-180, -90, 180, 90]}) |
"use strict"; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var helpers_1 = require("@turf/helpers"); | ||
const helpers_1 = require("@turf/helpers"); | ||
/** | ||
@@ -17,2 +10,3 @@ * Returns a random position within a {@link bounding box}. | ||
* @returns {Array<number>} Position [longitude, latitude] | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -23,2 +17,8 @@ * var position = turf.randomPosition([-180, -90, 180, 90]) | ||
function randomPosition(bbox) { | ||
checkBBox(bbox); | ||
return randomPositionUnchecked(bbox); | ||
} | ||
exports.randomPosition = randomPosition; | ||
// does not check bbox for validity, that is handled by the exported functions | ||
function randomPositionUnchecked(bbox) { | ||
if (Array.isArray(bbox)) { | ||
@@ -32,3 +32,13 @@ return coordInBBox(bbox); | ||
} | ||
exports.randomPosition = randomPosition; | ||
function checkBBox(bbox) { | ||
if (bbox == null) { | ||
return; | ||
} | ||
else if (Array.isArray(bbox)) { | ||
helpers_1.validateBBox(bbox); | ||
} | ||
else if (bbox.bbox != null) { | ||
helpers_1.validateBBox(bbox.bbox); | ||
} | ||
} | ||
/** | ||
@@ -42,2 +52,3 @@ * Returns a random {@link point}. | ||
* @returns {FeatureCollection<Point>} GeoJSON FeatureCollection of points | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -47,10 +58,10 @@ * var points = turf.randomPoint(25, {bbox: [-180, -90, 180, 90]}) | ||
*/ | ||
function randomPoint(count, options) { | ||
if (options === void 0) { options = {}; } | ||
function randomPoint(count, options = {}) { | ||
checkBBox(options.bbox); | ||
if (count === undefined || count === null) { | ||
count = 1; | ||
} | ||
var features = []; | ||
for (var i = 0; i < count; i++) { | ||
features.push(helpers_1.point(randomPosition(options.bbox))); | ||
const features = []; | ||
for (let i = 0; i < count; i++) { | ||
features.push(helpers_1.point(randomPositionUnchecked(options.bbox))); | ||
} | ||
@@ -71,2 +82,3 @@ return helpers_1.featureCollection(features); | ||
* @returns {FeatureCollection<Polygon>} GeoJSON FeatureCollection of polygons | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -76,4 +88,4 @@ * var polygons = turf.randomPolygon(25, {bbox: [-180, -90, 180, 90]}) | ||
*/ | ||
function randomPolygon(count, options) { | ||
if (options === void 0) { options = {}; } | ||
function randomPolygon(count, options = {}) { | ||
checkBBox(options.bbox); | ||
// Default param | ||
@@ -90,14 +102,14 @@ if (count === undefined || count === null) { | ||
} | ||
var features = []; | ||
var _loop_1 = function (i) { | ||
var vertices = []; | ||
var circleOffsets = __spreadArrays(Array(options.num_vertices + 1)).map(Math.random); | ||
const features = []; | ||
for (let i = 0; i < count; i++) { | ||
let vertices = []; | ||
const circleOffsets = [...Array(options.num_vertices + 1)].map(Math.random); | ||
// Sum Offsets | ||
circleOffsets.forEach(function (cur, index, arr) { | ||
circleOffsets.forEach((cur, index, arr) => { | ||
arr[index] = index > 0 ? cur + arr[index - 1] : cur; | ||
}); | ||
// scaleOffsets | ||
circleOffsets.forEach(function (cur) { | ||
circleOffsets.forEach((cur) => { | ||
cur = (cur * 2 * Math.PI) / circleOffsets[circleOffsets.length - 1]; | ||
var radialScaler = Math.random(); | ||
const radialScaler = Math.random(); | ||
vertices.push([ | ||
@@ -110,7 +122,4 @@ radialScaler * (options.max_radial_length || 10) * Math.sin(cur), | ||
// center the polygon around something | ||
vertices = vertices.map(vertexToCoordinate(randomPosition(options.bbox))); | ||
vertices = vertices.map(vertexToCoordinate(randomPositionUnchecked(options.bbox))); | ||
features.push(helpers_1.polygon([vertices])); | ||
}; | ||
for (var i = 0; i < count; i++) { | ||
_loop_1(i); | ||
} | ||
@@ -133,2 +142,3 @@ return helpers_1.featureCollection(features); | ||
* @returns {FeatureCollection<LineString>} GeoJSON FeatureCollection of linestrings | ||
* @throws {Error} if bbox is invalid | ||
* @example | ||
@@ -138,4 +148,3 @@ * var lineStrings = turf.randomLineString(25, {bbox: [-180, -90, 180, 90]}) | ||
*/ | ||
function randomLineString(count, options) { | ||
if (options === void 0) { options = {}; } | ||
function randomLineString(count, options = {}) { | ||
// Optional parameters | ||
@@ -146,6 +155,7 @@ options = options || {}; | ||
} | ||
var bbox = options.bbox; | ||
var num_vertices = options.num_vertices; | ||
var max_length = options.max_length; | ||
var max_rotation = options.max_rotation; | ||
const bbox = options.bbox; | ||
checkBBox(bbox); | ||
let num_vertices = options.num_vertices; | ||
let max_length = options.max_length; | ||
let max_rotation = options.max_rotation; | ||
if (count === undefined || count === null) { | ||
@@ -166,13 +176,13 @@ count = 1; | ||
} | ||
var features = []; | ||
for (var i = 0; i < count; i++) { | ||
var startingPoint = randomPosition(bbox); | ||
var vertices = [startingPoint]; | ||
for (var j = 0; j < num_vertices - 1; j++) { | ||
var priorAngle = j === 0 | ||
const features = []; | ||
for (let i = 0; i < count; i++) { | ||
const startingPoint = randomPositionUnchecked(bbox); | ||
const vertices = [startingPoint]; | ||
for (let j = 0; j < num_vertices - 1; j++) { | ||
const priorAngle = j === 0 | ||
? Math.random() * 2 * Math.PI | ||
: 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; | ||
var distance = Math.random() * max_length; | ||
const angle = priorAngle + (Math.random() - 0.5) * max_rotation * 2; | ||
const distance = Math.random() * max_length; | ||
vertices.push([ | ||
@@ -189,3 +199,3 @@ vertices[j][0] + distance * Math.cos(angle), | ||
function vertexToCoordinate(hub) { | ||
return function (cur) { | ||
return (cur) => { | ||
return [cur[0] + hub[0], cur[1] + hub[1]]; | ||
@@ -192,0 +202,0 @@ }; |
{ | ||
"name": "@turf/random", | ||
"version": "6.5.0", | ||
"version": "7.0.0-alpha.0", | ||
"description": "turf random module", | ||
@@ -57,5 +57,6 @@ "author": "Turf Authors", | ||
"dependencies": { | ||
"@turf/helpers": "^6.5.0" | ||
"@turf/helpers": "^7.0.0-alpha.0", | ||
"tslib": "^2.3.0" | ||
}, | ||
"gitHead": "5375941072b90d489389db22b43bfe809d5e451e" | ||
"gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189" | ||
} |
@@ -9,7 +9,7 @@ # @turf/random | ||
**Parameters** | ||
### Parameters | ||
- `bbox` **[Array][2]<[number][3]>** a bounding box inside of which positions are placed. (optional, default `[-180,-90,180,90]`) | ||
* `bbox` **[Array][2]<[number][3]>** a bounding box inside of which positions are placed. (optional, default `[-180,-90,180,90]`) | ||
**Examples** | ||
### Examples | ||
@@ -21,16 +21,19 @@ ```javascript | ||
Returns **[Array][2]<[number][3]>** Position [longitude, latitude] | ||
* Throws **[Error][4]** if bbox is invalid | ||
Returns **[Array][2]<[number][3]>** Position \[longitude, latitude] | ||
## randomPoint | ||
Returns a random [point][4]. | ||
Returns a random [point][5]. | ||
**Parameters** | ||
### Parameters | ||
- `count` **[number][3]** how many geometries will be generated (optional, default `1`) | ||
- `options` **[Object][5]** Optional parameters (optional, default `{}`) | ||
- `options.bbox` **[Array][2]<[number][3]>** 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][6]** Optional parameters (optional, default `{}`) | ||
**Examples** | ||
* `options.bbox` **[Array][2]<[number][3]>** a bounding box inside of which geometries are placed. (optional, default `[-180,-90,180,90]`) | ||
### Examples | ||
```javascript | ||
@@ -41,18 +44,22 @@ var points = turf.randomPoint(25, {bbox: [-180, -90, 180, 90]}) | ||
Returns **[FeatureCollection][6]<[Point][7]>** GeoJSON FeatureCollection of points | ||
* Throws **[Error][4]** if bbox is invalid | ||
Returns **[FeatureCollection][7]<[Point][8]>** GeoJSON FeatureCollection of points | ||
## randomPolygon | ||
Returns a random [polygon][8]. | ||
Returns a random [polygon][9]. | ||
**Parameters** | ||
### Parameters | ||
- `count` **[number][3]** how many geometries will be generated (optional, default `1`) | ||
- `options` **[Object][5]** Optional parameters (optional, default `{}`) | ||
- `options.bbox` **[Array][2]<[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`) | ||
* `count` **[number][3]** how many geometries will be generated (optional, default `1`) | ||
* `options` **[Object][6]** Optional parameters (optional, default `{}`) | ||
**Examples** | ||
* `options.bbox` **[Array][2]<[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`) | ||
### Examples | ||
```javascript | ||
@@ -63,19 +70,24 @@ var polygons = turf.randomPolygon(25, {bbox: [-180, -90, 180, 90]}) | ||
Returns **[FeatureCollection][6]<[Polygon][9]>** GeoJSON FeatureCollection of polygons | ||
* Throws **[Error][4]** if bbox is invalid | ||
Returns **[FeatureCollection][7]<[Polygon][10]>** GeoJSON FeatureCollection of polygons | ||
## randomLineString | ||
Returns a random [linestring][10]. | ||
Returns a random [linestring][11]. | ||
**Parameters** | ||
### Parameters | ||
- `count` **[number][3]** how many geometries will be generated (optional, default `1`) | ||
- `options` **[Object][5]** Optional parameters (optional, default `{}`) | ||
- `options.bbox` **[Array][2]<[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`) | ||
* `count` **[number][3]** how many geometries will be generated (optional, default `1`) | ||
* `options` **[Object][6]** Optional parameters (optional, default `{}`) | ||
**Examples** | ||
* `options.bbox` **[Array][2]<[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`) | ||
### Examples | ||
```javascript | ||
@@ -86,4 +98,6 @@ var lineStrings = turf.randomLineString(25, {bbox: [-180, -90, 180, 90]}) | ||
Returns **[FeatureCollection][6]<[LineString][11]>** GeoJSON FeatureCollection of linestrings | ||
* Throws **[Error][4]** if bbox is invalid | ||
Returns **[FeatureCollection][7]<[LineString][12]>** GeoJSON FeatureCollection of linestrings | ||
[1]: bounding | ||
@@ -95,18 +109,20 @@ | ||
[4]: point | ||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error | ||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object | ||
[5]: point | ||
[6]: https://tools.ietf.org/html/rfc7946#section-3.3 | ||
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object | ||
[7]: https://tools.ietf.org/html/rfc7946#section-3.1.2 | ||
[7]: https://tools.ietf.org/html/rfc7946#section-3.3 | ||
[8]: polygon | ||
[8]: https://tools.ietf.org/html/rfc7946#section-3.1.2 | ||
[9]: https://tools.ietf.org/html/rfc7946#section-3.1.6 | ||
[9]: polygon | ||
[10]: linestring | ||
[10]: https://tools.ietf.org/html/rfc7946#section-3.1.6 | ||
[11]: https://tools.ietf.org/html/rfc7946#section-3.1.4 | ||
[11]: linestring | ||
[12]: https://tools.ietf.org/html/rfc7946#section-3.1.4 | ||
<!-- This file is automatically generated. Please don't edit it directly: | ||
@@ -113,0 +129,0 @@ if you find an error, edit the source file (likely index.js), and re-run |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25155
478
146
2
1
+ Addedtslib@^2.3.0
+ Added@turf/helpers@7.1.0(transitive)
+ Added@types/geojson@7946.0.14(transitive)
+ Addedtslib@2.8.1(transitive)
- Removed@turf/helpers@6.5.0(transitive)
Updated@turf/helpers@^7.0.0-alpha.0