From 6cb2134a53422f471f4f9b77c34d67e1fb4d31db Mon Sep 17 00:00:00 2001 From: 张洋洋 <10611411+yang-yang-z@user.noreply.gitee.com> Date: 星期四, 09 一月 2025 10:16:56 +0800 Subject: [PATCH] [add]h5读取改造 --- src/main/java/com/se/simu/controller/FilesUploadController.java | 28 +++++++ src/main/java/com/se/simu/utils/FileUtil.java | 37 +++++++++ src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java | 22 ++++- src/main/resources/cityjson.json | 132 +++++++++++++++++++++++++++++++++ src/main/java/com/se/simu/utils/ZipUtils.java | 4 src/main/java/com/se/simu/controller/SimuController.java | 11 ++ 6 files changed, 224 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/se/simu/controller/FilesUploadController.java b/src/main/java/com/se/simu/controller/FilesUploadController.java index 3174ec0..be4aa87 100644 --- a/src/main/java/com/se/simu/controller/FilesUploadController.java +++ b/src/main/java/com/se/simu/controller/FilesUploadController.java @@ -1,6 +1,8 @@ package com.se.simu.controller; import com.se.simu.domain.vo.R; +import com.se.simu.utils.FileUtil; +import com.se.simu.utils.ZipUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; @@ -17,6 +19,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.List; @@ -69,6 +72,31 @@ } } + @ApiOperation("1-涓婁紶鍗曚釜shp鏂囦欢") + @PostMapping("/uploadShp") + public R<Object> uploadShp(@RequestParam("file") MultipartFile file) throws IOException { + if (file.isEmpty()) { + return success("鏂囦欢涓嶈兘涓虹┖"); + } + // 鑾峰彇褰撳墠骞存湀鏃� + String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); + String targetDir = Paths.get(uploadedFolder, date).toString(); + log.info("鐩爣鐩綍: {}", targetDir); + createDirectoriesIfNotExists(targetDir); + try { + // 鏂囦欢鍦板潃鍏ㄧО + Path filePath = Paths.get(targetDir, file.getOriginalFilename()); + // 鏂囦欢鍚� + file.transferTo(filePath); + ZipUtils.unzip(targetDir + "\\" + file.getOriginalFilename(),targetDir); + File zipfile=new File(targetDir + "\\" + file.getOriginalFilename()); + zipfile.delete(); + return success(FileUtil.getShpPath(targetDir), "鏂囦欢涓婁紶鎴愬姛"); + } catch (IOException e) { + log.error("鏂囦欢涓婁紶澶辫触", e); + return fail("鏂囦欢涓婁紶澶辫触"); + } + } @ApiOperation("1-涓婁紶鍗曚釜鏂囦欢") @PostMapping("/uploads") diff --git a/src/main/java/com/se/simu/controller/SimuController.java b/src/main/java/com/se/simu/controller/SimuController.java index b25fbe0..339d7b4 100644 --- a/src/main/java/com/se/simu/controller/SimuController.java +++ b/src/main/java/com/se/simu/controller/SimuController.java @@ -253,7 +253,7 @@ return fail("瑙f瀽鑼冨洿鏂囦欢澶辫触锛侀�夋嫨鑼冨洿涓嶈兘涓虹┖锛岃閲嶆柊閫夋嫨鏂囦欢锛�", false); } } - + JSONArray jsonArray = new JSONArray(); String stationFile = vo.getStationFile(); log.info("绔欑偣鏂囦欢shp鍦板潃锛歿}", stationFile); try { @@ -262,7 +262,7 @@ return fail("绔欑偣鏂囦欢shp鍦板潃涓嶈兘涓虹┖", false); } // 1 璇诲彇shp鏂囦欢锛岃幏鍙栫珯鐐瑰潗鏍囧�� - JSONArray jsonArray = ShpToolUtils.readShpGetLocal(stationFile); + jsonArray = ShpToolUtils.readShpGetLocal(stationFile); System.out.println("jsonArray = " + jsonArray); } catch (Exception e) { log.error("瑙f瀽绔欑偣鏂囦欢shp澶辫触"); @@ -270,6 +270,7 @@ String stationRainFile = vo.getStationRainFile(); log.info("绔欑偣闆ㄩ噺CSV鏂囦欢鍦板潃锛歿}", floodFile); + JSONArray array = new JSONArray(); try { // 鍒ゆ柇鍦板潃涓嶄负绌� if (StringUtils.isEmpty(stationRainFile)) { @@ -278,7 +279,7 @@ // 鍒涘缓琛ㄥ悕 鏃堕棿鎴� String tableName = "station_rain_" + System.currentTimeMillis(); // 1 璇诲彇CSV 鏂囦欢 - CsvToSQLiteUtils.readCsvSaveLocal(stationRainFile, tableName); + array = CsvToSQLiteUtils.readCsvSaveLocal(stationRainFile, tableName); // 鑾峰彇浠跨湡鏃堕棿 duration Integer duration = CsvToSQLiteUtils.getDuration(tableName); log.info("浠跨湡鏃堕棿 duration = {}", duration); @@ -328,6 +329,10 @@ return fail("闃叉睕鑼冨洿涓嶈兘涓虹┖", false); } } + //todo 缁勮cityjson + JSONObject jsonObject = new JSONObject(); + jsonObject.put("shp", jsonArray); + jsonObject.put("csv", array); // 寮�濮嬫ā鎷熻绠� boolean flag = simuFilesService.createByfiles(vo); return success(flag, flag ? "鎴愬姛" : "澶辫触"); diff --git a/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java b/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java index 165bb0d..d4df8b0 100644 --- a/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java +++ b/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java @@ -1,5 +1,8 @@ package com.se.simu.utils; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVRecord; @@ -156,8 +159,9 @@ * @param stationRainFile Station Rain 鏂囦欢 * @param tableName 琛ㄥ悕绉� */ - public static void readCsvSaveLocal(String stationRainFile, String tableName) { + public static JSONArray readCsvSaveLocal(String stationRainFile, String tableName) { // 1. 鍒涘缓SQLite鏁版嵁搴撹繛鎺� + JSONArray array=null; try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) { if (conn != null) { // 2. 鍒涘缓琛紙濡傛灉涓嶅瓨鍦級 @@ -170,7 +174,7 @@ // 4. 寮�濮嬩簨鍔� conn.setAutoCommit(false); // 5. 璇诲彇CSV鏂囦欢骞舵彃鍏ユ暟鎹� - readCsvAndInsertDatas(stationRainFile, conn, tableName); + array = readCsvAndInsertDatas(stationRainFile, conn, tableName); // 6. 鎻愪氦浜嬪姟 conn.commit(); System.out.println("鏁版嵁鎴愬姛鎻掑叆鍒癝QLite鏁版嵁搴擄紒"); @@ -178,11 +182,13 @@ } catch (SQLException e) { System.err.println("SQLite杩炴帴澶辫触: " + e.getMessage()); } + return array; } - private static void readCsvAndInsertDatas(String csvFilePath, Connection conn, String tableName) { + private static JSONArray readCsvAndInsertDatas(String csvFilePath, Connection conn, String tableName) { // 浣跨敤 Apache Commons CSV 璇诲彇CSV鏂囦欢 + JSONArray array = new JSONArray(); try (Reader reader = new InputStreamReader(Files.newInputStream(Paths.get(csvFilePath)), "GBK")) { Iterable<CSVRecord> records = CSVFormat.DEFAULT .withHeader("闆ㄩ噺绔�", "闄嶉洦閲�", "缁忓害", "绾害", "datatime") @@ -190,7 +196,6 @@ .parse(reader); String insertDataSql = "INSERT INTO " + tableName + " (station_name, rainfall, longitude, latitude, datetime) VALUES (?, ?, ?, ?, ?);"; - try (PreparedStatement pstmt = conn.prepareStatement(insertDataSql)) { // 鎵归噺澶у皬 int batchSize = 1000; @@ -210,7 +215,13 @@ pstmt.setDouble(3, longitude); pstmt.setDouble(4, latitude); pstmt.setString(5, datetime); - + JSONObject jsonObject = new JSONObject(); + jsonObject.put("stationName", stationName); + jsonObject.put("rainfall", rainfall); + jsonObject.put("longitude", longitude); + jsonObject.put("latitude", latitude); + jsonObject.put("datetime", datetime); + array.add(jsonObject); // 娣诲姞鍒版壒澶勭悊 pstmt.addBatch(); count++; @@ -228,6 +239,7 @@ } catch (IOException e) { System.err.println("璇诲彇CSV鎴栨彃鍏ユ暟鎹椂鍑洪敊: " + e.getMessage()); } + return array; } /** diff --git a/src/main/java/com/se/simu/utils/FileUtil.java b/src/main/java/com/se/simu/utils/FileUtil.java new file mode 100644 index 0000000..3e93833 --- /dev/null +++ b/src/main/java/com/se/simu/utils/FileUtil.java @@ -0,0 +1,37 @@ +package com.se.simu.utils; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * + */ +public class FileUtil { + public static String getShpPath(String dirPath) { + File root = new File(dirPath); + List<String> filePath=new ArrayList<>(); + traverse(root,filePath); + for (String path:filePath + ) { + if (path.endsWith(".shp")){ + return path; + } + } + return null; + } + + public static void traverse(File dir,List<String> filePath) { + File[] files = dir.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isDirectory()) { + traverse(file, filePath); + } else { + filePath.add(file.getAbsolutePath()); + } + } + } + } + +} diff --git a/src/main/java/com/se/simu/utils/ZipUtils.java b/src/main/java/com/se/simu/utils/ZipUtils.java index e855e73..5045b6c 100644 --- a/src/main/java/com/se/simu/utils/ZipUtils.java +++ b/src/main/java/com/se/simu/utils/ZipUtils.java @@ -10,7 +10,6 @@ import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; /** * zip @@ -55,8 +54,9 @@ // 杩欓噷涔熷彲浠ラ�夋嫨鎵撳嵃鐩綍璺緞 } } - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); + System.out.println("GBK缂栫爜"); try { File file = ZipUtil.unzip(zipFilePath, destDir, CharsetUtil.CHARSET_GBK); File[] file1 = file.listFiles(); diff --git a/src/main/resources/cityjson.json b/src/main/resources/cityjson.json new file mode 100644 index 0000000..7b2915e --- /dev/null +++ b/src/main/resources/cityjson.json @@ -0,0 +1,132 @@ +{ + "type": "CityJSON_Extension", + "name": "PipeLine", + "uri": "http://www.smartearth.cn/extension.schema. PipeLine.json", + "version": "1.0.0", + "description": "Extension for PipeLine model", + "extraCityObjects": { + "+PipeLine": { + "allOf": [ + { + "$ref": "../cityobjects.schema.json#/_AbstractCityObject" + }, + { + "properties": { + "type": { + "enum": [ + "+PipeLine" + ] + }, + "topLevel": { + "type": "boolean" + }, + "attributes": { + "type": "object", + "properties": { + "function": { + "type": "string" + }, + "bs": { + "type": "object", + "description": "鏍囪瘑", + "properties": { + "qqwybs": { + "type": "string", + "description": "鍏ㄧ悆鍞竴缂栫爜" + }, + "hydm": { + "type": "string", + "description": "琛屼笟浠g爜" + }, + "fldm": { + "type": "string", + "description": "鍒嗙被浠g爜" + }, + "mc": { + "type": "string", + "description": "鍚嶇О" + } + } + }, + "zt": { + "type": "object", + "description": "鐘舵��", + "properties": { + "ztgb": { + "type": "string", + "description": "鐘舵�佹敼鍙�" + } + } + }, + "sk": { + "type": "object", + "description": "鏃剁┖", + "properties": { + "cssj": { + "type": "string", + "description": "浜х敓鏃堕棿" + }, + "gxsj": { + "type": "string", + "description": "鏇存柊鏃堕棿" + }, + "xwsj": { + "type": "string", + "description": "娑堜骸鏃堕棿" + } + } + }, + "ssxzq": { + "type": "string", + "description": "鎵�灞炶鏀垮尯鍩�" + } + } + }, + "children": { + "type": "array", + "items": { + "type": "string" + } + }, + "geometry": { + "type": "array", + "items": { + "oneOf": [ + { + "$ref": "geomprimitives.schema.json#/MultiPoint" + }, + { + "$ref": "geomprimitives.schema.json#/MultiLineString" + }, + { + "$ref": "geomprimitives.schema.json#/MultiSurface" + }, + { + "$ref": "geomprimitives.schema.json#/CompositeSurface" + }, + { + "$ref": "geomprimitives.schema.json#/Solid" + }, + { + "$ref": "geomprimitives.schema.json#/CompositeSolid" + }, + { + "$ref": "geomprimitives.schema.json#/MultiSolid" + }, + { + "$ref": "geomtemplates.schema.json#/GeometryInstance" + } + ] + } + } + }, + "required": [ + "type", + "topLevel", + "geometry" + ] + } + ] + } + } +} \ No newline at end of file -- Gitblit v1.9.3