Socket
Socket
Sign inDemoInstall

geojson-random

Package Overview
Dependencies
0
Maintainers
21
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.2.0

.travis.yml

107

index.js

@@ -1,17 +0,106 @@

module.exports = function(count, type) {
switch (type) {
case 'point':
var features = [];
for (var i = 0; i < count; i++) { features.push(feature(point())); }
return collection(features);
module.exports = function() {
throw new Error('call .point() or .polygon() instead');
};
function position(bbox) {
if (bbox) return coordInBBBOX(bbox);
else return [lon(), lat()];
}
module.exports.position = position;
module.exports.point = function(count, bbox) {
var features = [];
for (i = 0; i < count; i++) {
features.push(feature(bbox ? point(position(bbox)) : point()));
}
return collection(features);
};
module.exports.polygon = function(count, num_vertices, max_radial_length, bbox) {
if (typeof num_vertices !== 'number') num_vertices = 10;
if (typeof max_radial_length !== 'number') max_radial_length = 10;
var features = [];
for (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(position(bbox)));
features.push(feature(polygon([vertices])));
}
function sumOffsets(cur, index, arr) {
arr[index] = (index > 0) ? cur + arr[index - 1] : cur;
}
function scaleOffsets(cur, index) {
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 collection(features);
};
function vertexToCoordinate(hub) {
return function(cur, index) { 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 point() { return { type: 'Point', coordinates: [lon(), lat()] }; }
function point(coordinates) {
return {
type: 'Point',
coordinates: coordinates || [lon(), lat()]
};
}
function coordInBBBOX(bbox) {
var lonSpan = bbox[2] - bbox[0],
latSpan = bbox[3] - bbox[1],
randInSpan = Math.random() * (lonSpan * latSpan);
return [
randInSpan % (lonSpan) + bbox[0],
randInSpan / (latSpan) + bbox[1]];
}
function pointInBBBOX() {
return {
type: 'Point',
coordinates: [lon(), lat()]
};
}
function polygon(coordinates) {
return {
type: 'Polygon',
coordinates: coordinates
};
}
function feature(geom) {
return { type: 'Feature', geometry: geom, properties: {} };
return {
type: 'Feature',
geometry: geom,
properties: {}
};
}
function collection(f) { return { type: 'FeatureCollection', features: f }; }
function collection(f) {
return {
type: 'FeatureCollection',
features: f
};
}

4

package.json
{
"name": "geojson-random",
"version": "0.1.0",
"version": "0.2.0",
"description": "generate random geojson features",

@@ -10,3 +10,3 @@ "main": "index.js",

"scripts": {
"test": "tap test.js"
"test": "tape test.js"
},

@@ -13,0 +13,0 @@ "repository": {

# geojson-random
Generate random GeoJSON features.
[![build status](https://secure.travis-ci.org/mapbox/geojson-random.png)](http://travis-ci.org/mapbox/geojson-random)
Generate random [GeoJSON](http://geojson.org/) features.
Usable in [node.js](http://nodejs.org/) and in browsers with [browserify](http://browserify.org/).
npm install -g geojson-random

@@ -11,5 +15,30 @@ geojson-random

```js
var geojsonRandom = require('geojson-random');
var random = require('geojson-random');
```
geojsonRandom(100, 'point'); // featurecollection of 100 random points
```
### `random.point(count, bbox)`
Return `count` points wrapped in a FeatureCollection.
An optional `bbox` parameter should be an array of numbers representing
a [bbox](http://geojson.org/geojson-spec.html#bounding-boxes) in WSEN order,
and if given, the point will reside within its bounds.
### `random.position(bbox?)`
Return a single GeoJSON [Position](http://geojson.org/geojson-spec.html#positions)
as a 2-element array of numbers in longitude, latitude order.
An optional `bbox` parameter should be an array of numbers representing
a [bbox](http://geojson.org/geojson-spec.html#bounding-boxes) in WSEN order,
and if given, the position will reside within its bounds.
### `random.polygon(count, num_vertices, max_radial_length)`
Return `count` polygons wrapped in a FeatureCollection.
* `num_vertices` is default `10` and is how many coordinates each Polygon
will contain.
* `max_radial_length` is the maximum number of decimal degrees latitude
or longitude that a vertex can reach out of the center of the Polygon.
Default is `10`.
var test = require('tape'),
geojsonRandom = require('./');
test('geojson-random', function(t) {
t.equal(geojsonRandom(100, 'point').features.length, 100, '100 points');
test('random.position()', function(t) {
var nobbox = geojsonRandom.position();
t.equal(nobbox.length, 2);
t.ok(nobbox[0] > -180 && nobbox[0] < 180, 'lon');
t.ok(nobbox[1] > -90 && nobbox[1] < 90, 'lat');
t.end();
});
test('random.position(bbox)', function(t) {
var withBbox = geojsonRandom.position([0, 0, 10, 10]);
t.equal(withBbox.length, 2);
t.ok(withBbox[0] >= 0 && withBbox[0] <= 10, 'lon');
t.ok(withBbox[1] >= 0 && withBbox[1] <= 10, 'lat');
t.end();
});
test('random.point()', function(t) {
var randomPoints = geojsonRandom.point(100);
t.equal(randomPoints.features.length, 100, '100 points');
t.equal(randomPoints.features[0].geometry.type, 'Point', 'features are points');
t.end();
});
test('random.point(bbox)', function(t) {
var randomPoints = geojsonRandom.point(1, [50, 50, 60, 60]);
t.equal(randomPoints.features.length, 1, '1 points');
var withBbox = randomPoints.features[0].geometry.coordinates;
t.ok(withBbox[0] >= 50 && withBbox[0] <= 60, 'lon');
t.ok(withBbox[1] >= 50 && withBbox[1] <= 60, 'lat');
t.end();
});
test('random.polygon', function(t) {
var randomPolygons = geojsonRandom.polygon(100);
t.equal(randomPolygons.features.length, 100, '100 polygons');
t.equal(randomPolygons.features[0].geometry.type, 'Polygon', 'features are polygons');
t.equal(randomPolygons.features[0].geometry.coordinates[0].length, 11, 'and have 11 positions in their outer rings');
var randomPolygonsHi = geojsonRandom.polygon(100, 20);
t.equal(randomPolygonsHi.features[0].geometry.coordinates[0].length, 21, 'and have 21 positions in their outer rings');
t.end();
});
test('random.polygon with bbox', function(t) {
var randomPolygonRing = geojsonRandom.polygon(1, 5, 5, [50, 50, 60, 60]).features[0].geometry.coordinates[0];
randomPolygonRing.forEach(function(withBbox) {
t.ok(withBbox[0] >= 40 && withBbox[0] <= 70, 'lon');
t.ok(withBbox[1] >= 40 && withBbox[1] <= 70, 'lat');
});
t.end();
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc