From a6fe405947004d6571806edabd8f14357e144cfa Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期一, 14 十月 2024 17:31:31 +0800
Subject: [PATCH] 1

---
 src/main/java/com/se/simu/helper/GdalHelper.java |  189 +++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 174 insertions(+), 15 deletions(-)

diff --git a/src/main/java/com/se/simu/helper/GdalHelper.java b/src/main/java/com/se/simu/helper/GdalHelper.java
index 5de0452..1601ea1 100644
--- a/src/main/java/com/se/simu/helper/GdalHelper.java
+++ b/src/main/java/com/se/simu/helper/GdalHelper.java
@@ -1,8 +1,13 @@
 package com.se.simu.helper;
 
 import lombok.extern.slf4j.Slf4j;
+import org.gdal.gdal.Band;
+import org.gdal.gdal.Dataset;
 import org.gdal.gdal.gdal;
-import org.gdal.ogr.ogr;
+import org.gdal.gdalconst.gdalconst;
+import org.gdal.ogr.*;
+import org.gdal.osr.SpatialReference;
+import org.gdal.osr.osr;
 
 import java.io.File;
 
@@ -10,12 +15,42 @@
  * GDAL甯姪绫�
  *
  * @author WWW
- * @date 2024-07-16
+ * @date 2024-09-12
  */
 @Slf4j
 @SuppressWarnings("ALL")
 public class GdalHelper {
+    public final static int I4326 = 4326;
+
+    public final static int I4490 = 4490;
+
+    public static SpatialReference SR4326;
+
+    public static SpatialReference SR4490;
+
+    public final static String CGCS2000 = "CGCS2000";
+
+    /**
+     * 鍒濆鍖�
+     */
     public static void init(String gdalPath) {
+        // 閰嶇疆鐜鍙橀噺
+        if (!StringHelper.isEmpty(gdalPath)) {
+            if (WebHelper.isWin()) {
+                gdal.SetConfigOption("GDAL_DATA", gdalPath + "/gdal-data");
+                gdal.SetConfigOption("PROJ_LIB", gdalPath + "/proj7/share");
+                //System.setProperty("PROJ_LIB", gdalPath + "/proj7/share")
+                gdal.SetConfigOption("GDAL_DRIVER_PATH", gdalPath + "/gdalplugins");
+
+                String path = System.getenv("PATH");
+                if (!path.contains(gdalPath)) {
+                    System.setProperty("PATH", path + ";" + gdalPath);
+                }
+            } else {
+                //System.setProperty("java.library.path", gdalPath);
+            }
+        }
+
         // 鏀寔涓枃璺緞
         gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
         // 灞炴�ц〃鏀寔涓枃锛欳P936
@@ -23,21 +58,145 @@
         gdal.SetConfigOption("PGEO_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s");
         gdal.SetConfigOption("MDB_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s");
 
-        // 閰嶇疆鐜鍙橀噺
-        if (!StringHelper.isEmpty(gdalPath)) {
-            gdal.SetConfigOption("GDAL_DATA", gdalPath + File.separator + "gdal-data");
-            gdal.SetConfigOption("PROJ_LIB", gdalPath + File.separator + "proj7" + File.separator + "share");
-            //System.setProperty("PROJ_LIB", gdalPath + File.separator + "proj7" + File.separator + "share")
-            gdal.SetConfigOption("GDAL_DRIVER_PATH", gdalPath + File.separator + "gdalplugins");
-
-            String path = System.getenv("PATH");
-            if (!path.contains(gdalPath)) {
-                System.setProperty("PATH", path + ";" + gdalPath);
-            }
-        }
-
         // 娉ㄥ唽鎵�鏈夌殑椹卞姩
         gdal.AllRegister();
         ogr.RegisterAll();
+        initSr();
+    }
+
+    /**
+     * 鍒濆鍖栧潗鏍囩郴
+     * <p>
+     * https://blog.csdn.net/CallmeAdo/article/details/127558139
+     */
+    public static void initSr() {
+        try {
+            SR4326 = new SpatialReference();
+            SR4326.ImportFromEPSG(I4326);
+            // 瀵逛簬lat/long椤哄簭鐨勫湴鐞咰RS锛屾暟鎹粛鐒舵槸long/lat椤哄簭鐨�
+            SR4326.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
+
+            SR4490 = new SpatialReference();
+            SR4490.ImportFromEPSG(I4490);
+            SR4490.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+        }
+    }
+
+    /**
+     * 鍒涘缓閲戝瓧濉�
+     */
+    public static void createPyramid(String file) {
+        Dataset ds = null;
+        try {
+            File f = new File(file);
+            if (!f.exists() || f.isDirectory()) {
+                return;
+            }
+
+            ds = gdal.Open(file, gdalconst.GA_ReadOnly);
+            if (null == ds || ds.getRasterCount() < 1 || null == ds.GetSpatialRef()) {
+                return;
+            }
+
+            // 鍒涘缓閲戝瓧濉�
+            Band band = ds.GetRasterBand(1);
+            if (0 == band.GetOverviewCount()) {
+                ds.BuildOverviews("nearest", new int[]{2, 4, 6, 8, 16}, null);
+            }
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+        } finally {
+            if (null != ds) {
+                ds.delete();
+            }
+        }
+    }
+
+    /**
+     * 閿�姣佽祫婧�
+     */
+    public static void delete(Layer layer, DataSource dataSource, Driver driver) {
+        try {
+            if (null != layer) {
+                layer.delete();
+            }
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+        }
+        try {
+            if (null != dataSource) {
+                dataSource.delete();
+            }
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+        }
+        try {
+            if (null != driver) {
+                driver.delete();
+            }
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+        }
+    }
+
+    public static Geometry getMinPoint(Dataset ds)
+    {
+        double[] transform = new double[6];
+        ds.GetGeoTransform(transform);
+
+        double xMin = transform[0];
+        double yMin = transform[3] - ds.getRasterYSize() * transform[1];
+
+        Geometry point = new Geometry(ogr.wkbPoint);
+        point.AddPoint(xMin, yMin, 0);
+
+        return Transform(ds, point);
+    }
+
+    public static Geometry getMaxPoint(Dataset ds)
+    {
+        /*
+         * transform[0] 宸︿笂瑙抶鍧愭爣
+         * transform[1] 涓滆タ鏂瑰悜鍒嗚鲸鐜�
+         * transform[2] 鏃嬭浆瑙掑害, 0琛ㄧず鍥惧儚 "鍖楁柟鏈濅笂"
+         *
+         * transform[3] 宸︿笂瑙抷鍧愭爣
+         * transform[4] 鏃嬭浆瑙掑害, 0琛ㄧず鍥惧儚 "鍖楁柟鏈濅笂"
+         * transform[5] 鍗楀寳鏂瑰悜鍒嗚鲸鐜�
+         */
+        double[] transform = new double[6];
+        ds.GetGeoTransform(transform);
+
+        double xMax = transform[0] + (ds.getRasterYSize() * transform[1]);
+        double yMax = transform[3];
+
+        Geometry point = new Geometry(ogr.wkbPoint);
+        point.AddPoint(xMax, yMax, 0);
+
+        return Transform(ds, point);
+    }
+
+    public static Geometry Transform(Dataset ds, Geometry point)
+    {
+        point.AssignSpatialReference(ds.GetSpatialRef());
+        if (ds.GetSpatialRef().IsGeographic() > 0)
+        {
+            return point;
+        }
+
+        String srsName = ds.GetSpatialRef().GetName();
+        //if (srsName.Contains(CGCS2000))
+        //{
+        //    point.TransformTo(sr4490);
+        //}
+        //else
+        //{
+        point.TransformTo(SR4326);
+        //}
+        //point.SwapXY();
+
+        return point;
     }
 }

--
Gitblit v1.9.3