张洋洋
2025-01-16 94d808f30053f8d03185cca31df3c8673d815699
[add]管线json
已添加3个文件
已修改2个文件
115 ■■■■■ 文件已修改
pom.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/controller/SimuController.java 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/utils/TiffCoordinateExtractorUtil.java 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/utils/TiffToRGBUtil.java 55 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/terrainmodule.json 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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>
src/main/java/com/se/simu/controller/SimuController.java
@@ -490,7 +490,24 @@
        fileWriter.close();
        return success(pointPath);
    }
    @ApiOperation(value = "地形转cityjson")
    @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 {
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;
    }
}
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();
        }
    }
}
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]]}}