吉安感知网项目-后端
linwei
2026-01-22 1510366fa12fafd5197ebcd8d7fbc45d2383218d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package org.sxkj.gd.utils;
 
import org.geotools.geometry.DirectPosition2D;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.*;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
 
/**
 * @Description TODO 航线拆分工具类
 * @Author AIX
 * @Date 2025/6/26 9:48
 * @Version 1.0
 */
public class GeomUtils {
 
    private static final GeometryFactory geometryFactory = new GeometryFactory();
 
    /**
     * 获取指定点周围指定距离(米)内的多边形区域
     *
     * @param lon      中心点经度
     * @param lat      中心点纬度
     * @param distance 距离(米)
     * @return 多边形几何对象
     */
    public static Geometry getAreaWithinDistance(double lon, double lat, double distance) throws Exception {
        // 创建投影CRS(使用UTM投影,适用于小范围距离计算)
        CoordinateReferenceSystem auto = CRS.decode("AUTO:42001," + lon + "," + lat);
        // 源CRS(WGS84)
        CoordinateReferenceSystem sourceCRS = DefaultGeographicCRS.WGS84;
        // 转换到投影CRS
        MathTransform toTransform = CRS.findMathTransform(sourceCRS, auto);
        // 转换回WGS84
        MathTransform fromTransform = CRS.findMathTransform(auto, sourceCRS);
 
        // 转换中心点到投影坐标系
        DirectPosition2D src = new DirectPosition2D(sourceCRS, lon, lat);
        DirectPosition2D dest = new DirectPosition2D();
        toTransform.transform(src, dest);
 
        // 在投影坐标系中创建缓冲区(距离单位为米)
        Point projPoint = geometryFactory.createPoint(new Coordinate(dest.x, dest.y));
        Geometry buffer = projPoint.buffer(distance);
 
        // 转换回WGS84坐标系
        Geometry transformedBuffer = JTS.transform(buffer, fromTransform);
 
        return transformedBuffer;
    }
 
    /**
     * 获取某点周围指定距离(米)的缓冲区
     */
    public static Polygon getBufferAroundPoint(double lon, double lat, double distance) throws Exception {
        Geometry area = getAreaWithinDistance(lon, lat, distance);
        return (Polygon) area;
    }
 
    /**
     * 获取某点周围指定距离(米)的缓冲区,返回WKT字符串格式
     *
     * @param lon      中心点经度
     * @param lat      中心点纬度
     * @param distance 距离(米)
     * @return WKT格式的多边形字符串,格式:POLYGON((lon1 lat1, lon2 lat2, ...))
     */
    public static String getBufferAroundPointAsString(double lon, double lat, double distance) throws Exception {
        Polygon polygon = getBufferAroundPoint(lon, lat, distance);
        return polygon.toText();
    }
 
    /**
     * 将Polygon对象转换为WKT字符串格式
     *
     * @param polygon 多边形对象
     * @return WKT格式的多边形字符串,格式:POLYGON((lon1 lat1, lon2 lat2, ...))
     */
    public static String polygonToString(Polygon polygon) {
        return polygon.toText();
    }
 
 
}