From 75a3851294865b31d228872ea677f80cd0cda028 Mon Sep 17 00:00:00 2001 From: 张洋洋 <10611411+yang-yang-z@user.noreply.gitee.com> Date: 星期三, 12 二月 2025 17:25:58 +0800 Subject: [PATCH] [add]tif裁剪 --- src/main/java/com/se/simu/utils/ProjectionToGeographicUtil.java | 3 src/main/java/com/se/simu/utils/TiffClipper.java | 42 ++++++++++++++ src/main/java/com/se/simu/utils/TiffToRGBUtil.java | 2 src/main/java/com/se/simu/controller/SimuController.java | 96 ++++++++++++++++---------------- 4 files changed, 92 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/se/simu/controller/SimuController.java b/src/main/java/com/se/simu/controller/SimuController.java index 31b3435..c1282d6 100644 --- a/src/main/java/com/se/simu/controller/SimuController.java +++ b/src/main/java/com/se/simu/controller/SimuController.java @@ -492,54 +492,54 @@ SemUtils.cityJsonToSem(pointPath, filePatn + "node.sem"); return success(filePatn + "node.sem"); } -// @ApiOperation(value = "绠$偣杞琧ityjson") -// @GetMapping(value = "/pointToCityJson", produces = "application/json; charset=UTF-8") -// public R<String> pointToCityJson(@RequestParam("shpPath") String shpPath) throws Exception { -// String path = outPath + "\\point\\"; -// List<Map<String, Object>> list = ShpReadUtils.readPointShp(shpPath); -// JSONObject jsonObject = getModule("pointmodule.json"); -// for (Map<String, Object> map : list -// ) { -// //鎷艰鍧愭爣 -// JSONArray vertice = new JSONArray(); -// vertice.add(map.get("lon")); -// vertice.add(map.get("lat")); -// vertice.add(0.0); -// jsonObject.getJSONArray("vertices").add(vertice); -// //鎷艰鍩虹淇℃伅 -// JSONObject cityObject = new JSONObject(); -// cityObject.put("type", "+PipePoint"); -// JSONObject attribute = new JSONObject(); -// attribute.put("name", map.get("fsw")); -// cityObject.put("attributes", attribute); -// JSONArray geometry = new JSONArray(); -// JSONObject metry = new JSONObject(); -// metry.put("type", "MultiPoint"); -// metry.put("lod", 0); -// JSONArray boundarie = new JSONArray(); -// boundarie.add(jsonObject.getJSONArray("vertices").size() - 1); -// metry.put("boundaries", boundarie); -// geometry.add(metry); -// cityObject.put("geometry", geometry); -// jsonObject.getJSONObject("CityObjects").put("UUID_" + UUID.randomUUID().toString(), cityObject); -// } -// long times = System.currentTimeMillis(); -// String pointPath = path + times + "\\绠$偣.json"; -// File dirFile = new File(path + times); -// if (!dirFile.exists()) { -// dirFile.mkdirs(); -// } -// FileWriter fileWriter = new FileWriter(pointPath); -// fileWriter.write(jsonObject.toJSONString()); -// fileWriter.close(); -// String filePatn = inPath + "\\tongzhou\\"; -// File file = new File(filePatn); -// if (!file.exists()) { -// file.mkdirs(); -// } -// SemUtils.cityJsonToSem(pointPath, filePatn + "node.sem"); -// return success(filePatn + "node.sem"); -// } + @ApiOperation(value = "绠$偣杞琧ityjson") + @GetMapping(value = "/pointToCityJson", produces = "application/json; charset=UTF-8") + public R<String> pointToCityJson(@RequestParam("shpPath") String shpPath) throws Exception { + String path = outPath + "\\point\\"; + List<Map<String, Object>> list = ShpReadUtils.readPointShp(shpPath); + JSONObject jsonObject = getModule("pointmodule.json"); + for (Map<String, Object> map : list + ) { + //鎷艰鍧愭爣 + JSONArray vertice = new JSONArray(); + vertice.add(map.get("lon")); + vertice.add(map.get("lat")); + vertice.add(0.0); + jsonObject.getJSONArray("vertices").add(vertice); + //鎷艰鍩虹淇℃伅 + JSONObject cityObject = new JSONObject(); + cityObject.put("type", "+PipePoint"); + JSONObject attribute = new JSONObject(); + attribute.put("name", map.get("fsw")); + cityObject.put("attributes", attribute); + JSONArray geometry = new JSONArray(); + JSONObject metry = new JSONObject(); + metry.put("type", "MultiPoint"); + metry.put("lod", 0); + JSONArray boundarie = new JSONArray(); + boundarie.add(jsonObject.getJSONArray("vertices").size() - 1); + metry.put("boundaries", boundarie); + geometry.add(metry); + cityObject.put("geometry", geometry); + jsonObject.getJSONObject("CityObjects").put("UUID_" + UUID.randomUUID().toString(), cityObject); + } + long times = System.currentTimeMillis(); + String pointPath = path + times + "\\绠$偣.json"; + File dirFile = new File(path + times); + if (!dirFile.exists()) { + dirFile.mkdirs(); + } + FileWriter fileWriter = new FileWriter(pointPath); + fileWriter.write(jsonObject.toJSONString()); + fileWriter.close(); + String filePatn = inPath + "\\tongzhou\\"; + File file = new File(filePatn); + if (!file.exists()) { + file.mkdirs(); + } + SemUtils.cityJsonToSem(pointPath, filePatn + "node.sem"); + return success(filePatn + "node.sem"); + } @ApiOperation(value = "绠$嚎sem鐢熸垚") @PostMapping(value = "/lineToSem", produces = "application/json; charset=UTF-8") diff --git a/src/main/java/com/se/simu/utils/ProjectionToGeographicUtil.java b/src/main/java/com/se/simu/utils/ProjectionToGeographicUtil.java index 17d2206..b68cdac 100644 --- a/src/main/java/com/se/simu/utils/ProjectionToGeographicUtil.java +++ b/src/main/java/com/se/simu/utils/ProjectionToGeographicUtil.java @@ -51,7 +51,6 @@ } public static void main(String[] args) { - System.out.println(getPoint(470554.2614775232,4416699.023973457)); - System.out.println(get4548Point(116.65575979450138,39.883938395445966)); + System.out.println(get4548Point(116.66,39.8962)); } } diff --git a/src/main/java/com/se/simu/utils/TiffClipper.java b/src/main/java/com/se/simu/utils/TiffClipper.java new file mode 100644 index 0000000..1f775b6 --- /dev/null +++ b/src/main/java/com/se/simu/utils/TiffClipper.java @@ -0,0 +1,42 @@ +package com.se.simu.utils; + +import org.gdal.gdal.Dataset; +import org.gdal.gdal.TranslateOptions; +import org.gdal.gdal.gdal; +import org.gdal.gdalconst.gdalconstConstants; + +import java.util.Vector; + +public class TiffClipper { + public static void cropTiffByLatLon(String inputFilePath, String outputFilePath, + double minX, double minY, double maxX, double maxY) throws Exception{ + // 娉ㄥ唽鎵�鏈� GDAL 椹卞姩 + gdal.AllRegister(); + Vector vector=new Vector(); + vector.add("-projwin"); + vector.add(String.valueOf(minX)); + vector.add(String.valueOf(maxY)); + vector.add(String.valueOf(maxX)); + vector.add(String.valueOf(minY)); + TranslateOptions options = new TranslateOptions(vector); + // 鎵撳紑杈撳叆鏁版嵁闆� + Dataset ds = gdal.Open(inputFilePath, gdalconstConstants.GA_ReadOnly); + if (ds == null) { + throw new Exception("鏃犳硶鎵撳紑杈撳叆鏂囦欢: " + inputFilePath); + } + // 鎵ц瑁佸壀鎿嶄綔 + Dataset outputDataset = gdal.Translate(outputFilePath, ds, options); + if (outputDataset != null) { + System.out.println("瑁佸壀鎴愬姛锛岃緭鍑烘枃浠�: " + outputFilePath); + // 閲婃斁鏁版嵁闆嗚祫婧� + outputDataset.delete(); + } else { + System.err.println("瑁佸壀澶辫触"); + } + // 閿�姣� GDAL 椹卞姩绠$悊鍣� + gdal.GDALDestroyDriverManager(); + } + public static void main(String[] args) throws Exception{ + cropTiffByLatLon("D:\\鍩庡競鍐呮稘\\sem\\DEM.tif","D:\\鍩庡競鍐呮稘\\sem\\DEM100.tif",470066.5191659781,4417962.449727667,470922.1436516798,4418059.07638588); + } +} diff --git a/src/main/java/com/se/simu/utils/TiffToRGBUtil.java b/src/main/java/com/se/simu/utils/TiffToRGBUtil.java index 6df3dec..66a41bd 100644 --- a/src/main/java/com/se/simu/utils/TiffToRGBUtil.java +++ b/src/main/java/com/se/simu/utils/TiffToRGBUtil.java @@ -14,7 +14,7 @@ public class TiffToRGBUtil { public static void main(String[] args) throws Exception{ - tifToPng("D:\\鍩庡競鍐呮稘\\sem\\DEM.tif","D:\\鍩庡競鍐呮稘\\sem\\DEM.png"); + tifToPng("D:\\鍩庡競鍐呮稘\\sem\\DEM100.tif","D:\\鍩庡競鍐呮稘\\sem\\DEM100.png"); } public static void tifToPng(String tifPath, String pngPath) throws Exception { // 娉ㄥ唽鎵�鏈夌殑 GDAL 椹卞姩 -- Gitblit v1.9.3