Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const turfLine = geoJsonFormat.writeFeatureObject(lineFeat);
// Union both geometries to a set of MultiLineStrings.
const unionGeom = union(polygonToLine(turfPolygon), turfLine);
// Buffer the input polygon to take great circle (un-)precision
// into account.
const bufferedTurfPolygon = buffer(turfPolygon, tolerance, {
units: 'meters'
});
let filteredSegments = [];
// Iterate over each segment and remove any segment that is not covered by
// the (buffered) input polygon.
segmentEach(unionGeom, (currentSegment, featureIndex, multiFeatureIndex) => {
const segmentCenter = centroid(currentSegment);
const isSegmentInPolygon = booleanPointInPolygon(segmentCenter, bufferedTurfPolygon);
if (isSegmentInPolygon) {
if (!filteredSegments[multiFeatureIndex]) {
filteredSegments[multiFeatureIndex] = [];
}
if (filteredSegments[multiFeatureIndex].length === 0) {
filteredSegments[multiFeatureIndex].push(
getCoords(currentSegment)[0],
getCoords(currentSegment)[1]
);
} else {
filteredSegments[multiFeatureIndex].push(
getCoords(currentSegment)[1]
if (!options.units) { options.units = "kilometers"; }
// validation
if (!pt) { throw new Error("pt is required"); }
if (Array.isArray(pt)) { pt = point(pt);
} else if (pt.type === "Point") { pt = feature(pt);
} 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);
}
export default function directionalMean(lines: FeatureCollection, options: {
planar?: boolean;
segment?: boolean;
} = {}): DirectionalMeanLine {
const isPlanar: boolean = !!options.planar; // you can't use options.planar || true here.
const isSegment: boolean = options.segment || false;
let sigmaSin: number = 0;
let sigmaCos: number = 0;
let countOfLines: number = 0;
let sumOfLen: number = 0;
const centroidList: Array> = [];
if (isSegment) {
segmentEach(lines, (currentSegment: any) => { // todo fix turf-meta's declaration file
const [sin1, cos1]: [number, number] = getCosAndSin(currentSegment.geometry.coordinates, isPlanar);
const lenOfLine = getLengthOfLineString(currentSegment, isPlanar);
if (isNaN(sin1) || isNaN(cos1)) {
return;
} else {
sigmaSin += sin1;
sigmaCos += cos1;
countOfLines += 1;
sumOfLen += lenOfLine;
centroidList.push(centroid(currentSegment));
}
});
// planar and segment
} else {
// planar and non-segment
featureEach(lines, (currentFeature: Feature, featureIndex: number) => {
// Containers
var features = [];
// Create Spatial Index
var tree = rbush();
// To-Do -- HACK way to support typescript
const line: any = lineSegment(line1);
tree.load(line);
var overlapSegment;
// Line Intersection
// Iterate over line segments
segmentEach(line2, function (segment) {
var doesOverlaps = false;
// Iterate over each segments which falls within the same bounds
featureEach(tree.search(segment), function (match) {
if (doesOverlaps === false) {
var coordsSegment = getCoords(segment).sort();
var coordsMatch: any = getCoords(match).sort();
// Segment overlaps feature
if (equal(coordsSegment, coordsMatch)) {
doesOverlaps = true;
// Overlaps already exists - only append last coordinate of segment
if (overlapSegment) overlapSegment = concatSegment(overlapSegment, segment);
else overlapSegment = segment;
// Match segments which don't share nodes (Issue #901)
} else if (
function directionalMean(lines, options) {
if (options === void 0) { options = {}; }
var isPlanar = !!options.planar; // you can't use options.planar || true here.
var isSegment = options.segment || false;
var sigmaSin = 0;
var sigmaCos = 0;
var countOfLines = 0;
var sumOfLen = 0;
var centroidList = [];
if (isSegment) {
meta_1.segmentEach(lines, function (currentSegment) {
var _a = getCosAndSin(currentSegment.geometry.coordinates, isPlanar), sin1 = _a[0], cos1 = _a[1];
var lenOfLine = getLengthOfLineString(currentSegment, isPlanar);
if (isNaN(sin1) || isNaN(cos1)) {
return;
}
else {
sigmaSin += sin1;
sigmaCos += cos1;
countOfLines += 1;
sumOfLen += lenOfLine;
centroidList.push(centroid_1.default(currentSegment));
}
});
// planar and segment
}
else {
let overlap = 0;
switch (type1) {
case 'MultiPoint':
const coords1 = coordAll(feature1);
const coords2 = coordAll(feature2);
coords1.forEach((coord1) => {
coords2.forEach((coord2) => {
if (coord1[0] === coord2[0] && coord1[1] === coord2[1]) overlap++;
});
});
break;
case 'LineString':
case 'MultiLineString':
segmentEach(feature1, (segment1) => {
segmentEach(feature2, (segment2) => {
if (lineOverlap(segment1, segment2).features.length) overlap++;
});
});
break;
case 'Polygon':
case 'MultiPolygon':
segmentEach(feature1, (segment1) => {
segmentEach(feature2, (segment2) => {
if (lineIntersect(segment1, segment2).features.length) overlap++;
});
});
break;
}
segmentEach(feature1, (segment1) => {
segmentEach(feature2, (segment2) => {
if (lineIntersect(segment1, segment2).features.length) overlap++;
});
});
break;