TransfromDataTool.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package com.skyversation.poiaddr.addquery;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.skyversation.poiaddr.bean.AddressResult;
  5. import com.skyversation.poiaddr.entity.AmapAddressV3;
  6. import com.skyversation.poiaddr.service.AreaService;
  7. import com.skyversation.poiaddr.util.AddressTools;
  8. import com.skyversation.poiaddr.util.CoordTransform2;
  9. import com.skyversation.poiaddr.util.status.AddressResultEnum;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.List;
  13. import java.util.UUID;
  14. public class TransfromDataTool {
  15. public static AddressResult wdResultToResult(JSONObject json) {
  16. AddressResult result = new AddressResult();
  17. JSONArray array = json.getJSONObject("data").getJSONArray("addrList");
  18. if (array == null || array.size() < 1) {
  19. result.setCode(AddressResultEnum.RESULT_NULL);
  20. return result;
  21. }
  22. for (int i = 0; i < array.size(); i++) {
  23. JSONObject jsonObject = array.getJSONObject(i);
  24. AddressResult.ContentBean content = new AddressResult.ContentBean();
  25. content.setPname(jsonObject.getString("city"));
  26. content.setCityname(jsonObject.getString("city"));
  27. content.setAdname(jsonObject.getString("town"));
  28. content.setCommunity(jsonObject.getString("community"));
  29. content.setCommunityCode(jsonObject.getString("communitycode"));
  30. content.setType(jsonObject.getString("datasource"));
  31. content.setAddress(jsonObject.getString("addr"));
  32. content.setDistance(jsonObject.getString("lv"));
  33. content.setName(jsonObject.getString("addr"));
  34. double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
  35. Double.parseDouble(jsonObject.getString("x")), Double.parseDouble(jsonObject.getString("y")));
  36. content.setLocation(points[0] + "," + points[1]);
  37. content.setLon(points[0]);
  38. content.setLat(points[1]);
  39. if (result.getData() == null) {
  40. result.setData(new ArrayList<>());
  41. }
  42. result.getData().add(content);
  43. }
  44. result.setCode(AddressResultEnum.WDJA_SUCCESS);
  45. return result;
  46. }
  47. public static AddressResult szxResultToResult2(JSONArray array) {
  48. AddressResult result = new AddressResult();
  49. if (array == null || array.size() < 1) {
  50. result.setCode(AddressResultEnum.RESULT_NULL);
  51. return result;
  52. }
  53. for (int i = 0; i < array.size(); i++) {
  54. JSONObject jsonObject = array.getJSONObject(i);
  55. AddressResult.ContentBean content = new AddressResult.ContentBean();
  56. content.setPname(jsonObject.getString("province"));
  57. content.setCityname(jsonObject.getString("district"));
  58. content.setAdname(jsonObject.getString("town"));
  59. content.setType(jsonObject.getString("type"));
  60. content.setAddress(jsonObject.getString("address"));
  61. content.setName(jsonObject.getString("address"));
  62. if (jsonObject.containsKey("location") && jsonObject.getJSONObject("location").containsKey("lat") && jsonObject.getJSONObject("location").containsKey("lng")) {
  63. double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
  64. Double.parseDouble(jsonObject.getJSONObject("location").getString("lat")), Double.parseDouble(jsonObject.getJSONObject("location").getString("lng")));
  65. content.setLocation(points[0] + "," + points[1]);
  66. content.setLat(points[0]);
  67. content.setLon(points[1]);
  68. }
  69. if (result.getData() == null) {
  70. result.setData(new ArrayList<>());
  71. }
  72. result.getData().add(content);
  73. }
  74. result.setCode(AddressResultEnum.WDJA_SUCCESS);
  75. return result;
  76. }
  77. public static AddressResult szxResultToResult2(JSONObject jsonObject) {
  78. AddressResult result = new AddressResult();
  79. if (jsonObject == null || jsonObject.size() < 1) {
  80. result.setCode(AddressResultEnum.RESULT_NULL);
  81. return result;
  82. }
  83. AddressResult.ContentBean content = new AddressResult.ContentBean();
  84. if (jsonObject.containsKey("总分") && jsonObject.get("总分") != null) {
  85. content.setPname(jsonObject.getString("province"));
  86. content.setCityname(jsonObject.getString("district"));
  87. content.setAdname(jsonObject.getString("town"));
  88. content.setType(jsonObject.getString("type"));
  89. content.setAddress(jsonObject.getString("address"));
  90. content.setName(jsonObject.getString("address"));
  91. content.setSearchAddress(jsonObject.getString("searchAddress"));
  92. content.setScore(jsonObject.getString("总分"));
  93. if (jsonObject.containsKey("location") && jsonObject.getJSONObject("location").containsKey("lat") && jsonObject.getJSONObject("location").containsKey("lng")) {
  94. double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
  95. Double.parseDouble(jsonObject.getJSONObject("location").getString("lat")), Double.parseDouble(jsonObject.getJSONObject("location").getString("lng")));
  96. content.setLocation(points[0] + "," + points[1]);
  97. content.setLat(points[0]);
  98. content.setLon(points[1]);
  99. }
  100. if (result.getData() == null) {
  101. result.setData(new ArrayList<>());
  102. }
  103. result.getData().add(content);
  104. result.setCode(AddressResultEnum.WDJA_SUCCESS);
  105. }
  106. return result;
  107. }
  108. public static AddressResult szxResultToResult(JSONObject json, String searchAddress) {
  109. AddressResult result = new AddressResult();
  110. JSONArray array = json.getJSONArray("result");
  111. if (array == null || array.size() < 1) {
  112. result.setCode(AddressResultEnum.RESULT_NULL);
  113. return result;
  114. }
  115. // TODO 将请求返回的市中心结果入库到pgsql中
  116. List<AmapAddressV3> amapAddressV3List = new ArrayList<>();
  117. try {
  118. for (int i = 0; i < array.size(); i++) {
  119. JSONObject item = array.getJSONObject(i);
  120. AmapAddressV3 amapAddressV3 = new AmapAddressV3();
  121. amapAddressV3.setId(UUID.randomUUID().toString());
  122. amapAddressV3.setName(item.get("name").toString());
  123. amapAddressV3.setType("测绘院结果");
  124. amapAddressV3.setAddress(item.get("address").toString());
  125. JSONObject localtion = item.getJSONObject("location");
  126. Double wgs84Lng = localtion.getDouble("lng");
  127. Double wgs84Lat = localtion.getDouble("lat");
  128. double[] points2 = CoordTransform2.getInstance().wgs84_to_shcj(wgs84Lng, wgs84Lat);
  129. double[] points = CoordTransform2.getInstance().wgs84_to_gcj02(wgs84Lng, wgs84Lat);
  130. amapAddressV3.setLocation(points[0] + "," + points[1]);
  131. amapAddressV3.setLon(String.valueOf(points2[0]));
  132. amapAddressV3.setLat(String.valueOf(points2[1]));
  133. amapAddressV3.setCreateTime(new Date());
  134. if (item.containsKey("ext_data")) {
  135. JSONObject ext_data = item.getJSONObject("ext_data");
  136. if (!ext_data.get("region_ss").toString().isEmpty()) {
  137. amapAddressV3.setPname(ext_data.get("region_ss").toString());
  138. amapAddressV3.setCityname(ext_data.get("region_ss").toString());
  139. }
  140. if (!ext_data.get("region_qx").toString().isEmpty()) {
  141. amapAddressV3.setAdname(ext_data.get("region_qx").toString());
  142. }
  143. if (!ext_data.get("region_jd").toString().isEmpty()) {
  144. amapAddressV3.setStreetTown(ext_data.get("region_jd").toString());
  145. }
  146. if (!ext_data.get("region_jw").toString().isEmpty()) {
  147. amapAddressV3.setResidentialCommittee(ext_data.get("region_jw").toString());
  148. }
  149. }
  150. amapAddressV3List.add(amapAddressV3);
  151. }
  152. if (amapAddressV3List.size() > 0) {
  153. AreaService.getInstance().saveAmapAddressV3(amapAddressV3List);
  154. }
  155. } catch (Exception e) {
  156. e.printStackTrace();
  157. }
  158. JSONObject jsonObject = com.skyversation.poiaddr.util.AddressTools.getInstance().findBestMatch(searchAddress, array, "address");
  159. if (jsonObject.containsKey("总分") && jsonObject.get("总分") != null && !jsonObject.getString("总分").isEmpty()) {
  160. AddressResult.ContentBean content = new AddressResult.ContentBean();
  161. content.setScore(jsonObject.getString("总分"));
  162. content.setSearchAddress(jsonObject.getString("searchAddress"));
  163. content.setType(jsonObject.getString("type_name"));
  164. content.setAddress(jsonObject.getString("address"));
  165. content.setName(jsonObject.getString("name"));
  166. // 使用分词方法得到区名和镇名
  167. content.setCityname(AddressTools.parseAddressCJ(jsonObject.getString("address"))[1]);
  168. double[] points = new double[]{jsonObject.getJSONObject("location").getDouble("lng"),
  169. jsonObject.getJSONObject("location").getDouble("lat")};
  170. content.setLon(points[0]);
  171. content.setLat(points[1]);
  172. if (jsonObject.containsKey("point_x") && jsonObject.containsKey("point_y") && jsonObject.get("point_x") != null && jsonObject.get("point_y") != null) {
  173. content.setLocation(jsonObject.getDouble("point_x") + "," + jsonObject.getDouble("point_y"));
  174. } else {
  175. // 市中心返回的结果没有sh2000坐标字段的话就把wgs84的坐标转成sh2000的坐标系并保存到location字段中
  176. double[] points2 = CoordTransform2.getInstance().wgs84_to_shcj(
  177. jsonObject.getJSONObject("location").getDouble("lng"), jsonObject.getJSONObject("location").getDouble("lat"));
  178. content.setLocation(points2[0] + "," + points2[1]);
  179. }
  180. content.setAdname(AddressTools.parseAddressCJ(jsonObject.getString("address"))[2]);
  181. if (jsonObject.containsKey("ext_data")) {
  182. JSONObject extData = jsonObject.getJSONObject("ext_data");
  183. if (extData.containsKey("region_jw") && !extData.getString("region_jw").isEmpty()) {
  184. content.setCommunity(extData.getString("region_jw"));
  185. }
  186. // 如果评分大于1.6,那么就使用接口返回的街镇,否则使用源搜索地址的街镇
  187. if (jsonObject.getString("总分").contains("rule_") || Float.parseFloat(jsonObject.getString("总分")) > 1.6) {
  188. if (extData.containsKey("region_jd") && !extData.getString("region_jd").isEmpty()) {
  189. content.setAdname(extData.getString("region_jd"));
  190. }
  191. } else if (AddressTools.parseAddressCJ(searchAddress)[2] != null && !AddressTools.parseAddressCJ(searchAddress)[2].isEmpty()) {
  192. content.setAdname(AddressTools.parseAddressCJ(searchAddress)[2]);
  193. }
  194. }
  195. if (result.getData() == null) {
  196. result.setData(new ArrayList<>());
  197. }
  198. result.getData().add(content);
  199. result.setCode(AddressResultEnum.SZX_SUCCESS);
  200. }
  201. return result;
  202. }
  203. public static AddressResult gdResultToResult(JSONObject json) {
  204. AddressResult result = new AddressResult();
  205. JSONArray array = json.getJSONArray("geocodes");
  206. if (array == null || array.size() < 1) {
  207. result.setCode(AddressResultEnum.RESULT_NULL);
  208. return result;
  209. }
  210. for (int i = 0; i < array.size(); i++) {
  211. JSONObject jsonObject = array.getJSONObject(i);
  212. AddressResult.ContentBean content = new AddressResult.ContentBean();
  213. content.setPname(jsonObject.getString("city"));
  214. content.setCityname(jsonObject.getString("city"));
  215. content.setAdname(jsonObject.getString("district"));
  216. content.setType(jsonObject.getString("level"));
  217. content.setAddress(jsonObject.getString("formatted_address"));
  218. content.setName(jsonObject.getString("street"));
  219. String[] loc = jsonObject.getString("location").split(",");
  220. double[] points = CoordTransform2.getInstance().gcj02_to_wgs84(
  221. Double.valueOf(loc[0]), Double.valueOf(loc[1]));
  222. content.setLocation(points[0] + "," + points[1]);
  223. content.setLon(points[0]);
  224. content.setLat(points[1]);
  225. if (result.getData() == null) {
  226. result.setData(new ArrayList<>());
  227. }
  228. result.getData().add(content);
  229. }
  230. result.setCode(AddressResultEnum.GD_SUCCESS);
  231. return result;
  232. }
  233. public AddressResult gdV3ResultToResult(String searchAddress, JSONArray array, boolean isInserted) {
  234. AddressResult result = new AddressResult();
  235. try {
  236. if (array == null || array.size() < 1) {
  237. result.setCode(AddressResultEnum.RESULT_NULL);
  238. return result;
  239. }
  240. JSONObject jsonObject = com.skyversation.poiaddr.util.AddressTools.getInstance().findBestMatch2(searchAddress, array, "address");
  241. if (jsonObject != null && jsonObject.containsKey("总分") && jsonObject.get("总分") != null && !jsonObject.getString("总分").isEmpty()) {
  242. AddressResult.ContentBean content = new AddressResult.ContentBean();
  243. if(jsonObject.getString("总分").contains("rule_")){
  244. content.setScore(jsonObject.getString("总分"));
  245. }else{
  246. // TODO 添加规则:rule_3,代表数据库中匹配到的数据,但是不属于rule_4和rule_2的第三种规则
  247. content.setScore("rule_3");
  248. }
  249. content.setSearchAddress(jsonObject.getString("searchAddress"));
  250. content.setPname(jsonObject.getString("pname"));
  251. content.setCityname(jsonObject.getString("adname"));
  252. if (jsonObject.containsKey("street_town") && !jsonObject.getString("street_town").isEmpty()) {
  253. content.setAdname(jsonObject.getString("street_town"));
  254. }
  255. content.setType(jsonObject.getString("type"));
  256. content.setAddress(jsonObject.getString("address"));
  257. content.setName(jsonObject.getString("name"));
  258. String[] loc = jsonObject.getString("location").split(",");
  259. double[] points = CoordTransform2.getInstance().gcj02_to_shcj(
  260. Double.parseDouble(loc[0]), Double.parseDouble(loc[1]));
  261. content.setLocation(points[0] + "," + points[1]);
  262. double[] points2 = CoordTransform2.getInstance().gcj02_to_wgs84(
  263. Double.parseDouble(loc[0]), Double.parseDouble(loc[1]));
  264. content.setLon(points2[0]);
  265. content.setLat(points2[1]);
  266. if (result.getData() == null) {
  267. result.setData(new ArrayList<>());
  268. }
  269. result.getData().add(content);
  270. }
  271. if (isInserted) {
  272. // TODO 将请求返回的高德结果入库到pgsql中
  273. List<AmapAddressV3> amapAddressV3List = new ArrayList<>();
  274. for (int i = 0; i < array.size(); i++) {
  275. JSONObject item = array.getJSONObject(i);
  276. AmapAddressV3 amapAddressV3 = new AmapAddressV3();
  277. amapAddressV3.setId(UUID.randomUUID().toString());
  278. amapAddressV3.setParent(item.get("parent").toString());
  279. amapAddressV3.setChildtype(item.get("childtype").toString());
  280. amapAddressV3.setName(item.get("name").toString());
  281. amapAddressV3.setType(item.get("type").toString());
  282. amapAddressV3.setTypecode(item.get("typecode").toString());
  283. amapAddressV3.setBizType(item.get("biz_type").toString());
  284. amapAddressV3.setAddress(item.get("address").toString());
  285. amapAddressV3.setLocation(item.get("location").toString());
  286. amapAddressV3.setTel(item.get("tel").toString());
  287. amapAddressV3.setPname(item.get("pname").toString());
  288. amapAddressV3.setAdname(item.get("adname").toString());
  289. amapAddressV3.setImportance(item.get("importance").toString());
  290. amapAddressV3.setShopid(item.get("shopid").toString());
  291. amapAddressV3.setShopinfo(item.get("shopinfo").toString());
  292. amapAddressV3.setPoiweight(item.get("poiweight").toString());
  293. amapAddressV3.setDistance(item.get("distance").toString());
  294. amapAddressV3.setBizExt(item.get("biz_ext").toString());
  295. amapAddressV3.setPhotos(item.get("photos").toString());
  296. amapAddressV3.setCreateTime(new Date());
  297. amapAddressV3List.add(amapAddressV3);
  298. }
  299. if (amapAddressV3List.size() > 0) {
  300. AreaService.getInstance().saveAmapAddressV3(amapAddressV3List);
  301. }
  302. }
  303. } catch (Exception e) {
  304. e.printStackTrace();
  305. System.err.println("gdV3ResultToResult err:" + e);
  306. }
  307. result.setCode(AddressResultEnum.GDV3_SUCCESS);
  308. result.setMessage("数据库查询成功!");
  309. /*if (isInserted) {
  310. System.out.println("*******高德查询结果返回,查询地址是:" + searchAddress);
  311. } else {
  312. System.out.println("&&&&&&&数据库查询结果返回,查询地址是:" + searchAddress);
  313. }*/
  314. return result;
  315. }
  316. }