AreaService.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. package com.skyversation.poiaddr.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.skyversation.poiaddr.addquery.AddressQueryEngine;
  6. import com.skyversation.poiaddr.addquery.Constant;
  7. import com.skyversation.poiaddr.bean.AddressResult;
  8. import com.skyversation.poiaddr.bean.GeoJsonBean;
  9. import com.skyversation.poiaddr.bean.Village;
  10. import com.skyversation.poiaddr.bean.Zerenwangluo;
  11. import com.skyversation.poiaddr.util.CoordTransform;
  12. import com.skyversation.poiaddr.util.status.AddressLevel;
  13. import org.geotools.geojson.geom.GeometryJSON;
  14. import org.geotools.geometry.jts.JTSFactoryFinder;
  15. import org.locationtech.jts.geom.Geometry;
  16. import org.locationtech.jts.geom.MultiPolygon;
  17. import org.locationtech.jts.io.ParseException;
  18. import org.locationtech.jts.io.WKTReader;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.core.io.ClassPathResource;
  21. import org.springframework.stereotype.Service;
  22. import javax.annotation.PostConstruct;
  23. import java.io.*;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. @Service
  27. public class AreaService {
  28. private static AreaService instance;
  29. public static AreaService getInstance() {
  30. if (instance == null) {
  31. return new AreaService();
  32. }
  33. return instance;
  34. }
  35. @Value("${app.area}")
  36. private String area;
  37. @Value("${app.town}")
  38. private String town;
  39. WKTReader reader = new WKTReader(JTSFactoryFinder.getGeometryFactory());
  40. List<JSONObject> featureList = new ArrayList<>();
  41. List<Village> propertiList = new ArrayList<>();
  42. List<MultiPolygon> mulPolygonList = new ArrayList<>();
  43. Zerenwangluo zrwl = null;
  44. List<Zerenwangluo.FeaturesDTO> zrwlFeatures = null;
  45. List<Zerenwangluo.FeaturesDTO.AttributesDTO> zrwlProperties = new ArrayList<>();
  46. List<MultiPolygon> zrwlMulPolygonList = new ArrayList<>();
  47. public List<GeoJsonBean> cjPolygonList = new ArrayList<>();
  48. public List<GeoJsonBean> wGridPolygonList = new ArrayList<>();
  49. public List<GeoJsonBean> gridPolygonList = new ArrayList<>();
  50. public List<GeoJsonBean> shAllAdministrativeDivisionPolygonList = new ArrayList<>();
  51. public List<GeoJsonBean> shAllToensPolygonList = new ArrayList<>();
  52. @PostConstruct
  53. public void initGeoData() {
  54. System.out.println("<<<<<<<<------AreaService>initGeoData------>>>>>>>>>");
  55. instance = this;
  56. if ("青浦".equals(area)) {
  57. Thread thread1 = new Thread(this::runGeoData);
  58. Thread thread2 = new Thread(this::runZEWLData);
  59. thread1.start();
  60. thread2.start();
  61. try {
  62. // 等待三个线程执行完毕
  63. if ("青浦".equals(area)) {
  64. thread1.join();
  65. thread2.join();
  66. }
  67. } catch (InterruptedException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. Thread thread3 = new Thread(this::initPolygons);
  72. thread3.start();
  73. try {
  74. thread3.join();
  75. } catch (InterruptedException e) {
  76. e.printStackTrace();
  77. }
  78. // TODO 初始化tableA、tableB
  79. /*testTablesService.testData();*/
  80. Constant.setTowns(town.split("、"));
  81. Constant.setArea(area);
  82. System.out.println("<<<<<<<<------根据配置文件为AREA赋值:" + Constant.getArea());
  83. System.out.println("<<<<<<<<------当前AMAP_CITY_CODE:" + Constant.getAMAP_CITY_CODE());
  84. // TODO 网络连通性测试,可以请求一下测试的市中心地址
  85. try {
  86. System.out.println("<<<<<<<<------开始网络连通性测试");
  87. List<String> addrs = new ArrayList<>();
  88. addrs.add("上海市青浦区馨浦苑西区2号楼1201");
  89. AddressResult addressResult = AddressQueryEngine.getInstance().commonSearchByName(addrs, AddressLevel.values()[0]);
  90. System.out.println("<<<<<<<<------网络连通性测试结果:" + addressResult);
  91. } catch (Exception e) {
  92. System.err.println("<<<<<<<<------网络连通性测试结果:" + e);
  93. }
  94. // TODO 数据库连接测试,可以查询表数据
  95. /*try {
  96. legalPersonServiceimpl.iterativeProcessing(1, 100, 1);
  97. } catch (Exception e) {
  98. System.err.println("<<<<<<<<------数据库连接测试异常:" + e);
  99. }*/
  100. }
  101. public void initPolygons() {
  102. List<List<GeoJsonBean>> lis = new ArrayList<>();
  103. if ("青浦".equals(area)) {
  104. lis.add(cjPolygonList);
  105. lis.add(wGridPolygonList);
  106. lis.add(gridPolygonList);
  107. }
  108. lis.add(shAllAdministrativeDivisionPolygonList);
  109. lis.add(shAllToensPolygonList);
  110. String[] path;
  111. if ("青浦".equals(area)) {
  112. path = new String[]{"geojson/qp_cunju.geojson", "geojson/青浦区微网格-wgs84.geojson",
  113. "geojson/责任网格-wgs84.geojson", "geojson/sh_all_administrative_division.geojson", "geojson/sh_all_towns.geojson"};
  114. } else {
  115. path = new String[]{"geojson/sh_all_administrative_division.geojson", "geojson/sh_all_towns.geojson"};
  116. }
  117. for (int j = 0; j < path.length; j++) {
  118. BufferedReader reader = null;
  119. try {
  120. // 创建 ClassPathResource 对象,指定静态文件的路径
  121. ClassPathResource resource = new ClassPathResource(path[j]);
  122. // 获取文件的输入流
  123. reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
  124. String line;
  125. StringBuilder sb = new StringBuilder();
  126. while ((line = reader.readLine()) != null) {
  127. sb.append(line).append("\r\n");
  128. }
  129. reader.close();
  130. JSONObject json = JSONObject.parseObject(sb.toString());
  131. JSONArray featrues = json.getJSONArray("features");
  132. for (int i = 0; i < featrues.size(); i++) {
  133. JSONObject properties = featrues.getJSONObject(i).getJSONObject("properties");
  134. JSONObject geoJson = new JSONObject();
  135. geoJson.put("type", "MultiPolygon");
  136. geoJson.put("coordinates",
  137. featrues.getJSONObject(i).getJSONObject("geometry").getJSONArray("coordinates"));
  138. GeometryJSON geometryJSON = new GeometryJSON();
  139. MultiPolygon multiPolygon = geometryJSON.readMultiPolygon(geoJson.toJSONString());
  140. GeoJsonBean geoJsonBean = new GeoJsonBean();
  141. if (path[j].contains("cunju")) {
  142. JSONObject resultProp = new JSONObject();
  143. resultProp.put("网格名称", properties.getString("__10"));
  144. resultProp.put("shape_leng", properties.getString("shape__length"));
  145. resultProp.put("范围信息", properties.getString("__8"));
  146. resultProp.put("村居代码", properties.getString("__5"));
  147. resultProp.put("街道代码", properties.getString("__9"));
  148. // resultProp.put("更新时间", properties.getString("__2"));
  149. resultProp.put("shape_area", properties.getString("shape__area"));
  150. resultProp.put("所属区县", properties.getString("__4"));
  151. resultProp.put("所属居委", properties.getString("cjname"));
  152. resultProp.put("所属街道", properties.getString("jz"));
  153. resultProp.put("面积", properties.getString("__1"));
  154. resultProp.put("objectid", properties.getString("objectid"));
  155. geoJsonBean.setProperties(resultProp);
  156. } else if (path[j].contains("微网格")) {
  157. JSONObject resultProp = new JSONObject();
  158. resultProp.put("objectid", properties.getString("OBJECTID"));
  159. resultProp.put("所属街道", properties.getString("æ\u0089\u0080å±\u009Eè¡\u0097"));
  160. resultProp.put("Shape_Leng", properties.getString("Shape_Leng"));
  161. resultProp.put("微格网代码", properties.getString("å¾®ç½\u0091æ ¼"));
  162. resultProp.put("所属区县", properties.getString("æ\u0089\u0080å±\u009Eå\u008Cº"));
  163. resultProp.put("Shape_Area", properties.getString("Shape_Area"));
  164. resultProp.put("微格网名称", properties.getString("å¾®ç½\u0091_1"));
  165. geoJsonBean.setProperties(resultProp);
  166. } else if (path[j].contains("责任网格")) {
  167. geoJsonBean.setProperties(properties);
  168. } else if (path[j].contains("sh_all")) {
  169. geoJsonBean.setProperties(properties);
  170. }
  171. geoJsonBean.setMultiPolygon(multiPolygon);
  172. lis.get(j).add(geoJsonBean);
  173. }
  174. } catch (IOException e) {
  175. e.printStackTrace();
  176. System.out.println("<<<<<<<<------run geo data error------>>>>>>>>>");
  177. }
  178. }
  179. System.out.println("<<<<<<<<------run geo data complete------>>>>>>>>>");
  180. }
  181. public GeoJsonBean isInGridPolygon(double lon, double lat) {
  182. Geometry point = null;
  183. try {
  184. point = reader.read("POINT (" + lon + " " + lat + ")");
  185. } catch (ParseException e) {
  186. throw new RuntimeException(e);
  187. }
  188. for (GeoJsonBean bean : gridPolygonList) {
  189. try {
  190. if (bean.getMultiPolygon().contains(point)) {
  191. return bean;
  192. }
  193. } catch (Exception e) {
  194. }
  195. }
  196. return null;
  197. }
  198. public GeoJsonBean isInWGridPolygon(double lon, double lat) {
  199. Geometry point = null;
  200. try {
  201. point = reader.read("POINT (" + lon + " " + lat + ")");
  202. } catch (ParseException e) {
  203. throw new RuntimeException(e);
  204. }
  205. for (GeoJsonBean bean : wGridPolygonList) {
  206. if (bean.getMultiPolygon().contains(point)) {
  207. return bean;
  208. }
  209. }
  210. return null;
  211. }
  212. public GeoJsonBean isInCJPolygon(double lon, double lat) {
  213. Geometry point = null;
  214. try {
  215. point = reader.read("POINT (" + lon + " " + lat + ")");
  216. } catch (ParseException e) {
  217. throw new RuntimeException(e);
  218. }
  219. for (GeoJsonBean bean : cjPolygonList) {
  220. if (bean.getMultiPolygon().contains(point)) {
  221. return bean;
  222. }
  223. }
  224. return null;
  225. }
  226. public GeoJsonBean isInadPolygon(double lon, double lat) {
  227. Geometry point = null;
  228. try {
  229. point = reader.read("POINT (" + lon + " " + lat + ")");
  230. } catch (ParseException e) {
  231. throw new RuntimeException(e);
  232. }
  233. for (GeoJsonBean bean : shAllAdministrativeDivisionPolygonList) {
  234. if (bean.getMultiPolygon().contains(point)) {
  235. return bean;
  236. }
  237. }
  238. return null;
  239. }
  240. public GeoJsonBean isInTownPolygon(double lon, double lat) {
  241. Geometry point = null;
  242. try {
  243. point = reader.read("POINT (" + lon + " " + lat + ")");
  244. } catch (ParseException e) {
  245. throw new RuntimeException(e);
  246. }
  247. for (GeoJsonBean bean : shAllToensPolygonList) {
  248. if (bean.getMultiPolygon().contains(point)) {
  249. return bean;
  250. }
  251. }
  252. return null;
  253. }
  254. /**
  255. * 初始化责任网络数据
  256. **/
  257. public void runZEWLData() {
  258. try {
  259. // 创建 ClassPathResource 对象,指定静态文件的路径
  260. ClassPathResource resource = new ClassPathResource("geojson/zerenwangluo.json");
  261. // 获取文件的输入流
  262. BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
  263. String line;
  264. StringBuilder sb = new StringBuilder();
  265. while ((line = reader.readLine()) != null) {
  266. sb.append(line).append("\r\n");
  267. }
  268. reader.close();
  269. zrwl = JSONObject.parseObject(sb.toString(), Zerenwangluo.class);
  270. zrwlFeatures = zrwl.getFeatures();
  271. for (int i = 0; i < zrwlFeatures.size(); i++) {
  272. Zerenwangluo.FeaturesDTO.AttributesDTO attributes = zrwlFeatures.get(i).getAttributes();
  273. zrwlProperties.add(attributes);
  274. Zerenwangluo.FeaturesDTO.GeometryDTO geometry = zrwlFeatures.get(i).getGeometry();
  275. JSONArray coordinates = new JSONArray();
  276. JSONArray coordinates1 = new JSONArray();
  277. for (List<List<Double>> rings : geometry.getRings()) {
  278. JSONArray coordinates2 = new JSONArray();
  279. for (List<Double> ring : rings) {
  280. JSONArray coordinates3 = new JSONArray();
  281. double x = ring.get(0);
  282. double y = ring.get(1);
  283. double[] dou = CoordTransform.getInstance().SH2000ToWGS84(x, y);
  284. coordinates3.add(dou[0]);
  285. coordinates3.add(dou[1]);
  286. coordinates2.add(coordinates3);
  287. }
  288. coordinates1.add(coordinates2);
  289. }
  290. coordinates.add(coordinates1);
  291. JSONObject geoJson = new JSONObject();
  292. geoJson.put("type", "MultiPolygon");
  293. geoJson.put("coordinates", coordinates);
  294. GeometryJSON geometryJSON = new GeometryJSON();
  295. MultiPolygon multiPolygon = geometryJSON.readMultiPolygon(geoJson.toJSONString());
  296. zrwlMulPolygonList.add(multiPolygon);
  297. }
  298. } catch (IOException e) {
  299. e.printStackTrace();
  300. System.out.println("<<<<<<<<------run geo data error------>>>>>>>>>");
  301. } catch (Exception e) {
  302. throw new RuntimeException(e);
  303. }
  304. System.out.println("<<<<<<<<------run geo data complete------>>>>>>>>>");
  305. }
  306. public JSONObject getLiabilityGridByLatlon(double lat, double lon) {
  307. Zerenwangluo.FeaturesDTO.AttributesDTO attribute = null;
  308. try {
  309. Geometry point = reader.read("POINT (" + lon + " " + lat + ")");
  310. boolean done = false;
  311. for (int i = 0; i < zrwlMulPolygonList.size(); i++) {
  312. if (done) {
  313. break;
  314. }
  315. MultiPolygon multiPolygon = zrwlMulPolygonList.get(i);
  316. String wktStr = multiPolygon.toString();
  317. if (wktStr.contains("), (")) {// 如果是多面,则拆分成单面再重新比对
  318. String[] wktStrs = wktStr.split("\\), \\(");
  319. List<MultiPolygon> polygons = new ArrayList<>();
  320. for (int j = 0; j < wktStrs.length; j++) {
  321. if (j == 0) {
  322. MultiPolygon polygon1 = (MultiPolygon) reader.read(wktStrs[0] + ")))");
  323. polygons.add(polygon1);
  324. } else if (j == wktStrs.length - 1) {
  325. MultiPolygon polygon2 = (MultiPolygon) reader.read("MULTIPOLYGON (((" + wktStrs[j]);
  326. polygons.add(polygon2);
  327. } else {
  328. MultiPolygon polygon3 = (MultiPolygon) reader.read("MULTIPOLYGON (((" + wktStrs[j] + ")))");
  329. polygons.add(polygon3);
  330. }
  331. }
  332. for (MultiPolygon polygon : polygons) {
  333. if (polygon.contains(point)) {
  334. attribute = zrwlProperties.get(i);
  335. done = true;
  336. break;
  337. }
  338. }
  339. } else {// 如果是单面,则直接比对
  340. if (multiPolygon.contains(point)) {
  341. attribute = zrwlProperties.get(i);
  342. break;
  343. }
  344. }
  345. }
  346. } catch (Exception e) {
  347. e.printStackTrace();
  348. return null;
  349. }
  350. return JSONObject.parseObject(JSON.toJSONString(attribute));
  351. }
  352. /**
  353. * 初始化村居数据
  354. **/
  355. public void runGeoData() {
  356. try {
  357. // 创建 ClassPathResource 对象,指定静态文件的路径
  358. ClassPathResource resource = new ClassPathResource("geojson/qp_cunju.geojson");
  359. // 获取文件的输入流
  360. BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
  361. String line;
  362. StringBuilder sb = new StringBuilder();
  363. while ((line = reader.readLine()) != null) {
  364. sb.append(line).append("\r\n");
  365. }
  366. reader.close();
  367. JSONObject json = JSONObject.parseObject(sb.toString());
  368. JSONArray features = json.getJSONArray("features");
  369. for (int i = 0; i < features.size(); i++) {
  370. JSONObject feature = features.getJSONObject(i);
  371. JSONObject geometry = feature.getJSONObject("geometry");
  372. GeometryJSON geometryJSON = new GeometryJSON();
  373. MultiPolygon multiPolygon = geometryJSON.readMultiPolygon(geometry.toJSONString());
  374. featureList.add(feature);
  375. propertiList.add(JSONObject.parseObject(feature.getJSONObject("properties").toJSONString(), Village.class));
  376. mulPolygonList.add(multiPolygon);
  377. }
  378. } catch (FileNotFoundException e) {
  379. e.printStackTrace();
  380. System.out.println("<<<<<<<<------run geo data error------>>>>>>>>>");
  381. } catch (IOException e) {
  382. e.printStackTrace();
  383. System.out.println("<<<<<<<<------run geo data error------>>>>>>>>>");
  384. } catch (Exception e) {
  385. e.printStackTrace();
  386. System.out.println("<<<<<<<<------run geo data error------>>>>>>>>>");
  387. }
  388. System.out.println("<<<<<<<<------run geo data complete------>>>>>>>>>");
  389. }
  390. public Village getAreaCodeByLatlon(double lat, double lon) {
  391. Village geoAC = null;
  392. try {
  393. Geometry point = reader.read("POINT (" + lon + " " + lat + ")");
  394. for (int i = 0; i < mulPolygonList.size(); i++) {
  395. if (mulPolygonList.get(i).contains(point)) {
  396. geoAC = propertiList.get(i);
  397. break;
  398. }
  399. }
  400. } catch (Exception e) {
  401. e.printStackTrace();
  402. return null;
  403. }
  404. return geoAC;
  405. }
  406. public List<Village> getAreaCodeByLatlons(JSONArray locations) {
  407. if (locations == null || locations.size() < 1) {
  408. return null;
  409. } else {
  410. List<Village> result = new ArrayList<>();
  411. try {
  412. for (int i = 0; i < locations.size(); i++) {
  413. JSONObject jsonObject = locations.getJSONObject(i);
  414. Village geoAreaCode = getAreaCodeByLatlon(jsonObject.getDouble("lat"), jsonObject.getDouble("lon"));
  415. result.add(geoAreaCode);
  416. }
  417. return result;
  418. } catch (Exception e) {
  419. return null;
  420. }
  421. }
  422. }
  423. }