Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
} = {}): FeatureCollection {
// Input validation being handled by Typescript
// collectionOf(points, 'Point', 'points must consist of a FeatureCollection of only Points');
// if (maxDistance === null || maxDistance === undefined) throw new Error('maxDistance is required');
// if (!(Math.sign(maxDistance) > 0)) throw new Error('maxDistance is invalid');
// if (!(minPoints === undefined || minPoints === null || Math.sign(minPoints) > 0)) throw new Error('options.minPoints is invalid');
// Clone points to prevent any mutations
if (options.mutate !== true) points = clone(points);
// Defaults
options.minPoints = options.minPoints || 3;
// create clustered ids
var dbscan = new clustering.DBSCAN();
var clusteredIds = dbscan.run(coordAll(points), convertLength(maxDistance, options.units), options.minPoints, distance);
// Tag points to Clusters ID
var clusterId = -1;
clusteredIds.forEach(function (clusterIds) {
clusterId++;
// assign cluster ids to input points
clusterIds.forEach(function (idx) {
var clusterPoint = points.features[idx];
if (!clusterPoint.properties) clusterPoint.properties = {};
clusterPoint.properties.cluster = clusterId;
clusterPoint.properties.dbscan = 'core';
});
});
// handle noise points, if any
// edges points are tagged by DBSCAN as both 'noise' and 'cluster' as they can "reach" less than 'minPoints' number of points
function rhumbDistance(from: Coord, to: Coord, options: {
units?: Units,
} = {}): number {
const origin = getCoord(from);
const destination = getCoord(to);
// compensate the crossing of the 180th meridian (https://macwright.org/2016/09/26/the-180th-meridian.html)
// solution from https://github.com/mapbox/mapbox-gl-js/issues/3250#issuecomment-294887678
destination[0] += (destination[0] - origin[0] > 180) ? -360 : (origin[0] - destination[0] > 180) ? 360 : 0;
const distanceInMeters = calculateRhumbDistance(origin, destination);
const distance = convertLength(distanceInMeters, "meters", options.units);
return distance;
}
function rhumbDestination<p>(origin: Coord, distance: number, bearing: number, options: {
units?: Units,
properties?: P,
} = {}): Feature {
const wasNegativeDistance = distance < 0;
let distanceInMeters = convertLength(Math.abs(distance), options.units, "meters");
if (wasNegativeDistance) distanceInMeters = -Math.abs(distanceInMeters);
const coords = getCoord(origin);
const destination = calculateRhumbDestination(coords, distanceInMeters, bearing);
// compensate the crossing of the 180th meridian (https://macwright.org/2016/09/26/the-180th-meridian.html)
// solution from https://github.com/mapbox/mapbox-gl-js/issues/3250#issuecomment-294887678
destination[0] += (destination[0] - coords[0] > 180) ? -360 : (coords[0] - destination[0] > 180) ? 360 : 0;
return point(destination, options.properties);
}
</p>
} else { featureOf(pt, "Point", "point"); }
if (!line) { throw new Error("line is required"); }
if (Array.isArray(line)) { line = lineString(line);
} else if (line.type === "LineString") { line = feature(line);
} else { featureOf(line, "LineString", "line"); }
let distance = Infinity;
const p = pt.geometry.coordinates;
segmentEach(line, (segment) => {
const a = segment!.geometry.coordinates[0];
const b = segment!.geometry.coordinates[1];
const d = distanceToSegment(p, a, b, options);
if (d < distance) { distance = d; }
});
return convertLength(distance, "degrees", options.units);
}