<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.9</version>
</dependency>
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot-starter</artifactId>
<version>1.5.0</version>
</dependency>
/**
* @author 敖癸
* @date 2021/8/19 - 17:16
*/
public interface GeoAtlasClient {
@Get("https://geo.datav.aliyun.com/areas_v3/bound/geojson")
JSONObject getChinaGeo(@Query("code") String adCode);
}
public class ChainGeo extends AbstractEntity{
private Integer adCode;
private String name;
private Integer pid;
@Enumerated(value = EnumType.STRING)
private Level level;
private String acRoutes;
public enum Level{
country,
province,
city,
district
}
}
@Test
void getGeo(){
get("100000_full");
}
private void get(String code){
JSONObject chinaGeo = geoAtlasClient.getChinaGeo(code);
JSONArray features = chinaGeo.getJSONArray("features");
List<JSONObject> featuresObjs = features.toList(JSONObject.class);
List<ChainGeo> chainGeos = new ArrayList<>();
featuresObjs.forEach(feature->{
JSONObject properties = feature.getJSONObject("properties");
String name = properties.getStr("name");
Integer adCode = properties.getInt("adcode");
Integer childrenNum = properties.getInt("childrenNum",0);
String level = properties.getStr("level","country");
JSONObject parent = properties.getJSONObject("parent");
Integer parentCode = 0;
if (parent != null){
parentCode = parent.getInt("adcode");
}
List<Integer> acroutes = (List) properties.get("acroutes");
String acRoutes = null;
if (CollUtil.isNotEmpty(acroutes)){
acRoutes = acroutes.stream().map(Object::toString).collect(Collectors.joining(","));
}
ChainGeo chainGeo = new ChainGeo();
chainGeo.setName(name)
.setPid(parentCode)
.setLevel(Level.valueOf(level))
.setAdCode(adCode)
.setAcRoutes(acRoutes);
chainGeos.add(chainGeo);
if(childrenNum > 0){
get(adCode+"_full");
}
});
chainGeoRepository.saveAll(chainGeos);
}
网友评论