package com.skyversation.poiaddr.addquery; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.skyversation.poiaddr.bean.AddressResult; import com.skyversation.poiaddr.util.CoordTransform2; import com.skyversation.poiaddr.util.status.AddressResultEnum; import java.util.ArrayList; import java.util.List; public class TransfromDataTool { public static AddressResult wdResultToResult(JSONObject json) { AddressResult result = new AddressResult(); JSONArray array = json.getJSONObject("data").getJSONArray("addrList"); if (array == null || array.size() < 1) { result.setCode(AddressResultEnum.RESULT_NULL); return result; } for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); AddressResult.ContentBean content = new AddressResult.ContentBean(); content.setPname(jsonObject.getString("city")); content.setCityname(jsonObject.getString("city")); content.setAdname(jsonObject.getString("town")); content.setCommunity(jsonObject.getString("community")); content.setCommunityCode(jsonObject.getString("communitycode")); content.setType(jsonObject.getString("datasource")); content.setAddress(jsonObject.getString("addr")); content.setDistance(jsonObject.getString("lv")); content.setName(jsonObject.getString("addr")); double[] points = CoordTransform2.getInstance().shcj_to_wgs84( Double.valueOf(jsonObject.getString("x")), Double.valueOf(jsonObject.getString("y"))); content.setLocation(points[0] + "," + points[1]); content.setLon(points[0]); content.setLat(points[1]); if (result.getData() == null) { result.setData(new ArrayList<>()); } result.getData().add(content); } result.setCode(AddressResultEnum.WDJA_SUCCESS); return result; } public static AddressResult szxResultToResult(JSONObject json) { AddressResult result = new AddressResult(); JSONArray array = json.getJSONArray("result"); if (array == null || array.size() < 1) { result.setCode(AddressResultEnum.RESULT_NULL); return result; } for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); AddressResult.ContentBean content = new AddressResult.ContentBean(); content.setType(jsonObject.getString("type_name")); List addressList = new ArrayList<>(); content.setAddress(jsonObject.getString("address")); if (!jsonObject.getString("address").isEmpty()) { addressList.add(jsonObject.getString("address")); } if (!jsonObject.getString("std_address").isEmpty()) { addressList.add(jsonObject.getString("std_address")); } content.setName(jsonObject.getString("name")); double[] points = new double[]{jsonObject.getJSONObject("location").getDouble("lng"), jsonObject.getJSONObject("location").getDouble("lat")}; content.setLocation(points[0] + "," + points[1]); content.setLon(points[0]); content.setLat(points[1]); if (jsonObject.containsKey("ext_data")) { JSONObject extData = jsonObject.getJSONObject("ext_data"); if (extData.containsKey("address") && !extData.getString("address").isEmpty()) { addressList.add(extData.getString("address")); } if (extData.containsKey("source_address") && !extData.getString("source_address").isEmpty()) { addressList.add(extData.getString("source_address")); } if (extData.containsKey("region_jw") && !extData.getString("region_jw").isEmpty()) { JSONObject cjJson = new JSONObject(); cjJson.put("所属街道", extData.getString("region_jd")); cjJson.put("所属居委", extData.getString("region_jw")); content.setCjJson(cjJson); } } for (String item : addressList) { if (item.contains("上海市") && item.contains(Constant.getArea() + "区") && item.length() > content.getAddress().length()) { content.setAddress(item); } } if (result.getData() == null) { result.setData(new ArrayList<>()); } result.getData().add(content); } result.setCode(AddressResultEnum.SZX_SUCCESS); return result; } public static AddressResult gdResultToResult(JSONObject json) { AddressResult result = new AddressResult(); JSONArray array = json.getJSONArray("geocodes"); if (array == null || array.size() < 1) { result.setCode(AddressResultEnum.RESULT_NULL); return result; } for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); AddressResult.ContentBean content = new AddressResult.ContentBean(); content.setPname(jsonObject.getString("city")); content.setCityname(jsonObject.getString("city")); content.setAdname(jsonObject.getString("district")); content.setType(jsonObject.getString("level")); content.setAddress(jsonObject.getString("formatted_address")); content.setName(jsonObject.getString("street")); String[] loc = jsonObject.getString("location").split(","); double[] points = CoordTransform2.getInstance().gcj02_to_wgs84( Double.valueOf(loc[0]), Double.valueOf(loc[1])); content.setLocation(points[0] + "," + points[1]); content.setLon(points[0]); content.setLat(points[1]); if (result.getData() == null) { result.setData(new ArrayList<>()); } result.getData().add(content); } result.setCode(AddressResultEnum.GD_SUCCESS); return result; } public static AddressResult gdV3ResultToResult(JSONArray array) { AddressResult result = new AddressResult(); if (array == null || array.size() < 1) { result.setCode(AddressResultEnum.RESULT_NULL); return result; } try { for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); AddressResult.ContentBean content = new AddressResult.ContentBean(); content.setPname(jsonObject.getString("pname")); content.setCityname(jsonObject.getString("cityname")); content.setAdname(jsonObject.getString("adname")); content.setType(jsonObject.getString("type")); content.setAddress(content.getCityname() + content.getAdname() + jsonObject.getString("address")); content.setName(jsonObject.getString("name")); String[] loc = jsonObject.getString("location").split(","); double[] points = CoordTransform2.getInstance().gcj02_to_wgs84( Double.parseDouble(loc[0]), Double.parseDouble(loc[1])); content.setLocation(points[0] + "," + points[1]); content.setLon(points[0]); content.setLat(points[1]); if (result.getData() == null) { result.setData(new ArrayList<>()); } result.getData().add(content); } } catch (Exception e) { System.err.println("gdV3ResultToResult err:" + e); } result.setCode(AddressResultEnum.WDJA_SUCCESS); result.setCode(AddressResultEnum.GDV3_SUCCESS); return result; } public static AddressResult yyszResultToResult(JSONObject json){ if(json==null){return null;} AddressResult result = new AddressResult(); JSONArray array = json.getJSONObject("content").getJSONArray("data"); if (array == null || array.isEmpty()) { result.setCode(AddressResultEnum.RESULT_NULL); return result; } for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); AddressResult.ContentBean content = new AddressResult.ContentBean(); content.setPname(jsonObject.getString("pname")); content.setCityname(jsonObject.getString("cityname")); content.setAdname(jsonObject.getString("adname")); content.setCommunity(jsonObject.getString("community")); content.setCommunityCode(jsonObject.getString("communityCode")); content.setType(jsonObject.getString("type")); content.setAddress(jsonObject.getString("address")); content.setDistance(jsonObject.getString("distance")); content.setName(jsonObject.getString("name")); content.setLocation(jsonObject.getString("location")); content.setLon(jsonObject.getDouble("lon")); content.setLat(jsonObject.getDouble("lat")); if (result.getData() == null) { result.setData(new ArrayList<>()); } result.getData().add(content); } result.setCode(AddressResultEnum.YYSZ_SUCCESS); return result; } }