判断两个lineString是否同向

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.linearref.LengthIndexedLine;

import java.util.*;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        Coordinate[] cs1 = {new Coordinate(1, 0), new Coordinate(1, 2)};
        Coordinate[] cs2 = {new Coordinate(0, 1), new Coordinate(1, 2)};
        boolean result = checkSameDir(geometryFactory.createLineString(cs1), geometryFactory.createLineString(cs2));
        System.out.println(result);
    }

    /**
     * 两条线段角度差在0.35之内算同向
     */
    public static boolean checkSameDir(LineString ln1, LineString ln2) {
        double angleDif = 0.35;
        int count1 = 0;
        int count2 = 0;
        List<Coordinate> coordinates = getLineStringPoints(ln1);
        for (int i = 0; i < coordinates.size() - 1; i++) {
            LineSegment lineSegment1 = new LineSegment(coordinates.get(i), coordinates.get(i + 1));
            LineSegment lineSegment2 = getPointLatelyLineString(coordinates.get(i), ln2);
            double angle2 = lineSegment2.angle();
            double angle1 = lineSegment1.angle();
            if (Math.abs(angle1 - angle2) < angleDif) {
                count2++;
            } else {
                if (angle1 * angle2 < 0) {
                    count1++;
                } else if (angle1 * angle2 > 0) {
                    count2++;
                }
            }
        }
        return count2 > count1;
    }

    /**
     * 获取距离lineString最近的线段
     */
    public static LineSegment getPointLatelyLineString(Coordinate coordinate, LineString lineString) {
        CoordinateSequence coordinateSequence = lineString.getCoordinateSequence();
        List<LineSegment> lineSegments = new ArrayList<>();
        Coordinate prevCoordinate = coordinateSequence.getCoordinate(0);
        for (int i = 1; i < coordinateSequence.size(); i++) {
            Coordinate coordinate1 = coordinateSequence.getCoordinate(i);
            LineSegment segment = new LineSegment(prevCoordinate, coordinate1);
            if (segment.getLength() > 0) {
                lineSegments.add(segment);
            }
            prevCoordinate = coordinate;
        }
        return lineSegments.stream()
                .min(Comparator.comparing(lineSegment -> lineSegment.distance(coordinate))).orElse(new LineSegment());
    }

    /***
     * 将lineString 分为20对点,按y抽拆,并且返回升序后的点对
     * @param lineString
     * @return
     */
    public static List<Coordinate> getLineStringPoints(LineString lineString) {
        int count = 20;
        LengthIndexedLine lengthIndex = new LengthIndexedLine(lineString);
        List<Coordinate> list = new ArrayList<>(count);
        Coordinate[] coordinates = lineString.getCoordinates();
        Map<Double, Coordinate> map = new HashMap<>(count);
        for (int i = 1; i < count; i++) {
            double offset = lineString.getLength() * ((double) i / count);
            map.put(offset, lengthIndex.extractPoint(offset));
        }
        for (Coordinate coordinate : coordinates) {
            map.put(lengthIndex.indexOf(coordinate), coordinate);
        }
        List<Map.Entry<Double, Coordinate>> orderList =
                map.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList());
        orderList.forEach(e -> list.add(e.getValue()));
        return list;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值