Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const pointOnLine = nearestPointOnLine(line, stopPoint)
// Ignore points too close to start or end stops
if ((!segment.stopAtStart ||
pointOnLine.properties.location > APPROX_MIN_SPACING_KM) &&
(!segment.stopAtEnd ||
segmentLengthKm - pointOnLine.properties.location > APPROX_MIN_SPACING_KM)
) {
pointOnLine.properties.stopId = stop.stop_id
pointOnLine.properties.stopCoords = [stop.stop_lon, stop.stop_lat]
candidateStops.push(pointOnLine)
}
}
}
})
// cluster stops that are close to each other
const clusteredStops = clustersDbscan(turf.featureCollection(candidateStops), APPROX_MIN_SPACING_KM, {minPoints: 1})
// in each cluster, find the stop that is closest to the segment
const closestStopPerCluster = []
clusteredStops.features.forEach(stop => {
const cluster = stop.properties.cluster
if (!closestStopPerCluster[cluster] ||
stop.properties.dist < closestStopPerCluster[cluster].properties.distance) {
closestStopPerCluster[cluster] = stop
}
})
// modify the segment if there are stops to add
if (closestStopPerCluster.length > 0) {
closestStopPerCluster.sort((a, b) => a.properties.location - b.properties.location)
const last = closestStopPerCluster.length - 1