全国省市区数据抓取

    
      cn.hutool
      hutool-all
      5.7.9
    
    
      com.dtflys.forest
      forest-spring-boot-starter
      1.5.0
    
/**
 * @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 featuresObjs = features.toList(JSONObject.class);
        List 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 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);
    }

你可能感兴趣的:(全国省市区数据抓取)