管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-04-23 0aee44b467e8afb547cf775da687c6b3ff4fb239
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
package com.lf.server.service.show;
 
import com.lf.server.entity.all.StaticData;
import com.lf.server.entity.ctrl.DownloadTileEntity;
import com.lf.server.entity.ctrl.ShpRecordEntity;
import com.lf.server.entity.data.MetaFileEntity;
import com.lf.server.entity.data.PublishEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.*;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 查询服务类
 * @author WWW
 */
@Service
public class InquiryService {
    @Resource
    PathHelper pathHelper;
 
    /**
     * 读取Shp第一条记录的WKT
     */
    public ShpRecordEntity readShpFirstRecord(List<MetaFileEntity> list) {
        String fileName = null;
        for (MetaFileEntity mf : list) {
            if (mf.getName().toLowerCase().contains(".shp")) {
                fileName = mf.getPath();
                break;
            }
        }
        if (StringHelper.isEmpty(fileName)) {
            return null;
        }
 
        ShpRecordEntity sr = ShpHelper.readShpFirstRecord(fileName);
        FileHelper.deleteFiles(list);
 
        return sr;
    }
 
    /**
     * 打包瓦片
     */
    public String zipTiles(DownloadTileEntity dt, PublishEntity pub, UserEntity ue) {
        if (!isTilePathExist(pub)) {
            return null;
        }
 
        List<File> list = findTiles(dt, pub);
        if (list.isEmpty()) {
            return null;
        }
 
        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
        String zipFile = pathHelper.getDownloadFullPath() + File.separator + tempName + ".zip";
 
        ZipFile zip = Zip4jHelper.createZipFile(zipFile, dt.getPwd());
        ZipParameters params = Zip4jHelper.getZipParams(true);
        addFiles(zip, params, list, pub.getPath() + File.separator);
 
        return "aaa";
    }
 
    /**
     * 瓦片路径是否存在
     */
    private boolean isTilePathExist(PublishEntity pub) {
        String tilePath = pathHelper.getConfig().getTilePath().replace("2d\\tiles", "") + File.separator + pub.getPath();
 
        File f = new File(tilePath);
        if (!f.exists() || !f.isDirectory()) {
            return false;
        }
 
        pub.setPath(tilePath);
 
        return true;
    }
 
    /**
     * 查找瓦片
     */
    private List<File> findTiles(DownloadTileEntity dt, PublishEntity pub) {
        List<File> list = new ArrayList<>();
        for (int i = 0; i < StaticData.I23; i++) {
            findTilesByZoom(dt, pub, i, list);
        }
 
        return list;
    }
 
    /**
     * 根据层次查找瓦片
     */
    private void findTilesByZoom(DownloadTileEntity dt, PublishEntity pub, int zoom, List<File> list) {
        File f = new File(pub.getPath() + File.separator + zoom);
        if (!f.exists() || !f.isDirectory()) {
            return;
        }
 
        int x = 0;
        int y = 0;
        // 关键算法
 
        String pngPath = String.format("%s\\%d\\%d.png", pub.getPath(), x, y);
        File pngFile = new File(pngPath);
        if (pngFile.exists()) {
            list.add(pngFile);
        }
    }
 
    /**
     * 添加文件至压缩包
     */
    private void addFiles(ZipFile zip, ZipParameters params, List<File> list, String basePath) {
        for (File f : list) {
            try {
                params.setFileNameInZip(f.getPath().replace(basePath, ""));
                zip.addStream(new FileInputStream(f), params);
            } catch (Exception e) {
                //
            }
        }
    }
}