package com.terra.system.service.show; import com.terra.system.entity.all.StaticData; import com.terra.system.entity.data.DownloadEntity; import com.terra.system.entity.show.PipelineEntity; import com.terra.system.entity.sys.UserEntity; import com.terra.system.helper.*; import com.terra.system.mapper.data.DownloadMapper; import com.terra.system.mapper.show.PipelineMapper; 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 javax.annotation.Resource; 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; /** * 管道分析服务类 * @author WWW */ @Service public class PipelineService implements PipelineMapper { @Resource PathHelper pathHelper; @Resource DownloadMapper downloadMapper; @Resource PipelineMapper pipelineMapper; private final static Log log = LogFactory.getLog(PipelineService.class); @Override public List selectPipelines(String name) { name = StringHelper.getLikeUpperStr(name); return pipelineMapper.selectPipelines(name); } @Override public List selectSegNames() { return pipelineMapper.selectSegNames(); } @Override public List selectPipeAnalysis(String tab, Integer gid) { return pipelineMapper.selectPipeAnalysis(tab, gid); } /** * 创建Zip包 * * @param ue 用户实体 * @param map 管道分析数据集合 * @param pwd 密码 * @return 下载文件GUID */ public String createZipFile(UserEntity ue, Map> 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; } /** * 创建GDB */ public static void createGdb(String filePath, Map> 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 list = map.get(key); layer = createLayer(dataSource, key.replace(".", "_"), list.get(0)); List 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 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 fields, List 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); } } /** * 添加Zip文件 */ 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; } }