wuww
2025-04-15 d45e75a608ce07a56cf82a829ce6bd740e672eeb
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package com.se.nsl.service;
 
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.se.nsl.config.PropertiesConfig;
import com.se.nsl.domain.dto.*;
import com.se.nsl.domain.po.DataPo;
import com.se.nsl.domain.po.PondingPo;
import com.se.nsl.domain.vo.BuildingDepthVo;
import com.se.nsl.helper.ComHelper;
import com.se.nsl.helper.GdalHelper;
import com.se.nsl.helper.ShpHelper;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
//import org.gdal.gdalconstConstants.gdalconst;
import org.gdal.gdalconst.gdalconstConstants;
import org.gdal.ogr.*;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
 
@Slf4j
@Service
@SuppressWarnings("ALL")
public class TestService {
    @Resource
    PropertiesConfig config;
 
    @Resource
    ResultService resultService;
 
    public final static double MAX_X_OFFSET = 0;
 
    public final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
 
    public void test(DataPo data) throws Exception {
        String basePath = config.getInPath() + File.separator + data.getInPath() + File.separator;
        ResultDto dto = new ResultDto(
                data.getInPath(),
                basePath + config.getTerrainFile(),
                basePath + config.getBuildingFile(),
                basePath + config.getWaterPath(),
                basePath + config.getFlowPath(),
                config.getInPath(),
                config.getOutPath(),
                data.getEpsg());
        LayerDto layer = new LayerDto(config.getVer(), data.getEpsg(), config.getSizes());
        process(dto, layer);
    }
 
    private void process(ResultDto dto, LayerDto layer) throws Exception {
        try {
            copeTerrain(dto, layer);
            //copeBuilding(dto, layer);
            List<BuildingDepthVo> buildings = copeWater(dto, layer);
            //copeFlow(dto, layer);
            copeLayerJson(dto, layer);
            //copeRainFallJson(dto, layer);
            //copeBuildingDepthJson(dto, buildings);
        } finally {
            File dir = new File(dto.getTemp());
            if (dir.exists()) {
                FileUtil.del(dir);
            }
        }
    }
 
    public void copeTerrain(ResultDto dto, LayerDto layer) {
        Dataset ds = null;
        try {
            // gdalconst,gdalconstConstants
            ds = gdal.Open(dto.getTerrainFile(), gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
 
            setTerrainInfo(ds, layer);
            createTerrainPng(dto, ds, layer);
            setWaterInfo(dto, layer);
        } finally {
            if (null != ds) ds.delete();
        }
    }
 
    public void setTerrainInfo(Dataset ds, LayerDto layer) {
        Geometry minPoint = GdalHelper.getMinPoint(ds);
        Geometry maxPoint = GdalHelper.getMaxPoint(ds);
        double minx = ComHelper.getMinVal(minPoint.GetX(0), 10000000);
        double miny = ComHelper.getMinVal(minPoint.GetY(0), 10000000);
        double maxx = ComHelper.getMaxVal(maxPoint.GetX(0) + MAX_X_OFFSET, 10000000);
        double maxy = ComHelper.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));
        layer.setExtension(new ExtensionDto(minx, miny, maxx, maxy, mm[0], mm[1]));
    }
 
    public void createTerrainPng(ResultDto dto, Dataset ds, LayerDto layer) {
        String terrainPath = dto.getOutPath() + File.separator + "terrain";
        File f = new File(terrainPath);
        if (!f.exists() || !f.isDirectory()) f.mkdirs();
 
        for (int[] sizes : layer.getTerrain().getSize()) {
            String tif = dto.getTemp() + File.separator + "terrain_" + sizes[0] + "_" + sizes[1] + ".tif";
            ComHelper.Resample(ds, tif, sizes[0], sizes[1], layer);
            if (!new File(tif).exists()) continue;
 
            String png = terrainPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
            Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
        }
    }
 
    public void Terrain2Png(LayerDto layer, String tif, String png, int width, int height) {
        Dataset ds = null;
        try {
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
 
            Band band = ds.GetRasterBand(1);
            float[] buffer = new float[width * height];
            //band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
            band.ReadRaster(0, 0, width, height, buffer);
            layer.getTerrain().getVals().put(width + "_" + height, buffer);
 
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            double differ = layer.getExtension().getMaxHeight() - layer.getExtension().getMinHeight(), minHeight = layer.getExtension().getMinHeight();
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    int offset = x + y * width;
                    if (Float.isNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
 
                    int r = 0, g, b;
                    if (buffer[offset] - layer.getExtension().getMaxHeight() > 0) {
                        g = b = 255;
                    } else {
                        int val = (int) ((buffer[offset] - minHeight) / differ * 65535);
                        g = val / 256;
                        b = val % 256;
                    }
 
                    Color color = new Color(r, g, b, 127);
                    //graphic.drawImage(image, x, y, 1, 1, color, null);
                    image.setRGB(x, y, color.getRGB());
                }
            }
            ComHelper.savePng(image, png);
        } finally {
            if (null != ds) ds.delete();
        }
    }
 
    public void setWaterInfo(ResultDto dto, LayerDto layer) {
        List<String> files = getFiles(dto.getWaterPath(), ".tif");
        layer.getWaters().setFiles(files);
        if (null == files || files.size() == 0) return;
 
        setWaterData(layer, files);
        setWaterHeight(layer, files);
    }
 
    public List<BuildingDepthVo> copeWater(ResultDto dto, LayerDto layer) {
        List<String> files = layer.getWaters().getFiles();
        if (files.size() == 0 || files.size() != layer.getWaters().getData().size()) return null;
 
        processWaters(dto, files, layer);
 
        if (new File(dto.getBuildingFile()).exists()) {
            return processBuilding(dto, files, layer);
        }
 
        return null;
    }
 
    public List<String> getFiles(String path, String suffix) {
        List<String> files = new ArrayList<>();
        ComHelper.getFiles(files, new File(path), suffix);
        files.sort((a, b) -> a.compareToIgnoreCase(b));
 
        return files;
    }
 
    public void setWaterData(LayerDto layer, List<String> files) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MILLISECOND, 0);
 
        for (String file : files) {
            String fileName = ComHelper.getNameWithExt(file);
            int year = Integer.parseInt(fileName.substring(0, 4));
            int month = Integer.parseInt(fileName.substring(4, 6));
            int day = Integer.parseInt(fileName.substring(6, 8));
            int hour = Integer.parseInt(fileName.substring(8, 10));
            int minute = Integer.parseInt(fileName.substring(10, 12));
            int second = Integer.parseInt(fileName.substring(12, 14));
 
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, day);
            calendar.set(Calendar.HOUR_OF_DAY, hour);
            calendar.set(Calendar.MINUTE, minute);
            calendar.set(Calendar.SECOND, second);
 
            layer.getWaters().getData().add(calendar.getTime().getTime());
        }
        layer.getDuration().setStart(layer.getWaters().getData().get(0));
        layer.getDuration().setEnd(layer.getWaters().getData().get(layer.getWaters().getData().size() - 1));
    }
 
    public void setWaterHeight(LayerDto layer, List<String> files) {
        /*files.parallelStream().forEach(file -> {
            Dataset ds = null;
            try {
                ds = gdal.Open(file, gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) return;
 
                double[] mm = new double[2];
                ds.GetRasterBand(1).ComputeRasterMinMax(mm, 0);
                layer.getWaters().setHeight(mm[0], mm[1]);
            } finally {
                if (null != ds) ds.delete();
            }
        });*/
 
        int c = files.size();
        int step = c <= 1 ? 1 : c / 10;
        for (int i = 0; i < c; i += step) {
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) return;
 
                double[] mm = new double[2];
                ds.GetRasterBand(1).ComputeRasterMinMax(mm, 0);
                layer.getWaters().setHeight(mm[0], mm[1]);
            } finally {
                if (null != ds) ds.delete();
            }
        }
 
        layer.getExtension().setMaxHeight(layer.getExtension().getMaxHeight() + layer.getWaters().getMaxHeight());
        layer.getExtension().setMaxHeight(ComHelper.getMaxVal(layer.getExtension().getMaxHeight(), 1000000));
        layer.getExtension().setMinHeight(ComHelper.getMaxVal(layer.getExtension().getMinHeight(), 1000000));
        layer.getExtension().setDiffer();
    }
 
    public void processWaters(ResultDto dto, List<String> files, LayerDto layer) {
        for (int i = 0, c = files.size(); i < c; i++) {
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) return;
                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
 
                createWaterPng(dto, ds, layer, layer.getWaters().getData().get(i));
                //if (config.getCopyTif()) copyWaterTif(dto, ds, layer.getWaters().getData().get(i));
                createVectors(dto, ds, layer, layer.getWaters().getData().get(i));
            } finally {
                if (null != ds) ds.delete();
            }
        }
    }
 
    public void createWaterPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
        String waterPath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks;
        File dir = new File(waterPath);
        if (!dir.exists() || !dir.isDirectory()) dir.mkdirs();
 
        for (int[] sizes : layer.getTerrain().getSize()) {
            String fileName = ComHelper.getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
            String tif = dto.getTemp() + File.separator + fileName + ".tif";
            ComHelper.Resample(ds, tif, sizes[0], sizes[1], layer);
            if (!new File(tif).exists()) continue;
 
            String png = waterPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
            water2Png(dto, layer, tif, png, sizes[0], sizes[1]);
        }
    }
 
    public void water2Png(ResultDto dto, LayerDto layer, String tif, String png, int width, int height) {
        Dataset ds = null;
        try {
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
 
            Band band = ds.GetRasterBand(1);
            float[] buffer = new float[width * height];
            band.ReadRaster(0, 0, width, height, buffer);
            double[] transform = ds.GetGeoTransform();
 
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            //double differ = layer.getWaters().getMaxHeight() - layer.getWaters().getMinHeight(), minHeight = layer.getWaters().getMinHeight();
            double differ = layer.getExtension().getDiffer(), maxHeight = layer.getExtension().getMaxHeight(), minHeight = layer.getExtension().getMinHeight();
            float[] ts = layer.getTerrain().getVals().get(width + "_" + height);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    int offset = x + y * width;
                    //if (Float.isNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
                    if (Float.isNaN(buffer[offset]) || buffer[offset] <= 0.001 || Float.isNaN(ts[offset])) continue;
 
                    //double X = transform[0] + x * transform[1] + y * transform[2];
                    //double Y = transform[3] + x * transform[4] + y * transform[5];
                    //BuildingDto building = intersects(dto, X, Y);
                    //if (null != building) continue;
 
                    int r = 0, g, b;
                    if (buffer[offset] + ts[offset] > maxHeight) {
                        g = b = 255;
                    } else {
                        int val = (int) ((buffer[offset] + ts[offset] - minHeight) / differ * 65535);
                        g = val / 256;
                        b = val % 256;
                    }
 
                    Color color = new Color(r, g, b, 127);
                    image.setRGB(x, y, color.getRGB());
                }
            }
            ComHelper.savePng(image, png);
        } finally {
            if (null != ds) ds.delete();
        }
    }
 
    public List<BuildingDepthVo> processBuilding(ResultDto dto, List<String> files, LayerDto layer) {
        List<BuildingDepthVo> list = new CopyOnWriteArrayList<>();
        for (int i = 0, c = files.size(); i < c; i++) {
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) continue;
                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
 
                copeBuildingDepth(dto, ds, layer.getWaters().getData().get(i), list);
            } finally {
                if (null != ds) ds.delete();
            }
        }
 
        return list;
    }
 
    public void copeBuildingDepth(ResultDto dto, Dataset ds, long ticks, List<BuildingDepthVo> list) {
        double[] transform = ds.GetGeoTransform();
        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]);
 
        for (BuildingDto building : dto.getBuildingList()) {
            Double val = getValue(ds, building, xSize, ySize, minX, maxY, pixelWidth, pixelHeight);
            list.add(new BuildingDepthVo(building.getId(), ticks, val));
        }
    }
 
    public Double getValue(Dataset ds, BuildingDto building, int xSize, int ySize, double minX, double maxY, double pixelWidth, double pixelHeight) {
        double[] env = new double[4];
        building.getGeom().GetEnvelope(env);
 
        int startX = (int) Math.floor((env[0] - minX) / pixelWidth);
        int endX = (int) Math.floor((env[1] - minX) / pixelWidth);
        int startY = (int) Math.floor((maxY - env[3]) / Math.abs(pixelHeight));
        int endY = (int) Math.floor((maxY - env[2]) / Math.abs(pixelHeight));
        if (startX < 0) startX = 0;
        if (startY < 0) startY = 0;
        if (endX > ds.getRasterXSize()) endX = ds.getRasterXSize();
        if (endY > ds.getRasterYSize()) endY = ds.getRasterYSize();
        if (endX - startX < 1 || endY - startY < 1) return null;
 
        int width = endX - startX;
        int height = endY - startY;
        double[] pixelValues = new double[width * height];
        ds.GetRasterBand(1).ReadRaster(startX, startY, width, height, pixelValues);
 
        Double val = Arrays.stream(pixelValues).max().getAsDouble();
 
        return ComHelper.isValid(val) ? val : null;
    }
 
    public void createVectors(ResultDto dto, Dataset ds, LayerDto layer, Long ticks) {
        String filePath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "polygonize.geojson";
        ShpHelper.polygonize2Geojson(ds, filePath);
 
        /*List<Geometry> geometries = getGeometries(filePath);
        if (CollectionUtils.isEmpty(geometries)) return;
 
        List<PondingPo> list = copePonding(dto, ds, layer, geometries);
        if (CollectionUtils.isEmpty(list)) return;
 
        try {
            filePath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.json";
            ComHelper.writeJson(filePath, JSON.toJSONString(list));
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }*/
    }
 
    public List<Geometry> getGeometries(String filePath) {
        if (!FileUtil.exist(filePath)) return null;
 
        Driver driver = null;
        DataSource dataSource = null;
        org.gdal.ogr.Layer layer = null;
        try {
            driver = ogr.GetDriverByName("GeoJSON");
            if (null == driver) return null;
 
            DataSource ds = driver.Open(filePath);
            if (null == ds) return null;
 
            layer = ds.GetLayer(0);
            List<Geometry> list = new ArrayList<>();
            for (long i = 0, d = layer.GetFeatureCount(); i < d; i++) {
                Feature f = layer.GetFeature(i);
                if (f.GetFieldAsDouble("val") < -999) continue;
 
                list.add(f.GetGeometryRef());
            }
 
            return list;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        } finally {
            GdalHelper.delete(layer, dataSource, driver);
        }
    }
 
    public List<PondingPo> copePonding(ResultDto dto, Dataset ds, LayerDto layer, List<Geometry> geometries) {
        double[] transform = ds.GetGeoTransform();
        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
 
        List<PondingPo> list = new ArrayList<>();
        for (Geometry geometry : geometries) {
            //List<PointDto> points = getValues(ds, geometry, transform, xSize, ySize);
            //if (CollectionUtils.isEmpty(points)) continue;
 
            //PointDto point = Collections.max(points);
            PointDto point = getValues(ds, geometry, transform, xSize, ySize);
            if (null == point) continue;
 
            list.add(new PondingPo(geometry, point));
        }
 
        return list;
    }
 
    public PointDto getValues(Dataset ds, Geometry g, double[] transform, int xSize, int ySize) {
        double[] env = new double[4];
        g.GetEnvelope(env);
 
        int startX = (int) Math.floor((env[0] - transform[0]) / transform[1]);
        int endX = (int) Math.floor((env[1] - transform[0]) / transform[1]);
        int startY = (int) Math.floor((transform[3] - env[3]) / Math.abs(transform[5]));
        int endY = (int) Math.floor((transform[3] - env[2]) / Math.abs(transform[5]));
        if (startX < 0) startX = 0;
        if (startY < 0) startY = 0;
        if (endX > ds.getRasterXSize()) endX = ds.getRasterXSize();
        if (endY > ds.getRasterYSize()) endY = ds.getRasterYSize();
        if (endX - startX < 1 || endY - startY < 1) return null;
 
        int width = endX - startX;
        int height = endY - startY;
        double[] pixelValues = new double[width * height];
        ds.GetRasterBand(1).ReadRaster(startX, startY, width, height, pixelValues);
 
        Double val = Arrays.stream(pixelValues).max().getAsDouble();
 
        return new PointDto(g.Centroid().GetX(), g.Centroid().GetY(), ComHelper.isValid(val) ? val : 0.0);
    }
 
    public void copeFlow(ResultDto dto, LayerDto layer) {
        List<String> files = getFiles(dto.getFlowPath(), ".tif");
        if (null == files || files.size() != layer.getWaters().getData().size()) return;
 
        for (int i = 0, c = files.size(); i < c; i++) {
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) return;
                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
 
                createFlowPng(dto, ds, layer, layer.getWaters().getData().get(i));
            } finally {
                if (null != ds) ds.delete();
            }
        }
    }
 
    public void createFlowPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
        String flowPath = dto.getOutPath() + File.separator + "flows" + File.separator + ticks;
        File dir = new File(flowPath);
        if (!dir.exists() || !dir.isDirectory()) dir.mkdirs();
 
        for (int[] sizes : layer.getTerrain().getSize()) {
            String name = ComHelper.getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
            String tif = dto.getTemp() + File.separator + name + ".tif";
            ComHelper.Resample(ds, tif, sizes[0], sizes[1], layer);
            if (!new File(tif).exists()) continue;
 
            String png = flowPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
            vxyTif2Png(layer, tif, png, sizes[0], sizes[1]);
        }
    }
 
    public void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) {
        Dataset ds = null;
        try {
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
 
            float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height];
            ds.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer);
            ds.GetRasterBand(2).ReadRaster(0, 0, width, height, vyBuffer);
 
            createFlowPng(vxBuffer, vyBuffer, png, width, height);
        } finally {
            if (null != ds) ds.delete();
        }
    }
 
    public void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int offset = x + y * width;
                float fx = ComHelper.getFloatValue(vxBuffer[offset]);
                float fy = ComHelper.getFloatValue(vyBuffer[offset]);
                if (Float.isNaN(fx) && Float.isNaN(fy) || (fx == 0 && fy == 0)) continue;
 
                fx = Float.isNaN(fx) ? 0 : fx;
                fy = Float.isNaN(fy) ? 0 : fy;
                double dr = Math.sqrt(Math.pow(fx, 2) + Math.pow(fy, 2));
 
                int r = (int) (dr / 4 * 255);
                int g = (int) ((fx / dr * 0.5 + 0.5) * 255);
                int b = (int) ((fy / dr * 0.5 + 0.5) * 255);
 
                Color color = new Color(ComHelper.getSafeValue(r), ComHelper.getSafeValue(g), ComHelper.getSafeValue(b), 127);
                image.setRGB(x, y, color.getRGB());
            }
        }
        ComHelper.savePng(image, png);
    }
 
    public void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException {
        layer.getWaters().setFiles(null);
        layer.getTerrain().setEpsg(null);
        layer.getExtension().setDiffer(null);
 
        String json = JSON.toJSONString(layer);
        // String json = JSONUtil.toJsonPrettyStr(layer);
        String filePath = dto.getOutPath() + File.separator + "layer.json";
 
        ComHelper.writeJson(filePath, json);
    }
}