From 096abd16d07228aba3189302a14b24f56bb63abd Mon Sep 17 00:00:00 2001
From: 张洋洋 <10611411+yang-yang-z@user.noreply.gitee.com>
Date: 星期五, 10 一月 2025 14:24:02 +0800
Subject: [PATCH] [add]h5读取改造

---
 src/main/resources/win32-x86-64/blosc.dll                |    0 
 src/main/java/com/se/simu/utils/ZarrUtils.java           |  124 ++++++++++++++++----
 src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java    |   77 ++++++++++++
 src/main/java/com/se/simu/domain/vo/StationRainVo.java   |   65 ++++++++++
 pom.xml                                                  |   11 +
 src/main/java/com/se/simu/controller/SimuController.java |   24 +++-
 6 files changed, 265 insertions(+), 36 deletions(-)

diff --git a/pom.xml b/pom.xml
index 7cffb53..abb5d58 100644
--- a/pom.xml
+++ b/pom.xml
@@ -275,6 +275,17 @@
         </repository>
     </repositories>
     <build>
+        <resources>
+            <resource>
+                <directory>src/main/resources</directory>
+                <includes>
+                    <include>win32-x86-64/blosc.dll</include>
+                    <include>**/*.yml</include>
+                    <include>**/*.xml</include>
+                </includes>
+                <filtering>false</filtering>
+            </resource>
+        </resources>
         <finalName>SimuServer</finalName>
         <plugins>
             <plugin>
diff --git a/src/main/java/com/se/simu/controller/SimuController.java b/src/main/java/com/se/simu/controller/SimuController.java
index 339d7b4..f8d151d 100644
--- a/src/main/java/com/se/simu/controller/SimuController.java
+++ b/src/main/java/com/se/simu/controller/SimuController.java
@@ -8,13 +8,11 @@
 import com.se.simu.domain.dto.GeLayer;
 import com.se.simu.domain.po.DataPo;
 import com.se.simu.domain.po.SimuPo;
-import com.se.simu.domain.vo.CreateFilesSimuVo;
-import com.se.simu.domain.vo.CreateSimuVo;
-import com.se.simu.domain.vo.R;
-import com.se.simu.domain.vo.SimuVo;
+import com.se.simu.domain.vo.*;
 import com.se.simu.service.*;
 import com.se.simu.utils.CsvToSQLiteUtils;
 import com.se.simu.utils.ShpToolUtils;
+import com.se.simu.utils.ZarrUtils;
 import io.swagger.annotations.*;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -271,13 +269,13 @@
         String stationRainFile = vo.getStationRainFile();
         log.info("绔欑偣闆ㄩ噺CSV鏂囦欢鍦板潃锛歿}", floodFile);
         JSONArray array = new JSONArray();
+        // 鍒涘缓琛ㄥ悕 鏃堕棿鎴�
+        String tableName = "station_rain_" + System.currentTimeMillis();
         try {
             // 鍒ゆ柇鍦板潃涓嶄负绌�
             if (StringUtils.isEmpty(stationRainFile)) {
                 return fail("绔欑偣闆ㄩ噺CSV鏂囦欢鍦板潃涓嶈兘涓虹┖", false);
             }
-            // 鍒涘缓琛ㄥ悕 鏃堕棿鎴�
-            String tableName = "station_rain_" + System.currentTimeMillis();
             // 1 璇诲彇CSV 鏂囦欢
             array = CsvToSQLiteUtils.readCsvSaveLocal(stationRainFile, tableName);
             // 鑾峰彇浠跨湡鏃堕棿 duration
@@ -341,5 +339,19 @@
         }
     }
 
+    @ApiOperation(value = "tarr鏂囦欢缁勮")
+    @GetMapping(value = "/testZarr", produces = "application/json; charset=UTF-8")
+    public R<Boolean> testZarr(@RequestParam("name") String tableName) throws IOException {
+        List<String> list = CsvToSQLiteUtils.getNameList(tableName);
+        String basePath="D:\\鍩庡競鍐呮稘\\sem\\闆ㄩ噺绔欑偣鏁版嵁\\闄嶉洦閲弞arr\\";
+        for (String src : list
+        ) {
+            List<StationRainVo> stationRainVos = CsvToSQLiteUtils.getList(tableName, src);
+            ZarrUtils.saveZarrRainfall(basePath+src,stationRainVos);
+            ZarrUtils.saveZarrTime(basePath+src,stationRainVos);
+            System.out.println(src+"鐨剒arr鏁版嵁鐢熸垚=====================");
+        }
+        return success(true);
+    }
 
 }
diff --git a/src/main/java/com/se/simu/domain/vo/StationRainVo.java b/src/main/java/com/se/simu/domain/vo/StationRainVo.java
new file mode 100644
index 0000000..f8d1716
--- /dev/null
+++ b/src/main/java/com/se/simu/domain/vo/StationRainVo.java
@@ -0,0 +1,65 @@
+package com.se.simu.domain.vo;
+
+
+public class StationRainVo {
+
+    private Long id;
+
+    private String stationName;
+
+    private Double rainfall;
+
+    private Double longitude;
+
+    private Double latitude;
+
+    private String datetime;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getStationName() {
+        return stationName;
+    }
+
+    public void setStationName(String stationName) {
+        this.stationName = stationName;
+    }
+
+    public Double getRainfall() {
+        return rainfall;
+    }
+
+    public void setRainfall(Double rainfall) {
+        this.rainfall = rainfall;
+    }
+
+    public Double getLongitude() {
+        return longitude;
+    }
+
+    public void setLongitude(Double longitude) {
+        this.longitude = longitude;
+    }
+
+    public Double getLatitude() {
+        return latitude;
+    }
+
+    public void setLatitude(Double latitude) {
+        this.latitude = latitude;
+    }
+
+    public String getDatetime() {
+        return datetime;
+    }
+
+    public void setDatetime(String datetime) {
+        this.datetime = datetime;
+    }
+}
diff --git a/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java b/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java
index d4df8b0..94c8c5e 100644
--- a/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java
+++ b/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java
@@ -3,6 +3,7 @@
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.se.simu.domain.vo.StationRainVo;
 import org.apache.commons.csv.CSVFormat;
 import org.apache.commons.csv.CSVRecord;
 
@@ -12,6 +13,10 @@
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.sql.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
 
 /**
  * CSV 鍒� SQLite 瀹炵敤绋嬪簭
@@ -161,7 +166,7 @@
      */
     public static JSONArray readCsvSaveLocal(String stationRainFile, String tableName) {
         // 1. 鍒涘缓SQLite鏁版嵁搴撹繛鎺�
-        JSONArray array=null;
+        JSONArray array = null;
         try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) {
             if (conn != null) {
                 // 2. 鍒涘缓琛紙濡傛灉涓嶅瓨鍦級
@@ -268,7 +273,6 @@
         } catch (SQLException e) {
             System.err.println("SQLite杩炴帴澶辫触: " + e.getMessage());
         }
-
         return null;
     }
 
@@ -299,4 +303,73 @@
         return 1440;
     }
 
+    /**
+     * 鍒嗙粍鏌ヨ
+     *
+     * @param tableName 琛ㄥ悕绉�
+     * @return {@link Double}
+     */
+    public static List<String> getNameList(String tableName) {
+        // 1. 鍒涘缓SQLite鏁版嵁搴撹繛鎺�
+        try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) {
+            if (conn != null) {
+                // 2. 鎵цSQL鏌ヨ
+                String queryDataSql = "SELECT station_name FROM " + tableName + " GROUP BY station_name";
+                // 3. 澶勭悊鏌ヨ缁撴灉
+                try (PreparedStatement pstmt = conn.prepareStatement(queryDataSql)) {
+                    ResultSet rs = pstmt.executeQuery();
+                    List<String> list = new ArrayList<>();
+                    while (rs.next()) {
+                        // 鑾峰彇鎬诲拰
+                        list.add(rs.getString("station_name"));
+                    }
+                    return list;
+                } catch (SQLException e) {
+                    System.err.println("鏌ヨ鏁版嵁鏃跺嚭閿�: " + e.getMessage());
+                }
+            }
+        } catch (SQLException e) {
+            System.err.println("SQLite杩炴帴澶辫触: " + e.getMessage());
+        }
+        return null;
+    }
+
+    /**
+     * 鍒嗙粍鏌ヨ
+     *
+     * @param tableName 琛ㄥ悕绉�
+     * @return {@link Double}
+     */
+    public static List<StationRainVo> getList(String tableName, String name) {
+        // 1. 鍒涘缓SQLite鏁版嵁搴撹繛鎺�
+        try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) {
+            if (conn != null) {
+                // 2. 鎵цSQL鏌ヨ
+                String queryDataSql = "SELECT * FROM " + tableName + " WHERE station_name='" + name + "' ORDER BY datetime asc";
+                // 3. 澶勭悊鏌ヨ缁撴灉
+                try (PreparedStatement pstmt = conn.prepareStatement(queryDataSql)) {
+                    ResultSet rs = pstmt.executeQuery();
+                    List<StationRainVo> list = new ArrayList<>();
+                    StationRainVo vo;
+                    while (rs.next()) {
+                        vo = new StationRainVo();
+                        vo.setId(rs.getLong("id"));
+                        vo.setStationName(rs.getString("station_name"));
+                        vo.setRainfall(rs.getDouble("rainfall"));
+                        vo.setLatitude(rs.getDouble("latitude"));
+                        vo.setLongitude(rs.getDouble("longitude"));
+                        vo.setDatetime(rs.getString("datetime"));
+                        list.add(vo);
+                    }
+                    return list;
+                } catch (SQLException e) {
+                    System.err.println("鏌ヨ鏁版嵁鏃跺嚭閿�: " + e.getMessage());
+                }
+            }
+        } catch (SQLException e) {
+            System.err.println("SQLite杩炴帴澶辫触: " + e.getMessage());
+        }
+        return null;
+    }
+
 }
diff --git a/src/main/java/com/se/simu/utils/ZarrUtils.java b/src/main/java/com/se/simu/utils/ZarrUtils.java
index 736b32c..598ef34 100644
--- a/src/main/java/com/se/simu/utils/ZarrUtils.java
+++ b/src/main/java/com/se/simu/utils/ZarrUtils.java
@@ -4,63 +4,131 @@
 import com.bc.zarr.DataType;
 import com.bc.zarr.ZarrArray;
 import com.bc.zarr.ZarrGroup;
+import com.se.simu.domain.vo.StationRainVo;
 import org.apache.commons.csv.CSVFormat;
 import org.apache.commons.csv.CSVParser;
 import org.apache.commons.csv.CSVRecord;
+
 import java.io.FileReader;
 import java.io.IOException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
 
 public class ZarrUtils {
-    public static void main(String[] args) throws Exception{
-        // 杈撳叆 CSV 鏂囦欢鐨勮矾寰�
-        String csvFilePath = "D:\\鍩庡競鍐呮稘\\sem\\闆ㄩ噺绔欑偣鏁版嵁\\闆ㄩ噺绔欏寘鍚潗鏍�.csv";
-        // 杈撳嚭 Zarr 鏂囦欢鐨勮矾寰�
-        String zarrFilePath = "D:\\鍩庡競鍐呮稘\\sem\\闆ㄩ噺绔欑偣鏁版嵁\\闆ㄩ噺绔欏寘鍚潗鏍�.zarr";
-        try (CSVParser csvParser = new CSVParser(new FileReader(csvFilePath), CSVFormat.DEFAULT.withFirstRecordAsHeader())) {
+//    public static void main(String[] args) throws Exception {
+//        // 杈撳叆 CSV 鏂囦欢鐨勮矾寰�
+//        String csvFilePath = "D:\\鍩庡競鍐呮稘\\sem\\闆ㄩ噺绔欑偣鏁版嵁\\闆ㄩ噺绔欏寘鍚潗鏍�.csv";
+//        // 杈撳嚭 Zarr 鏂囦欢鐨勮矾寰�
+//        String zarrFilePath = "D:\\鍩庡競鍐呮稘\\sem\\闆ㄩ噺绔欑偣鏁版嵁\\澶у簽鏉�";
+//        try (CSVParser csvParser = new CSVParser(new FileReader(csvFilePath), CSVFormat.DEFAULT.withFirstRecordAsHeader())) {
+//            // 鑾峰彇 CSV 鍒楁暟
+//            int numColumns = csvParser.getHeaderMap().size();
+//            // 璁$畻 CSV 琛屾暟
+//            int numRows = 0;
+//            for (CSVRecord record : csvParser) {
+//                numRows++;
+//            }
+//            // 閲嶆柊鍒濆鍖� CSV 瑙f瀽鍣紝鍥犱负涔嬪墠鐨勯亶鍘嗗凡缁忚�楀敖浜嗚凯浠e櫒
+//            csvParser.close();
+//            CSVParser csvRecords = new CSVParser(new FileReader(csvFilePath), CSVFormat.DEFAULT.withFirstRecordAsHeader());
+//            // 鍒涘缓 Zarr 缁�
+//            Path zarrPath = Paths.get(zarrFilePath);
+//            ZarrGroup zarrGroup = ZarrGroup.create(zarrPath);
+//            // 瀹氫箟 Zarr 鏁扮粍鐨勭淮搴�
+//            int[] shape = {numRows, numColumns};
+//            // 瀹氫箟鍒嗗潡澶у皬
+//            int[] chunks = {1440};
+//            // 鍒涘缓 Zarr 鏁扮粍
+//            ArrayParams params = new ArrayParams();
+//            params.shape(chunks);
+//            params.chunks(chunks);
+//            params.dataType(DataType.f4);
+//            ZarrArray zarrArray = zarrGroup.createArray("rainfall", params);
+//            int rowIndex = 0;
+//            for (CSVRecord record : csvRecords) {
+//                double value = Double.parseDouble(record.get(1));
+//                zarrArray.write(value);
+//                rowIndex++;
+//            }
+//            // 鍏抽棴 Zarr 缁�
+//            //zarrGroup.close();
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//        }
+//    }
+
+    public static void saveZarrRainfall(String path, List<StationRainVo> stationRainVos){
+        try {
             // 鑾峰彇 CSV 鍒楁暟
-            int numColumns = csvParser.getHeaderMap().size();
+            int numColumns = 5;
             // 璁$畻 CSV 琛屾暟
-            int numRows = 0;
-            for (CSVRecord record : csvParser) {
-                numRows++;
-            }
-            // 閲嶆柊鍒濆鍖� CSV 瑙f瀽鍣紝鍥犱负涔嬪墠鐨勯亶鍘嗗凡缁忚�楀敖浜嗚凯浠e櫒
-            csvParser.close();
-            CSVParser  csvRecords = new CSVParser(new FileReader(csvFilePath), CSVFormat.DEFAULT.withFirstRecordAsHeader());
+            int numRows = stationRainVos.size();
             // 鍒涘缓 Zarr 缁�
-            Path zarrPath = Paths.get(zarrFilePath);
+            Path zarrPath = Paths.get(path);
             ZarrGroup zarrGroup = ZarrGroup.create(zarrPath);
             // 瀹氫箟 Zarr 鏁扮粍鐨勭淮搴�
             int[] shape = {numRows, numColumns};
             // 瀹氫箟鍒嗗潡澶у皬
             int[] chunks = {1440};
             // 鍒涘缓 Zarr 鏁扮粍
-            ArrayParams params=new ArrayParams();
+            ArrayParams params = new ArrayParams();
             params.shape(chunks);
             params.chunks(chunks);
             params.dataType(DataType.f4);
-            ZarrArray zarrArray = zarrGroup.createArray("rainfall",params);
+            ZarrArray zarrArray = zarrGroup.createArray("rainfall", params);
             int rowIndex = 0;
-            for (CSVRecord record : csvRecords) {
-                int[] x={rowIndex};
-//                for (int colIndex = 0; colIndex < numColumns; colIndex++) {
-//                    //double value = Double.parseDouble(record.get(colIndex));
-//                    String value = record.get(colIndex);
-//                    int[] y={colIndex};
-//                    //zarrArray.write(value);
-//                    zarrArray.write(value,x,y);
-//                }
-                double value = Double.parseDouble(record.get(1));
+            for (StationRainVo record : stationRainVos) {
+                double value = record.getRainfall();
                 zarrArray.write(value);
                 rowIndex++;
             }
             // 鍏抽棴 Zarr 缁�
             //zarrGroup.close();
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }
 
+    public static void saveZarrTime(String path, List<StationRainVo> stationRainVos){
+        try {
+            // 鑾峰彇 CSV 鍒楁暟
+            int numColumns = 5;
+            // 璁$畻 CSV 琛屾暟
+            int numRows = stationRainVos.size();
+            // 鍒涘缓 Zarr 缁�
+            Path zarrPath = Paths.get(path);
+            ZarrGroup zarrGroup = ZarrGroup.create(zarrPath);
+            // 瀹氫箟 Zarr 鏁扮粍鐨勭淮搴�
+            int[] shape = {numRows, numColumns};
+            // 瀹氫箟鍒嗗潡澶у皬
+            int[] chunks = {1440};
+            // 鍒涘缓 Zarr 鏁扮粍
+            ArrayParams params = new ArrayParams();
+            params.shape(chunks);
+            params.chunks(chunks);
+            params.dataType(DataType.i4);
+            ZarrArray zarrArray = zarrGroup.createArray("time", params);
+            int rowIndex = 0;
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
+            for (StationRainVo record : stationRainVos) {
+                long value = sdf.parse(record.getDatetime()).getTime();
+                zarrArray.write(value);
+                rowIndex++;
+            }
+            // 鍏抽棴 Zarr 缁�
+            //zarrGroup.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static void main(String[] args) throws Exception{
+        System.out.println("2023/7/31 6:30");
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
+        Date date = sdf.parse("2023/7/31 6:30");
+        System.out.println(date.getTime());
+    }
 }
diff --git a/src/main/resources/win32-x86-64/blosc.dll b/src/main/resources/win32-x86-64/blosc.dll
new file mode 100644
index 0000000..2093a88
--- /dev/null
+++ b/src/main/resources/win32-x86-64/blosc.dll
Binary files differ

--
Gitblit v1.9.3