xiewj 7 kuukautta sitten
vanhempi
commit
3baa59573e

+ 6 - 1
4dkankan-utils-geo-query/pom.xml

@@ -14,6 +14,7 @@
     <properties>
         <fastjson.version>2.0.6</fastjson.version>
         <hutool.version>5.8.6</hutool.version>
+        <proj4j.version>1.1.4</proj4j.version>
     </properties>
 
     <dependencies>
@@ -46,7 +47,11 @@
             <version>2.3.12.RELEASE</version>
         </dependency>
 
-
+        <dependency>
+            <groupId>org.locationtech.proj4j</groupId>
+            <artifactId>proj4j</artifactId>
+            <version>${proj4j.version}</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 3 - 3
4dkankan-utils-geo-query/src/main/java/com/fdkankan/geo/GeoQueryUtil.java

@@ -41,10 +41,10 @@ import java.nio.file.StandardCopyOption;
 @ConfigurationProperties(prefix = "geoquery")
 public class GeoQueryUtil {
     @Value("${geoquery.dataFilePath:/mnt/geoQuery/GeoJSON.json}")
-    private String dataFilePath;
+    private String dataFilePath="I:\\geoserver\\AreaCity-Query-Geometry\\GeoJSON-Polygon-ok_geo-240508-162233";
 
     @Value("${geoquery.saveWkbsFilePath:/mnt/geoQuery/GeoJSON.wkbs}")
-    private String saveWkbsFilePath;
+    private String saveWkbsFilePath="I:\\geoserver\\AreaCity-Query-Geometry\\GeoJSON.wkbs";
 
     @PostConstruct
     public void init() {
@@ -145,7 +145,7 @@ public class GeoQueryUtil {
     public static void main(String[] args) throws Exception {
         GeoQueryUtil queryUtil  = new GeoQueryUtil();
         queryUtil.init();
-        AreaCityQuery.QueryResult queryResult = queryUtil.queryPointInfo("120.885676,23.544815");
+        boolean queryResult = queryUtil.queryPoint("101.44,17.93");
         System.out.println(queryResult);
 
     }

+ 53 - 0
4dkankan-utils-geo-query/src/main/java/com/fdkankan/geo/GeoTransformUtil.java

@@ -0,0 +1,53 @@
+package com.fdkankan.geo;
+
+import org.locationtech.proj4j.*;
+
+/**
+ * @author Xiewj
+ * @date 2024/12/31
+ */
+public class GeoTransformUtil {
+
+    static String wgs84CRS = "4326";
+
+    /**
+     * 获取wgs84坐标
+     *
+     * @param sourceCRS   原始坐标系代码 EPSG:3857; 只需要EPSG冒号后的编码
+     * @param coordinate   new ProjCoordinate(lon, lat) 坐标系代码   原始数据坐标系 "4326";  "EPSG:3857";
+     * @return
+     */
+    public static Double[] getWgs84(ProjCoordinate coordinate, String sourceCRS) {
+        ProjCoordinate projCoordinate = Proj4Tool.convertByTargetCRS(coordinate, sourceCRS, wgs84CRS);
+        return new Double[]{projCoordinate.x,projCoordinate.y,coordinate.z};
+    }
+    /**
+     * 获取高德坐标系  wgs84ToGcj02
+     * 即wgs84 转 高德
+     * @param coordinate   new ProjCoordinate(lon, lat) 坐标系代码   wgs84坐标系
+     * @return
+     */
+    public static Double[] wgs84ToGcj02(ProjCoordinate coordinate) {
+        Double[] doubles = MapTool.WGS84ToGCJ02(coordinate.x, coordinate.y);
+        return new Double[]{doubles[0],doubles[1],coordinate.z};
+    }
+    /**
+     * 获取百度坐标系  gcj02ToBd09
+     * 即高德 转 百度
+     * @param coordinate   new ProjCoordinate(lon, lat) 坐标系代码   wgs84坐标系
+     * @return
+     */
+    public static Double[] gcj02ToBd09(ProjCoordinate coordinate) {
+        Double[] doubles = MapTool.GCJ02ToBD09(coordinate.x, coordinate.y);
+        return new Double[]{doubles[0],doubles[1],coordinate.z};
+    }
+
+    public static void main(String[] args) {
+        ProjCoordinate coordinate1 = new ProjCoordinate(478041.3,3960170.3,83.25);
+        Double[] wgs84 = getWgs84(coordinate1, "4548");
+        System.out.println("wgs84:"+wgs84[0]+","+wgs84[1]+","+wgs84[2]);
+        ProjCoordinate coordinate2 = new ProjCoordinate(wgs84[0],wgs84[1],wgs84[2]);
+        Double[] gcj02 = wgs84ToGcj02(coordinate2);
+        System.out.println("gcj02:"+gcj02[0]+","+gcj02[1]+","+gcj02[2]);
+    }
+}

+ 115 - 0
4dkankan-utils-geo-query/src/main/java/com/fdkankan/geo/MapTool.java

@@ -0,0 +1,115 @@
+package com.fdkankan.geo;
+
+/**
+ * @author Xiewj
+ * @date 2024/12/31
+ */
+public class MapTool {
+    private static double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
+    private static double PI = 3.1415926535897932384626;
+    private static double a = 6378245.0;
+    private static double ee = 0.00669342162296594323;
+
+    /**
+     * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
+     * 即 百度 转 谷歌、高德
+     * @param bd_lon
+     * @param bd_lat
+     * @return Double[lon,lat]
+     */
+    public static Double[] BD09ToGCJ02(Double bd_lon,Double bd_lat){
+        double x = bd_lon - 0.0065;
+        double y = bd_lat - 0.006;
+        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_PI);
+        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_PI);
+        Double[] arr = new Double[2];
+        arr[0] = z * Math.cos(theta);
+        arr[1] = z * Math.sin(theta);
+        return arr;
+    }
+
+    /**
+     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
+     * 即谷歌、高德 转 百度
+     * @param gcj_lon
+     * @param gcj_lat
+     * @return Double[lon,lat]
+     */
+    public static Double[] GCJ02ToBD09(Double gcj_lon,Double gcj_lat){
+        double z = Math.sqrt(gcj_lon * gcj_lon + gcj_lat * gcj_lat) + 0.00002 * Math.sin(gcj_lat * x_PI);
+        double theta = Math.atan2(gcj_lat, gcj_lon) + 0.000003 * Math.cos(gcj_lon * x_PI);
+        Double[] arr = new Double[2];
+        arr[0] = z * Math.cos(theta) + 0.0065;
+        arr[1] = z * Math.sin(theta) + 0.006;
+        return arr;
+    }
+
+    /**
+     * WGS84转GCJ02
+     * @param wgs_lon
+     * @param wgs_lat
+     * @return Double[lon,lat]
+     */
+    public static Double[] WGS84ToGCJ02(Double wgs_lon,Double wgs_lat){
+        GeoQueryUtil queryUtil=new GeoQueryUtil();
+        queryUtil.init();
+        if (!queryUtil.queryPoint(wgs_lon, wgs_lat)) {
+            return new Double[]{wgs_lon,wgs_lat};
+        }
+        double dlat = transformlat(wgs_lon - 105.0, wgs_lat - 35.0);
+        double dlng = transformlng(wgs_lon - 105.0, wgs_lat - 35.0);
+        double radlat = wgs_lat / 180.0 * PI;
+        double magic = Math.sin(radlat);
+        magic = 1 - ee * magic * magic;
+        double sqrtmagic = Math.sqrt(magic);
+        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
+        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
+        Double[] arr = new Double[2];
+        arr[0] = wgs_lon + dlng;
+        arr[1] = wgs_lat + dlat;
+        return arr;
+    }
+
+    /**
+     * GCJ02转WGS84
+     * @param gcj_lon
+     * @param gcj_lat
+     * @return Double[lon,lat]
+     */
+    public static Double[] GCJ02ToWGS84(Double gcj_lon,Double gcj_lat){
+        GeoQueryUtil queryUtil=new GeoQueryUtil();
+        queryUtil.init();
+        if (!queryUtil.queryPoint(gcj_lon, gcj_lat)) {
+            return new Double[]{gcj_lon,gcj_lat};
+        }
+        double dlat = transformlat(gcj_lon - 105.0, gcj_lat - 35.0);
+        double dlng = transformlng(gcj_lon - 105.0, gcj_lat - 35.0);
+        double radlat = gcj_lat / 180.0 * PI;
+        double magic = Math.sin(radlat);
+        magic = 1 - ee * magic * magic;
+        double sqrtmagic = Math.sqrt(magic);
+        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
+        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
+        double mglat = gcj_lat + dlat;
+        double mglng = gcj_lon + dlng;
+        return new Double[]{gcj_lon * 2 - mglng, gcj_lat * 2 - mglat};
+    }
+
+    private static Double transformlat(double lng, double lat) {
+        double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
+        ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
+        ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
+        ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
+        return ret;
+    }
+
+    private static Double transformlng(double lng,double lat) {
+        double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
+        ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
+        ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
+        ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
+        return ret;
+    }
+
+
+}

+ 31 - 0
4dkankan-utils-geo-query/src/main/java/com/fdkankan/geo/Proj4Tool.java

@@ -0,0 +1,31 @@
+package com.fdkankan.geo;
+
+import org.locationtech.proj4j.*;
+
+/**
+ * @author Xiewj
+ * @date 2024/12/31
+ */
+public class Proj4Tool {
+    /**
+     * proj4的常用坐标转换方法
+     *
+     * @param coordinate 经度或者x轴 纬度或者y轴
+     * @param targetCRS  坐标系代码   原始数据坐标系 "4326";  "EPSG:3857";
+     *                   //     * @param targetCRS   坐标系代码  结果数据坐标系
+     * @return
+     */
+    public static ProjCoordinate convertByTargetCRS(ProjCoordinate coordinate, String sourceCRS, String targetCRS) {
+        CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
+        CRSFactory csFactory = new CRSFactory();
+
+        CoordinateReferenceSystem crs1 = csFactory.createFromName("EPSG:" + sourceCRS);
+        CoordinateReferenceSystem crs2 = csFactory.createFromName("EPSG:" + targetCRS);
+
+        CoordinateTransform trans = ctFactory.createTransform(crs1, crs2);
+
+        ProjCoordinate p2 = new ProjCoordinate();
+
+        return trans.transform(coordinate, p2);
+    }
+}