Skip to content

Commit

Permalink
feat(random): use geotools helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Dec 16, 2019
1 parent 0794f4b commit 2bfbf1c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 121 deletions.
134 changes: 39 additions & 95 deletions lib/geojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/* dependencies */
const _ = require('lodash');
const turf = require('@turf/turf');
const { getNumbers } = require('@lykmapipo/env');
const {
GEO_POINT,
GEO_LINESTRING,
Expand All @@ -22,17 +21,21 @@ const {
isMultiPolygon,
isGeometryCollection,
isGeometry,
randomPoint,
randomLineString,
randomPolygon,
randomMultiPoint,
randomMultiLineString,
randomMultiPolygon,
randomGeometry,
randomGeometryCollection,
} = require('@lykmapipo/geo-tools');
const {
isPosition,
isPolygonCoor
} = require('geojson-validation');


/* constants */
const GEOJSON_DEFAULT_BBOX = 'GEOJSON_DEFAULT_BBOX';


/* geojson geometry types */
exports.TYPE_POINT = GEO_POINT;
exports.TYPE_LINESTRING = GEO_LINESTRING;
Expand Down Expand Up @@ -182,15 +185,10 @@ exports.centroidOf = function (geojson) {
* geometries are placed
* @return {Object|Object[]} random geojson point(s)
*/
exports.randomPoint = function (size = 1, bbox = [-80, 30, -60, 60]) {
exports.randomPoint = function (size = 1) {
const _size = (size && size > 0 ? size : 1);

let _bbox = getNumbers(GEOJSON_DEFAULT_BBOX);
_bbox = !_.isEmpty(_bbox) ? [].concat(_bbox) : [].concat(bbox);

const points = turf.randomPoint(_size, { bbox: _bbox });
let sample = turf.sample(points, _size);
sample = _.map(sample.features, 'geometry');
let sample = _.map(_.range(_size), () => randomPoint());
sample = (sample.length > 1 ? sample : _.first(sample));
return sample;
};
Expand All @@ -204,28 +202,10 @@ exports.randomPoint = function (size = 1, bbox = [-80, 30, -60, 60]) {
* geometries are placed
* @return {Object|Object[]} random geojson point(s)
*/
exports.randomMultiPoint = function (size = 1, bbox = [-80, 30, -60, 60]) {
exports.randomMultiPoint = function (size = 1) {
const _size = (size && size > 0 ? size : 1);

let _bbox = getNumbers(GEOJSON_DEFAULT_BBOX);
_bbox = !_.isEmpty(_bbox) ? [].concat(_bbox) : [].concat(bbox);

const points = turf.randomPoint(_size, { bbox: _bbox });
const _points = turf.randomPoint(_size, { bbox: _bbox });
let sample = turf.sample(points, _size);
let _sample = turf.sample(_points, _size);
sample = _.map(sample.features, 'geometry');
_sample = _.map(_sample.features, 'geometry');
sample = _.map(sample, function (value, index) {
const next = _sample[index];
return {
type: exports.TYPE_MULTIPOINT,
coordinates: [
value.coordinates,
next.coordinates
]
};
});
let sample = _.map(_.range(_size), () => randomMultiPoint());
sample = (sample.length > 1 ? sample : _.first(sample));
return sample;
};
Expand All @@ -239,15 +219,10 @@ exports.randomMultiPoint = function (size = 1, bbox = [-80, 30, -60, 60]) {
* geometries are placed
* @return {Object|Object[]} random geojson linestring(s)
*/
exports.randomLineString = function (size = 1, bbox = [-80, 30, -60, 60]) {
exports.randomLineString = function (size = 1) {
const _size = (size && size > 0 ? size : 1);

let _bbox = getNumbers(GEOJSON_DEFAULT_BBOX);
_bbox = !_.isEmpty(_bbox) ? [].concat(_bbox) : [].concat(bbox);

const linestring = turf.randomLineString(_size, { bbox: _bbox });
let sample = turf.sample(linestring, _size);
sample = _.map(sample.features, 'geometry');
let sample = _.map(_.range(_size), () => randomLineString());
sample = (sample.length > 1 ? sample : _.first(sample));
return sample;
};
Expand All @@ -261,28 +236,10 @@ exports.randomLineString = function (size = 1, bbox = [-80, 30, -60, 60]) {
* geometries are placed
* @return {Object|Object[]} random geojson multilinestring(s)
*/
exports.randomMultiLineString = function (size = 1, bbox = [-80, 30, -60, 60]) {
exports.randomMultiLineString = function (size = 1) {
const _size = (size && size > 0 ? size : 1);

let _bbox = getNumbers(GEOJSON_DEFAULT_BBOX);
_bbox = !_.isEmpty(_bbox) ? [].concat(_bbox) : [].concat(bbox);

const linestrings = turf.randomLineString(_size, { bbox: _bbox });
const _linestrings = turf.randomLineString(_size, { bbox: _bbox });
let sample = turf.sample(linestrings, _size);
let _sample = turf.sample(_linestrings, _size);
sample = _.map(sample.features, 'geometry');
_sample = _.map(_sample.features, 'geometry');
sample = _.map(sample, function (value, index) {
const next = _sample[index];
return {
type: exports.TYPE_MULTILINESTRING,
coordinates: [
value.coordinates,
next.coordinates
]
};
});
let sample = _.map(_.range(_size), () => randomMultiLineString());
sample = (sample.length > 1 ? sample : _.first(sample));
return sample;
};
Expand All @@ -296,15 +253,10 @@ exports.randomMultiLineString = function (size = 1, bbox = [-80, 30, -60, 60]) {
* geometries are placed
* @return {Object|Object[]} random geojson polygon(s)
*/
exports.randomPolygon = function (size = 1, bbox = [-80, 30, -60, 60]) {
exports.randomPolygon = function (size = 1) {
const _size = (size && size > 0 ? size : 1);

let _bbox = getNumbers(GEOJSON_DEFAULT_BBOX);
_bbox = !_.isEmpty(_bbox) ? [].concat(_bbox) : [].concat(bbox);

const polygons = turf.randomPolygon(_size, { bbox: _bbox });
let sample = turf.sample(polygons, _size);
sample = _.map(sample.features, 'geometry');
let sample = _.map(_.range(_size), () => randomPolygon());
sample = (sample.length > 1 ? sample : _.first(sample));
return sample;
};
Expand All @@ -318,28 +270,10 @@ exports.randomPolygon = function (size = 1, bbox = [-80, 30, -60, 60]) {
* geometries are placed
* @return {Object|Object[]} random geojson multipolygon(s)
*/
exports.randomMultiPolygon = function (size = 1, bbox = [-80, 30, -60, 60]) {
exports.randomMultiPolygon = function (size = 1) {
const _size = (size && size > 0 ? size : 1);

let _bbox = getNumbers(GEOJSON_DEFAULT_BBOX);
_bbox = !_.isEmpty(_bbox) ? [].concat(_bbox) : [].concat(bbox);

const polygons = turf.randomPolygon(_size, { bbox: _bbox });
const _polygons = turf.randomPolygon(_size, { bbox: _bbox });
let sample = turf.sample(polygons, _size);
let _sample = turf.sample(_polygons, _size);
sample = _.map(sample.features, 'geometry');
_sample = _.map(_sample.features, 'geometry');
sample = _.map(sample, function (value, index) {
const next = _sample[index];
return {
type: exports.TYPE_MULTIPOLYGON,
coordinates: [
value.coordinates,
next.coordinates
]
};
});
let sample = _.map(_.range(_size), () => randomMultiPolygon());
sample = (sample.length > 1 ? sample : _.first(sample));
return sample;
};
Expand All @@ -354,17 +288,27 @@ exports.randomMultiPolygon = function (size = 1, bbox = [-80, 30, -60, 60]) {
* geometries are placed
* @return {Object|Object[]} random geojson geometrycollection(s)
*/
exports.randomGeometryCollection = function (size = 1, bbox = [-80, 30, -60, 60]) {
exports.randomGeometryCollection = function (size = 1) {
const _size = (size && size > 0 ? size : 1);

const points = [].concat(exports.randomPoint(_size, bbox));
const polygons = [].concat(exports.randomPolygon(_size, bbox));
let sample = _.zipWith(points, polygons, function (point, polygon) {
return {
type: exports.TYPE_GEOMETRYCOLLECTION,
geometries: [].concat([point, polygon])
};
});
let sample = _.map(_.range(_size), () => randomGeometryCollection());
sample = (sample.length > 1 ? sample : _.first(sample));
return sample;
};


/**
* @name randomGeometry
* @description generate random geojson geometry(s)
* @param {Number} [size=1] number of point to generate
* @param {Number[]} [bbox=[-80, 30, -60, 60]] A bounding box inside of which
* geometries are placed
* @return {Object|Object[]} random geojson point(s)
*/
exports.randomGeometry = function (size = 1) {
const _size = (size && size > 0 ? size : 1);

let sample = _.map(_.range(_size), () => randomGeometry());
sample = (sample.length > 1 ? sample : _.first(sample));
return sample;
};
Expand Down
26 changes: 0 additions & 26 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,32 +134,6 @@ describe('GeoJSON', () => {
expect(centroid.coordinates).to.exist;
});


describe('bbox', () => {
before(() => {
process.env.GEOJSON_DEFAULT_BBOX = '-70, 20, -50, 40';
});

it('should be able to generate randomPoint with .env bbox', () => {
const point = randomPoint();
expect(point).to.exist;
expect(point).to.be.an('object');
expect(point.type).to.exist;
expect(point.type).to.be.equal('Point');
expect(point.coordinates).to.exist;

//asset bbox
expect(point.coordinates[0]).to.be.above(-70);
expect(point.coordinates[0]).to.be.below(-50);
expect(point.coordinates[1]).to.be.above(20);
expect(point.coordinates[1]).to.be.below(40);
});

after(() => {
delete process.env.GEOJSON_DEFAULT_BBOX;
});
});

describe('parseCoordinateString', () => {
it('should not parse empty string coordinates', () => {
const coords = '';
Expand Down

0 comments on commit 2bfbf1c

Please sign in to comment.