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";
|
}
|
}
|