TransfromDataTool.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.util.AddressTools;
  6. import com.skyversation.poiaddr.util.CoordTransform2;
  7. import com.skyversation.poiaddr.util.status.AddressResultEnum;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class TransfromDataTool {
  11. public static AddressResult wdResultToResult(JSONObject json) {
  12. AddressResult result = new AddressResult();
  13. JSONArray array = json.getJSONObject("data").getJSONArray("addrList");
  14. if (array == null || array.size() < 1) {
  15. result.setCode(AddressResultEnum.RESULT_NULL);
  16. return result;
  17. }
  18. for (int i = 0; i < array.size(); i++) {
  19. JSONObject jsonObject = array.getJSONObject(i);
  20. AddressResult.ContentBean content = new AddressResult.ContentBean();
  21. content.setPname(jsonObject.getString("city"));
  22. content.setCityname(jsonObject.getString("city"));
  23. content.setAdname(jsonObject.getString("town"));
  24. content.setCommunity(jsonObject.getString("community"));
  25. content.setCommunityCode(jsonObject.getString("communitycode"));
  26. content.setType(jsonObject.getString("datasource"));
  27. content.setAddress(jsonObject.getString("addr"));
  28. content.setDistance(jsonObject.getString("lv"));
  29. content.setName(jsonObject.getString("addr"));
  30. double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
  31. Double.parseDouble(jsonObject.getString("x")), Double.parseDouble(jsonObject.getString("y")));
  32. content.setLocation(points[0] + "," + points[1]);
  33. content.setLon(points[0]);
  34. content.setLat(points[1]);
  35. if (result.getData() == null) {
  36. result.setData(new ArrayList<>());
  37. }
  38. result.getData().add(content);
  39. }
  40. result.setCode(AddressResultEnum.WDJA_SUCCESS);
  41. return result;
  42. }
  43. public static AddressResult szxResultToResult2(JSONArray array) {
  44. AddressResult result = new AddressResult();
  45. if (array == null || array.size() < 1) {
  46. result.setCode(AddressResultEnum.RESULT_NULL);
  47. return result;
  48. }
  49. for (int i = 0; i < array.size(); i++) {
  50. JSONObject jsonObject = array.getJSONObject(i);
  51. AddressResult.ContentBean content = new AddressResult.ContentBean();
  52. content.setPname(jsonObject.getString("province"));
  53. content.setCityname(jsonObject.getString("district"));
  54. content.setAdname(jsonObject.getString("town"));
  55. content.setType(jsonObject.getString("type"));
  56. content.setAddress(jsonObject.getString("address"));
  57. content.setName(jsonObject.getString("address"));
  58. if (jsonObject.containsKey("location") && jsonObject.getJSONObject("location").containsKey("lat") && jsonObject.getJSONObject("location").containsKey("lng")) {
  59. double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
  60. Double.parseDouble(jsonObject.getJSONObject("location").getString("lat")), Double.parseDouble(jsonObject.getJSONObject("location").getString("lng")));
  61. content.setLocation(points[0] + "," + points[1]);
  62. content.setLat(points[0]);
  63. content.setLon(points[1]);
  64. }
  65. if (result.getData() == null) {
  66. result.setData(new ArrayList<>());
  67. }
  68. result.getData().add(content);
  69. }
  70. result.setCode(AddressResultEnum.WDJA_SUCCESS);
  71. return result;
  72. }
  73. public static AddressResult szxResultToResult2(JSONObject jsonObject) {
  74. AddressResult result = new AddressResult();
  75. if (jsonObject == null || jsonObject.size() < 1) {
  76. result.setCode(AddressResultEnum.RESULT_NULL);
  77. return result;
  78. }
  79. AddressResult.ContentBean content = new AddressResult.ContentBean();
  80. if (jsonObject.containsKey("总分") && jsonObject.get("总分") != null) {
  81. content.setPname(jsonObject.getString("province"));
  82. content.setCityname(jsonObject.getString("district"));
  83. content.setAdname(jsonObject.getString("town"));
  84. content.setType(jsonObject.getString("type"));
  85. content.setAddress(jsonObject.getString("address"));
  86. content.setName(jsonObject.getString("address"));
  87. content.setSearchAddress(jsonObject.getString("searchAddress"));
  88. content.setScore(jsonObject.getString("总分"));
  89. if (jsonObject.containsKey("location") && jsonObject.getJSONObject("location").containsKey("lat") && jsonObject.getJSONObject("location").containsKey("lng")) {
  90. double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
  91. Double.parseDouble(jsonObject.getJSONObject("location").getString("lat")), Double.parseDouble(jsonObject.getJSONObject("location").getString("lng")));
  92. content.setLocation(points[0] + "," + points[1]);
  93. content.setLat(points[0]);
  94. content.setLon(points[1]);
  95. }
  96. if (result.getData() == null) {
  97. result.setData(new ArrayList<>());
  98. }
  99. result.getData().add(content);
  100. result.setCode(AddressResultEnum.WDJA_SUCCESS);
  101. }
  102. return result;
  103. }
  104. public static AddressResult szxResultToResult(JSONObject json, String searchAddress) {
  105. AddressResult result = new AddressResult();
  106. JSONArray array = json.getJSONArray("result");
  107. if (array == null || array.size() < 1) {
  108. result.setCode(AddressResultEnum.RESULT_NULL);
  109. return result;
  110. }
  111. JSONObject jsonObject = com.skyversation.poiaddr.util.AddressTools.getInstance().findBestMatch(searchAddress, array, "address");
  112. if (jsonObject.containsKey("总分") && jsonObject.get("总分") != null && !jsonObject.getString("总分").isEmpty()) {
  113. AddressResult.ContentBean content = new AddressResult.ContentBean();
  114. content.setScore(jsonObject.getString("总分"));
  115. content.setSearchAddress(jsonObject.getString("searchAddress"));
  116. content.setType(jsonObject.getString("type_name"));
  117. List<String> addressList = new ArrayList<>();
  118. content.setAddress(jsonObject.getString("address"));
  119. if (!jsonObject.getString("address").isEmpty()) {
  120. addressList.add(jsonObject.getString("address"));
  121. }
  122. // 从返回地址信息里面判断区,首先得到上海市所有的区,然后遍历区名
  123. for (String area : Constant.allAreas) {
  124. if (content.getAddress().contains(area)) {
  125. content.setAdname(area);
  126. break;
  127. }
  128. }
  129. if (!jsonObject.getString("std_address").isEmpty()) {
  130. addressList.add(jsonObject.getString("std_address"));
  131. }
  132. content.setName(jsonObject.getString("name"));
  133. double[] points = new double[]{jsonObject.getJSONObject("location").getDouble("lng"),
  134. jsonObject.getJSONObject("location").getDouble("lat")};
  135. content.setLon(points[1]);
  136. content.setLat(points[0]);
  137. if (jsonObject.get("point_x") != null && jsonObject.get("point_y") != null) {
  138. content.setLocation(jsonObject.getDouble("point_x") + "," + jsonObject.getDouble("point_y"));
  139. }
  140. if (jsonObject.containsKey("ext_data")) {
  141. JSONObject extData = jsonObject.getJSONObject("ext_data");
  142. if (extData.containsKey("address") && !extData.getString("address").isEmpty()) {
  143. addressList.add(extData.getString("address"));
  144. }
  145. if (extData.containsKey("source_address") && !extData.getString("source_address").isEmpty()) {
  146. addressList.add(extData.getString("source_address"));
  147. }
  148. if (extData.containsKey("region_jw") && !extData.getString("region_jw").isEmpty()) {
  149. JSONObject cjJson = new JSONObject();
  150. cjJson.put("所属街道", extData.getString("region_jd"));
  151. cjJson.put("所属居委", extData.getString("region_jw"));
  152. content.setCjJson(cjJson);
  153. }
  154. }
  155. for (String item : addressList) {
  156. // item.contains("上海市") && item.contains(Constant.getArea() + "区") &&
  157. if (item.length() > content.getAddress().length()) {
  158. content.setAddress(item);
  159. }
  160. }
  161. if (result.getData() == null) {
  162. result.setData(new ArrayList<>());
  163. }
  164. result.getData().add(content);
  165. result.setCode(AddressResultEnum.SZX_SUCCESS);
  166. }
  167. return result;
  168. }
  169. public static AddressResult gdResultToResult(JSONObject json) {
  170. AddressResult result = new AddressResult();
  171. JSONArray array = json.getJSONArray("geocodes");
  172. if (array == null || array.size() < 1) {
  173. result.setCode(AddressResultEnum.RESULT_NULL);
  174. return result;
  175. }
  176. for (int i = 0; i < array.size(); i++) {
  177. JSONObject jsonObject = array.getJSONObject(i);
  178. AddressResult.ContentBean content = new AddressResult.ContentBean();
  179. content.setPname(jsonObject.getString("city"));
  180. content.setCityname(jsonObject.getString("city"));
  181. content.setAdname(jsonObject.getString("district"));
  182. content.setType(jsonObject.getString("level"));
  183. content.setAddress(jsonObject.getString("formatted_address"));
  184. content.setName(jsonObject.getString("street"));
  185. String[] loc = jsonObject.getString("location").split(",");
  186. double[] points = CoordTransform2.getInstance().gcj02_to_wgs84(
  187. Double.valueOf(loc[0]), Double.valueOf(loc[1]));
  188. content.setLocation(points[0] + "," + points[1]);
  189. content.setLon(points[0]);
  190. content.setLat(points[1]);
  191. if (result.getData() == null) {
  192. result.setData(new ArrayList<>());
  193. }
  194. result.getData().add(content);
  195. }
  196. result.setCode(AddressResultEnum.GD_SUCCESS);
  197. return result;
  198. }
  199. public static AddressResult gdV3ResultToResult(JSONArray array) {
  200. AddressResult result = new AddressResult();
  201. if (array == null || array.size() < 1) {
  202. result.setCode(AddressResultEnum.RESULT_NULL);
  203. return result;
  204. }
  205. try {
  206. for (int i = 0; i < array.size(); i++) {
  207. JSONObject jsonObject = array.getJSONObject(i);
  208. AddressResult.ContentBean content = new AddressResult.ContentBean();
  209. content.setPname(jsonObject.getString("pname"));
  210. content.setCityname(jsonObject.getString("cityname"));
  211. content.setAdname(jsonObject.getString("adname"));
  212. content.setType(jsonObject.getString("type"));
  213. content.setAddress(content.getCityname() + content.getAdname() + jsonObject.getString("address"));
  214. content.setName(jsonObject.getString("name"));
  215. String[] loc = jsonObject.getString("location").split(",");
  216. double[] points = CoordTransform2.getInstance().gcj02_to_wgs84(
  217. Double.parseDouble(loc[0]), Double.parseDouble(loc[1]));
  218. content.setLocation(points[0] + "," + points[1]);
  219. content.setLon(points[0]);
  220. content.setLat(points[1]);
  221. if (result.getData() == null) {
  222. result.setData(new ArrayList<>());
  223. }
  224. result.getData().add(content);
  225. }
  226. } catch (Exception e) {
  227. System.err.println("gdV3ResultToResult err:" + e);
  228. }
  229. result.setCode(AddressResultEnum.WDJA_SUCCESS);
  230. result.setCode(AddressResultEnum.GDV3_SUCCESS);
  231. return result;
  232. }
  233. }