Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function pointGrid(bbox, cellSide, options) {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
// var units = options.units;
var mask = options.mask;
var properties = options.properties;
// Containers
var results = [];
// Input Validation
if (cellSide === null || cellSide === undefined) throw new Error('cellSide is required');
if (!isNumber(cellSide)) throw new Error('cellSide is invalid');
if (!bbox) throw new Error('bbox is required');
if (!Array.isArray(bbox)) throw new Error('bbox must be array');
if (bbox.length !== 4) throw new Error('bbox must contain 4 numbers');
if (mask && ['Polygon', 'MultiPolygon'].indexOf(getType(mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon');
var west = bbox[0];
var south = bbox[1];
var east = bbox[2];
var north = bbox[3];
var xFraction = cellSide / (distance([west, south], [east, south], options));
var cellWidth = xFraction * (east - west);
var yFraction = cellSide / (distance([west, south], [west, north], options));
var cellHeight = yFraction * (north - south);
var bboxWidth = (east - west);
function ellipse(center, xSemiAxis, ySemiAxis, options) {
// Optional params
options = options || {};
var steps = options.steps || 64;
var units = options.units || 'kilometers';
var angle = options.angle || 0;
var pivot = options.pivot || center;
var properties = options.properties || center.properties || {};
// validation
if (!center) throw new Error('center is required');
if (!xSemiAxis) throw new Error('xSemiAxis is required');
if (!ySemiAxis) throw new Error('ySemiAxis is required');
if (!isObject(options)) throw new Error('options must be an object');
if (!isNumber(steps)) throw new Error('steps must be a number');
if (!isNumber(angle)) throw new Error('angle must be a number');
var centerCoords = getCoord(center);
if (units === 'degrees') {
var angleRad = degreesToRadians(angle);
} else {
xSemiAxis = rhumbDestination(center, xSemiAxis, 90, {units: units});
ySemiAxis = rhumbDestination(center, ySemiAxis, 0, {units: units});
xSemiAxis = getCoord(xSemiAxis)[0] - centerCoords[0];
ySemiAxis = getCoord(ySemiAxis)[1] - centerCoords[1];
}
var coordinates = [];
for (var i = 0; i < steps; i += 1) {
var stepAngle = i * -360 / steps;
var x = ((xSemiAxis * ySemiAxis) / Math.sqrt(Math.pow(ySemiAxis, 2) + (Math.pow(xSemiAxis, 2) * Math.pow(getTanDeg(stepAngle), 2))));
var y = ((xSemiAxis * ySemiAxis) / Math.sqrt(Math.pow(xSemiAxis, 2) + (Math.pow(ySemiAxis, 2) / Math.pow(getTanDeg(stepAngle), 2))));
function convert(geojson: any, projection: string, options: {mutate?: boolean} = {}): any {
// Optional parameters
options = options || {};
var mutate = options.mutate;
// Validation
if (!geojson) throw new Error('geojson is required');
// Handle Position
if (Array.isArray(geojson) && isNumber(geojson[0])) geojson = (projection === 'mercator') ? convertToMercator(geojson) : convertToWgs84(geojson);
// Handle GeoJSON
else {
// Handle possible data mutation
if (mutate !== true) geojson = clone(geojson);
coordEach(geojson, function (coord) {
var newCoord = (projection === 'mercator') ? convertToMercator(coord) : convertToWgs84(coord);
coord[0] = newCoord[0];
coord[1] = newCoord[1];
});
}
return geojson;
}
export function randomPolygon(count?: number, options: {
bbox?: BBox,
num_vertices?: number,
max_radial_length?: number,
} = {}): FeatureCollection {
// Default param
if (count === undefined || count === null) { count = 1; }
if (!isNumber(options.num_vertices) || options.num_vertices === undefined) {
options.num_vertices = 10;
}
if (!isNumber(options.max_radial_length) || options.max_radial_length === undefined) {
options.max_radial_length = 10;
}
const features = [];
for (let i = 0; i < count; i++) {
let vertices: any[] = [];
const circleOffsets = Array.apply(null, new Array(options.num_vertices + 1)).map(Math.random);
// Sum Offsets
circleOffsets.forEach((cur: any, index: number, arr: any[]) => {
arr[index] = (index > 0) ? cur + arr[index - 1] : cur;
});
function centerMedian(
features: FeatureCollection,
options: { weight?: string, tolerance?: number, counter?: number} = {}
): Feature,
[key: string]: any
}> {
// Optional params
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
var counter = options.counter || 10;
if (!isNumber(counter)) throw new Error('counter must be a number');
var weightTerm = options.weight;
// Calculate mean center:
var meanCenter = centerMean(features, {weight: options.weight});
// Calculate center of every feature:
var centroids: any = featureCollection([]);
featureEach(features, function (feature) {
centroids.features.push(centroid(feature, {properties: {weight: feature.properties[weightTerm]}}));
});
centroids.properties = {
tolerance: options.tolerance,
medianCandidates: []
};
return findMedian(meanCenter.geometry.coordinates, [0, 0], centroids, counter);
export function containsNumber(coordinates) {
if (coordinates.length > 1 && isNumber(coordinates[0]) && isNumber(coordinates[1])) {
return true;
}
if (Array.isArray(coordinates[0]) && coordinates[0].length) {
return containsNumber(coordinates[0]);
}
throw new Error('coordinates must only contain numbers');
}
featureEach(collection, function (point, i) {
var w = point.properties[weight] || 1;
if (!isNumber(w)) throw new Error('Weight for feature index ' + i + ' is not a number!');
sumXs += getCoords(point)[0] * w;
sumYs += getCoords(point)[1] * w;
sumNs += w;
});
if (sumNs === 0) throw new Error('Sum of weights is equal to zero!');
function containsNumber(coordinates) {
if (coordinates.length > 1 && helpers.isNumber(coordinates[0]) && helpers.isNumber(coordinates[1])) {
return true;
}
if (Array.isArray(coordinates[0]) && coordinates[0].length) {
return containsNumber(coordinates[0]);
}
throw new Error('coordinates must only contain numbers');
}
geomEach(geojson, function (geom, featureIndex, properties) {
let weight = properties[options.weight];
weight = (weight === undefined || weight === null) ? 1 : weight;
if (!isNumber(weight)) throw new Error('weight value must be a number for feature index ' + featureIndex);
weight = Number(weight);
if (weight > 0) {
coordEach(geom, function (coord) {
sumXs += coord[0] * weight;
sumYs += coord[1] * weight;
sumNs += weight;
});
}
});
return point([sumXs / sumNs, sumYs / sumNs], options.properties, options);