dcb
2025-06-30 59b432c883011119649c283cc9d4c1d357599802
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.se.nsl.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bc.zarr.ZarrArray;
import com.bc.zarr.ZarrGroup;
import com.se.nsl.config.PropertiesConfig;
import com.se.nsl.domain.po.Simu;
import com.se.nsl.domain.vo.SimuResult;
import com.se.nsl.domain.vo.SimuVo;
import com.se.nsl.helper.StringHelper;
import com.se.nsl.mapper.SimuMapper;
import com.se.nsl.utils.CoordinateTransformer;
import com.se.nsl.utils.SolverTifUtil;
import com.se.nsl.utils.TimeFormatUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ucar.ma2.InvalidRangeException;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
@Slf4j
@Service
@SuppressWarnings("ALL")
public class SimuService {
    public static final String TIF_EXTSION = ".tif";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyyMMddHHmmss";
    @Resource
    SimuMapper simuMapper;
 
    @Resource
    PropertiesConfig config;
 
    /**
     * 分页查询推演模拟
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return 分页后的推演模拟
     */
    public IPage<Simu> selectPage(SimuVo vo, int pageNum, int pageSize) {
        QueryWrapper<Simu> wrapper = getPageWrapper(vo, pageNum, pageSize);
 
        Page<Simu> page = new Page<>(pageNum, pageSize);
        page.addOrder(OrderItem.desc("id"));
 
        return simuMapper.selectPage(page, wrapper);
    }
 
    private QueryWrapper<Simu> getPageWrapper(SimuVo vo, int pageNum, int pageSize) {
        QueryWrapper<Simu> wrapper = new QueryWrapper<>();
        if (null != vo.getId()) {
            wrapper.eq("id", vo.getId());
        }
        if (!StringHelper.isEmpty(vo.getName())) {
            wrapper.like("lower(name)", vo.getName().trim().toLowerCase());
        }
        if (!StringHelper.isEmpty(vo.getServiceName())) {
            wrapper.like("service_name", vo.getServiceName().trim());
        }
        if (null != vo.getType()) {
            wrapper.eq("type", vo.getType());
        }
        if (null != vo.getAreaType()) {
            wrapper.eq("area_type", vo.getAreaType());
        }
        if (null != vo.getStatus()) {
            wrapper.eq("status", vo.getStatus());
        }
 
        return wrapper;
    }
 
    /**
     * 根据ID批量删除推演模拟
     *
     * @param ids 要删除的区域ID列表
     * @return 删除成功的记录数
     */
    public int deleteByIds(List<Integer> ids) {
        return simuMapper.deleteBatchIds(ids);
    }
 
    /**
     * 新增推演模拟
     *
     * @param Simu 推演模拟对象
     * @return 新增成功的记录数
     */
    public int insert(Simu simu) {
        return simuMapper.insert(simu);
    }
 
    public int inserts(List<Simu> list) {
        return simuMapper.inserts(list);
    }
 
    /**
     * 根据ID查询
     *
     * @param id ID
     * @return Simu
     */
    public Simu selectById(Integer id) {
        return simuMapper.selectById(id);
    }
 
    /**
     * 修改推演模拟
     *
     * @param Simu 推演模拟对象
     * @return 修改成功的记录数
     */
    public int updateById(Simu simu) {
        return simuMapper.updates(Arrays.asList(simu));
    }
 
    public SimuResult queryByPosition(double lon, double lat, long time, String serviceName) {
        //transform coordiante from 4326 to 4548
        double[] xy = CoordinateTransformer.transform(4326, 4548, lon, lat);
//        System.out.println(String.format("转换前的坐标:x:%s,y:%s", lon, lat));
//        System.out.println(String.format("转换后的坐标:x:%s,y:%s", xy[0], xy[1]));
        //read from zarr
//        return queryByZarr(xy, time, serviceName);
 
        //read from tif file
        return queryByTif(xy, time, serviceName);
 
    }
 
    public List<SimuResult> queryByPosition(double lon, double lat, String serviceName) {
        //transform coordiante from 4326 to 4548
        double[] xy = CoordinateTransformer.transform(4326, config.getEpsg(), lon, lat);
//        System.out.println(String.format("转换前的坐标:x:%s,y:%s", lon, lat));
//        System.out.println(String.format("转换后的坐标:x:%s,y:%s", xy[0], xy[1]));
        //read from zarr
//        return queryByZarr(xy, time, serviceName);
 
        //read from tif file
        return queryByTif(xy, serviceName);
 
    }
 
    private SimuResult queryByZarr(double[] xy, long time, String serviceName) {
        double x = xy[0];
        double y = xy[1];
        String prefix = formatTime(time);
        File inPath = new File(config.getInPath());
//        File tifDir = new File(inPath, serviceName + File.separator + "depth");
//        int index = 0;
//        File[] files = tifDir.listFiles();
//        for (File file : files) {
//            String name = file.getName();
//            if (!name.endsWith(TIF_EXTSION)) continue;
//            if (name.equals(prefix + TIF_EXTSION)) {
//                break;
//            }
//            index++;
//        }
        File dem = new File(inPath, serviceName + File.separator + "DEM.tif");
        //TODO
 
        File zarr = new File(inPath, serviceName + File.separator + "result.zarr");
        try {
            ZarrGroup group = ZarrGroup.open(zarr.toPath());
            ZarrArray depthArray = group.openArray("depth");
            int[] shape = new int[] {60, 637, 351};
//            int[] offset = new int[]{210, 384};
            float[] depth = (float[]) depthArray.read(shape);
            System.out.println("depth:" + depth.length);
        } catch (IOException | InvalidRangeException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
 
    private SimuResult queryByTif(double[] xy, long time, String serviceName) {
        File inPath = new File(config.getInPath());
        String prefix = formatTime(time);
        String child = serviceName + File.separator + "depth" + File.separator + prefix + TIF_EXTSION;
        File tifFile = new File(inPath, child);
        if (!tifFile.exists()) {
            return null;
        }
        return  SolverTifUtil.getSimuResult(tifFile, xy);
    }
 
    private List<SimuResult> queryByTif(double[] xy, String serviceName) {
        List<SimuResult> res = new ArrayList<>();
        File inPath = new File(config.getInPath());
        Path depthPath = Paths.get(inPath.getAbsolutePath(), serviceName, "depth");
        File depthDir = depthPath.toFile();
        File[] files = depthDir.listFiles();
        for (File tifFile : files) {
            String name = tifFile.getName();
            if (!name.endsWith(TIF_EXTSION)) continue;
            SimuResult result = SolverTifUtil.getSimuResult(tifFile, xy);
            if (result == null) continue;
            res.add(result);
        }
        return res;
    }
 
    private String formatTime(long time) {
        return TimeFormatUtil.formatTime(time, YYYY_MM_DD_HH_MM_SS);
    }
 
}