@turf/random
Advanced tools
Comparing version 4.7.3 to 5.0.4
@@ -1,25 +0,41 @@ | ||
/// <reference types="geojson" /> | ||
import { BBox, FeatureCollection, Feature, Point, LineString, Polygon, Position } from '@turf/helpers'; | ||
type Points = GeoJSON.FeatureCollection<GeoJSON.Point>; | ||
type Polygons = GeoJSON.FeatureCollection<GeoJSON.Polygon>; | ||
type Features = GeoJSON.FeatureCollection<any>; | ||
type BBox = Array<number>; | ||
/** | ||
* http://turfjs.org/docs/#randomposition | ||
*/ | ||
export function randomPosition(bbox?: BBox | {bbox?: BBox}): Position | ||
interface Options { | ||
bbox?: BBox | ||
num_vertices?: number | ||
max_radial_length?: number | ||
} | ||
/** | ||
* http://turfjs.org/docs/#randompoint | ||
*/ | ||
export function randomPoint( | ||
count?: number, | ||
options?: { | ||
bbox?: BBox | ||
} | ||
): FeatureCollection<Point> | ||
interface Random { | ||
/** | ||
* http://turfjs.org/docs/#random | ||
*/ | ||
(type?: 'point' | 'points', count?: number, options?: Options): Points; | ||
(type?: 'polygon' | 'polygons', count?: number, options?: Options): Polygons; | ||
} | ||
declare const random: Random; | ||
declare namespace random { } | ||
export = random; | ||
/** | ||
* http://turfjs.org/docs/#randomlinestring | ||
*/ | ||
export function randomLineString( | ||
count?: number, | ||
options?: { | ||
bbox?: BBox, | ||
num_vertices?: number, | ||
max_length?: number, | ||
max_rotation?: number | ||
} | ||
): FeatureCollection<LineString> | ||
/** | ||
* http://turfjs.org/docs/#randompolygon | ||
*/ | ||
export function randomPolygon( | ||
count?: number, | ||
options?: { | ||
bbox?: BBox, | ||
num_vertices?: number, | ||
max_radial_length?: number | ||
} | ||
): FeatureCollection<LineString> |
209
index.js
@@ -1,50 +0,177 @@ | ||
var random = require('geojson-random'); | ||
import { | ||
point, | ||
lineString, | ||
polygon, | ||
featureCollection, | ||
isObject, | ||
isNumber | ||
} from '@turf/helpers'; | ||
/** | ||
* Generates random {@link GeoJSON} data, including {@link Point|Points} and {@link Polygon|Polygons}, for testing | ||
* and experimentation. | ||
* Returns a random position within a {@link bounding box}. | ||
* | ||
* @name random | ||
* @param {string} [type='point'] type of features desired: 'points' or 'polygons' | ||
* @param {number} [count=1] how many geometries should be generated. | ||
* @param {Object} options options relevant to the feature desired. Can include: | ||
* @param {Array<number>} options.bbox a bounding box inside of which geometries | ||
* are placed. In the case of {@link Point} features, they are guaranteed to be within this bounds, | ||
* while {@link Polygon} features have their centroid within the bounds. | ||
* @param {number} [options.num_vertices=10] options.vertices the number of vertices added | ||
* to polygon features. | ||
* @param {Number} [options.max_radial_length=10] the total number of decimal | ||
* degrees longitude or latitude that a polygon can extent outwards to | ||
* from its center. | ||
* @returns {FeatureCollection} generated random features | ||
* @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 points = turf.random('points', 100, { | ||
* bbox: [-70, 40, -60, 60] | ||
* }); | ||
* var position = turf.randomPosition([-180, -90, 180, 90]) | ||
* //=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()]; | ||
} | ||
/** | ||
* Returns a random {@link point}. | ||
* | ||
* var polygons = turf.random('polygons', 4, { | ||
* bbox: [-70, 40, -60, 60] | ||
* }); | ||
* @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, 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; | ||
var features = []; | ||
for (var i = 0; i < count; i++) { | ||
features.push(point(randomPosition(bbox))); | ||
} | ||
return featureCollection(features); | ||
} | ||
/** | ||
* Returns a random {@link polygon}. | ||
* | ||
* //addToMap | ||
* var addToMap = [points, polygons] | ||
* @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<Point>} GeoJSON FeatureCollection of points | ||
* @example | ||
* var polygons = turf.randomPolygon(25, {bbox: [-180, -90, 180, 90]}) | ||
* //=polygons | ||
*/ | ||
module.exports = function (type, count, options) { | ||
export function randomPolygon(count, options) { | ||
// Optional parameters | ||
options = options || {}; | ||
count = count || 1; | ||
switch (type) { | ||
case 'point': | ||
case 'points': | ||
case undefined: | ||
return random.point(count, options.bbox); | ||
case 'polygon': | ||
case 'polygons': | ||
return random.polygon( | ||
count, | ||
options.num_vertices, | ||
options.max_radial_length, | ||
options.bbox); | ||
default: | ||
throw new Error('Unknown type given: valid options are points and polygons'); | ||
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; | ||
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); | ||
vertices[vertices.length - 1] = vertices[0]; // close the ring | ||
// center the polygon around something | ||
vertices = vertices.map(vertexToCoordinate(randomPosition(bbox))); | ||
features.push(polygon([vertices])); | ||
} | ||
}; | ||
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); | ||
} | ||
/** | ||
* 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<Point>} GeoJSON FeatureCollection of points | ||
* @example | ||
* var lineStrings = turf.randomLineString(25, {bbox: [-180, -90, 180, 90]}) | ||
* //=lineStrings | ||
*/ | ||
export function randomLineString(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_length = options.max_length; | ||
var max_rotation = options.max_rotation; | ||
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; | ||
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) ? | ||
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; | ||
vertices.push([ | ||
vertices[j][0] + distance * Math.cos(angle), | ||
vertices[j][1] + distance * Math.sin(angle) | ||
]); | ||
} | ||
features.push(lineString(vertices)); | ||
} | ||
return featureCollection(features); | ||
} | ||
function vertexToCoordinate(hub) { | ||
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]]; | ||
} |
{ | ||
"name": "@turf/random", | ||
"version": "4.7.3", | ||
"version": "5.0.4", | ||
"description": "turf random module", | ||
"main": "index.js", | ||
"main": "main", | ||
"module": "index", | ||
"jsnext:main": "index", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
"index.d.ts", | ||
"main.js" | ||
], | ||
"scripts": { | ||
"test": "node test.js", | ||
"bench": "node bench.js" | ||
"pretest": "rollup -c ../../rollup.config.js", | ||
"test": "node -r @std/esm test.js", | ||
"bench": "node -r @std/esm bench.js" | ||
}, | ||
@@ -30,9 +34,15 @@ "repository": { | ||
"devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"glob": "~4.3.5", | ||
"tape": "^4.6.3" | ||
"@std/esm": "*", | ||
"benchmark": "*", | ||
"glob": "*", | ||
"rollup": "*", | ||
"tape": "*" | ||
}, | ||
"dependencies": { | ||
"geojson-random": "^0.2.2" | ||
"@turf/helpers": "^5.0.4" | ||
}, | ||
"@std/esm": { | ||
"esm": "js", | ||
"cjs": true | ||
} | ||
} |
# @turf/random | ||
# random | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
Generates random [GeoJSON](http://geojson.org/geojson-spec.html#geojson-objects) data, including [Points](http://geojson.org/geojson-spec.html#point) and [Polygons](http://geojson.org/geojson-spec.html#polygon), for testing | ||
and experimentation. | ||
## randomPosition | ||
Random Position | ||
**Parameters** | ||
- `type` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** type of features desired: 'points' or 'polygons' (optional, default `'point'`) | ||
- `count` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** how many geometries should be generated. (optional, default `1`) | ||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** options relevant to the feature desired. Can include: | ||
- `options.bbox` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** a bounding box inside of which geometries | ||
are placed. In the case of [Point](http://geojson.org/geojson-spec.html#point) features, they are guaranteed to be within this bounds, | ||
while [Polygon](http://geojson.org/geojson-spec.html#polygon) features have their centroid within the bounds. | ||
- `options.num_vertices` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** options.vertices the number of vertices added | ||
to polygon features. (optional, default `10`) | ||
- `options.max_radial_length` **\[[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** the total number of decimal | ||
degrees longitude or latitude that a polygon can extent outwards to | ||
from its center. (optional, default `10`) | ||
- `bbox` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[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]`) | ||
@@ -25,16 +16,70 @@ **Examples** | ||
```javascript | ||
var points = turf.random('points', 100, { | ||
bbox: [-70, 40, -60, 60] | ||
}); | ||
var position = turf.randomPosition([-180, -90, 180, 90]) | ||
//=position | ||
``` | ||
var polygons = turf.random('polygons', 4, { | ||
bbox: [-70, 40, -60, 60] | ||
}); | ||
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** Position [longitude, latitude] | ||
//addToMap | ||
var addToMap = [points, polygons] | ||
## randomPoint | ||
Random Point | ||
**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)<[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]`) | ||
**Examples** | ||
```javascript | ||
var points = turf.randomPoint(25, {bbox: [-180, -90, 180, 90]}) | ||
//=points | ||
``` | ||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)** generated random features | ||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** GeoJSON FeatureCollection of points | ||
## randomPolygon | ||
Random Polygon | ||
**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)<[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`) | ||
**Examples** | ||
```javascript | ||
var polygons = turf.randomPolygon(25, {bbox: [-180, -90, 180, 90]}) | ||
//=polygons | ||
``` | ||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** GeoJSON FeatureCollection of points | ||
## randomLineString | ||
Random LineString | ||
**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)<[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`) | ||
**Examples** | ||
```javascript | ||
var lineStrings = turf.randomLineString(25, {bbox: [-180, -90, 180, 90]}) | ||
//=lineStrings | ||
``` | ||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** GeoJSON FeatureCollection of points | ||
<!-- This file is automatically generated. Please don't edit it directly: | ||
@@ -41,0 +86,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
21978
6
353
108
5
1
+ Added@turf/helpers@^5.0.4
+ Added@turf/helpers@5.1.5(transitive)
- Removedgeojson-random@^0.2.2
- Removedgeojson-random@0.2.2(transitive)