全国各省市县级边界范围及其中心城区查询(调用百度地图)

做它的原因
接了老师一个项目,老师要我先去收集各个省市县级的边界坐标以及中心城区范围,然后老师要用R语言去画图调用。然后我就直接百度去下载现成的数据,但是老师说不行,要最新的,以前网上的数据都是以前的。现在好多行政区县都已经更新了。近几年的数据还没有。所以只能自己做了。也能确保数据的准确性。
特地强调。确实echarts可以画地图。但是里面的边界数据并不精确。如果你需要做的项目是边界必须特别精确的,而是最新的。你得自己获取。echarts里的china.js数据已经老了。

这里写图片描述
这里写图片描述
前期准备
首先我们需要城市信息。哪里来这些城市的信息呢?第一个想到的是百度文科直接下载,第二个是爬虫。但是老师说,还是爬虫好,让我把中国天气预报网上的数据爬下来。那里的城市信息准确。然后我就用八爪鱼爬了(懒得自己写爬虫。。。)。爬下来以后发现。数据不对。天气预报网上的地方确实是最新的。但是天气预备有个特点。相邻区域就不一定会报。比如上海市中心就包含了好几个区。其次用风景区代替城市。导致我查找的地址不准确。做出来画的地图不对。所以,我还是去百度文库里下了一份2016年的省市县级的名称。
这里写图片描述
不过数据不准确还是得做处理。可以看见,首先,各个省没有分开。意思是我省的数据也是需要的。然后应该纯在省,空,空这样的数据元。还有安庆这里就写了安庆。有些百度是可以直接出来的。但是还有的不行比如巢湖。这个城市你在查找边界范围的时候不输入巢湖市出不来。所以我们需要对这些数据做处理。把省级分开。把名字补全,那些结尾不是区,镇,县,街道,旗(蒙古的比较多)的数据,尾部补上市,便于查找。把重复的这里写图片描述给规整,市级是三沙市就是三沙市,县级下面不应该再来个三沙市。
当然这么做还有个前提,要读取excel文件。
我这里采用的是个包pio-3.16。具体方式是参考 http://blog.csdn.net/sinat_29581293/article/details/52122191 它的。
然后展示下,我修改数据的文件

package dealWithSource;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.InputStream;
import java.util.ArrayList;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
 * 
 * @描述:测试excel读取
 * 
 */

public class importExcel
{
   
    /** 总行数 */
    private int totalRows= 0;
    /** 总列数 */
    private int totalCells= 0;
    /** 错误信息 */
    private String errorInfo;

    /** 构造方法 */
    public importExcel()
    {
    }

    public int getTotalRows()
    {
        return totalRows;
    }

    public int getTotalCells()
    {
        return totalCells;
    }

    public String getErrorInfo()
    {
        return errorInfo;
    }

    public boolean validateExcel(String filePath)
    {
        /** 检查文件名是否为空或者是否是Excel格式的文件 */
        if(filePath == null || !(WDWUtil.isExcel2003(filePath) || WDWUtil.isExcel2007(filePath)))
        {
            errorInfo= "文件名不是excel格式";
            return false;
        }
        /** 检查文件是否存在 */
        File file= new File(filePath);
        if(file == null || !file.exists())
        {
            errorInfo= "文件不存在";
            return false;
        }
        return true;
    }

    public List<List<String>> read(String filePath)
    {
        List<List<String>> dataLst= new ArrayList<List<String>>();
        InputStream is= null;
        try
        {
            /** 验证文件是否合法 */
            if(!validateExcel(filePath))
            {
                System.out.println(errorInfo);
                return null;
            }
            /** 判断文件的类型,是2003还是2007 */
            boolean isExcel2003= true;
            if(WDWUtil.isExcel2007(filePath))
            {
                isExcel2003= false;
            }
            /** 调用本类提供的根据流读取的方法 */
            File file= new File(filePath);
            is= new FileInputStream(file);
            dataLst= read(is,isExcel2003);
            is.close();
        }catch (Exception ex)
        {
            ex.printStackTrace();
        }finally
        {
            if(is != null)
            {
                try
                {
                    is.close();
                }catch (IOException e)
                {
                    is= null;
                    e.printStackTrace();
                }
            }
        }
        /** 返回最后读取的结果 */
        return dataLst;
    }

    public List<List<String>> read(InputStream inputStream, boolean isExcel2003)
    {
        List<List<String>> dataLst= null;
        try
        {
            /** 根据版本选择创建Workbook的方式 */
            Workbook wb= null;
            if(isExcel2003)
            {
                wb= new HSSFWorkbook(inputStream);
            }else
            {
                wb= new XSSFWorkbook(inputStream);
            }
            dataLst= read(wb);
        }catch (IOException e)
        {

            e.printStackTrace();
        }
        return dataLst;
    }

    private List<List<String>> read(Workbook wb)
    {
        List<List<String>> dataLst= new ArrayList<List<String>>();
        /** 得到第一个shell */
        Sheet sheet= wb.getSheetAt(0);
        /** 得到Excel的行数 */
        this.totalRows= sheet.getPhysicalNumberOfRows();
        /** 得到Excel的列数 */
        if(this.totalRows >= 1 && sheet.getRow(0) != null)
        {
            this.totalCells= sheet.getRow(0).getPhysicalNumberOfCells();
        }
        /** 循环Excel的行 */
        for(int r= 0; r < this.totalRows; r++)
        {
            Row row= sheet.getRow(r);
            if(row == null)
            {
                continue;
            }
            List<String> rowLst= new ArrayList<String>();
            /** 循环Excel的列 */
            for(int c= 0; c < this.getTotalCells(); c++)
            {
                Cell cell= row.getCell(c);
                String cellValue= "";
                if(null != cell)
                {
                    // 以下是判断数据的类型
                    switch(cell.getCellType())
                    {
                        case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                            cellValue= cell.getNumericCellValue() + "";
                            break;
                        case HSSFCell.CELL_TYPE_STRING: // 字符串
                            cellValue= cell.getStringCellValue();
                            break;
                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                            cellValue= cell.getBooleanCellValue() + "";
                            break;
                        case HSSFCell.CELL_TYPE_FORMULA: // 公式
                            cellValue= cell.getCellFormula() + "";
                            break;
                        case HSSFCell.CELL_TYPE_BLANK: // 空值
                            cellValue= "";
                            break;
                        case HSSFCell.CELL_TYPE_ERROR: // 故障
                            cellValue= "非法字符";
                            break;
                        default:
                            cellValue= "未知类型";
                            break;
                    }
                }
                rowLst.add(cellValue);
            }
            /** 保存第r行的第c列 */
            dataLst.add(rowLst);
        }
        return dataLst;
    }

    // 到处表格
    public void write(List<List<String>> source)
    {
        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb= new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet= wb.createSheet("省市");
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row= sheet.createRow((int) 0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style= wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell= row.createCell((short) 0);
        cell.setCellValue("省");
        cell.setCellStyle(style);
        cell= row.createCell((short) 1);
        cell.setCellValue("市");
        cell.setCellStyle(style);
        cell= row.createCell((short) 2);
        cell.setCellValue("县");
        cell.setCellStyle(style);

        // 第五步,写入实体数据 实际应用中这些数据从数据库得到,
        List<List<String>> list= source;
        List<String> temp=new ArrayList<String>();
        for(int i= 0; i < list.size(); i++)
        {
            row= sheet.createRow((int) i + 1);
            temp=list.get(i);
            // 第四步,创建单元格,并设置值
            for(int j=0;j<temp.size();j++)
            {
                row.createCell((short) j).setCellValue(temp.get(j));
            }

        }
        // 第六步,将文件存到指定位置
        try
        {
            FileOutputStream fout= new FileOutputStream("E:/模版.xls");
            wb.write(fout);
            fout.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception
    {
        importExcel poi= new importExcel();
        // List<List<String>> list = poi.read("d:/aaa.xls");
        List<List<String>> list= poi.read("E:/QQ文件/中国各省市邮政编码、电话区号大全.xls");
        if(list != null)
        {
            for(int i= 0; i < list.size(); i++)
            {
                System.out.print("第" + (i) + "行");
                List<String> cellList= list.get(i);
                for(int j= 0; j < cellList.size(); j++)
                {
                    // System.out.print(" 第" + (j + 1) + "列值:");
                    System.out.print("    " + cellList.get(j));
                }
                System.out.println();
            }

        }

    }

}

class WDWUtil
{
    public static boolean isExcel2003(String filePath)
    {
        return filePath.matches("^.+\\.(?i)(xls)$");
    }

    public static boolean isExcel2007(String filePath)
    {
        return filePath.matches("^.+\\.(?i)(xlsx)$");
    }
}
package dealWithSource;

import java.util.ArrayList;
import java.util.List;

//处理表单。把表格调节成标准格式 (省,市,县)
public class createTable
{

    public static void main(String[] args)
    {
        importExcel poi= new importExcel();
        // 省 市 县 
        List<List<String>> result= new ArrayList<List<String>>();
        try
        {
            List<List<String>> list= poi.read("E:/全国县级以上城市行政区划表.xls");

            if(list != null)
            {
                //上一个省的名字,用于把省分隔开
                String previousProvince="";
                for(int i= 0; i < list.size(); i++)
                {
                    List<String> cellList= list.get(i);
                    List<String> res= new ArrayList<String>();

                    String province=cellList.get(0).trim();
                    String city= dealWith(cellList.get(1).trim());
                    String country=dealWith(cellList.get(2).trim());

                    if(!previousProvince.equals(province))
                    {
                        previousProvince=province;
                        //当遇到一个新的省的时候,那一行,就一个省字段,市,县为空
                        res.add(province);
                        res.add("");
                        res.add("");
                        result.add(res);
                        res= new ArrayList<String>();
                    }
                    //省和市的照常添加
                    res.add(province);
                    res.add(city);
                    if(country.equals("")||country.equals(city))
                    {
                        res.add("");// 县为空
                    }
                    else
                    {
                        res.add(country);
                    }
                    System.out.println(province+" "+city+" "+country);
                    result.add(res);
                }

            }
            poi.write(result);

        }catch (Exception e)
        {
            poi.write(result);
            e.printStackTrace();
        }
    }

    //给没加市的城市加上市,便于查找相应地区
    public static String dealWith(String result)
    {
        result=result.trim();
        if(result==null||result.equals(""))
            return result;
        if(result.substring(result.length()-1).equals("镇")||result.substring(result.length()-1
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
百度地图提供了一种方便的方法,可以通过JSON边界下载来获取地图数据。 首先,要使用百度地图JSON边界下载功能,需要使用开发者密钥。开发者密钥用于向百度地图API发送请求,并获取相应的地图数据。 接下来,可以通过发送HTTP GET请求来获取JSON边界数据。在请求URL中,需要包含相应的参数,如请求类型(type)为边界类型(bound)等。百度地图支持多个边界类型,例如省级边界(province)、市级边界(city)和区县级边界(district)等。 请求发送后,百度地图API会返回相应的JSON数据,其中包括了所请求的边界数据。可以通过解析JSON数据,获取到地图的边界坐标信息。 对于省、市、区县等不同级别的边界数据,百度地图提供了相应的接口和参数,可以根据需求进行选择和调整。例如,可以通过设置返回数据的坐标类型(坐标系类型),进行数据的适配和转换。 JSON边界下载功能在许多应用场景中都有广泛的应用。例如,可以通过获取市级边界数据,进行城市边界划定和画图等操作。也可以通过获取区县边界数据,进行行政区划相关的分析和展示等。 总之,百度地图的JSON边界下载功能提供了一种方便的方式,可以获取到所需的地图边界数据。通过发送请求,并解析返回的JSON数据,可以实现对地图数据的灵活应用和定制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值