package com.ltgk.boat.utils;


import com.ltgk.port.domain.vo.ParkedAreaForAppVO;
import org.locationtech.jts.geom.*;

/**
 * 判断点位是否在区域内工具类
 *
 * @author Qi ChengBin
 * @date 2025/06/03
 */
public class JudgePointInArea {

    /**
     * 判断点位是否在相关区域内
     * -- 在区域内 返回true
     *
     * @param judgePoint: 所判断的点位
     * @param points:     点位组成的区域
     * @return boolean
     * @author Qi ChengBin
     * @date 2025/6/3
     */
    public static boolean judgePointInArea(ParkedAreaForAppVO.Point judgePoint, String[] points) {
        int nPoints = points.length;
        // 坐标转换修正
        final double SCALE = 1e6;
        GeometryFactory geometryFactory = new GeometryFactory();

        // 构建多边形坐标
        Coordinate[] coordinates = new Coordinate[nPoints];
        for (int i = 0; i < nPoints; i++) {
            String[] coord = points[i].split(",");
            double lng = Double.parseDouble(coord[0]);
            double lat = Double.parseDouble(coord[1]);
            coordinates[i] = new Coordinate(lng * SCALE, lat * SCALE);
        }

        // 创建多边形
        LinearRing ring = geometryFactory.createLinearRing(coordinates);
        Polygon region = geometryFactory.createPolygon(ring);

        // 创建检查点（注意经度纬度顺序）
        double checkLng = judgePoint.getLongitude();
        double checkLat = judgePoint.getLatitude();
        Point point = geometryFactory.createPoint(
                new Coordinate(checkLng * SCALE, checkLat * SCALE));

        return region.contains(point);
    }
}
