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();
|
}
|
|
|
}
|