1
13693261870
2024-09-18 19df007763d19dd6fa16ac44048e1dbf368186db
src/main/java/com/se/simu/service/SedbService.java
@@ -4,14 +4,17 @@
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.se.simu.domain.SeDb;
import com.se.simu.domain.SeField;
import com.se.simu.domain.SeFile;
import com.se.simu.domain.SeLayer;
import com.se.simu.domain.dto.GeDb;
import com.se.simu.domain.dto.GeField;
import com.se.simu.domain.dto.GeFile;
import com.se.simu.domain.dto.GeLayer;
import com.se.simu.helper.RsaHelper;
import com.se.simu.helper.ShpHelper;
import com.se.simu.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@@ -64,6 +67,12 @@
    @Value("#{'${sedb.sysFields}'}")
    List<String> sysFields;
    @Value("${sedb.demFile}")
    String demFile;
    @Value("${sedb.demType}")
    String demType;
    @Value("${sedb.demName}")
    String demName;
@@ -74,22 +83,23 @@
    public boolean test(String bbox, String taskName) throws Exception {
        String token = getToken();
        SeDb db = getSeDb(token);
        GeDb db = getSeDb(token);
        db.setBbox(bbox);
        List<SeLayer> layers = getLayers(token, db);
        List<GeLayer> layers = getLayers(token, db);
        queryData(token, db, layers);
        String basePath = inPath + File.separator + taskName;
        createPath(basePath);
        createShps(basePath, layers);
        SeDb fileDb = getFileDb(token);
        GeDb fileDb = getFileDb(token);
        String fileId = getFileId(token, fileDb.getDbid());
        List<SeFile> files = getFileNames(token, fileDb.getDbid(), fileId);
        List<GeFile> files = getFileNames(token, fileDb.getDbid(), fileId);
        String filePath = inPath + File.separator + fileDb.getDbid();
        downloadFiles(token, filePath, files, fileDb.getDbid(), fileId);
        clipDemFile(filePath, files, basePath, bbox);
        return true;
    }
@@ -131,20 +141,20 @@
        return obj.getStr("data");
    }
    public SeDb getSeDb(String token) {
    public GeDb getSeDb(String token) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("token", token);
        JSONObject obj = restTemplate.postForObject(host + "geo-service/entitydb/list/canview", map, JSONObject.class);
        JSONArray data = obj.getJSONArray("data");
        List<SeDb> list = JSONUtil.toList(data, SeDb.class);
        List<GeDb> list = JSONUtil.toList(data, GeDb.class);
        if (CollectionUtils.isEmpty(list)) return null;
        return list.stream().filter(db -> null != db.getName() && db.getName().contains(dbName)).findFirst().orElse(null);
    }
    public List<SeLayer> getLayers(String token, SeDb db) {
    public List<GeLayer> getLayers(String token, GeDb db) {
        String uri = String.format("%sgeo-service/entitydb/map/config?dbid=%s&token=%s", host, db.getDbid(), token);
        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
@@ -152,18 +162,18 @@
        JSONArray arr = data.getJSONArray("layers");
        if (null == arr || arr.size() == 0) return null;
        List<SeLayer> layers = new ArrayList<>();
        List<GeLayer> layers = new ArrayList<>();
        for (int i = 0, c = arr.size(); i < c; i++) {
            JSONObject jb = arr.getJSONObject(i);
            String name = jb.getStr("name");
            if (layerNames.contains(name)) {
                String id = jb.getStr("id");
                String queryType = getQueryType(jb);
                List<SeField> fields = JSONUtil.toList(jb.getJSONArray("fields"), SeField.class);
                List<GeField> fields = JSONUtil.toList(jb.getJSONArray("fields"), GeField.class);
                fields = fields.stream().filter(f -> !sysFields.contains(f.getName())).collect(Collectors.toList());
                String shpName = shpNames.get(layerNames.indexOf(name));
                layers.add(new SeLayer(id, name, queryType, fields, shpName, db));
                layers.add(new GeLayer(id, name, queryType, fields, shpName, db));
            }
        }
@@ -184,8 +194,8 @@
        return "polygon";
    }
    public void queryData(String token, SeDb db, List<SeLayer> layers) throws Exception {
        for (SeLayer layer : layers) {
    public void queryData(String token, GeDb db, List<GeLayer> layers) throws Exception {
        for (GeLayer layer : layers) {
            int count = getCount(token, db, layer);
            if (0 == count) throw new Exception(layer.getName() + ",图层数据为空");
@@ -199,7 +209,7 @@
        }
    }
    private int getCount(String token, SeDb db, SeLayer layer) {
    private int getCount(String token, GeDb db, GeLayer layer) {
        Map<String, Object> map = new HashMap<>(6);
        map.put("token", token);
        map.put("dbid", db.getDbid());
@@ -214,7 +224,7 @@
        return obj.getInt("data");
    }
    private JSONArray query(String token, SeDb db, SeLayer layer, int start, int count) {
    private JSONArray query(String token, GeDb db, GeLayer layer, int start, int count) {
        Map<String, Object> map = new HashMap<>(9);
        map.put("token", token);
        map.put("start", start);
@@ -234,8 +244,8 @@
        return data.getJSONArray("features");
    }
    public void createShps(String basePath, List<SeLayer> layers) throws Exception {
        for (SeLayer layer : layers) {
    public void createShps(String basePath, List<GeLayer> layers) throws Exception {
        for (GeLayer layer : layers) {
            String path = String.format("%s\\%s.shp", basePath, layer.getShpName());
            if (!ShpHelper.createShp(path, layer)) {
                throw new Exception(layer.getName() + ",创建ShapeFile文件失败!");
@@ -243,12 +253,12 @@
        }
    }
    public SeDb getFileDb(String token) {
    public GeDb getFileDb(String token) {
        String uri = String.format("%sfile-service/docdb/query/canview?token=%s", host, token);
        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
        JSONArray data = obj.getJSONArray("data");
        List<SeDb> list = JSONUtil.toList(data, SeDb.class);
        List<GeDb> list = JSONUtil.toList(data, GeDb.class);
        if (CollectionUtils.isEmpty(list)) return null;
        return list.stream().filter(db -> null != db.getName() && db.getName().contains(dbName)).findFirst().orElse(null);
@@ -271,34 +281,34 @@
        return null;
    }
    public List<SeFile> getFileNames(String token, String dbid, String fileId) {
    public List<GeFile> getFileNames(String token, String dbid, String fileId) {
        String uri = String.format("%sfile-service/doc/cluster/struct/list?token=%s&dbid=%s&cluster_fileid=%s&onlychild=true&folder_stairs=",
                host, token, dbid, fileId);
        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
        JSONArray data = obj.getJSONArray("data");
        return JSONUtil.toList(data, SeFile.class);
        return JSONUtil.toList(data, GeFile.class);
    }
    public void downloadFiles(String token, String path, List<SeFile> files, String dbid, String fileId) throws IOException {
    public void downloadFiles(String token, String path, List<GeFile> files, String dbid, String fileId) throws IOException {
        File f = new File(path);
        if (!f.exists() || !f.isDirectory()) {
            f.mkdirs();
        }
        for (SeFile seFile : files) {
            String filePath = path + File.separator + seFile.getName();
        for (GeFile geFile : files) {
            String filePath = path + File.separator + geFile.getName();
            f = new File(filePath);
            if (f.exists() && f.length() == seFile.getSize()) {
            if (f.exists() && f.length() == geFile.getSize()) {
                continue;
            }
            if (f.exists() && f.length() < seFile.getSize()) {
            if (f.exists() && f.length() < geFile.getSize()) {
                f.delete();
            }
            String uri = String.format("%sfile-service/fileparser/cluster/download/%s?token=%s&dbid=%s&cluster_fileid=%s",
                    host, seFile.getName(), token, dbid, fileId);
                    host, geFile.getName(), token, dbid, fileId);
            downloadFile(uri, filePath);
        }
    }
@@ -321,4 +331,27 @@
        fs.close();
        is.close();
    }
    private void clipDemFile(String filePath, List<GeFile> files, String basePath, String bbox) throws Exception {
        String target = basePath + File.separator + demName;
        for (GeFile file : files) {
            if (file.getName().toLowerCase().endsWith(demType)) {
                String source = filePath + File.separator + file.getName();
                clipDem(source, target, bbox);
                break;
            }
        }
    }
    private void clipDem(String source, String target, String bbox) throws Exception {
        Dataset ds = null;
        try {
            ds = gdal.Open(source, gdalconst.GA_ReadOnly);
            if (null == ds || ds.getRasterCount() < 1 || null == ds.GetSpatialRef()) throw new Exception("DEM数据无效");
        } finally {
            if (null != ds) ds.delete();
        }
    }
}