通过一堆点,线上的点或者靠近线的点,来截取线段,将线段截取成多段
/**
* 通过点截取线,点可以是线上的形状点也可以是靠近线的点
*
* 线 ------------------------------------------
* 点 . . . . . .
*
* 截取完的线段:
* .---------.-----.------.-------.-----------.
*
*/
public static List<Geometry> subLineUseCoordinates(Geometry lineString, Coordinate[] coordinates){
List<LinearLocation> linearLocations = new ArrayList<>();
LocationIndexedLine locationIndexedLine = new LocationIndexedLine(lineString);
LinearLocation startIndex = locationIndexedLine.getStartIndex();
LinearLocation endIndex = locationIndexedLine.getEndIndex();
//加入线的收尾 偏移量
linearLocations.add(startIndex);
linearLocations.add(endIndex);
//加入点的偏移量
for (Coordinate coordinate : coordinates) {
LinearLocation indexOfCoordinate = locationIndexedLine.indexOf(coordinate);
linearLocations.add(indexOfCoordinate);
}
//排序
linearLocations.sort(Comparator.comparingDouble(e-> e.getSegmentIndex()+e.getSegmentFraction()));
List<Geometry> subGeos = new ArrayList<>();
//截取
for (int i= 0; i < linearLocations.size(); i++) {
int next = i+1;
if(next >= linearLocations.size()){
break;
}
LinearLocation currentLinear = linearLocations.get(i);
LinearLocation nextLinear = linearLocations.get(next);
subGeos.add(locationIndexedLine.extractLine(currentLinear, nextLinear));
}
return subGeos;
}