TransfromDataTool.java 11 KB

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