月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-17 796b44ea813a1133beae4f3a67f1c0263510c0c7
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
package com.moon.server.service.show;
 
import com.moon.server.entity.all.StaticData;
import com.moon.server.entity.data.DownloadEntity;
import com.moon.server.entity.show.PipelineEntity;
import com.moon.server.entity.sys.UserEntity;
import com.moon.server.mapper.data.DownloadMapper;
import com.moon.server.mapper.show.PipelineMapper;
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.gdal.ogr.*;
import org.gdal.osr.SpatialReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Service
@SuppressWarnings("ALL")
public class PipelineService implements PipelineMapper {
    @Autowired
    PathHelper pathHelper;
 
    @Autowired
    DownloadMapper downloadMapper;
 
    @Autowired
    PipelineMapper pipelineMapper;
 
    private final static Log log = LogFactory.getLog(PipelineService.class);
 
    @Override
    public List<PipelineEntity> selectPipelines(String name) {
        name = StringHelper.getLikeUpperStr(name);
 
        return pipelineMapper.selectPipelines(name);
    }
 
    @Override
    public List<PipelineEntity> selectSegNames() {
        return pipelineMapper.selectSegNames();
    }
 
    @Override
    public List<PipelineEntity> selectPipeAnalysis(String tab, Integer gid) {
        return pipelineMapper.selectPipeAnalysis(tab, gid);
    }
 
    public String createZipFile(UserEntity ue, Map<String, List<PipelineEntity>> map, String pwd) throws Exception {
        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
        String tempPath = pathHelper.getTempPath(tempName);
        String filePath = tempPath + File.separator + tempName + ".gdb";
 
        File file = new File(filePath);
        if (file.exists() && file.isDirectory()) {
            FileHelper.deleteDir(filePath);
        }
        createGdb(filePath, map);
 
        String zipName = tempName + ".gdb.zip";
        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
 
        ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd);
        ZipParameters params = Zip4jHelper.getZipParams(true);
        addZipFiles(zip, params, file.listFiles());
 
        String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd));
        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
        int rows = downloadMapper.insert(downloadEntity);
 
        return rows > 0 ? downloadEntity.getGuid() : null;
    }
 
    public static void createGdb(String filePath, Map<String, List<PipelineEntity>> map) throws Exception {
        Driver driver = null;
        DataSource dataSource = null;
        try {
            driver = ogr.GetDriverByName("FileGDB");
            if (null == driver) {
                log.error("PipelineService.createGdb.driver(FileGDB) is null.");
                return;
            }
            dataSource = driver.CreateDataSource(filePath, null);
            if (null == dataSource) {
                log.error("PipelineService.createGdb.dataSource is null. " + filePath);
                return;
            }
 
            for (String key : map.keySet()) {
                Layer layer = null;
                try {
                    List<PipelineEntity> list = map.get(key);
                    layer = createLayer(dataSource, key.replace(".", "_"), list.get(0));
 
                    List<Field> fields = new ArrayList<>();
                    getFields(PipelineEntity.class, fields);
 
                    GdbHelper.addLayerField(layer, fields);
                    setLayerData(layer, fields, list);
                } finally {
                    if (null != layer) {
                        layer.delete();
                    }
                }
            }
 
            dataSource.SyncToDisk();
            dataSource.FlushCache();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw ex;
        } finally {
            GdbHelper.delete(dataSource, driver);
        }
    }
 
    private static Layer createLayer(DataSource dataSource, String tab, PipelineEntity pe) {
        int geomType = pe.getWkt().contains("POINT") ? ogr.wkbMultiPoint : ogr.wkbMultiLineString;
 
        SpatialReference sr = new SpatialReference();
        sr.ImportFromEPSG(4490);
 
        return dataSource.CreateLayer(tab, sr, geomType, null);
    }
 
    private static void getFields(Class clazz, List<Field> list) {
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field f : fields) {
                if (StaticData.PIPE_EXCLUDE_FIELDS.contains(f.getName())) {
                    continue;
                }
 
                f.setAccessible(true);
                list.add(f);
            }
 
            if (!StaticData.OBJECT.equals(clazz.getSuperclass().getName())) {
                getFields(clazz.getSuperclass(), list);
            }
        } catch (Exception ex) {
            //
        }
    }
 
    private static void setLayerData(Layer layer, List<Field> fields, List<PipelineEntity> list) throws Exception {
        for (PipelineEntity t : list) {
            Feature f = new Feature(layer.GetLayerDefn());
 
            String wkt = t.getWkt();
            if (!wkt.contains("MULTI")) {
                wkt = wkt.replace("POINT", "MULTIPOINT").replace("LINESTRING", "MULTILINESTRING(") + (wkt.contains("LINESTRING") ? ")" : "");
            }
 
            Geometry geom = Geometry.CreateFromWkt(wkt);
            f.SetGeometry(geom);
 
            GdbHelper.setFeatureData(f, fields, t);
            layer.CreateFeature(f);
        }
    }
 
    private void addZipFiles(ZipFile zip, ZipParameters params, File[] files) {
        if (null == files || files.length == 0) {
            return;
        }
 
        for (File f : files) {
            try {
                zip.addFile(f, params);
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
    }
 
    private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) throws Exception {
        DownloadEntity de = new DownloadEntity();
        de.setName(FileHelper.getFileName(file));
        // 1-Shp文件,2-专题图,3-元数据,4-业务数据,5-管道分析,6-统计报告,7-附件文件,8-瓦片文件
        de.setType(5);
        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;
    }
}