I have a polygon of a farming field. I am dropping 2 Points along the field borders in order to easily measure the distance between those points.
This works great most of the time, but in some situations the sliced line takes "the long way round".
How do I slice a linestring in the direction of the shortest distance between the 2 points?
import { polygonToLine } from '@turf/polygon-to-line'
import lineSlice from '@turf/line-slice'
const slicedLineString = lineSlice(
startPoint,
endPoint,
polygonToLine(polygonFeature)
)
Blank context image of the field:
The below image is correct, the shortest distance shown by the black line:
The below image shows the black line measuring in the wrong direction, not the shortest distance/direction between the 2 points.
I solved this by:
const lineString = polygonToLine(JSON.parse(this.parcel.geometry))
// Loop through all coordinates of linestring Feature, find coordinate pair that is furthest from both the start and end coordinates
let furthestDistance = 0
let furthestIndex = 0
for (let i = 0; i < lineString.geometry.coordinates.length; i++) {
const distanceToStart = distance(lineString.geometry.coordinates[i], this.startCoordinates, {
units: 'kilometers'
})
const distanceToEnd = distance(lineString.geometry.coordinates[i], this.endCoordinates, {
units: 'kilometers'
})
const totalDistance = distanceToStart + distanceToEnd
if (totalDistance > furthestDistance) {
furthestDistance = totalDistance
furthestIndex = i
}
}
// Re-order linestring geometry coordinates so that the furthest coordinate pair is at the start
const reorderedCoordinates = lineString.geometry.coordinates
.slice(furthestIndex)
.concat(lineString.geometry.coordinates.slice(0, furthestIndex))
lineString.geometry.coordinates = reorderedCoordinates
this.slicedLine = lineSlice(this.startCoordinates, this.endCoordinates, lineString)