1
13693261870
2024-09-30 6827092435f97dcd7bf557eb01711d24f437ce55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.se.simu.service;
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.se.simu.config.PropertiesConfig;
import com.se.simu.domain.dto.ExtensionDto;
import com.se.simu.domain.dto.LayerDto;
import com.se.simu.domain.dto.ResultDto;
import com.se.simu.helper.GdalHelper;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.ogr.Geometry;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
/**
 * 处理结果服务类
 *
 * @author WWW
 * @date   2024-09-29
 */
@Slf4j
@Service
@SuppressWarnings("ALL")
public class ResultService {
    @Resource
    PropertiesConfig config;
 
    public final static double MAX_X_OFFSET = 0.002;
 
    public void process(ResultDto dto) throws Exception {
        try {
            LayerDto layer = new LayerDto(config.getVer());
 
            copeTerrain(dto, layer);
            copeBuilding(dto, layer);
            copeWater(dto, layer);
            copeFlow(dto, layer);
            copeLayerJson(dto, layer);
        } finally {
            File dir = new File(dto.getTemp());
            if (dir.exists()) {
                FileUtil.del(dir);
            }
        }
    }
 
    private void copeTerrain(ResultDto dto, LayerDto layer) {
        Dataset ds = null;
        try {
            ds = gdal.Open(dto.getTerrainFile(), gdalconst.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount() || null == ds.GetSpatialRef()) return;
 
            setTerrainInfo(ds, layer);
            setWaterInfo(dto, layer);
            createTerrainPng(dto, ds, layer);
        } finally {
            if (null != ds) ds.delete();
        }
    }
 
    private void setTerrainInfo(Dataset ds, LayerDto layer) {
        Geometry minPoint = GdalHelper.getMinPoint(ds);
        Geometry maxPoint = GdalHelper.getMaxPoint(ds);
        double minx = getMinVal(minPoint.GetX(0), 10000000);
        double miny = getMinVal(minPoint.GetY(0), 10000000);
        double maxx = getMaxVal(maxPoint.GetX(0) + MAX_X_OFFSET, 10000000);
        double maxy = getMaxVal(maxPoint.GetY(0), 10000000);
        layer.setExtension(new ExtensionDto(minx, miny, maxx, maxy, Double.MAX_VALUE, Double.MIN_VALUE));
 
        Band band = ds.GetRasterBand(1);
        double[] mm = new double[2];
        band.ComputeRasterMinMax(mm, 0);
        layer.getTerrain().setHeight(getMinVal(mm[0], 1000), getMaxVal(mm[1], 1000));
    }
 
    private static double getMinVal(double val, double radix) {
        return ((long) Math.floor(val * radix)) / radix;
    }
 
    private static double getMaxVal(double val, double radix) {
        return ((long) Math.ceil(val * radix)) / radix;
    }
 
    private void setWaterInfo(ResultDto dto, LayerDto layer) {
 
    }
 
    private void createTerrainPng(ResultDto dto, Dataset ds, LayerDto layer) {
 
    }
 
    private void copeBuilding(ResultDto dto, LayerDto layer) {
        //
    }
 
    private void copeWater(ResultDto dto, LayerDto layer) {
        //
    }
 
    private void copeFlow(ResultDto dto, LayerDto layer) {
        //
    }
 
    private void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException {
        String json = JSONUtil.toJsonPrettyStr(layer);
        String filePath = dto.getOutPath() + File.separator + "layer.json";
 
        FileWriter fw = new FileWriter(filePath);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(json);
        bw.close();
        fw.close();
    }
 
    public String test() throws Exception {
        ResultDto dto = new ResultDto(
                "202409",
                "D:\\simu\\input\\tongzhou-local-mesh2-terrain.tif",
                "D:\\simu\\input\\tongzhou-local-mesh2-buildings.tif",
                "D:\\simu\\input\\waters",
                "D:\\simu\\input\\flows",
                "D:\\simu\\out",
                "");
        process(dto);
 
        return "OK";
    }
}