From ab6d5c7ee8d247ba4c6f36b9e31583b0d2c6ed04 Mon Sep 17 00:00:00 2001 From: wuww <252740454@qq.com> Date: 星期一, 21 四月 2025 16:31:17 +0800 Subject: [PATCH] 修改单元测试方法 --- src/main/java/com/se/nsl/controller/TestController.java | 75 +++++++++++++++ src/main/java/com/se/nsl/service/TestService.java | 4 src/main/java/com/se/nsl/helper/GdalHelper.java | 4 src/test/java/com/se/nsl/AppTest.java | 167 ++++++++++++++++---------------- pom.xml | 3 5 files changed, 164 insertions(+), 89 deletions(-) diff --git a/pom.xml b/pom.xml index 97b2166..cd0b5b2 100644 --- a/pom.xml +++ b/pom.xml @@ -120,7 +120,8 @@ <artifactId>gdal</artifactId> <version>3.5.0</version> <!--<version>3.10.0</version> - <scope>system</scope> + <version>3.10.1</version>--> + <!--<scope>system</scope> <systemPath>${project.basedir}/libs/gdal.jar</systemPath>--> </dependency> <!--rainfall--> diff --git a/src/main/java/com/se/nsl/controller/TestController.java b/src/main/java/com/se/nsl/controller/TestController.java index da021bc..5d18af0 100644 --- a/src/main/java/com/se/nsl/controller/TestController.java +++ b/src/main/java/com/se/nsl/controller/TestController.java @@ -20,7 +20,13 @@ import javax.annotation.Resource; import java.io.File; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; import java.util.List; @Api(tags = "Test") @@ -235,4 +241,73 @@ return success(list.size() > 0 ? regionMapper.inserts(list) : 0); } + + @ApiOperation(value = "testDepthFiles <") + @GetMapping("/testDepthFiles") + @ApiImplicitParams({ + @ApiImplicitParam(name = "oldPath", value = "杈撳叆璺緞", dataType = "String", paramType = "query", example = "D:\\other\\simu\\uwsolver\\chicago"), + @ApiImplicitParam(name = "newPath", value = "寮�濮嬫椂闂�", dataType = "Integer", paramType = "query", example = "D:\\other\\simu\\uwsolver\\chicago\\depth"), + @ApiImplicitParam(name = "year", value = "骞�", dataType = "Integer", paramType = "query", example = "2025"), + @ApiImplicitParam(name = "month", value = "鏈�", dataType = "Integer", paramType = "query", example = "4"), + @ApiImplicitParam(name = "day", value = "鏃�", dataType = "Integer", paramType = "query", example = "21") + }) + public R<Object> testDepthFiles(String oldPath, String newPath, Integer year, Integer month, Integer day) { + try { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(new Date(year - 1900, month - 1, day, 0, 0, 0)); + SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); + + for (File file : new File(oldPath).listFiles()) { + if (!file.exists() || !file.isDirectory()) continue; + + File tif = new File(oldPath + "\\" + file.getName() + "\\depth.tif"); + if (!tif.exists() || tif.isDirectory()) continue; + + /*int second = Integer.parseInt(fileName); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.HOUR, 0); + calendar.add(Calendar.SECOND, second);*/ + calendar.add(Calendar.SECOND, 1); + String newName = df.format(calendar.getTime()); + String newFile = newPath + File.separator + newName + ".tif"; + + System.out.println(newFile); + tif.renameTo(new File(newFile)); + file.delete(); + } + + return success("ok"); + } catch (Exception ex) { + return fail(ex, null); + } + } + + @ApiOperation(value = "testTifFiles <") + @GetMapping("/testTifFiles") + @ApiImplicitParams({ + @ApiImplicitParam(name = "oldPath", value = "杈撳叆璺緞", dataType = "String", paramType = "query", example = "D:\\other\\simu\\uwsolver\\chicago\\depth"), + @ApiImplicitParam(name = "newPath", value = "寮�濮嬫椂闂�", dataType = "Integer", paramType = "query", example = "D:\\other\\simu\\uwsolver\\chicago\\depth2"), + @ApiImplicitParam(name = "step", value = "姝ラ暱", dataType = "Integer", paramType = "query", example = "20") + }) + public R<Object> testTifFiles(String oldPath, String newPath, Integer step) { + try { + File[] files = new File(oldPath).listFiles(); + for (int i = 0, c = files.length; i < c; i += step) { + File file = files[i]; + if (!file.exists() || !file.isFile() || !file.getName().endsWith(".tif")) continue; + + String newFile = newPath + File.separator + file.getName(); + + System.out.println(newFile); + file.renameTo(new File(newFile)); + //Files.copy(file.toPath(), Paths.get(newFile), StandardCopyOption.REPLACE_EXISTING); + file.delete(); + } + + return success("ok"); + } catch (Exception ex) { + return fail(ex, null); + } + } } diff --git a/src/main/java/com/se/nsl/helper/GdalHelper.java b/src/main/java/com/se/nsl/helper/GdalHelper.java index 84dbe91..b5dabab 100644 --- a/src/main/java/com/se/nsl/helper/GdalHelper.java +++ b/src/main/java/com/se/nsl/helper/GdalHelper.java @@ -32,9 +32,9 @@ // 閰嶇疆鐜鍙橀噺 if (!StringHelper.isEmpty(gdalPath)) { if (WebHelper.isWin()) { + gdal.SetConfigOption("GDAL_HOME", gdalPath); gdal.SetConfigOption("GDAL_DATA", gdalPath + "/gdal-data"); - gdal.SetConfigOption("PROJ_LIB", gdalPath + "/proj7/share"); - //System.setProperty("PROJ_LIB", gdalPath + "/proj7/share") + gdal.SetConfigOption("PROJ_LIB", gdalPath + "/projlib"); ///proj7/share gdal.SetConfigOption("GDAL_DRIVER_PATH", gdalPath + "/gdalplugins"); String path = System.getenv("PATH"); diff --git a/src/main/java/com/se/nsl/service/TestService.java b/src/main/java/com/se/nsl/service/TestService.java index 9a46acd..11843f8 100644 --- a/src/main/java/com/se/nsl/service/TestService.java +++ b/src/main/java/com/se/nsl/service/TestService.java @@ -323,8 +323,8 @@ if (buffer[offset] + ts[offset] > maxHeight) { g = b = 255; } else { - //int val = (int) ((buffer[offset] + ts[offset] - minHeight) / differ * 65535); - int val = (int) (buffer[offset] / differ * 65535); + int val = (int) ((buffer[offset] + ts[offset] - minHeight) / differ * 65535); + //int val = (int) (buffer[offset] / differ * 65535); g = val / 256; b = val % 256; } diff --git a/src/test/java/com/se/nsl/AppTest.java b/src/test/java/com/se/nsl/AppTest.java index ccc94c2..2a54a2b 100644 --- a/src/test/java/com/se/nsl/AppTest.java +++ b/src/test/java/com/se/nsl/AppTest.java @@ -3,7 +3,6 @@ import com.se.nsl.domain.po.Region; import com.se.nsl.helper.GdalHelper; import com.se.nsl.mapper.RegionMapper; -import javafx.scene.input.DataFormat; import org.gdal.gdal.Dataset; import org.gdal.gdal.Driver; import org.gdal.gdal.gdal; @@ -39,6 +38,67 @@ RegionMapper regionMapper; @Test + public void test() { + //procDepthFiles(); + System.out.println("> test ----------------------"); + readZarr(); + } + + private void procDepthFiles() { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(new Date(2025 - 1900, 4 - 1, 21, 0, 0, 0)); + SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); + + String oldPath = "D:\\other\\simu\\uwsolver\\chicago"; + String newPath = "D:\\other\\simu\\uwsolver\\chicago\\depth"; + for (File file : new File(oldPath).listFiles()) { + if (!file.exists() || !file.isDirectory()) continue; + + File tif = new File(oldPath + "\\" + file.getName() + "\\depth.tif"); + if (!tif.exists() || tif.isDirectory()) continue; + + //String fileName = file.getName().replace("2025041200", "").replace(".tif", ""); + /*int second = Integer.parseInt(fileName); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.HOUR, 0); + calendar.add(Calendar.SECOND, second);*/ + calendar.add(Calendar.SECOND, 1); + String newName = df.format(calendar.getTime()); + String newFile = newPath + File.separator + newName + ".tif"; + + System.out.println(newFile); + tif.renameTo(new File(newFile)); + file.delete(); + } + } + + //@Test + public void collectPngToList() { + System.out.println("------------------------------------------- start"); + int size = 1024; + String outputPath = "D:\\other\\simu\\out\\20250412\\waters"; + List<String> list = new ArrayList<>(); + for (File file : new File(outputPath).listFiles()) { + if (!file.exists() || file.isFile()) continue; + + File pngFile = new File(file.getPath() + File.separator + size + "_" + size + ".png"); + if (!pngFile.exists() || pngFile.isDirectory()) continue; + + list.add("file '" + pngFile.getPath().replace("\\", "/") + "'"); + System.out.println(list.get(list.size() - 1)); + } + + Path path = Paths.get(outputPath + File.separator + "list.txt"); + try { + Files.write(path, list, StandardCharsets.UTF_8); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("------------------------------------------- end"); + } + + //@Test public void insertRegion() { GdalHelper.init("D:\\soft\\release-1928-x64-dev\\release-1928-x64\\bin"); String path = "D:\\Documents\\WeChat Files\\wxid_llap8o5q8rm521\\FileStorage\\File\\2025-04\\瀛欒儭娌焔eometry.json"; @@ -72,100 +132,39 @@ } //@Test - public void procDepthFiles() { - String path = "D:\\other\\simu\\uwsolver\\20250412\\depth"; - /*for (File file : new File(path).listFiles()) { - if (!file.exists() || !file.isDirectory()) continue; + public void readZarr() { + //GdalHelper.init("D:\\soft\\release-1928-x64-dev\\release-1928-x64\\bin"); + //GdalHelper.init("C:\\Program Files\\GDAL"); - File tif = new File(path + "\\" + file.getName() + "\\depth.tif"); - if (!tif.exists() || tif.isDirectory()) continue; - - File newFile = new File(path + "\\2025041200" + file.getName() + ".tif"); - System.out.println(newFile.getPath()); - tif.renameTo(newFile); - file.delete(); + /*try { + System.setProperty("java.library.path", "C:\\Program Files\\GDAL"); + java.lang.reflect.Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); + fieldSysPath.setAccessible(true); + fieldSysPath.set(null, null); + //System.load("C:\\Program Files\\GDAL\\gdaljni.dll"); + } catch (Exception e) { + e.printStackTrace(); }*/ - Calendar calendar = Calendar.getInstance(); - calendar.setTime(new Date(2025 - 1900, 3, 12, 0, 0, 0)); - SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); + gdal.AllRegister(); + System.out.println("GDAL Initialized. Version: " + gdal.VersionInfo("RELEASE_NAME")); - String oldPath = "D:\\other\\simu\\uwsolver\\20250412\\depth-old"; - String newPath = "D:\\other\\simu\\uwsolver\\20250412\\depth"; - for (File file : new File(oldPath).listFiles()) { - String fileName = file.getName().replace("2025041200", "").replace(".tif", ""); - int second = Integer.parseInt(fileName); - calendar.set(Calendar.SECOND, 0); - calendar.set(Calendar.MINUTE, 0); - calendar.set(Calendar.HOUR, 0); - calendar.add(Calendar.SECOND, second); - String newName = df.format(calendar.getTime()); - String newFile = newPath + File.separator + newName + ".tif"; - - System.out.println(newFile); - file.renameTo(new File(newFile)); - } - } - - //@Test - public void collectPngToList() { - System.out.println("------------------------------------------- start"); - int size = 1024; - String outputPath = "D:\\other\\simu\\out\\20250412\\waters"; - List<String> list = new ArrayList<>(); - for (File file : new File(outputPath).listFiles()) { - if (!file.exists() || file.isFile()) continue; - - File pngFile = new File(file.getPath() + File.separator + size + "_" + size + ".png"); - if (!pngFile.exists() || pngFile.isDirectory()) continue; - - list.add("file '" + pngFile.getPath().replace("\\", "/") + "'"); - System.out.println(list.get(list.size() - 1)); - } - - Path path = Paths.get(outputPath + File.separator + "list.txt"); - try { - Files.write(path, list, StandardCharsets.UTF_8); - } catch (IOException e) { - e.printStackTrace(); - } - System.out.println("------------------------------------------- end"); - } - - //@Test - public void readZarr() throws Exception { - GdalHelper.init("D:\\soft\\release-1928-x64-dev\\release-1928-x64\\bin"); - - String zarrPath = "D:\\other\\simu\\CudaUWSolver.zarr"; + String zarrPath = "D:\\other\\simu\\out\\out.zarr"; // "D:\\other\\simu\\out\\CudaUWSolver.zarr"; Dataset ds = null; try { Driver driver = gdal.GetDriverByName("Zarr"); ds = gdal.Open(zarrPath, gdalconst.GA_ReadOnly); - if (null == ds || ds.getRasterCount() < 1) return; - System.out.println(ds.getRasterCount()); + if (null == ds || ds.getRasterCount() < 1) { + System.err.println("Could not open input dataset: " + zarrPath); + return; + } + + System.out.println("Successfully opened..."); + System.out.println("Driver: " + ds.GetDriver().getShortName() + "/" + ds.GetDriver().getLongName()); + System.out.println("Size: " + ds.getRasterXSize() + "x" + ds.getRasterYSize() + "x" + ds.getRasterCount()); } finally { if (null != ds) ds.delete(); } - } - - //@Test - public void readZarr2() throws Exception { - String zarrInputPath = "D:\\other\\simu\\out\\out.zarr"; - gdal.AllRegister(); - System.out.println("GDAL Initialized. Version: " + gdal.VersionInfo("RELEASE_NAME")); - - Dataset inputDataset = gdal.Open(zarrInputPath, gdalconst.GA_ReadOnly); - - if (inputDataset == null) { - System.err.println("Could not open input dataset: " + zarrInputPath); - return; - } - - System.out.println("Successfully opened"); - System.out.println( - "Driver: " + inputDataset.GetDriver().getShortName() + "/" + inputDataset.GetDriver().getLongName()); - System.out.println("Size: " + inputDataset.getRasterXSize() + "x" + inputDataset.getRasterYSize() + "x" - + inputDataset.getRasterCount()); } } -- Gitblit v1.9.3