@nbbj/stroll
Version:
NodeJS computational library for finding the optimal walking path to be as exposed to nature as possible.
95 lines (75 loc) • 4.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Geometry = void 0;
var TurfBBox = _interopRequireWildcard(require("@turf/bbox"));
var TurfCircle = _interopRequireWildcard(require("@turf/circle"));
var TurfHelpers = _interopRequireWildcard(require("@turf/helpers"));
var TurfPointGrid = _interopRequireWildcard(require("@turf/point-grid"));
var TurfRandom = _interopRequireWildcard(require("@turf/random"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/*
MIT License
Copyright (c) 2019 Nate Holland, Petar Mitev, NBBJ
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* Module for getting Turf.js geometry and helpers.
*/
class Geometry {
/**
* Get a bounding box around a location with a given radius.
* @param {Number} lat Latitude of location.
* @param {Number} long Longitude of location.
* @param {Number} radius The radius of the bounding geometry from the given lat/long origin.
* @returns {BBox} A Turf.js bounding box object.
*/
static BoundingBoxByRadius(lat, long, radius) {
const point = TurfHelpers.point([long, lat]);
const buffer = TurfCircle.default(point, radius);
return TurfBBox.default(buffer);
}
/**
* Get a collection of random points which fall within a given bounding radius from an origin
* lat/long point.
* @param {Number} lat Latitude of location.
* @param {Number} long Longitude of location.
* @param {Number} radius The radius of the bounding geometry from the given lat/long origin.
* @param {Number} numPoints How many points to return
* @returns {FeatureCollection<Point, any>} A collection of Turf.JS points.
*/
static PointGridRandom(lat, long, radius, numPoints) {
const bbox = this.BoundingBoxByRadius(lat, long, radius);
return TurfRandom.randomPoint(numPoints, bbox);
}
/**
* Get a collection of points in a gird which fall within a given bounding radius from an origin
* lat/long point.
* @param {Number} lat Latitude of location.
* @param {Number} long Longitude of location.
* @param {Number} radius The radius of the bounding geometry from the given lat/long origin.
* @param {Number} pointDist How far apart the points should be in the point grid.
* @returns {Array<Point>} A collection of Turf.JS points.
*/
static PointGrid(lat, long, radius, pointDist) {
const bbox = this.BoundingBoxByRadius(lat, long, radius);
return TurfPointGrid.default(bbox, pointDist);
}
}
exports.Geometry = Geometry;