Fxfw2SdeUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package com.onemap.analyse.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.onemap.analyse.domain.*;
  5. import com.onemap.analyse.domain.ESRI.EsriFeature;
  6. import com.onemap.analyse.domain.ESRI.EsriField;
  7. import com.onemap.analyse.domain.ESRI.EsriGeometry;
  8. import com.onemap.analyse.domain.ESRI.EsriJsonModel;
  9. import com.onemap.analyse.domain.GeoJSON.Geometry;
  10. import com.onemap.analyse.task.PythonExecute;
  11. import com.onemap.common.core.utils.StringUtils;
  12. import com.onemap.common.core.web.domain.RequestResult;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.stereotype.Component;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. @Component
  23. public class Fxfw2SdeUtils {
  24. public static String temp;
  25. public String getTemp() {
  26. return temp;
  27. }
  28. @Value("${Hgxfx.temp}")
  29. public void setTemp(String temp) {
  30. Fxfw2SdeUtils.temp = temp;
  31. }
  32. static FxfwDictDTO fxfwDictDTO = new FxfwDictDTO();
  33. //shp入库的方法标识码
  34. static String shpPyFun = "shp2sde";
  35. //esrijson入库的方法标识码
  36. static String esrijsonPyFun = "esriJson2sde";
  37. /**
  38. * 合规性分析范围数据入库
  39. *
  40. * @param hgxfxEntityDTO
  41. * @param requestHgxfxDTO
  42. * @return
  43. * @throws IOException
  44. */
  45. public static RequestResult vector2Sde(HgxfxEntityDTO hgxfxEntityDTO, HgxfxDTO requestHgxfxDTO) throws IOException {
  46. Integer fwlx = hgxfxEntityDTO.getFwlx();
  47. String vectorFilePath = "";
  48. String pyResult = "";
  49. if (fxfwDictDTO.getDraw().equals(fwlx)) {//自定义绘制范围
  50. EsriJsonModel esriModel = new EsriJsonModel();
  51. //字段汉化
  52. Map<String, String> fieldAliases = new HashMap<>();
  53. fieldAliases.put("RWBSM", "RWBSM");
  54. fieldAliases.put("RWLX", "RWLX");
  55. esriModel.setFieldAliases(fieldAliases);
  56. //geojson绘制范围转换
  57. String xzfw = hgxfxEntityDTO.getXzfw();
  58. if (xzfw.contains(";")) {//多个分析范围
  59. String[] xzfws = xzfw.split(";");
  60. //要素
  61. List<EsriFeature> features = new ArrayList<>();
  62. for (int i = 0; i < xzfws.length; i++) {
  63. if (StringUtils.isEmpty(xzfws[i])) {
  64. continue;
  65. }
  66. JSONObject geojson = new JSONObject(JSON.parseObject(xzfws[i]));
  67. Geometry geom = (Geometry) JSONObject.toJavaObject(geojson, Geometry.class);
  68. //字段信息
  69. List<EsriField> fields = new ArrayList<EsriField>();
  70. fields.add(new EsriField().init("RWBSM", "RWBSM"));
  71. fields.add(new EsriField().init("RWLX", "RWLX"));
  72. esriModel.setFields(fields);
  73. EsriFeature feature = new EsriFeature();
  74. Map<String, Object> attributes = new HashMap<>();
  75. attributes.put("RWBSM", requestHgxfxDTO.getBsm());
  76. attributes.put("RWLX", "合规性分析");
  77. feature.setAttributes(attributes);
  78. EsriGeometry geometry = new EsriGeometry();
  79. geometry.setRings(geom.getCoordinates());
  80. feature.setGeometry(geometry);
  81. features.add(feature);
  82. }
  83. esriModel.setFeatures(features);
  84. } else {//单个分析范围
  85. JSONObject geojson = new JSONObject(JSON.parseObject(hgxfxEntityDTO.getXzfw()));
  86. Geometry geom = (Geometry) JSONObject.toJavaObject(geojson, Geometry.class);
  87. //字段信息
  88. List<EsriField> fields = new ArrayList<EsriField>();
  89. fields.add(new EsriField().init("RWBSM", "RWBSM"));
  90. fields.add(new EsriField().init("RWLX", "RWLX"));
  91. esriModel.setFields(fields);
  92. //要素
  93. List<EsriFeature> features = new ArrayList<>();
  94. EsriFeature feature = new EsriFeature();
  95. Map<String, Object> attributes = new HashMap<>();
  96. attributes.put("RWBSM", requestHgxfxDTO.getBsm());
  97. attributes.put("RWLX", "合规性分析");
  98. feature.setAttributes(attributes);
  99. EsriGeometry geometry = new EsriGeometry();
  100. geometry.setRings(geom.getCoordinates());
  101. feature.setGeometry(geometry);
  102. features.add(feature);
  103. esriModel.setFeatures(features);
  104. }
  105. vectorFilePath = temp + "\\temp\\" + System.currentTimeMillis() + ".json";
  106. //写入文件
  107. FileOutputStream fos = null;
  108. String fileinput = JSON.toJSONString(esriModel);
  109. Boolean writeStatus = true;
  110. try {
  111. fos = new FileOutputStream(vectorFilePath, false);
  112. //true表示在文件末尾追加
  113. fos.write(fileinput.toString().getBytes());
  114. fos.close();
  115. } catch (FileNotFoundException e) {
  116. fos.close();
  117. e.printStackTrace();
  118. writeStatus = false;
  119. }
  120. if (!writeStatus) {
  121. return RequestResult.error("自定义范围数据转换失败", null);
  122. }
  123. Map<String, String> params = new HashMap<>();
  124. params.put("json", vectorFilePath);
  125. params.put("table", "KJGH.T_FZSS_FXRW_GIS");
  126. pyResult = PythonExecute.RunGisHelper(esrijsonPyFun, params);
  127. } else if (fxfwDictDTO.getShp().equals(fwlx)) {//shp文件
  128. vectorFilePath = hgxfxEntityDTO.getXzfw();
  129. List<Field> listField = new ArrayList<Field>();
  130. listField.add(new Field().init("RWBSM", requestHgxfxDTO.getBsm()));
  131. listField.add(new Field().init("RWLX", "合规性分析"));
  132. Map<String, Object> params = new HashMap<>();
  133. params.put("shpfile", vectorFilePath);
  134. params.put("table", "KJGH.T_FZSS_FXRW_GIS");
  135. params.put("fields", listField);
  136. pyResult = PythonExecute.RunGisHelper(shpPyFun, params);
  137. } else {
  138. return RequestResult.error("分析类型未定义", null);
  139. }
  140. if (pyResult.contains("####OK####")) {
  141. //return RequestResult.success("分析范围数据入库成功",1);
  142. } else if (pyResult.contains("####ERROR####")) {
  143. return RequestResult.error("分析范围数据入库失败", 0);
  144. }
  145. System.out.println("合规性分析范围要素插入成功!! RWBSM = " + requestHgxfxDTO.getBsm());
  146. return null;
  147. }
  148. /**
  149. * 辅助选址分析范围数据入库
  150. *
  151. * @param fzxzDTO
  152. * @return
  153. * @throws IOException
  154. */
  155. public static RequestResult fzxzvector2Sde(FzxzDTO fzxzDTO, String rwlx) throws IOException {
  156. Integer fwlx = fzxzDTO.getFwlx();
  157. rwlx = StringUtils.isEmpty(rwlx) ? "辅助选址" : rwlx;
  158. String vectorFilePath = "";
  159. String pyResult = "";
  160. if (fxfwDictDTO.getRegion().equals(fwlx)) {
  161. } else if (fxfwDictDTO.getDraw().equals(fwlx)) {//自定义绘制范围
  162. EsriJsonModel esriModel = new EsriJsonModel();
  163. //字段汉化
  164. Map<String, String> fieldAliases = new HashMap<>();
  165. fieldAliases.put("RWBSM", "RWBSM");
  166. fieldAliases.put("RWLX", "RWLX");
  167. esriModel.setFieldAliases(fieldAliases);
  168. //geojson绘制范围转换
  169. JSONObject geojson = new JSONObject(JSON.parseObject(fzxzDTO.getXzfw()));
  170. Geometry geom = (Geometry) JSONObject.toJavaObject(geojson, Geometry.class);
  171. //字段信息
  172. List<EsriField> fields = new ArrayList<EsriField>();
  173. fields.add(new EsriField().init("RWBSM", "RWBSM"));
  174. fields.add(new EsriField().init("RWLX", "RWLX"));
  175. esriModel.setFields(fields);
  176. //要素
  177. List<EsriFeature> features = new ArrayList<>();
  178. EsriFeature feature = new EsriFeature();
  179. Map<String, Object> attributes = new HashMap<>();
  180. attributes.put("RWBSM", fzxzDTO.getBsm());
  181. attributes.put("RWLX", rwlx);
  182. feature.setAttributes(attributes);
  183. EsriGeometry geometry = new EsriGeometry();
  184. geometry.setRings(geom.getCoordinates());
  185. feature.setGeometry(geometry);
  186. features.add(feature);
  187. esriModel.setFeatures(features);
  188. vectorFilePath = temp + "\\temp\\" + System.currentTimeMillis() + ".json";
  189. //写入文件
  190. FileOutputStream fos = null;
  191. String fileinput = JSON.toJSONString(esriModel);
  192. Boolean writeStatus = true;
  193. try {
  194. fos = new FileOutputStream(vectorFilePath, false);
  195. //true表示在文件末尾追加
  196. fos.write(fileinput.toString().getBytes());
  197. fos.close();
  198. } catch (FileNotFoundException e) {
  199. fos.close();
  200. e.printStackTrace();
  201. writeStatus = false;
  202. }
  203. if (!writeStatus) {
  204. return RequestResult.error("自定义范围数据转换失败", null);
  205. }
  206. Map<String, String> params = new HashMap<>();
  207. params.put("json", vectorFilePath);
  208. params.put("table", "KJGH.T_FZSS_FXRW_GIS");
  209. pyResult = PythonExecute.RunGisHelper(esrijsonPyFun, params);
  210. fzxzDTO.setXzfw("");
  211. } else if (fxfwDictDTO.getShp().equals(fwlx)) {//shp文件
  212. vectorFilePath = fzxzDTO.getXzfw();
  213. List<Field> listField = new ArrayList<Field>();
  214. listField.add(new Field().init("RWBSM", fzxzDTO.getBsm()));
  215. listField.add(new Field().init("RWLX", rwlx));
  216. Map<String, Object> params = new HashMap<>();
  217. params.put("shpfile", vectorFilePath);
  218. params.put("table", "KJGH.T_FZSS_FXRW_GIS");
  219. params.put("fields", listField);
  220. pyResult = PythonExecute.RunGisHelper(shpPyFun, params);
  221. fzxzDTO.setXzfw("");
  222. } else {
  223. return RequestResult.error("分析类型未定义", null);
  224. }
  225. if (pyResult.contains("####OK####")) {
  226. } else if (pyResult.contains("####ERROR####")) {
  227. return RequestResult.error("分析范围数据入库失败", 0);
  228. }
  229. System.out.println("辅助选址分析范围要素插入成功!! RWBSM = " + fzxzDTO.getBsm());
  230. return null;
  231. }
  232. }