Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
: 0
const newLineSegment = lineString(newShape)
const distanceAdded = lineDistance(newLineSegment, 'meters')
const newPatternSegments = [...patternSegments]
if (splitInterval > 0) {
// If split interval is provided (e.g., to add stops at intervals along
// new segment), split new line segment and add temp control points (later
// to be assigned stop IDs).
const numIntervals = Math.floor(distanceAdded / splitInterval)
let previousDistance = 0
// Iterate over intervals and store positions for constructing stops
for (let i = 1; i <= numIntervals; i++) {
const splitDistance = (i * splitInterval)
// Default unit for lineSliceAlong is km (meters not supported).
// console.log(`slicing line from ${previousDistance} m to ${splitDistance} m.`, newLineSegment)
const slicedSegment = lineSliceAlong(newLineSegment, previousDistance / 1000, splitDistance / 1000)
const newCoords = slicedSegment.geometry.coordinates
// Push new coordinates to updated pattern segments
newPatternSegments.push(newCoords)
// Add stops along new line segment at distance (initial dist +
// stopInterval)
const endPoint = point(newCoords[newCoords.length - 1])
const controlPoint = {
id: generateUID(),
point: endPoint,
// If control points are to become stops, point type and stop ID must
// be added later (e.g., in addStopAtInterval).
pointType: POINT_TYPE.ANCHOR,
distance: initialDistance + splitDistance
}
// $FlowFixMe
clonedControlPoints.push(controlPoint)
// FIXME this is breaking stuff when the remaining line has only one coordinate!!!
// console.log(`remaining line dist = ${remainingLineDistance}`, remainingLine, scaledDistanceInMeters / 1000)
if (scaledDistanceInMeters === remainingLineDistance) {
// This should only be the case with the last stop
// console.log(`pattern stop ${i}/${patternStops.length} distance is equal to remaining lint distance, setting to end of shape`)
// FIXME: what if this happens not on the last pattern stop
insertPoint = pointAtLastCoordinate(remainingLine)
insertIndex = shapePointsCopy.length - 1
distance = remainingLineDistance
itemsToDelete = 1
} else {
// This should be the case for all but first and last stops
distance = scaledDistanceInMeters
// Use the pattern line to find the lat/lng to use for the generated
// shape point.
const lineSegment = lineSliceAlong(
patternLine,
0,
scaledDistanceInMeters / 1000
)
insertPoint = pointAtLastCoordinate(lineSegment)
// Insert generated shape point in the correct location according to
// its distance along the line.
insertIndex = shapePointsCopy.findIndex(
sp => sp.shapeDistTraveled > distance
)
itemsToDelete = 0
}
}
}
}
// Insert projected stop into shape points list
// points
const sliceDistance = (distance - previousDistance) / 1000
let lineSegment
// console.log(remainingLine, sliceDistance, stopControlPoints[i])
if (sliceDistance <= 0 || isNaN(sliceDistance)) {
console.warn(`Slice distance for pattern stop index=${i} is ${sliceDistance} (${distance} = ${previousDistance} * scale ${distScaleFactor}). Reverting to straight line.`)
// If there is no more line segment left, extend line to stop control
// point and TODO signal warning to user?
const {coordinates} = remainingLine.geometry
lineSegment = lineString([
coordinates[coordinates.length - 1],
[stopControlPoints[i].shapePtLon, stopControlPoints[i].shapePtLat]
])
} else {
// Otherwise, slice line at specified distance.
lineSegment = lineSliceAlong(remainingLine, 0, sliceDistance)
if (!lineSegment) {
console.warn(`Could not slice remaining line segment at index=${i}`)
}
const endOfLine = lineDistance(remainingLine, 'kilometers')
if (sliceDistance >= endOfLine) {
// Do not attempt to slice line if the distance parameters are not
// correctly aligned. This seems to sometimes happen perhaps due to
// rounding errors on the final pattern stop.
console.warn(`Slice begin distance (${sliceDistance} km) is greater than or equal to end distance (${endOfLine} km). Cannot slice.`)
} else {
try {
remainingLine = lineSliceAlong(
remainingLine,
sliceDistance,
endOfLine
)
if (sliceDistance <= 0 || isNaN(sliceDistance)) {
// If slice distance is zero or negative, coerce to one meter.
// Prepare geometries for debugging.
// patternLine.properties.stroke = randomColor()
const features = featurecollection([
patternLine,
remainingLine,
insertPoint,
point([stop.stop_lon, stop.stop_lat])
])
console.warn(`Slice distance for pattern stop index=${i} is ${sliceDistance} (${distance} - ${previousDistance}), setting to 1 meter
http://geojson.io/#data=data:application/json,${encodeURIComponent(JSON.stringify(features))}`, patternStop)
sliceDistance = 1 / 1000 // set slice distance to 1 meter if negative or zero
}
// console.log(`slicing line at ${sliceDistance} km`, remainingLine)
const lineSegment = lineSliceAlong(remainingLine, 0, sliceDistance)
const totalLength = lineDistance(remainingLine)
// console.log(`slice #${i}/${patternStops.length - 1}`, lineSegment)
if (i < patternStops.length - 1) {
// Compute remaining line segment (only if not working on the final stop)
try {
remainingLine = lineSliceAlong(
remainingLine,
sliceDistance,
totalLength
)
} catch (err) {
console.warn(`Could not slice line for stop index=${i} (at ${sliceDistance} km / ${totalLength} km)`, remainingLine, err)
}
// console.log('remainingLine', remainingLine)
if (remainingLine.geometry.coordinates.length < 2) {
throw new Error(
])
} else {
// Otherwise, slice line at specified distance.
lineSegment = lineSliceAlong(remainingLine, 0, sliceDistance)
if (!lineSegment) {
console.warn(`Could not slice remaining line segment at index=${i}`)
}
const endOfLine = lineDistance(remainingLine, 'kilometers')
if (sliceDistance >= endOfLine) {
// Do not attempt to slice line if the distance parameters are not
// correctly aligned. This seems to sometimes happen perhaps due to
// rounding errors on the final pattern stop.
console.warn(`Slice begin distance (${sliceDistance} km) is greater than or equal to end distance (${endOfLine} km). Cannot slice.`)
} else {
try {
remainingLine = lineSliceAlong(
remainingLine,
sliceDistance,
endOfLine
)
} catch (err) {
console.warn(`Could not slice remaining line segment at stop index=${i}`, remainingLine, `${sliceDistance} km to ${endOfLine} km`, err)
}
}
}
patternSegments.push(lineSegment.geometry.coordinates)
previousDistance = distance
}
continue
}
// console.log(`generating shape point for stop #${i} at distance: ${scaledDistanceInMeters}`, patternStop, remainingLine)
if (i === 0 && scaledDistanceInMeters !== 0) {
remainingLine,
insertPoint,
point([stop.stop_lon, stop.stop_lat])
])
console.warn(`Slice distance for pattern stop index=${i} is ${sliceDistance} (${distance} - ${previousDistance}), setting to 1 meter
http://geojson.io/#data=data:application/json,${encodeURIComponent(JSON.stringify(features))}`, patternStop)
sliceDistance = 1 / 1000 // set slice distance to 1 meter if negative or zero
}
// console.log(`slicing line at ${sliceDistance} km`, remainingLine)
const lineSegment = lineSliceAlong(remainingLine, 0, sliceDistance)
const totalLength = lineDistance(remainingLine)
// console.log(`slice #${i}/${patternStops.length - 1}`, lineSegment)
if (i < patternStops.length - 1) {
// Compute remaining line segment (only if not working on the final stop)
try {
remainingLine = lineSliceAlong(
remainingLine,
sliceDistance,
totalLength
)
} catch (err) {
console.warn(`Could not slice line for stop index=${i} (at ${sliceDistance} km / ${totalLength} km)`, remainingLine, err)
}
// console.log('remainingLine', remainingLine)
if (remainingLine.geometry.coordinates.length < 2) {
throw new Error(
`Remaining line after pattern stop #${i}/${patternStops.length -
1} has fewer than two coordinates`
)
}
}
patternSegments.push(lineSegment.geometry.coordinates)
function sliceLineSegments(line, segmentLength, units, callback) {
var lineLength = lineDistance(line, {units: units});
// If the line is shorter than the segment length then the orginal line is returned.
if (lineLength <= segmentLength) return callback(line);
var numberOfSegments = lineLength / segmentLength;
// If numberOfSegments is integer, no need to plus 1
if (!Number.isInteger(numberOfSegments)) {
numberOfSegments = Math.floor(numberOfSegments) + 1;
}
for (var i = 0; i < numberOfSegments; i++) {
var outline = lineSliceAlong(line, segmentLength * i, segmentLength * (i + 1), {units: units});
callback(outline, i);
}
}