123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- 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.entity.AmapAddressV3;
- import com.skyversation.poiaddr.service.AreaService;
- import com.skyversation.poiaddr.util.AddressTools;
- import com.skyversation.poiaddr.util.CoordTransform2;
- import com.skyversation.poiaddr.util.status.AddressResultEnum;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.UUID;
- 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.parseDouble(jsonObject.getString("x")), Double.parseDouble(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 szxResultToResult2(JSONArray array) {
- AddressResult result = new AddressResult();
- 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("province"));
- content.setCityname(jsonObject.getString("district"));
- content.setAdname(jsonObject.getString("town"));
- content.setType(jsonObject.getString("type"));
- content.setAddress(jsonObject.getString("address"));
- content.setName(jsonObject.getString("address"));
- if (jsonObject.containsKey("location") && jsonObject.getJSONObject("location").containsKey("lat") && jsonObject.getJSONObject("location").containsKey("lng")) {
- double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
- Double.parseDouble(jsonObject.getJSONObject("location").getString("lat")), Double.parseDouble(jsonObject.getJSONObject("location").getString("lng")));
- content.setLocation(points[0] + "," + points[1]);
- content.setLat(points[0]);
- content.setLon(points[1]);
- }
- if (result.getData() == null) {
- result.setData(new ArrayList<>());
- }
- result.getData().add(content);
- }
- result.setCode(AddressResultEnum.WDJA_SUCCESS);
- return result;
- }
- public static AddressResult szxResultToResult2(JSONObject jsonObject) {
- AddressResult result = new AddressResult();
- if (jsonObject == null || jsonObject.size() < 1) {
- result.setCode(AddressResultEnum.RESULT_NULL);
- return result;
- }
- AddressResult.ContentBean content = new AddressResult.ContentBean();
- if (jsonObject.containsKey("总分") && jsonObject.get("总分") != null) {
- content.setPname(jsonObject.getString("province"));
- content.setCityname(jsonObject.getString("district"));
- content.setAdname(jsonObject.getString("town"));
- content.setType(jsonObject.getString("type"));
- content.setAddress(jsonObject.getString("address"));
- content.setName(jsonObject.getString("address"));
- content.setSearchAddress(jsonObject.getString("searchAddress"));
- content.setScore(jsonObject.getString("总分"));
- if (jsonObject.containsKey("location") && jsonObject.getJSONObject("location").containsKey("lat") && jsonObject.getJSONObject("location").containsKey("lng")) {
- double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
- Double.parseDouble(jsonObject.getJSONObject("location").getString("lat")), Double.parseDouble(jsonObject.getJSONObject("location").getString("lng")));
- content.setLocation(points[0] + "," + points[1]);
- content.setLat(points[0]);
- content.setLon(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, String searchAddress) {
- AddressResult result = new AddressResult();
- JSONArray array = json.getJSONArray("result");
- if (array == null || array.size() < 1) {
- result.setCode(AddressResultEnum.RESULT_NULL);
- return result;
- }
- // TODO 将请求返回的市中心结果入库到pgsql中
- List<AmapAddressV3> amapAddressV3List = new ArrayList<>();
- try {
- for (int i = 0; i < array.size(); i++) {
- JSONObject item = array.getJSONObject(i);
- AmapAddressV3 amapAddressV3 = new AmapAddressV3();
- amapAddressV3.setId(UUID.randomUUID().toString());
- amapAddressV3.setName(item.get("name").toString());
- amapAddressV3.setType("测绘院结果");
- amapAddressV3.setAddress(item.get("address").toString());
- JSONObject localtion = item.getJSONObject("location");
- Double wgs84Lng = localtion.getDouble("lng");
- Double wgs84Lat = localtion.getDouble("lat");
- double[] points2 = CoordTransform2.getInstance().wgs84_to_shcj(wgs84Lng, wgs84Lat);
- double[] points = CoordTransform2.getInstance().wgs84_to_gcj02(wgs84Lng, wgs84Lat);
- amapAddressV3.setLocation(points[0] + "," + points[1]);
- amapAddressV3.setLon(String.valueOf(points2[0]));
- amapAddressV3.setLat(String.valueOf(points2[1]));
- amapAddressV3.setCreateTime(new Date());
- if (item.containsKey("ext_data")) {
- JSONObject ext_data = item.getJSONObject("ext_data");
- if (!ext_data.get("region_ss").toString().isEmpty()) {
- amapAddressV3.setPname(ext_data.get("region_ss").toString());
- amapAddressV3.setCityname(ext_data.get("region_ss").toString());
- }
- if (!ext_data.get("region_qx").toString().isEmpty()) {
- amapAddressV3.setAdname(ext_data.get("region_qx").toString());
- }
- if (!ext_data.get("region_jd").toString().isEmpty()) {
- amapAddressV3.setStreetTown(ext_data.get("region_jd").toString());
- }
- if (!ext_data.get("region_jw").toString().isEmpty()) {
- amapAddressV3.setResidentialCommittee(ext_data.get("region_jw").toString());
- }
- }
- amapAddressV3List.add(amapAddressV3);
- }
- if (amapAddressV3List.size() > 0) {
- AreaService.getInstance().saveAmapAddressV3(amapAddressV3List);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- JSONObject jsonObject = com.skyversation.poiaddr.util.AddressTools.getInstance().findBestMatch(searchAddress, array, "address");
- if (jsonObject.containsKey("总分") && jsonObject.get("总分") != null && !jsonObject.getString("总分").isEmpty()) {
- AddressResult.ContentBean content = new AddressResult.ContentBean();
- content.setScore(jsonObject.getString("总分"));
- content.setSearchAddress(jsonObject.getString("searchAddress"));
- content.setType(jsonObject.getString("type_name"));
- content.setAddress(jsonObject.getString("address"));
- content.setName(jsonObject.getString("name"));
- // 使用分词方法得到区名和镇名
- content.setCityname(AddressTools.parseAddressCJ(jsonObject.getString("address"))[1]);
- double[] points = new double[]{jsonObject.getJSONObject("location").getDouble("lng"),
- jsonObject.getJSONObject("location").getDouble("lat")};
- content.setLon(points[0]);
- content.setLat(points[1]);
- if (jsonObject.containsKey("point_x") && jsonObject.containsKey("point_y") && jsonObject.get("point_x") != null && jsonObject.get("point_y") != null) {
- content.setLocation(jsonObject.getDouble("point_x") + "," + jsonObject.getDouble("point_y"));
- } else {
- // 市中心返回的结果没有sh2000坐标字段的话就把wgs84的坐标转成sh2000的坐标系并保存到location字段中
- double[] points2 = CoordTransform2.getInstance().wgs84_to_shcj(
- jsonObject.getJSONObject("location").getDouble("lng"), jsonObject.getJSONObject("location").getDouble("lat"));
- content.setLocation(points2[0] + "," + points2[1]);
- }
- content.setAdname(AddressTools.parseAddressCJ(jsonObject.getString("address"))[2]);
- if (jsonObject.containsKey("ext_data")) {
- JSONObject extData = jsonObject.getJSONObject("ext_data");
- if (extData.containsKey("region_jw") && !extData.getString("region_jw").isEmpty()) {
- content.setCommunity(extData.getString("region_jw"));
- }
- // 如果评分大于1.6,那么就使用接口返回的街镇,否则使用源搜索地址的街镇
- if (jsonObject.getString("总分").contains("rule_") || Float.parseFloat(jsonObject.getString("总分")) > 1.6) {
- if (extData.containsKey("region_jd") && !extData.getString("region_jd").isEmpty()) {
- content.setAdname(extData.getString("region_jd"));
- }
- } else if (AddressTools.parseAddressCJ(searchAddress)[2] != null && !AddressTools.parseAddressCJ(searchAddress)[2].isEmpty()) {
- content.setAdname(AddressTools.parseAddressCJ(searchAddress)[2]);
- }
- }
- 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 AddressResult gdV3ResultToResult(String searchAddress, JSONArray array, boolean isInserted) {
- AddressResult result = new AddressResult();
- try {
- if (array == null || array.size() < 1) {
- result.setCode(AddressResultEnum.RESULT_NULL);
- return result;
- }
- JSONObject jsonObject = com.skyversation.poiaddr.util.AddressTools.getInstance().findBestMatch(searchAddress, array, "address");
- if (jsonObject != null && jsonObject.containsKey("总分") && jsonObject.get("总分") != null && !jsonObject.getString("总分").isEmpty()) {
- AddressResult.ContentBean content = new AddressResult.ContentBean();
- content.setScore(jsonObject.getString("总分"));
- content.setSearchAddress(jsonObject.getString("searchAddress"));
- content.setPname(jsonObject.getString("pname"));
- content.setCityname(jsonObject.getString("adname"));
- if (jsonObject.containsKey("street_town") && !jsonObject.getString("street_town").isEmpty()) {
- content.setAdname(jsonObject.getString("street_town"));
- }
- content.setType(jsonObject.getString("type"));
- content.setAddress(content.getPname() + content.getAdname() + jsonObject.getString("address"));
- content.setName(jsonObject.getString("name"));
- String[] loc = jsonObject.getString("location").split(",");
- double[] points = CoordTransform2.getInstance().gcj02_to_shcj(
- Double.parseDouble(loc[0]), Double.parseDouble(loc[1]));
- content.setLocation(points[0] + "," + points[1]);
- double[] points2 = CoordTransform2.getInstance().gcj02_to_wgs84(
- Double.parseDouble(loc[0]), Double.parseDouble(loc[1]));
- content.setLon(points2[0]);
- content.setLat(points2[1]);
- if (result.getData() == null) {
- result.setData(new ArrayList<>());
- }
- result.getData().add(content);
- }
- if (isInserted) {
- // TODO 将请求返回的高德结果入库到pgsql中
- List<AmapAddressV3> amapAddressV3List = new ArrayList<>();
- for (int i = 0; i < array.size(); i++) {
- JSONObject item = array.getJSONObject(i);
- AmapAddressV3 amapAddressV3 = new AmapAddressV3();
- amapAddressV3.setId(UUID.randomUUID().toString());
- amapAddressV3.setParent(item.get("parent").toString());
- amapAddressV3.setChildtype(item.get("childtype").toString());
- amapAddressV3.setName(item.get("name").toString());
- amapAddressV3.setType(item.get("type").toString());
- amapAddressV3.setTypecode(item.get("typecode").toString());
- amapAddressV3.setBizType(item.get("biz_type").toString());
- amapAddressV3.setAddress(item.get("address").toString());
- amapAddressV3.setLocation(item.get("location").toString());
- amapAddressV3.setTel(item.get("tel").toString());
- amapAddressV3.setPname(item.get("pname").toString());
- amapAddressV3.setAdname(item.get("adname").toString());
- amapAddressV3.setImportance(item.get("importance").toString());
- amapAddressV3.setShopid(item.get("shopid").toString());
- amapAddressV3.setShopinfo(item.get("shopinfo").toString());
- amapAddressV3.setPoiweight(item.get("poiweight").toString());
- amapAddressV3.setDistance(item.get("distance").toString());
- amapAddressV3.setBizExt(item.get("biz_ext").toString());
- amapAddressV3.setPhotos(item.get("photos").toString());
- amapAddressV3.setCreateTime(new Date());
- amapAddressV3List.add(amapAddressV3);
- }
- if (amapAddressV3List.size() > 0) {
- AreaService.getInstance().saveAmapAddressV3(amapAddressV3List);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println("gdV3ResultToResult err:" + e);
- }
- result.setCode(AddressResultEnum.GDV3_SUCCESS);
- result.setMessage("数据库查询成功!");
- /*if (isInserted) {
- System.out.println("*******高德查询结果返回,查询地址是:" + searchAddress);
- } else {
- System.out.println("&&&&&&&数据库查询结果返回,查询地址是:" + searchAddress);
- }*/
- return result;
- }
- }
|