TransfromDataTool.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. content.setPname(jsonObject.getString("province"));
  81. content.setCityname(jsonObject.getString("district"));
  82. content.setAdname(jsonObject.getString("town"));
  83. content.setType(jsonObject.getString("type"));
  84. content.setAddress(jsonObject.getString("address"));
  85. content.setName(jsonObject.getString("address"));
  86. content.setSearchAddress(jsonObject.getString("searchAddress"));
  87. content.setScore(jsonObject.getString("总分"));
  88. if (jsonObject.containsKey("location") && jsonObject.getJSONObject("location").containsKey("lat") && jsonObject.getJSONObject("location").containsKey("lng")) {
  89. double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
  90. Double.parseDouble(jsonObject.getJSONObject("location").getString("lat")), Double.parseDouble(jsonObject.getJSONObject("location").getString("lng")));
  91. content.setLocation(points[0] + "," + points[1]);
  92. content.setLat(points[0]);
  93. content.setLon(points[1]);
  94. }
  95. if (result.getData() == null) {
  96. result.setData(new ArrayList<>());
  97. }
  98. result.getData().add(content);
  99. result.setCode(AddressResultEnum.WDJA_SUCCESS);
  100. return result;
  101. }
  102. public static AddressResult szxResultToResult(JSONObject json) {
  103. AddressResult result = new AddressResult();
  104. JSONArray array = json.getJSONArray("result");
  105. if (array == null || array.size() < 1) {
  106. result.setCode(AddressResultEnum.RESULT_NULL);
  107. return result;
  108. }
  109. for (int i = 0; i < array.size(); i++) {
  110. JSONObject jsonObject = array.getJSONObject(i);
  111. AddressResult.ContentBean content = new AddressResult.ContentBean();
  112. content.setType(jsonObject.getString("type_name"));
  113. List<String> addressList = new ArrayList<>();
  114. content.setAddress(jsonObject.getString("address"));
  115. if (!jsonObject.getString("address").isEmpty()) {
  116. addressList.add(jsonObject.getString("address"));
  117. }
  118. if (!jsonObject.getString("std_address").isEmpty()) {
  119. addressList.add(jsonObject.getString("std_address"));
  120. }
  121. content.setName(jsonObject.getString("name"));
  122. double[] points = new double[]{jsonObject.getJSONObject("location").getDouble("lng"),
  123. jsonObject.getJSONObject("location").getDouble("lat")};
  124. content.setLocation(points[0] + "," + points[1]);
  125. content.setLon(points[0]);
  126. content.setLat(points[1]);
  127. if (jsonObject.containsKey("ext_data")) {
  128. JSONObject extData = jsonObject.getJSONObject("ext_data");
  129. if (extData.containsKey("address") && !extData.getString("address").isEmpty()) {
  130. addressList.add(extData.getString("address"));
  131. }
  132. if (extData.containsKey("source_address") && !extData.getString("source_address").isEmpty()) {
  133. addressList.add(extData.getString("source_address"));
  134. }
  135. if (extData.containsKey("region_jw") && !extData.getString("region_jw").isEmpty()) {
  136. JSONObject cjJson = new JSONObject();
  137. cjJson.put("所属街道", extData.getString("region_jd"));
  138. cjJson.put("所属居委", extData.getString("region_jw"));
  139. content.setCjJson(cjJson);
  140. }
  141. }
  142. for (String item : addressList) {
  143. if (item.contains("上海市") && item.contains(Constant.getArea() + "区") && item.length() > content.getAddress().length()) {
  144. content.setAddress(item);
  145. }
  146. }
  147. if (result.getData() == null) {
  148. result.setData(new ArrayList<>());
  149. }
  150. result.getData().add(content);
  151. }
  152. result.setCode(AddressResultEnum.SZX_SUCCESS);
  153. return result;
  154. }
  155. public static AddressResult gdResultToResult(JSONObject json) {
  156. AddressResult result = new AddressResult();
  157. JSONArray array = json.getJSONArray("geocodes");
  158. if (array == null || array.size() < 1) {
  159. result.setCode(AddressResultEnum.RESULT_NULL);
  160. return result;
  161. }
  162. for (int i = 0; i < array.size(); i++) {
  163. JSONObject jsonObject = array.getJSONObject(i);
  164. AddressResult.ContentBean content = new AddressResult.ContentBean();
  165. content.setPname(jsonObject.getString("city"));
  166. content.setCityname(jsonObject.getString("city"));
  167. content.setAdname(jsonObject.getString("district"));
  168. content.setType(jsonObject.getString("level"));
  169. content.setAddress(jsonObject.getString("formatted_address"));
  170. content.setName(jsonObject.getString("street"));
  171. String[] loc = jsonObject.getString("location").split(",");
  172. double[] points = CoordTransform2.getInstance().gcj02_to_wgs84(
  173. Double.valueOf(loc[0]), Double.valueOf(loc[1]));
  174. content.setLocation(points[0] + "," + points[1]);
  175. content.setLon(points[0]);
  176. content.setLat(points[1]);
  177. if (result.getData() == null) {
  178. result.setData(new ArrayList<>());
  179. }
  180. result.getData().add(content);
  181. }
  182. result.setCode(AddressResultEnum.GD_SUCCESS);
  183. return result;
  184. }
  185. public static AddressResult gdV3ResultToResult(JSONArray array) {
  186. AddressResult result = new AddressResult();
  187. if (array == null || array.size() < 1) {
  188. result.setCode(AddressResultEnum.RESULT_NULL);
  189. return result;
  190. }
  191. try {
  192. for (int i = 0; i < array.size(); i++) {
  193. JSONObject jsonObject = array.getJSONObject(i);
  194. AddressResult.ContentBean content = new AddressResult.ContentBean();
  195. content.setPname(jsonObject.getString("pname"));
  196. content.setCityname(jsonObject.getString("cityname"));
  197. content.setAdname(jsonObject.getString("adname"));
  198. content.setType(jsonObject.getString("type"));
  199. content.setAddress(content.getCityname() + content.getAdname() + jsonObject.getString("address"));
  200. content.setName(jsonObject.getString("name"));
  201. String[] loc = jsonObject.getString("location").split(",");
  202. double[] points = CoordTransform2.getInstance().gcj02_to_wgs84(
  203. Double.parseDouble(loc[0]), Double.parseDouble(loc[1]));
  204. content.setLocation(points[0] + "," + points[1]);
  205. content.setLon(points[0]);
  206. content.setLat(points[1]);
  207. if (result.getData() == null) {
  208. result.setData(new ArrayList<>());
  209. }
  210. result.getData().add(content);
  211. }
  212. } catch (Exception e) {
  213. System.err.println("gdV3ResultToResult err:" + e);
  214. }
  215. result.setCode(AddressResultEnum.WDJA_SUCCESS);
  216. result.setCode(AddressResultEnum.GDV3_SUCCESS);
  217. return result;
  218. }
  219. }