From dfded458c5ee662a2dc4a51a6d0640e1a4f4a4bb Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 25 八月 2023 14:16:20 +0800
Subject: [PATCH] 修改栅格读取服务

---
 src/main/java/com/moon/server/entity/all/StaticData.java      |   54 +------------
 src/main/java/com/moon/server/service/data/RasterService.java |  140 ++++++++++++++++++++++++++++++++++
 2 files changed, 144 insertions(+), 50 deletions(-)

diff --git a/src/main/java/com/moon/server/entity/all/StaticData.java b/src/main/java/com/moon/server/entity/all/StaticData.java
index a87d926..5a6dbb2 100644
--- a/src/main/java/com/moon/server/entity/all/StaticData.java
+++ b/src/main/java/com/moon/server/entity/all/StaticData.java
@@ -76,34 +76,16 @@
 
     public static final int I104903 = 104903;
 
-    /**
-     * 瀛楃1
-     */
     public final static String S1 = "1";
 
-    /**
-     * 绛夊彿
-     */
     public final static String EQ = "=";
 
-    /**
-     * 瀛楃鐐�
-     */
     public final static String POINT = ".";
 
-    /**
-     * 閫楀彿
-     */
     public final static String COMMA = ",";
 
-    /**
-     * 娉㈡氮鍙�
-     */
     public final static String TILDE = "~";
 
-    /**
-     * 鍗曞紩鍙�
-     */
     public final static String SINGLE_QUOTES = "'";
 
     public final static String BBOREHOLE = "bborehole";
@@ -114,9 +96,6 @@
 
     public final static String TEXT_XML = "text/xml";
 
-    /**
-     * 姝f枩鏉�
-     */
     public final static String SLASH = "/";
 
     public final static String IN = "in";
@@ -157,53 +136,26 @@
 
     public final static String SYS_META = "sysmeta";
 
-    /**
-     * 鐗堟湰鍙�
-     */
     public final static String VERSION = "1.0.0";
 
-    /**
-     * 浠ょ墝閿�
-     */
     public final static String TOKEN_KEY = "token";
 
-    /**
-     * Cookie涓护鐗岄敭
-     */
     public final static String TOKEN_COOKIE_KEY = "token";
 
-    /**
-     * 鏂囨湰缂栫爜鏂瑰紡
-     */
     public final static String TEXT_ENCODER = "UTF-8";
 
-    /**
-     * 鎬昏川妫�
-     */
     public final static String CHECK_MAIN = "checkMain";
 
-    /**
-     * Object瀵硅薄
-     */
     public final static String OBJECT = "java.lang.Object";
 
-    /**
-     * Cookie涓璬ruid閿�
-     */
     public final static String DRUID_COOKIE_KEY = "JSESSIONID";
 
     public final static String YES = "YES";
 
     public final static String NO = "NO";
 
-    /**
-     * 绾胯矾
-     */
     public final static String ROUTE = "绾胯矾";
 
-    /**
-     * 绌胯法瓒�
-     */
     public final static String CROSSING = "绌胯法瓒�";
 
     public final static String LINESTRING = "LINESTRING";
@@ -234,6 +186,12 @@
             "        AUTHORITY[\"EPSG\",\"9122\"]],\r\n" +
             "    AUTHORITY[\"ESRI\",\"104903\"]]";
 
+    public final static String CGCS2000 = "CGCS2000";
+
+    public final static String MOON200 = "GCS_Moon_2000";
+
+    public final static List<String> EPSGS = new ArrayList<>(Arrays.asList("EPSG:4326", "EPSG:4490", "ESRI:104903"));
+
     /**
      * 16杩涘埗
      */
diff --git a/src/main/java/com/moon/server/service/data/RasterService.java b/src/main/java/com/moon/server/service/data/RasterService.java
index 3a6c070..be905d5 100644
--- a/src/main/java/com/moon/server/service/data/RasterService.java
+++ b/src/main/java/com/moon/server/service/data/RasterService.java
@@ -1,10 +1,15 @@
 package com.moon.server.service.data;
 
+import com.moon.server.entity.all.StaticData;
 import com.moon.server.entity.data.MetaEntity;
+import com.moon.server.helper.StringHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.gdal.gdal.ColorTable;
 import org.gdal.gdal.Dataset;
 import org.gdal.gdal.gdal;
+import org.gdal.ogr.Geometry;
+import org.gdal.ogr.ogr;
 import org.gdal.osr.SpatialReference;
 import org.springframework.stereotype.Service;
 
@@ -17,12 +22,33 @@
  */
 @Service
 public class RasterService {
+    private SpatialReference sr4326;
+
+    private SpatialReference sr4490;
+
+    private SpatialReference sr104903;
+
     private final static Log log = LogFactory.getLog(RasterService.class);
+
+    /**
+     * 鍒濆鍖栫┖闂村紩鐢�
+     */
+    private void initSpatialReference() {
+        if (null == sr4326) {
+            sr4326 = new SpatialReference();
+            sr4326.ImportFromEPSG(StaticData.I4326);
+
+            sr4490 = new SpatialReference();
+            sr4490.ImportFromEPSG(StaticData.I4490);
+
+            sr104903 = new SpatialReference(StaticData.MOON_2000_WKT);
+        }
+    }
 
     /**
      * 璇诲彇鏍呮牸淇℃伅
      */
-    public void ReadRasterInfo(MetaEntity me, String file) {
+    public void readRasterInfo(MetaEntity me, String file) {
         Dataset ds = null;
         try {
             File f = new File(file);
@@ -35,7 +61,35 @@
                 return;
             }
 
-            SpatialReference sr = ds.GetSpatialRef();
+            // EPSG缂栫爜
+            String epsg = getEpsg(ds);
+            // 娉㈡鏁�
+            String bands = "" + ds.getRasterCount();
+            //vd.Meta.band_type = Enum.GetName(typeof(DataType), ds.GetRasterBand(1).DataType); // 鏁版嵁绫诲瀷
+            ColorTable colorTable = ds.GetRasterBand(1).GetRasterColorTable();
+            // 鏁版嵁棰滆壊琛�
+            String ct = null == colorTable ? null : colorTable.toString();
+            // 楂樼▼鍩哄噯
+            String hDatum = null;
+
+            double[] transform = new double[6];
+            ds.GetGeoTransform(transform);
+            // 鍒嗚鲸鐜�
+            String resolution = String.format("%f,%f", transform[1], transform[5]);
+
+            if (!StaticData.EPSGS.contains(epsg)) {
+                return;
+            }
+
+            Geometry minPoint = getMinPoint(ds);
+            Geometry maxPoint = getMaxPoint(ds);
+            double xmin = minPoint.GetX(0);
+            double ymin = minPoint.GetY(0);
+            double xmax = maxPoint.GetX(0);
+            double ymax = maxPoint.GetY(0);
+
+            // 鍥涜嚦鑼冨洿
+            String geom = String.format("ST_GeomFromText('POLYGON ((%f %f,%f %f,%f %f,%f %f,%f %f))')", xmin, ymax, xmax, ymax, xmax, ymin, xmin, ymin, xmin, ymax);
         } catch (Exception ex) {
             log.error(ex.getMessage(), ex);
         } finally {
@@ -44,4 +98,86 @@
             }
         }
     }
+
+    /**
+     * 鑾峰彇EPSG缂栫爜
+     */
+    private String getEpsg(Dataset ds) {
+        SpatialReference sr = ds.GetSpatialRef();
+        if (sr != null) {
+            // 鍧愭爣绯荤粺
+            String coorSys = sr.GetName();
+            if (StaticData.MOON200.equals(coorSys)) {
+                return "ESRI:" + StaticData.I104903;
+            } else {
+                String code = sr.GetAuthorityCode(null);
+                return StringHelper.isEmpty(code) ? null : "EPSG:" + code;
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * 鑾峰彇Dataset鐨勬渶灏忕偣
+     */
+    private Geometry getMinPoint(Dataset ds) {
+        double[] transform = new double[6];
+        ds.GetGeoTransform(transform);
+
+        String epsg = ds.GetSpatialRef().GetAuthorityCode(null);
+        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, epsg);
+    }
+
+    /**
+     * 鑾峰彇Dataset鐨勬渶澶х偣
+     */
+    private Geometry getMaxPoint(Dataset ds) {
+        double[] transform = new double[6];
+        ds.GetGeoTransform(transform);
+
+        String epsg = ds.GetSpatialRef().GetAuthorityCode(null);
+        double xMax = transform[0] + (ds.getRasterXSize() * transform[1]);
+        double yMax = transform[3];
+
+        Geometry point = new Geometry(ogr.wkbPoint);
+        point.AddPoint(xMax, yMax, 0);
+
+        return transform(ds, point, epsg);
+    }
+
+    /**
+     * 鍧愭爣杞崲
+     */
+    private Geometry transform(Dataset ds, Geometry point, String epsg) {
+        this.initSpatialReference();
+
+        if (StringHelper.isEmpty(epsg)) {
+            point.AssignSpatialReference(sr104903);
+            return point;
+        }
+        if (String.valueOf(StaticData.I4326).equals(epsg)) {
+            point.AssignSpatialReference(sr4326);
+            return point;
+        }
+        if (String.valueOf(StaticData.I4490).equals(epsg)) {
+            point.AssignSpatialReference(sr4490);
+            return point;
+        }
+
+        point.AssignSpatialReference(ds.GetSpatialRef());
+        if (ds.GetSpatialRef().GetName().contains(StaticData.CGCS2000)) {
+            point.TransformTo(sr4490);
+        } else {
+            point.TransformTo(sr4326);
+        }
+
+        return point;
+    }
 }

--
Gitblit v1.9.3