|
@@ -48,8 +48,7 @@ public class AddressQueryEngine {
|
|
|
AddressResult addressResult = new AddressResult();
|
|
|
for (String addr : addrs) {
|
|
|
// 创建请求
|
|
|
- addressResult = sj_szxSearchByName(addr);
|
|
|
-// addressResult = szxSearchByName(addr);
|
|
|
+ addressResult = sj_szxSearchByName(addr, 3);
|
|
|
if (addressResult != null) {
|
|
|
addressResult.setCode(AddressResultEnum.SZX_SUCCESS);
|
|
|
addressResult.setMessage("成功");
|
|
@@ -95,8 +94,21 @@ public class AddressQueryEngine {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static Map<String, String> initSigns() {
|
|
|
+ Map<String, String> headerMap = new HashMap<>();
|
|
|
+ String timestamp = System.currentTimeMillis() + "";
|
|
|
+ String appCode = "7unv4vbwqxnq4a7m9h";
|
|
|
+ String UUid = UUID.randomUUID().toString();
|
|
|
+ String appSecret = "w6tawvf4k3ck4ikij9";
|
|
|
+ headerMap.put("x-timestamp", timestamp);
|
|
|
+ headerMap.put("x-appcode", appCode);
|
|
|
+ headerMap.put("x-uuid", UUid);
|
|
|
+// 签名
|
|
|
+ headerMap.put("x-sign", AddressQueryEngine.getSigns(timestamp, appCode, UUid, appSecret));
|
|
|
+ return headerMap;
|
|
|
+ }
|
|
|
|
|
|
- public String getSigns(String timestamp, String appCode, String UUid, String appSecret) {
|
|
|
+ public static String getSigns(String timestamp, String appCode, String UUid, String appSecret) {
|
|
|
// 先拼接
|
|
|
String signData = timestamp + appCode + UUid;
|
|
|
// 生成sign
|
|
@@ -112,24 +124,36 @@ public class AddressQueryEngine {
|
|
|
return sign;
|
|
|
}
|
|
|
|
|
|
- public AddressResult sj_szxSearchByName(String address) {
|
|
|
+ public AddressResult sj_szxSearchByName(String address, Integer level) {
|
|
|
// 鉴权
|
|
|
- Map<String, String> headerMap = new HashMap<>();
|
|
|
- String timestamp = System.currentTimeMillis() + "";
|
|
|
- String appCode = "7unv4vbwqxnq4a7m9h";
|
|
|
- String UUid = UUID.randomUUID().toString();
|
|
|
- String appSecret = "w6tawvf4k3ck4ikij9";
|
|
|
- headerMap.put("x-timestamp", timestamp);
|
|
|
- headerMap.put("x-appcode", appCode);
|
|
|
- headerMap.put("x-uuid", UUid);
|
|
|
-// 签名
|
|
|
- headerMap.put("x-sign", getSigns(timestamp, appCode, UUid, appSecret));
|
|
|
JSONObject params = new JSONObject();
|
|
|
JSONArray requestJson = new JSONArray();
|
|
|
JSONObject paramObject = new JSONObject();
|
|
|
paramObject.put("name", "query");
|
|
|
paramObject.put("position", "QUERY");
|
|
|
- paramObject.put("value", address);
|
|
|
+ switch (level) {
|
|
|
+ case 3:
|
|
|
+ paramObject.put("value", address);
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ if (address.contains("号")) {
|
|
|
+ paramObject.put("value", address.substring(0, address.lastIndexOf("号") + 1));
|
|
|
+ } else {
|
|
|
+ level--;
|
|
|
+ return sj_szxSearchByName(address, level);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ if (address.contains("弄")) {
|
|
|
+ paramObject.put("value", address.substring(0, address.lastIndexOf("弄") + 1));
|
|
|
+ } else {
|
|
|
+ paramObject.put("value", address);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (!address.startsWith("上海")) {
|
|
|
+ paramObject.put("value", "上海市" + paramObject.get("value"));
|
|
|
+ }
|
|
|
requestJson.add(paramObject);
|
|
|
// 判断是否是关键字版搜索服务
|
|
|
if (Constant.SJ_SZX_SEARCH_BY_NAME.contains("1742459783686")) {
|
|
@@ -146,14 +170,25 @@ public class AddressQueryEngine {
|
|
|
}
|
|
|
params.put("requestJson", requestJson);
|
|
|
// 发起请求
|
|
|
- ResponseEntity response = AddressNetTools.getInstance().requestGetOrPost(HttpMethod.POST, Constant.SJ_SZX_SEARCH_BY_NAME, params, headerMap, 1);
|
|
|
+ ResponseEntity response = AddressNetTools.getInstance().requestGetOrPost(HttpMethod.POST, Constant.SJ_SZX_SEARCH_BY_NAME, params, initSigns(), 5);
|
|
|
if (response != null && response.hasBody()) {
|
|
|
String body = response.getBody() + "";
|
|
|
- if (!StringUtils.hasText(body))
|
|
|
+ if (!StringUtils.hasText(body)) {
|
|
|
+ if (level > 0) {
|
|
|
+ level--;
|
|
|
+ return sj_szxSearchByName(address, level);
|
|
|
+ }
|
|
|
return AddressTools.getInstance().faildQuery(AddressResultEnum.RESULT_NULL, "搜索无结果");
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
if (body.indexOf("[") != 0) {
|
|
|
- return TransfromDataTool.szxResultToResult(JSONObject.parseObject(body), address);
|
|
|
+ AddressResult addressResult = TransfromDataTool.szxResultToResult(JSONObject.parseObject(body), address);
|
|
|
+ if (addressResult.getCode().equals(AddressResultEnum.RESULT_NULL) && level > 0) {
|
|
|
+ level--;
|
|
|
+ return sj_szxSearchByName(address, level);
|
|
|
+ }
|
|
|
+ return addressResult;
|
|
|
} else {
|
|
|
// 将得分最高的结果映射到实体类
|
|
|
return TransfromDataTool.szxResultToResult2(com.skyversation.poiaddr.util.AddressTools.getInstance().findBestMatch(address, JSONArray.parseArray(body), "address"));
|
|
@@ -163,58 +198,10 @@ public class AddressQueryEngine {
|
|
|
return AddressTools.getInstance().faildQuery(AddressResultEnum.DATA_FROMAT_FAILD, "格式化失败");
|
|
|
}
|
|
|
} else {
|
|
|
- System.out.println("地址[" + address + "]未查询到数据!");
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public Object sj_szxSearchByName_to_server(String address) {
|
|
|
-// 鉴权
|
|
|
- Map<String, String> headerMap = new HashMap<>();
|
|
|
- String timestamp = System.currentTimeMillis() + "";
|
|
|
- String appCode = "7unv4vbwqxnq4a7m9h";
|
|
|
- String UUid = UUID.randomUUID().toString();
|
|
|
- String appSecret = "w6tawvf4k3ck4ikij9";
|
|
|
- headerMap.put("x-timestamp", timestamp);
|
|
|
- headerMap.put("x-appcode", appCode);
|
|
|
- headerMap.put("x-uuid", UUid);
|
|
|
-// 签名
|
|
|
- headerMap.put("x-sign", getSigns(timestamp, appCode, UUid, appSecret));
|
|
|
- JSONObject params = new JSONObject();
|
|
|
- JSONArray requestJson = new JSONArray();
|
|
|
- JSONObject paramObject = new JSONObject();
|
|
|
- paramObject.put("name", "query");
|
|
|
- paramObject.put("position", "QUERY");
|
|
|
- paramObject.put("value", address);
|
|
|
- requestJson.add(paramObject);
|
|
|
-// 判断是否是关键字版搜索服务
|
|
|
- if (Constant.SJ_SZX_SEARCH_BY_NAME.contains("1742459783686")) {
|
|
|
- JSONObject paramObject2 = new JSONObject();
|
|
|
- paramObject2.put("name", "region");
|
|
|
- paramObject2.put("position", "QUERY");
|
|
|
- paramObject2.put("value", Constant.getAMAP_CITY_CODE());
|
|
|
- requestJson.add(paramObject2);
|
|
|
- JSONObject paramObject3 = new JSONObject();
|
|
|
- paramObject3.put("name", "page_size");
|
|
|
- paramObject3.put("position", "QUERY");
|
|
|
- paramObject3.put("value", "10");
|
|
|
- requestJson.add(paramObject3);
|
|
|
- }
|
|
|
- params.put("requestJson", requestJson);
|
|
|
-// 发起请求
|
|
|
- ResponseEntity response = AddressNetTools.getInstance().requestGetOrPost(HttpMethod.POST, Constant.SJ_SZX_SEARCH_BY_NAME, params, headerMap, 1);
|
|
|
- if (response != null && response.hasBody()) {
|
|
|
- String body = response.getBody() + "";
|
|
|
- if (!StringUtils.hasText(body))
|
|
|
- return AddressTools.getInstance().faildQuery(AddressResultEnum.RESULT_NULL, "搜索无结果");
|
|
|
- try {
|
|
|
- return JSONObject.parseObject(body);
|
|
|
- } catch (Exception e) {
|
|
|
- System.err.println("请求地址:" + address + ";返回结果:" + body + ";处理异常:" + e);
|
|
|
- return AddressTools.getInstance().faildQuery(AddressResultEnum.DATA_FROMAT_FAILD, "格式化失败");
|
|
|
+ if (level > 0) {
|
|
|
+ level--;
|
|
|
+ return sj_szxSearchByName(address, level);
|
|
|
}
|
|
|
- } else {
|
|
|
System.out.println("地址[" + address + "]未查询到数据!");
|
|
|
return null;
|
|
|
}
|
|
@@ -308,11 +295,22 @@ public class AddressQueryEngine {
|
|
|
} else {
|
|
|
AreaService areaService = AreaService.getInstance();
|
|
|
for (AddressResult.ContentBean content : result.getData()) {
|
|
|
- if ((content.getAdname() == null || content.getAdname().isEmpty() || content.getCityname() == null || content.getCityname().isEmpty()) && content.getLat() != null && content.getLon() != null) {
|
|
|
- GeoJsonBean adBean = areaService.isInadPolygon(content.getLon(), content.getLat());
|
|
|
- content.setAdJson(adBean == null ? new JSONObject() : adBean.getProperties());
|
|
|
- GeoJsonBean townBean = areaService.isInTownPolygon(content.getLon(), content.getLat());
|
|
|
- content.setTownJson(townBean == null ? new JSONObject() : townBean.getProperties());
|
|
|
+ if ((content.getAdname() == null || content.getAdname().isEmpty() || content.getCityname() == null || content.getCityname().isEmpty() || content.getCommunity() == null || content.getCommunity().isEmpty()) && content.getLat() != null && content.getLon() != null) {
|
|
|
+// 判断是否存在行政区划数据
|
|
|
+ if (content.getCityname() == null || content.getCityname().isEmpty()) {
|
|
|
+ GeoJsonBean adBean = areaService.isInadPolygon(content.getLon(), content.getLat());
|
|
|
+ content.setAdJson(adBean == null ? new JSONObject() : adBean.getProperties());
|
|
|
+ }
|
|
|
+// 判断是否存在街镇数据
|
|
|
+ if (content.getAdname() == null || content.getAdname().isEmpty()) {
|
|
|
+ GeoJsonBean townBean = areaService.isInTownPolygon(content.getLon(), content.getLat());
|
|
|
+ content.setTownJson(townBean == null ? new JSONObject() : townBean.getProperties());
|
|
|
+ }
|
|
|
+// 判断是否存在村居数据
|
|
|
+ if (content.getCommunity() == null || content.getCommunity().isEmpty()) {
|
|
|
+ GeoJsonBean cjBean = areaService.isInResidentialCommitteePolygon(content.getLon(), content.getLat());
|
|
|
+ content.setCjJson(cjBean == null ? new JSONObject() : cjBean.getProperties());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
@@ -448,12 +446,13 @@ public class AddressQueryEngine {
|
|
|
return address.replaceAll("上海市", "").replaceAll(Constant.getArea() + "区", "").replaceAll("-", "").replaceAll("_", "").replaceAll("/", "").replaceAll(" ", "").replaceAll(",", "").replaceAll("\\.", "").replaceAll(",", "").replaceAll("。", "").replaceAll("\\+", "").replaceAll("\\*", "");
|
|
|
}
|
|
|
|
|
|
- public static String townReplaceAll(String address) {
|
|
|
+ public static String townReplace_ct(String address) {
|
|
|
+ address = address.replaceAll(" ", "");
|
|
|
// 替换市
|
|
|
address = address.replaceAll("上海市", "");
|
|
|
String[] towns = Constant.getTowns();
|
|
|
for (String town : towns) {
|
|
|
- if(address.contains(town)){
|
|
|
+ if (address.contains(town)) {
|
|
|
address = address.substring(address.indexOf(town) + town.length());
|
|
|
}
|
|
|
}
|
|
@@ -464,6 +463,11 @@ public class AddressQueryEngine {
|
|
|
if (address.contains("委员会")) {
|
|
|
address = address.substring(address.indexOf("委员会") + 3);
|
|
|
}
|
|
|
+ return address;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String townReplaceAll(String address) {
|
|
|
+ address = townReplace_ct(address);
|
|
|
return address.replaceAll("小区", "0").replaceAll("号", "0").replaceAll("弄", "0").replaceAll("室", "0").replaceAll("户", "0").replaceAll("单元", "0").replaceAll("幢", "0");
|
|
|
}
|
|
|
|