1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| public final static List<String> CITIES = Arrays.asList("北京市", "上海市", "天津市", "重庆市"); public final static String ADDRESS = "address"; public final static String PROVINCE = "province"; public final static String CITY = "city"; public final static String COUNTY = "county"; public final static String VILLAGE = "village"; public final static String STYLE = "style"; public final static String SP_CITY = "市辖区";
public static void initData() throws IOException { ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("classpath:data/divisions.json"); ObjectMapper objectMapper = new ObjectMapper(); List<ChildrenCode> divisions = objectMapper.readValue(resource.getInputStream(), new TypeReference<>() { }); Divisions.pca = divisions; for (ChildrenCode division : divisions) { Divisions.provinces.add(new Divisions.Province(division.getCode() + "0000", division.getName())); Divisions.provinceMap.put(division.getName(), division.getCode() + "0000"); if (!division.getChildren().isEmpty()) { for (ChildrenCode child1 : division.getChildren()) { if (SP_CITY.equals(child1.getName())) { child1.setName(division.getName() + child1.getName()); Divisions.cityMap.put(division.getName(), division.getCode() + "0000"); } Divisions.cities.add(new Divisions.City(child1.getCode() + "00", child1.getName())); Divisions.cityMap.put(child1.getName(), child1.getCode() + "00"); if (!child1.getChildren().isEmpty()) { for (ChildrenCode child2 : child1.getChildren()) { Divisions.areas.add(new Divisions.Area(child2.getCode(), child2.getName())); if ((division.getName() + SP_CITY).equals(child1.getName())) { Divisions.areaMap.put(division.getName() + child2.getName(), child2.getCode()); } else { Divisions.areaMap.put(child1.getName() + child2.getName(), child2.getCode()); } } } } } } }
public static Map<String, String> addressResolution(String address) { String temp; for (String city : CITIES) { if (address.indexOf(city) == 0 && !address.contains(SP_CITY)) { temp = StrUtil.removePrefix(address, city); if (temp.indexOf(city) != 0) { address = city + address; break; } } } String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)"; Matcher m = Pattern.compile(regex).matcher(address); String province, city, county, town, village; Map<String, String> row = new LinkedHashMap<>(); while (m.find()) { province = m.group(PROVINCE); row.put(PROVINCE, province == null ? "" : province.trim()); city = m.group(CITY); row.put(CITY, city == null ? "" : city.trim()); county = m.group(COUNTY); row.put(COUNTY, county == null ? "" : county.trim()); town = m.group("town"); row.put("town", town == null ? "" : town.trim()); village = m.group(VILLAGE); row.put(VILLAGE, village == null ? "" : village.trim()); } return row; }
public static Map<String, String> addressCodeResolution(String address) { if (ADDRESS.equals(checkChinesAddress(address))) { Map<String, String> map = addressResolution(address); String province = map.get(PROVINCE); String city = map.get(CITY); map.put(PROVINCE, getCode(PROVINCE, map.get(PROVINCE))); map.put(CITY, getCode(CITY, map.get(CITY))); if (SP_CITY.equals(city)) { map.put(CITY, getCode(CITY, province + city)); city = province; } map.put(COUNTY, getCode(COUNTY, city + map.get(COUNTY))); return map; } return null; }
public static String getCode(String type, String name) { return switch (type) { case PROVINCE -> Divisions.provinceMap.get(name); case CITY -> Divisions.cityMap.get(name); case COUNTY -> Divisions.areaMap.get(name); default -> name; }; }
public static String checkChinesAddress(String address) { Map<String, String> map = addressResolution(address); if (map.size() == 0) { return STYLE; } if (StrUtil.isBlank(map.get(PROVINCE))) { return PROVINCE; } if (StrUtil.isBlank(map.get(CITY))) { return CITY; } if (StrUtil.isBlank(map.get(COUNTY))) { return COUNTY; } if (StrUtil.isBlank(map.get(VILLAGE))) { return VILLAGE; } return ADDRESS; }
|