TransfromDataTool.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 szxResultToResult(JSONObject json) {
  43. AddressResult result = new AddressResult();
  44. JSONArray array = json.getJSONArray("result");
  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.setType(jsonObject.getString("type_name"));
  53. List<String> addressList = new ArrayList<>();
  54. content.setAddress(jsonObject.getString("address"));
  55. if (!jsonObject.getString("address").isEmpty()) {
  56. addressList.add(jsonObject.getString("address"));
  57. }
  58. if (!jsonObject.getString("std_address").isEmpty()) {
  59. addressList.add(jsonObject.getString("std_address"));
  60. }
  61. content.setName(jsonObject.getString("name"));
  62. double[] points = new double[]{jsonObject.getJSONObject("location").getDouble("lng"),
  63. jsonObject.getJSONObject("location").getDouble("lat")};
  64. content.setLocation(points[0] + "," + points[1]);
  65. content.setLon(points[0]);
  66. content.setLat(points[1]);
  67. if (jsonObject.containsKey("ext_data")) {
  68. JSONObject extData = jsonObject.getJSONObject("ext_data");
  69. if (extData.containsKey("address") && !extData.getString("address").isEmpty()) {
  70. addressList.add(extData.getString("address"));
  71. }
  72. if (extData.containsKey("source_address") && !extData.getString("source_address").isEmpty()) {
  73. addressList.add(extData.getString("source_address"));
  74. }
  75. if (extData.containsKey("region_jw") && !extData.getString("region_jw").isEmpty()) {
  76. JSONObject cjJson = new JSONObject();
  77. cjJson.put("所属街道", extData.getString("region_jd"));
  78. cjJson.put("所属居委", extData.getString("region_jw"));
  79. content.setCjJson(cjJson);
  80. }
  81. }
  82. for (String item : addressList) {
  83. if (item.contains("上海市") && item.contains(Constant.getArea() + "区") && item.length() > content.getAddress().length()) {
  84. content.setAddress(item);
  85. }
  86. }
  87. if (result.getData() == null) {
  88. result.setData(new ArrayList<>());
  89. }
  90. result.getData().add(content);
  91. }
  92. result.setCode(AddressResultEnum.SZX_SUCCESS);
  93. return result;
  94. }
  95. public static AddressResult gdResultToResult(JSONObject json) {
  96. AddressResult result = new AddressResult();
  97. JSONArray array = json.getJSONArray("geocodes");
  98. if (array == null || array.size() < 1) {
  99. result.setCode(AddressResultEnum.RESULT_NULL);
  100. return result;
  101. }
  102. for (int i = 0; i < array.size(); i++) {
  103. JSONObject jsonObject = array.getJSONObject(i);
  104. AddressResult.ContentBean content = new AddressResult.ContentBean();
  105. content.setPname(jsonObject.getString("city"));
  106. content.setCityname(jsonObject.getString("city"));
  107. content.setAdname(jsonObject.getString("district"));
  108. content.setType(jsonObject.getString("level"));
  109. content.setAddress(jsonObject.getString("formatted_address"));
  110. content.setName(jsonObject.getString("street"));
  111. String[] loc = jsonObject.getString("location").split(",");
  112. double[] points = CoordTransform2.getInstance().gcj02_to_wgs84(
  113. Double.valueOf(loc[0]), Double.valueOf(loc[1]));
  114. content.setLocation(points[0] + "," + points[1]);
  115. content.setLon(points[0]);
  116. content.setLat(points[1]);
  117. if (result.getData() == null) {
  118. result.setData(new ArrayList<>());
  119. }
  120. result.getData().add(content);
  121. }
  122. result.setCode(AddressResultEnum.GD_SUCCESS);
  123. return result;
  124. }
  125. public static AddressResult gdV3ResultToResult(JSONArray array) {
  126. AddressResult result = new AddressResult();
  127. if (array == null || array.size() < 1) {
  128. result.setCode(AddressResultEnum.RESULT_NULL);
  129. return result;
  130. }
  131. try {
  132. for (int i = 0; i < array.size(); i++) {
  133. JSONObject jsonObject = array.getJSONObject(i);
  134. AddressResult.ContentBean content = new AddressResult.ContentBean();
  135. content.setPname(jsonObject.getString("pname"));
  136. content.setCityname(jsonObject.getString("cityname"));
  137. content.setAdname(jsonObject.getString("adname"));
  138. content.setType(jsonObject.getString("type"));
  139. content.setAddress(content.getCityname() + content.getAdname() + jsonObject.getString("address"));
  140. content.setName(jsonObject.getString("name"));
  141. String[] loc = jsonObject.getString("location").split(",");
  142. double[] points = CoordTransform2.getInstance().gcj02_to_wgs84(
  143. Double.parseDouble(loc[0]), Double.parseDouble(loc[1]));
  144. content.setLocation(points[0] + "," + points[1]);
  145. content.setLon(points[0]);
  146. content.setLat(points[1]);
  147. if (result.getData() == null) {
  148. result.setData(new ArrayList<>());
  149. }
  150. result.getData().add(content);
  151. }
  152. } catch (Exception e) {
  153. System.err.println("gdV3ResultToResult err:" + e);
  154. }
  155. result.setCode(AddressResultEnum.WDJA_SUCCESS);
  156. result.setCode(AddressResultEnum.GDV3_SUCCESS);
  157. return result;
  158. }
  159. public static AddressResult yyszResultToResult(JSONObject json){
  160. if(json==null){return null;}
  161. AddressResult result = new AddressResult();
  162. JSONArray array = json.getJSONObject("content").getJSONArray("data");
  163. if (array == null || array.isEmpty()) {
  164. result.setCode(AddressResultEnum.RESULT_NULL);
  165. return result;
  166. }
  167. for (int i = 0; i < array.size(); i++) {
  168. JSONObject jsonObject = array.getJSONObject(i);
  169. AddressResult.ContentBean content = new AddressResult.ContentBean();
  170. content.setPname(jsonObject.getString("pname"));
  171. content.setCityname(jsonObject.getString("cityname"));
  172. content.setAdname(jsonObject.getString("adname"));
  173. content.setCommunity(jsonObject.getString("community"));
  174. content.setCommunityCode(jsonObject.getString("communityCode"));
  175. content.setType(jsonObject.getString("type"));
  176. content.setAddress(jsonObject.getString("address"));
  177. content.setDistance(jsonObject.getString("distance"));
  178. content.setName(jsonObject.getString("name"));
  179. content.setLocation(jsonObject.getString("location"));
  180. content.setLon(jsonObject.getDouble("lon"));
  181. content.setLat(jsonObject.getDouble("lat"));
  182. if (result.getData() == null) {
  183. result.setData(new ArrayList<>());
  184. }
  185. result.getData().add(content);
  186. }
  187. result.setCode(AddressResultEnum.YYSZ_SUCCESS);
  188. return result;
  189. }
  190. }