Browse Source

增加创建圆方法(备用)

欧阳劲驰 2 weeks ago
parent
commit
b1a673d3e1

+ 97 - 1
src/main/java/com/shkpr/service/alambizplugin/commtools/GeomUtil.java

@@ -1,18 +1,114 @@
 package com.shkpr.service.alambizplugin.commtools;
 
+import com.global.base.log.LogLevelFlag;
+import com.global.base.log.LogPrintMgr;
+import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
+import org.geotools.referencing.CRS;
+import org.geotools.referencing.GeodeticCalculator;
+import org.locationtech.jts.geom.Coordinate;
+import org.locationtech.jts.geom.Envelope;
 import org.locationtech.jts.geom.GeometryFactory;
+import org.locationtech.jts.geom.Polygon;
 import org.locationtech.jts.geom.PrecisionModel;
+import org.locationtech.jts.util.GeometricShapeFactory;
+import org.opengis.referencing.FactoryException;
+import org.opengis.referencing.operation.TransformException;
 
 /**
  * 地理工具类
  *
  * @author 欧阳劲驰
- * @since JDK1.8
+ * @since 1.0.0
  */
 public class GeomUtil {
     /**
+     * log
+     */
+    private final static String mStrClassName = "GeomUtil";
+    private final static String mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
+    /**
      * geom工厂
      */
     private static final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4490);
+    /**
+     * 形状工厂
+     */
+    private final static GeometricShapeFactory shapeFactory = new GeometricShapeFactory(geometryFactory);
+
+    /**
+     * 创建圆
+     *
+     * @param centre  圆心
+     * @param radius  半径
+     * @param crsCode 坐标系
+     * @return 圆
+     */
+    public static Polygon createCircle(Coordinate centre, double radius, String crsCode) {
+        try {
+            //端点数量
+            int extremePointsTotal = 32;
+            //经度
+            double lng = centre.getX();
+            //纬度
+            double lat = centre.getY();
+
+            //创建大地计算器,并设置坐标系
+            GeodeticCalculator gc = new GeodeticCalculator(CRS.decode("EPSG:4490"));
+
+            //计算东点(90度),正东移动
+            gc.setStartingGeographicPoint(lng, lat);
+            gc.setDirection(90, radius);
+            //获取结果
+            double[] eastCoords = gc.getDestinationPosition().getCoordinate();
+            Coordinate eastPoint = new Coordinate(eastCoords[1], eastCoords[0]); // 创建 Coordinate 对象
+
+            //计算西点(270度),正西移动
+            gc.setStartingGeographicPoint(lng, lat);
+            gc.setDirection(270, radius);
+            //获取结果
+            double[] westCoords = gc.getDestinationPosition().getCoordinate();
+            Coordinate westPoint = new Coordinate(westCoords[1], westCoords[0]);
+
+            //计算北点(0度),正北移动
+            gc.setStartingGeographicPoint(lng, lat);
+            gc.setDirection(0, radius);
+            //获取结果
+            double[] northCoords = gc.getDestinationPosition().getCoordinate();
+            Coordinate northPoint = new Coordinate(northCoords[1], northCoords[0]);
+
+            //计算南点(180度)
+            gc.setStartingGeographicPoint(lng, lat);
+            gc.setDirection(180, radius);
+            //获取结果
+            double[] southCoords = gc.getDestinationPosition().getCoordinate();
+            Coordinate southPoint = new Coordinate(southCoords[1], southCoords[0]);
+
+            //创建边界框
+            double x1 = westPoint.x;
+            double x2 = eastPoint.x;
+            double y1 = southPoint.y;
+            double y2 = northPoint.y;
+            Envelope envelope = new Envelope(x1, x2, y1, y2);
+
+            //设置圆心
+            Coordinate coordinate = new Coordinate(lng, lat);
+            shapeFactory.setCentre(coordinate);
+            //设置边界
+            shapeFactory.setEnvelope(envelope);
+            //设置边界点数量
+            shapeFactory.setNumPoints(extremePointsTotal);
+
+            //创建圆
+            Polygon circle = shapeFactory.createCircle();
+            //设置坐标系
+            circle.setSRID(4490);
+
+            return circle;
+        } catch (TransformException | FactoryException e) {
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
+                    , String.format("geom创建失败: error:%s", e));
 
+            return null;
+        }
+    }
 }