中国地图省市县区数据抓取

抓取地址:DataV.GeoAtlas地理小工具系列

https://datav.aliyun.com/portal/school/atlas/area_selector#&lat=30.332329214580188&lng=106.75386074913891&zoom=4.5

抓取代码:

抓取后,可以用 Beyond Compare 4  工具查看比较数据抓取是否正确。

package com.lc.iot.controller.collection;

import com.alibaba.fastjson.JSON;
import com.lc.iot.domain.map.MapItemModel;
import com.lc.iot.domain.map.MapModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Slf4j
@RestController
@RequestMapping(value = "/geoArea")
public class GeoAreaController {

    @GetMapping("/GetArea")
    public String GetArea() throws IOException {
        String countryUrl = "https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=100000_full";
        String localCountryPath = "F:\\tools\\map\\" + "100000" + ".json";
        MapModel countryModel = null;
        if (!new File(localCountryPath).exists()) {
            String countryResult = new RestTemplate().getForObject(countryUrl, String.class);
            FileUtils.write(new File(localCountryPath), countryResult, "UTF-8", false);
            countryModel = JSON.parseObject(countryResult, MapModel.class);
        } else {
            String countryResult = FileUtils.readFileToString(new File(localCountryPath), StandardCharsets.UTF_8);
            countryModel = JSON.parseObject(countryResult, MapModel.class);
        }
        log.info(String.format("%s请求成功", countryUrl));
        for (MapItemModel provinceItem : countryModel.features) {
            String provinceAdcode = provinceItem.properties.adcode;
            String provinceName = provinceItem.properties.name;
            Integer provinceChildrenNum = provinceItem.properties.childrenNum;
            if (provinceChildrenNum == null) {
                break;
            }
            String provinceUrl = "https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=" + provinceAdcode + (provinceChildrenNum > 0 ? "_full" : "");
            try {
                String localProvincePath = "F:\\tools\\map\\" + provinceAdcode + ".json";
                MapModel provinceModel = new MapModel();
                if (!new File(localProvincePath).exists()) {
                    String provinceResult = new RestTemplate().getForObject(provinceUrl, String.class);
                    FileUtils.write(new File(localProvincePath), provinceResult, "UTF-8", false);
                    provinceModel = JSON.parseObject(provinceResult, MapModel.class);
                } else {
                    String provinceResult = FileUtils.readFileToString(new File(localProvincePath), StandardCharsets.UTF_8);
                    provinceModel = JSON.parseObject(provinceResult, MapModel.class);
                }
                for (MapItemModel cityItem : provinceModel.features) {
                    String cityAdcode = cityItem.properties.adcode;
                    String cityName = cityItem.properties.name;
                    Integer cityChildrenNum = cityItem.properties.childrenNum;
                    if (cityChildrenNum == null) {
                        break;
                    }
                    String cityUrl = "https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=" + cityAdcode + (cityChildrenNum > 0 ? "_full" : "");
                    try {
                        String localCityPath = "F:\\tools\\map\\" + cityAdcode + ".json";
                        MapModel cityModel = new MapModel();
                        if (!new File(localCityPath).exists()) {
                            String cityResult = new RestTemplate().getForObject(cityUrl, String.class);
                            FileUtils.write(new File(localCityPath), cityResult, "UTF-8", false);
                            cityModel = JSON.parseObject(cityResult, MapModel.class);
                        } else {
                            String cityResult = FileUtils.readFileToString(new File(localCityPath), StandardCharsets.UTF_8);
                            cityModel = JSON.parseObject(cityResult, MapModel.class);
                        }
                        for (MapItemModel townItem : cityModel.features) {
                            String townAdcode = townItem.properties.adcode;
                            String townName = townItem.properties.name;
                            Integer townChildrenNum = townItem.properties.childrenNum;
                            try {
                                String localTownPath = "F:\\tools\\map\\" + townAdcode + ".json";
                                if (!new File(localTownPath).exists()) {
                                    if (townChildrenNum == null) {
                                        break;
                                    }
                                    String townUrl = "https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=" + townAdcode + (townChildrenNum > 0 ? "_full" : "");
                                    String townResult = new RestTemplate().getForObject(townUrl, String.class);
                                    FileUtils.write(new File(localTownPath), townResult, "UTF-8", false);
                                    MapModel townModel = JSON.parseObject(townResult, MapModel.class);
                                }
                            } catch (Exception ex) {
                                log.info(String.format("请求%s - %s数据失败", provinceName + ">>" + cityName + ">>" + townName, cityUrl));
                            }
                        }
                    } catch (Exception cityEx) {
                        log.info(String.format("请求%s - %s数据失败", provinceName + ">>" + cityName, cityUrl));
                    }
                }
            } catch (Exception provinceEx) {
                log.info(String.format("请求%s - %s数据失败", provinceName, provinceUrl));
            }
        }
        return "";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
UniApp 是一款跨平台的开发框架,可以同时开发运行在多个平台的应用程序。在 UniApp 的开发中,经常需要用到省市县区数据,而省市县区数据通常是存储在 js 文件中的。 省市县区的 js 文件一般采用 JSON 格式来存储数据。文件中包含了各个省份的名称和编码,以及每个省份下面的市区数据。每个市区也会包含它下面的县区数据。 开发者可以根据项目的需求,选择合适的省市县区数据文件。通常,在项目的静态资源目录下会有一个名为 "area-data.js" 或者 "city-data.js" 的文件。开发者可以通过导入这个文件来使用省市县区数据。 在前端代码中,可以通过引入这个 js 文件,将省市县区数据赋值给一个变量,以便在页面中使用。开发者可以使用 JavaScript 的方法来处理和遍历这些数据,比如根据省份获取对应的市区数据,或者根据市区获取对应的县区数据等。 通过使用省市县区的 js 文件,可以方便、快捷地获取到需要的地区数据,使得开发者可以更加快速地开发出与地址相关的功能和模块。同时,这种数据的存储方式也方便了数据的更新和维护,开发者只需要替换掉对应的 js 文件即可更新最新的省市县区数据。 总之,UniApp 提供了便捷的省市县区数据文件,使得开发者可以方便地使用这些数据来开发项目中的地址相关功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广东数字化转型

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值