1
13693261870
2024-09-14 c07a4aa6ecabddb15e0e4ca2a8c3524bf05d3e7a
src/main/java/com/se/simu/service/SedbService.java
@@ -1,5 +1,6 @@
package com.se.simu.service;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
@@ -7,14 +8,16 @@
import com.se.simu.domain.SeField;
import com.se.simu.domain.SeLayer;
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.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
@@ -28,6 +31,9 @@
@Service
@SuppressWarnings("ALL")
public class SedbService {
    @Value("${sys.path.in}")
    String inPath;
    @Value("${sedb.host}")
    String host;
@@ -46,16 +52,19 @@
    @Value("#{'${sedb.layerNames}'}")
    List<String> layerNames;
    @Value("#{'${sedb.shpNames}'}")
    List<String> shpNames;
    @Value("#{'${sedb.sysFields}'}")
    List<String> sysFields;
    String password;
    @Resource
    RestTemplate restTemplate;
    public String test() throws Exception {
    public boolean test() throws Exception {
        // 469538.6536261877,4416744.922022615,469853.14714664617,4417049.378602433
        String bbox = "116.64388473935195,39.884315914604464,116.64754729082588,39.887069143903496";
        String token = getToken();
@@ -63,10 +72,12 @@
        SeDb db = getSeDb(token);
        db.setBbox(bbox);
        List<SeLayer> layers = getLayers(token, db.getDbid());
        List<SeLayer> layers = getLayers(token, db);
        queryData(token, db, layers);
        return db.getDbid();
        createShps(inPath + "\\20240913", layers);
        return true;
    }
    public String getToken() throws Exception {
@@ -82,7 +93,7 @@
    }
    public String getPassword() throws Exception {
        if (StringUtils.isEmpty(password)) {
        if (StringHelper.isEmpty(password)) {
            String key = getPublicKey();
            RsaHelper.setPublicKey(key);
            password = RsaHelper.encrypt(pwd);
@@ -111,8 +122,8 @@
        return list.stream().filter(db -> dbName.equals(db.getName())).findFirst().orElse(null);
    }
    public List<SeLayer> getLayers(String token, String dbid) {
        String uri = String.format("%sgeo-service/entitydb/map/config?dbid=%s&token=%s", host, dbid, token);
    public List<SeLayer> getLayers(String token, SeDb 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);
        JSONObject data = obj.getJSONObject("data");
@@ -125,15 +136,30 @@
            String name = jb.getStr("name");
            if (layerNames.contains(name)) {
                String id = jb.getStr("id");
                Integer dataType = jb.getInt("_data_type");
                String queryType = getQueryType(jb);
                List<SeField> fields = JSONUtil.toList(jb.getJSONArray("fields"), SeField.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, dataType, fields));
                layers.add(new SeLayer(id, name, queryType, fields, shpName, db));
            }
        }
        return layers;
    }
    private String getQueryType(JSONObject jb) {
        String qt = jb.getStr("pointlod");
        if (!StringHelper.isEmpty(qt)) {
            return "point";
        }
        qt = jb.getStr("polylinelod");
        if (!StringHelper.isEmpty(qt)) {
            return "polyline";
        }
        return "polygon";
    }
    public void queryData(String token, SeDb db, List<SeLayer> layers) throws Exception {
@@ -151,12 +177,14 @@
        }
    }
    public int getCount(String token, SeDb db, SeLayer layer) {
        Map<String, Object> map = new HashMap<>(4);
    private int getCount(String token, SeDb db, SeLayer layer) {
        Map<String, Object> map = new HashMap<>(6);
        map.put("token", token);
        map.put("dbid", db.getDbid());
        map.put("bbox", db.getBbox());
        map.put("layerid", layer.getId());
        map.put("returnCountOnly", true);
        map.put("inSR", 4326);
        JSONObject obj = restTemplate.postForObject(host + "geo-service/entitydbdata/layer/query", map, JSONObject.class);
        if (null == obj || 200 != obj.getInt("code")) return 0;
@@ -164,8 +192,8 @@
        return obj.getInt("data");
    }
    public JSONArray query(String token, SeDb db, SeLayer layer, int start, int count) {
        Map<String, Object> map = new HashMap<>(8);
    private JSONArray query(String token, SeDb db, SeLayer layer, int start, int count) {
        Map<String, Object> map = new HashMap<>(9);
        map.put("token", token);
        map.put("start", start);
        map.put("count", count);
@@ -174,12 +202,28 @@
        map.put("containCount", false);
        map.put("layerid", layer.getId());
        map.put("querytype", layer.getQueryType());
        map.put("inSR", 4326);
        JSONObject obj = restTemplate.postForObject(host + "geo-service/entitydbdata/layer/query", map, JSONObject.class);
        if (null == obj || 200 != obj.getInt("code")) return null;
        JSONObject data = obj.getJSONObject("data");
        return data.getJSONArray("items");
        return data.getJSONArray("features");
    }
    public void createShps(String basePath, List<SeLayer> layers) throws Exception {
        File f = new File(basePath);
        if (f.exists() && f.isDirectory()) {
            FileUtil.del(f);
        }
        f.mkdirs();
        for (SeLayer layer : layers) {
            String path = String.format("%s\\%s.shp", basePath, layer.getShpName());
            if (!ShpHelper.createShp(path, layer)) {
                throw new Exception(layer.getName() + ",创建ShapeFile文件失败!");
            }
        }
    }
}