TransfromDataTool.java 9.7 KB

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