From 94d808f30053f8d03185cca31df3c8673d815699 Mon Sep 17 00:00:00 2001
From: 张洋洋 <10611411+yang-yang-z@user.noreply.gitee.com>
Date: 星期四, 16 一月 2025 13:09:49 +0800
Subject: [PATCH] [add]管线json

---
 src/main/java/com/se/simu/utils/TiffToRGBUtil.java               |   55 +++++++++++++++++++++++++++
 src/main/resources/terrainmodule.json                            |    1 
 src/main/java/com/se/simu/utils/TiffCoordinateExtractorUtil.java |   35 +++++++++++++++++
 pom.xml                                                          |    5 ++
 src/main/java/com/se/simu/controller/SimuController.java         |   19 +++++++++
 5 files changed, 114 insertions(+), 1 deletions(-)

diff --git a/pom.xml b/pom.xml
index bf667d9..e9ee0c3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -303,6 +303,11 @@
             <artifactId>proj4j</artifactId>
             <version>0.1.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-imaging</artifactId>
+            <version>1.0-alpha2</version>
+        </dependency>
     </dependencies>
     <repositories>
         <repository>
diff --git a/src/main/java/com/se/simu/controller/SimuController.java b/src/main/java/com/se/simu/controller/SimuController.java
index f9bc490..d4cae22 100644
--- a/src/main/java/com/se/simu/controller/SimuController.java
+++ b/src/main/java/com/se/simu/controller/SimuController.java
@@ -490,7 +490,24 @@
         fileWriter.close();
         return success(pointPath);
     }
-
+    @ApiOperation(value = "鍦板舰杞琧ityjson")
+    @GetMapping(value = "/terrainToCityJson", produces = "application/json; charset=UTF-8")
+    public R<String> terrainToCityJson(@RequestParam("tifPath") String tifPath) throws Exception {
+        long times = System.currentTimeMillis();
+        String path = outPath + "\\terrain\\"+times+"\\";
+        File dirFile = new File(path+"appearance");
+        if (!dirFile.exists()) {
+            dirFile.mkdirs();
+        }
+        String pngPath=path+"appearance\\terrain.png";
+        TiffToRGBUtil.tiffToPng(tifPath,pngPath);
+        JSONObject jsonObject = getModule("terrainmodule.json");
+        jsonObject.put("vertices",TiffCoordinateExtractorUtil.getCoordinate(tifPath));
+        FileWriter fileWriter = new FileWriter(path + "terrain.json");
+        fileWriter.write(jsonObject.toJSONString());
+        fileWriter.close();
+        return success(path);
+    }
     public JSONObject getModule(String moduleName) {
         JSONObject jsonObject = new JSONObject();
         try {
diff --git a/src/main/java/com/se/simu/utils/TiffCoordinateExtractorUtil.java b/src/main/java/com/se/simu/utils/TiffCoordinateExtractorUtil.java
new file mode 100644
index 0000000..3f29b30
--- /dev/null
+++ b/src/main/java/com/se/simu/utils/TiffCoordinateExtractorUtil.java
@@ -0,0 +1,35 @@
+package com.se.simu.utils;
+
+import com.alibaba.fastjson.JSONArray;
+import org.gdal.gdal.Dataset;
+import org.gdal.gdal.gdal;
+
+public class TiffCoordinateExtractorUtil {
+    public static void main(String[] args) {
+        getCoordinate("D:\\鍩庡競鍐呮稘\\sem\\DEM.tif");
+    }
+    public static JSONArray getCoordinate(String tifPath){
+        gdal.AllRegister();
+        JSONArray array=new JSONArray();
+        Dataset dataset = gdal.Open(tifPath);
+        if (dataset!= null) {
+            double[] geotransform = dataset.GetGeoTransform();
+            if (geotransform!= null) {
+                double xmin = geotransform[0];
+                double ymax = geotransform[3];
+                double xmax = geotransform[0] + geotransform[1] * dataset.getRasterXSize();
+                double ymin = geotransform[3] + geotransform[5] * dataset.getRasterYSize();
+                System.out.println("宸︿笂瑙掔粡绾害: (" + xmin + ", " + ymax + ")");
+                array.add(ProjectionToGeographicUtil.getPoint(xmin,ymax));
+                System.out.println("鍙充笂瑙掔粡绾害: (" + xmax + ", " + ymax + ")");
+                array.add(ProjectionToGeographicUtil.getPoint(xmax,ymax));
+                System.out.println("宸︿笅瑙掔粡绾害: (" + xmin + ", " + ymin + ")");
+                array.add(ProjectionToGeographicUtil.getPoint(xmin,ymin));
+                System.out.println("鍙充笅瑙掔粡绾害: (" + xmax + ", " + ymin + ")");
+                array.add(ProjectionToGeographicUtil.getPoint(xmax,ymin));
+            }
+        }
+        System.out.println(array.toJSONString());
+        return array;
+    }
+}
diff --git a/src/main/java/com/se/simu/utils/TiffToRGBUtil.java b/src/main/java/com/se/simu/utils/TiffToRGBUtil.java
new file mode 100644
index 0000000..1519534
--- /dev/null
+++ b/src/main/java/com/se/simu/utils/TiffToRGBUtil.java
@@ -0,0 +1,55 @@
+package com.se.simu.utils;
+
+import org.apache.commons.imaging.*;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+
+public class TiffToRGBUtil {
+
+    public static void tiffToPng(String tifPath, String pngPath) throws Exception {
+        // 杈撳嚭鐨� PNG 鏂囦欢璺緞
+        String outputPngFilePath = pngPath;
+        try {
+            // 璇诲彇 TIF 鍥惧儚
+            File tiffFile = new File(tifPath);
+            BufferedImage tifImage = Imaging.getBufferedImage(tiffFile);
+            int width = tifImage.getWidth();
+            int height = tifImage.getHeight();
+            // 鍒涘缓涓�涓柊鐨� BufferedImage 瀵硅薄锛岀被鍨嬩负 TYPE_INT_ARGB
+            BufferedImage pngImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+            // 閬嶅巻 TIF 鍥惧儚鐨勬瘡涓儚绱�
+            for (int y = 0; y < height; y++) {
+                for (int x = 0; x < width; x++) {
+                    // 鑾峰彇 TIF 鍥惧儚涓綋鍓嶅儚绱犵殑棰滆壊鍊�
+                    int pixel = tifImage.getRGB(x, y);
+                    // 鍦ㄨ繖閲屽彲浠ュ鍍忕礌鐨勯鑹插�艰繘琛屼竴浜涘鐞嗭紝渚嬪鏍规嵁楂樺害淇℃伅璋冩暣棰滆壊
+                    // 鍋囪鎴戜滑绠�鍗曞湴灏嗗儚绱犵殑绾㈣壊鍒嗛噺鏍规嵁楂樺害杩涜璋冩暣
+                    int red = (pixel >> 16) & 0xff;
+                    int green = (pixel >> 8) & 0xff;
+                    int blue = (pixel) & 0xff;
+                    // 鍋囪楂樺害淇℃伅瀛樺偍鍦ㄧ豢鑹插垎閲忎腑锛屼綘鍙互鏍规嵁瀹為檯鎯呭喌璋冩暣
+                    //height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1)
+                    int heightValue = (int) (-10000 + ((red * 256 * 256 + green * 256 + blue) * 0.1));
+                    System.out.println(heightValue);
+                    // 绠�鍗曞湴灏嗙孩鑹插垎閲忔牴鎹珮搴﹀�艰繘琛岃皟鏁达紝渚嬪锛岃秺楂樿秺绾�
+                    // 纭繚绾㈣壊鍒嗛噺涓嶈秴杩� 255
+                    //red = Math.min((int) (red + (heightValue * 0.5)), 255);
+                    // 閲嶆柊缁勫悎 ARGB 鍊�
+                    Color color = new Color(red, green, blue);
+                    System.out.printf("Pixel (%d, %d): R=%d, G=%d, B=%d%n", x, y, red, green, blue);
+                    int newRgb = color.getRGB();
+                    // 灏嗗鐞嗗悗鐨勫儚绱犻鑹插�艰缃埌鏂扮殑 PNG 鍥惧儚涓�
+                    pngImage.setRGB(x, y, newRgb);
+                }
+            }
+            // 淇濆瓨涓� PNG 鍥惧儚
+            ImageIO.write(pngImage, "png", new File(outputPngFilePath));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}
diff --git a/src/main/resources/terrainmodule.json b/src/main/resources/terrainmodule.json
new file mode 100644
index 0000000..b6990de
--- /dev/null
+++ b/src/main/resources/terrainmodule.json
@@ -0,0 +1 @@
+{"type":"CityJSON","version":"1.0","CityObjects":{"UUID_63b11762-6a6a-4d91-b497-6314fee1ce0f":{"type":"+Terrain","geometry":[{"type":"MultiSurface","boundaries":[[[0,1,2,3]]],"texture":{"visual":{"values":[[[0,0,1,2,3]]]}},"lod":0}]}},"appearance":{"textures":[{"type":"PNG","image":"appearances/terrain.png"}],"vertices-texture":[[0.0,0.0],[0.0,1.0],[1.0,1.0],[1.0,0.0]]}}
\ No newline at end of file

--
Gitblit v1.9.3