月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-08-14 e002c67732b571f0b20cca8321ca8ee1ddba2e05
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
package com.moon.server.service.show;
 
import com.moon.server.entity.all.StaticData;
import com.moon.server.entity.ctrl.DownloadTileEntity;
import com.moon.server.entity.ctrl.ShpRecordEntity;
import com.moon.server.entity.data.DownloadEntity;
import com.moon.server.entity.data.MetaFileEntity;
import com.moon.server.entity.data.PublishEntity;
import com.moon.server.entity.sys.UserEntity;
import com.moon.server.service.data.DownloadService;
import com.moon.server.service.data.PublishService;
import com.moon.server.helper.*;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;
 
    @Resource
    PublishService publishService;
 
    @Resource
    DownloadService downloadService;
 
    private final static Log log = LogFactory.getLog(InquiryService.class);
 
    /**
     * 读取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);
 
        String dbPwd = Md5Helper.reverse(Md5Helper.generate(dt.getPwd()));
        DownloadEntity de = getDownloadEntity(ue, zipFile, dbPwd);
        int rows = downloadService.insert(de);
        if (de.getId() > 0) {
            insertPubDown(pub, de, ue);
        }
 
        return rows > 0 ? de.getGuid() : null;
    }
 
    /**
     * 瓦片路径是否存在
     */
    private boolean isTilePathExist(PublishEntity pub) {
        String tilePath = pathHelper.getConfig().getTilePath() + 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<>();
 
        File view = new File(pub.getPath() + File.separator + "view.htm");
        if (view.exists() && !view.isDirectory()) {
            list.add(view);
        }
 
        for (int i = 0; i < StaticData.I23; i++) {
            List<File> files = findTilesByZoom(dt, pub, i);
            if (files.size() > 0) {
                list.addAll(files);
            }
        }
 
        return list;
    }
 
    /**
     * 根据层次查找瓦片
     */
    private List<File> findTilesByZoom(DownloadTileEntity dt, PublishEntity pub, int zoom) {
        List<File> list = new ArrayList<>();
        File f = new File(pub.getPath() + File.separator + zoom);
        if (!f.exists() || !f.isDirectory()) {
            return list;
        }
 
        int[] leftTop = deg2num(zoom, dt.getXmin(), dt.getYmax());
        int[] rightBottom = deg2num(zoom, dt.getXmax(), dt.getYmin());
 
        for (int x = leftTop[0]; x <= rightBottom[0]; x++) {
            for (int y = leftTop[1]; y <= rightBottom[1]; y++) {
                String pngPath = String.format("%s\\%d\\%d\\%d.png", pub.getPath(), zoom, x, y);
 
                File pngFile = new File(pngPath);
                if (pngFile.exists()) {
                    list.add(pngFile);
                }
            }
        }
 
        return list;
    }
 
    /**
     * 角度转数值
     */
    private int[] deg2num(int zoom, double x, double y) {
        double yRad = Math.toRadians(y);
        double n = Math.pow(2.0, zoom);
 
        int xTile = (int) ((x + 180.0) / 360.0 * n);
        int yTile = (int) ((1.0 - Math.log(Math.tan(yRad) + (1 / Math.cos(yRad))) / Math.PI) / 2.0 * n);
 
        return new int[]{xTile, yTile};
    }
 
    /**
     * 添加文件至压缩包
     */
    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 ex) {
                log.error(ex.getMessage(), ex);
            }
        }
    }
 
    /**
     * 获取下载实体类
     */
    private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) {
        DownloadEntity de = new DownloadEntity();
        de.setName(FileHelper.getFileName(file));
        // 1-Shp文件,2-专题图,3-元数据,4-业务数据,5-管道分析,6-统计报告,7-附件文件,8-瓦片文件
        de.setType(8);
        de.setSizes(FileHelper.sizeToMb(new File(file).length()));
        de.setDepid(ue.getDepid());
        de.setDcount(0);
        de.setPwd(pwd);
        de.setUrl(FileHelper.getRelativePath(file));
        de.setDescr("瓦片文件");
        de.setGuid(FileHelper.getFileMd5(file));
        de.setCreateUser(ue.getId());
        // de.setGeom(null)
 
        return de;
    }
 
    /**
     * 插入数据发布-下载表
     */
    private void insertPubDown(PublishEntity pub, DownloadEntity de, UserEntity ue) {
        publishService.insertPubDown(pub.getId(), de.getId(), ue.getId());
    }
}