From 4e7ae7e903a07389607cf47ad8f2bbd5e8ccd8d6 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 30 八月 2023 10:18:50 +0800
Subject: [PATCH] 元数据查询接口添加传感器类型、元数据类型、开始日期、结束日期参数

---
 src/main/java/com/moon/server/service/data/RasterService.java |  181 ++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 177 insertions(+), 4 deletions(-)

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..21f89d2 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,16 @@
 package com.moon.server.service.data;
 
-import com.moon.server.entity.data.MetaEntity;
+import com.moon.server.entity.all.StaticData;
+import com.moon.server.entity.data.MetaFileEntity;
+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.gdalconst.gdalconst;
+import org.gdal.ogr.Geometry;
+import org.gdal.ogr.ogr;
 import org.gdal.osr.SpatialReference;
 import org.springframework.stereotype.Service;
 
@@ -17,12 +23,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(MetaFileEntity mf, String file) {
         Dataset ds = null;
         try {
             File f = new File(file);
@@ -30,12 +57,54 @@
                 return;
             }
 
-            ds = gdal.Open(file, 0);
-            if (null == ds || ds.getRasterCount() == 0) {
+            ds = gdal.Open(file, gdalconst.GA_ReadOnly);
+            if (null == ds || ds.getRasterCount() == 0 || null == ds.GetSpatialRef()) {
                 return;
             }
 
             SpatialReference sr = ds.GetSpatialRef();
+            // 鍧愭爣绯荤粺
+            mf.setCoorSys(sr.GetName());
+            if (StaticData.MOON200.equals(mf.getCoorSys())) {
+                // EPSG缂栫爜
+                mf.setEpsg("EPSG:" + StaticData.I104903);
+            } else {
+                // EPSG缂栫爜锛歅ROJCS銆丟EOGCS銆丟EOGCS|UNIT 鎴� NULL
+                String code = sr.GetAuthorityCode(null);
+                mf.setEpsg(StringHelper.isEmpty(code) ? null : "EPSG:" + code);
+            }
+
+            // 琛屽垪鏁�
+            mf.setGridsize(String.format("%d,%d", ds.getRasterXSize(), ds.getRasterYSize()));
+            // 娉㈡鏁�
+            mf.setBands(String.valueOf(ds.getRasterCount()));
+            // 鏁版嵁绫诲瀷
+            int dataType = ds.GetRasterBand(1).GetRasterDataType();
+            mf.setBandType(getDataType(dataType));
+            // 鏁版嵁棰滆壊琛�
+            ColorTable colorTable = ds.GetRasterBand(1).GetRasterColorTable();
+            mf.setCt(null == colorTable ? null : colorTable.toString());
+            // 楂樼▼鍩哄噯
+            mf.sethDatum(null);
+
+            // 鍒嗚鲸鐜�
+            double[] tr = new double[6];
+            ds.GetGeoTransform(tr);
+            mf.setResolution(String.format("%f,%f", tr[1], tr[5]));
+
+            if (tr[StaticData.I0] == 0.0 && tr[StaticData.I1] == 1.0 && tr[StaticData.I2] == 0.0 && tr[StaticData.I3] == 0.0 && tr[StaticData.I4] == 0.0 && tr[StaticData.I5] == 1.0) {
+                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);
+            mf.setGeom(geom);
         } catch (Exception ex) {
             log.error(ex.getMessage(), ex);
         } finally {
@@ -44,4 +113,108 @@
             }
         }
     }
+
+    /**
+     * 鑾峰彇鏁版嵁绫诲瀷
+     */
+    private String getDataType(int dataType) {
+        if (dataType == gdalconst.GDT_Byte) {
+            return "GDT_Byte";
+        }
+        if (dataType == gdalconst.GDT_UInt16) {
+            return "GDT_UInt16";
+        }
+        if (dataType == gdalconst.GDT_Int16) {
+            return "GDT_Int16";
+        }
+        if (dataType == gdalconst.GDT_UInt32) {
+            return "GDT_UInt32";
+        }
+        if (dataType == gdalconst.GDT_Int32) {
+            return "GDT_Int32";
+        }
+        if (dataType == gdalconst.GDT_Float32) {
+            return "GDT_Float32";
+        }
+        if (dataType == gdalconst.GDT_Float64) {
+            return "GDT_Float64";
+        }
+        if (dataType == gdalconst.GDT_CInt16) {
+            return "GDT_CInt16";
+        }
+        if (dataType == gdalconst.GDT_CInt32) {
+            return "GDT_CInt32";
+        }
+        if (dataType == gdalconst.GDT_CFloat32) {
+            return "GDT_CFloat32";
+        }
+        if (dataType == gdalconst.GDT_CFloat64) {
+            return "GDT_CFloat64";
+        }
+
+        return null;
+    }
+
+    /**
+     * 鑾峰彇Dataset鐨勬渶灏忕偣
+     */
+    private 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);
+    }
+
+    /**
+     * 鑾峰彇Dataset鐨勬渶澶х偣
+     */
+    private 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.getRasterXSize() * transform[1]);
+        double yMax = transform[3];
+
+        Geometry point = new Geometry(ogr.wkbPoint);
+        point.AddPoint(xMax, yMax, 0);
+
+        return transform(ds, point);
+    }
+
+    /**
+     * 鍧愭爣杞崲
+     */
+    private Geometry transform(Dataset ds, Geometry point) {
+        this.initSpatialReference();
+
+        point.AssignSpatialReference(ds.GetSpatialRef());
+        if (ds.GetSpatialRef().IsGeographic() > 0) {
+            return point;
+        }
+
+        String srsName = ds.GetSpatialRef().GetName();
+        if (srsName.contains(StaticData.CGCS2000)) {
+            point.TransformTo(sr4490);
+        } else {
+            point.TransformTo(sr4326);
+        }
+        point.SwapXY();
+
+        return point;
+    }
 }

--
Gitblit v1.9.3