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; 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; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.*; import java.util.stream.Collectors; /** * GEDB服务类 * * @author WWW * @date 2024-09-12 */ @Slf4j @Service @SuppressWarnings("ALL") public class GedbService { @Value("${sys.path.in}") String inPath; @Value("${gedb.host}") String host; @Value("${gedb.user}") String user; @Value("${gedb.pwd}") String pwd; @Value("${gedb.dbName}") String dbName; @Value("${gedb.pageSize}") Integer pageSize; @Value("#{'${gedb.layerNames}'}") List layerNames; @Value("#{'${gedb.shpNames}'}") List shpNames; @Value("#{'${gedb.sysFields}'}") List sysFields; @Value("${gedb.demFile}") String demFile; @Value("${gedb.demType}") String demType; @Value("${gedb.demName}") String demName; String password; @Resource RestTemplate restTemplate; public boolean test(String bbox, String taskName) throws Exception { String token = getToken(); GeDb db = getSeDb(token); db.setBbox(bbox); List layers = getLayers(token, db); queryData(token, db, layers); String basePath = inPath + File.separator + taskName; createPath(basePath); createShps(basePath, layers); GeDb fileDb = getFileDb(token); String fileId = getFileId(token, fileDb.getDbid()); List 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; } private void createPath(String basePath) { File f = new File(basePath); if (f.exists() && f.isDirectory()) { FileUtil.del(f); } f.mkdirs(); } public String getToken() throws Exception { Map map = new HashMap<>(2); map.put("userid", user); map.put("password", getPassword()); JSONObject obj = restTemplate.postForObject(host + "account-service/security/login", map, JSONObject.class); JSONObject data = obj.getJSONObject("data"); return data.getStr("token"); } public String getPassword() throws Exception { if (StringHelper.isEmpty(password)) { String key = getPublicKey(); RsaHelper.setPublicKey(key); password = RsaHelper.encrypt(pwd); } return password; } public String getPublicKey() { //{"datetime":"2024-09-12 17:24:38","code":200,"data":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtFwJCh2taVTEi05C8qT2oG7Y+rDmJhlO4zicpSeRtiro9LsytePeWI7BXM6sfDU0WeKun1izawcfgGkZgnoJuMBluAOKI1tL0uCrR+DreNLqMVtnXHwoWEIk/hGJedDWaf3q22aGDyEB5h9qCq0JklSShP1Ih4ppap4LmgxdTPQIDAQAB"} JSONObject obj = restTemplate.getForObject(host + "account-service/security/publickey", JSONObject.class); return obj.getStr("data"); } public GeDb getSeDb(String token) { Map 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 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 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); JSONObject data = obj.getJSONObject("data"); JSONArray arr = data.getJSONArray("layers"); if (null == arr || arr.size() == 0) return null; List 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 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 GeLayer(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, GeDb db, List layers) throws Exception { for (GeLayer layer : layers) { int count = getCount(token, db, layer); if (0 == count) throw new Exception(layer.getName() + ",图层数据为空"); int pageCount = (count - 1) / pageSize + 1; for (int i = 0; i < pageCount; i++) { JSONArray data = query(token, db, layer, i + 1, pageSize); if (null != data && data.size() > 0) { layer.addData(data); } } } } private int getCount(String token, GeDb db, GeLayer layer) { Map 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; return obj.getInt("data"); } private JSONArray query(String token, GeDb db, GeLayer layer, int start, int count) { Map map = new HashMap<>(9); map.put("token", token); map.put("start", start); map.put("count", count); map.put("dbid", db.getDbid()); map.put("bbox", db.getBbox()); 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("features"); } public void createShps(String basePath, List 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文件失败!"); } } } 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 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 String getFileId(String token, String dbid) { String uri = String.format("%sfile-service/doc/catagory/file/query?token=%s&dbid=%s&catagory=%s&count=%d&start=%d&like=", host, token, dbid, "image", 9999, 1); JSONObject obj = restTemplate.getForObject(uri, JSONObject.class); JSONArray items = obj.getJSONObject("data").getJSONArray("items"); for (int i = 0, c = items.size(); i < c; i++) { JSONObject jb = items.getJSONObject(i); if (demName.equals(jb.getStr("filename"))) { return jb.getStr("fileid"); } } return null; } public List 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, GeFile.class); } public void downloadFiles(String token, String path, List files, String dbid, String fileId) throws IOException { File f = new File(path); if (!f.exists() || !f.isDirectory()) { f.mkdirs(); } for (GeFile geFile : files) { String filePath = path + File.separator + geFile.getName(); f = new File(filePath); if (f.exists() && f.length() == geFile.getSize()) { continue; } 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, geFile.getName(), token, dbid, fileId); downloadFile(uri, filePath); } } private void downloadFile(String uri, String filePath) throws IOException { URL url = new URL(uri); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream(); byte[] buffer = new byte[1024]; FileOutputStream fs = new FileOutputStream(filePath); int read = 0, sum = 0; while ((read = is.read(buffer)) != -1) { sum += read; fs.write(buffer, 0, read); } fs.flush(); fs.close(); is.close(); } private void clipDemFile(String filePath, List 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(); } } }