| | |
| | | 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.SeDb; |
| | | import com.se.simu.domain.SeField; |
| | | import com.se.simu.domain.SeFile; |
| | | 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.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; |
| | | |
| | |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | | public class SedbService { |
| | | @Value("${sys.path.in}") |
| | | String inPath; |
| | | |
| | | @Value("${sedb.host}") |
| | | String host; |
| | | |
| | |
| | | @Value("#{'${sedb.layerNames}'}") |
| | | List<String> layerNames; |
| | | |
| | | @Value("#{'${sedb.shpNames}'}") |
| | | List<String> shpNames; |
| | | |
| | | @Value("#{'${sedb.sysFields}'}") |
| | | List<String> sysFields; |
| | | |
| | | String password; |
| | | @Value("${sedb.demName}") |
| | | String demName; |
| | | |
| | | String password; |
| | | |
| | | @Resource |
| | | RestTemplate restTemplate; |
| | | |
| | | public String test() throws Exception { |
| | | String bbox = "116.64388473935195,39.884315914604464,116.64754729082588,39.887069143903496"; |
| | | |
| | | public boolean test(String bbox, String taskName) throws Exception { |
| | | String token = getToken(); |
| | | |
| | | 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(); |
| | | String basePath = inPath + File.separator + taskName; |
| | | createPath(basePath); |
| | | createShps(basePath, layers); |
| | | |
| | | SeDb fileDb = getFileDb(token); |
| | | String fileId = getFileId(token, fileDb.getDbid()); |
| | | List<SeFile> files = getFileNames(token, fileDb.getDbid(), fileId); |
| | | |
| | | String filePath = inPath + File.separator + fileDb.getDbid(); |
| | | downloadFiles(token, filePath, files, fileDb.getDbid(), fileId); |
| | | |
| | | 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 { |
| | |
| | | } |
| | | |
| | | public String getPassword() throws Exception { |
| | | if (StringUtils.isEmpty(password)) { |
| | | if (StringHelper.isEmpty(password)) { |
| | | String key = getPublicKey(); |
| | | RsaHelper.setPublicKey(key); |
| | | password = RsaHelper.encrypt(pwd); |
| | |
| | | List<SeDb> list = JSONUtil.toList(data, SeDb.class); |
| | | if (CollectionUtils.isEmpty(list)) return null; |
| | | |
| | | return list.stream().filter(db -> dbName.equals(db.getName())).findFirst().orElse(null); |
| | | return list.stream().filter(db -> null != db.getName() && db.getName().contains(dbName)).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"); |
| | |
| | | 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 { |
| | |
| | | } |
| | | } |
| | | |
| | | public int getCount(String token, SeDb db, SeLayer layer) { |
| | | 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()); |
| | |
| | | return obj.getInt("data"); |
| | | } |
| | | |
| | | public JSONArray query(String token, SeDb db, SeLayer layer, int start, int count) { |
| | | 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); |
| | |
| | | |
| | | return data.getJSONArray("features"); |
| | | } |
| | | |
| | | public void createShps(String basePath, List<SeLayer> layers) throws Exception { |
| | | 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文件失败!"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public SeDb 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); |
| | | 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<SeFile> 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); |
| | | } |
| | | |
| | | public void downloadFiles(String token, String path, List<SeFile> 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(); |
| | | f = new File(filePath); |
| | | if (f.exists() && f.length() == seFile.getSize()) { |
| | | continue; |
| | | } |
| | | if (f.exists() && f.length() < seFile.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); |
| | | 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(); |
| | | } |
| | | } |