实现全国行政区域结构化管理

一、为什么要写本文?

A、在做很多系统开发的时候,区域管理之前一直是基于js加载json格式文档来实现的三级联动。但很多系统设计的时候,有些表需要一个行政区域的外键;

B、一直以来想自主实现一次行政区域的管理在系统设计级别上

C、希望此文,能给有A、B想法的朋友给予快速完成idea的参考以及给“拿来党”(注:是拿来主义的狂热粉丝)提供力所能及的帮助。

D、近一两年自己工作是做一些深度学习中图像识别的工作,觉得博文可以给予别人帮助,因为本人看过很多别人的博文,收益颇丰。从此篇开始,以后工作中遇到的问题值得分享的,都会写文分享。完成个人从“拿来主义”到“造车轮”转变的起点,更希望以此鼓励跟我一样的朋友“开始分享”!

二、具体实现

A、行政区域划分一手的资料在哪里?

个人查阅相关资料,觉得国务院网站应该有,但是查询后发现只有枝干性质的说明。再次查阅相关资料发现,最全最直接负责更新的国家部门是民政部,以下是民政部官方网站http://www.mca.gov.cn 。其中在本文书写的时期最新的行政区域网址如下(该网址极有可能更新) http://www.mca.gov.cn/article/sj/xzqh/2019/

B、打开A中最新的划分链接

 

C、找到准确的最新文档位置后,我们需要完成数据的采集及落地的各自系统的DB中。

具体的分析如下,行政区域代码总共6位,前2位代表省级行政区域,中间两位代表市级行政区域,最后两位代表县区级行政区域划分。

D、因为不论我们是爬取还是直接复制的数据在结构化上不统一所以需要做一定的结构化改变,具体来说主要是直辖市和港澳地区以及台湾省(这是我国不可分割的一部分,作为爱国人士,炎黄子孙所以中国的领土所以一定要包括进来)。

具体的调整如下图,将直辖市增加一行,将港澳及台湾增加两级

对比

 

E、具体的数据结构和代码实现如下

数据库设计(按个人系统需要设计)

Sql语句如下

DROP TABLE IF EXISTS `area`;

CREATE TABLE `area` (

  `id` bigint(16) NOT NULL COMMENT '编号',

  `parent_id` bigint(16) DEFAULT NULL COMMENT '父级编号',

  `description` varchar(128) DEFAULT NULL COMMENT '名称',

  `rank_number` bigint(16) DEFAULT '0' COMMENT '排序',

  `display_status` varchar(16) DEFAULT NULL COMMENT '是否显示',

  `create_time` datetime DEFAULT NULL,

  `update_time` datetime DEFAULT NULL,

  `remark` varchar(128) DEFAULT NULL COMMENT '备注',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

F、Java语言实现从文本到结构化数据

package com.awaymeet.fly.common.utils;

 

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

 

public class ReadText {

     public static void main(String[] args) throws IOException {

         File f=new File("D://a.txt");//将从行政区域划分的网站上采集下来或者手动复制下来的文本放入这个文件

         FileReader fileReader=new FileReader(f);

         BufferedReader bufferedReader=new BufferedReader(fileReader);//读取文件BufferedReader

        

         String readLine;//读取的每一行文本内容

         String preCode=null;//前一行的行政区域编码

        

         //当前省市县的编码

         String currentCodePrivince=null;

         String currentCodeCity=null;

         String currentCodeCounty=null;

        

         //之前省市县的编码

         String preCodePrivince=null;

         String preCodeCity=null;

         String preCodeCounty=null;

        

         int countPrivince=0;//统计省个数,辅助看看而已

         int countCity=0;//统计市个数,辅助看看而已

         int countCounty=0;//统计县区个数,辅助看看而已

        

        

         String singlePrivinceCode = null,singleCityCode= null;//用于记录读取省市变化的时候记录某一个省市

         String privinceSqlTxt = "",citySqlTxt= "",countySqlTxt= "";//最终的Sql语句

        

         while(null != (readLine=bufferedReader.readLine())){

              readLine=StringUtils.deleteWhitespace(readLine);

              String currentCode=readLine.substring(0,6);

              String currentArea=readLine.substring(6);

             

             

              currentCodePrivince=currentCode.substring(0,2);

              currentCodeCity=currentCode.substring(2,4);

              currentCodeCounty=currentCode.substring(4);

              if(null != preCode){

                   preCodePrivince=preCode.substring(0,2);

                   preCodeCity=preCode.substring(2,4);

                   preCodeCounty=preCode.substring(4);               

              }

              if(currentCodePrivince.equals(preCodePrivince)){

                   //System.out.println(currentCode+currentArea);

                   if(currentCodeCity.equals(preCodeCity)){

                       //System.out.println(currentCode+currentArea);

                       if(currentCodeCounty.equals(preCodeCounty)){

                            System.out.println(currentCode+currentArea);

                       }else{

                            System.out.println("父级市"+singleCityCode+"——下一个县区"+currentCode+currentArea);

                            countCounty++;

                            countySqlTxt+="INSERT INTO `area` VALUES ('"+currentCode+"', '"+singleCityCode+"', '"+currentArea+"', null, 'open', '2019-07-13 21:50:02', '2019-07-13 21:50:02', null);";

                       }

                   }else{

                       singleCityCode=currentCode;

                       System.out.println("父级省"+singlePrivinceCode+"——下一个市"+currentCode+currentArea);

                       countCity++;

                       citySqlTxt+="INSERT INTO `area` VALUES ('"+currentCode+"', '"+singlePrivinceCode+"', '"+currentArea+"', null, 'open', '2019-07-13 21:50:02', '2019-07-13 21:50:02', null);";

                   }

              }else{

                   singlePrivinceCode=currentCode;

                   System.out.println("下一个省"+currentCode+currentArea);

                   countPrivince++;

                   privinceSqlTxt+="INSERT INTO `area` VALUES ('"+currentCode+"', null, '"+currentArea+"', null, 'open', '2019-07-13 21:50:02', '2019-07-13 21:50:02', null);";

              }

              preCode=currentCode;

         }

         bufferedReader.close();

        

         System.out.println(countPrivince);

         System.out.println(countCity);

         System.out.println(countCounty);

        

        

         File sqlFile=new File("D://sql.sql");//sql语句保存为文本

         FileWriter fileWriter=new FileWriter(sqlFile);

         fileWriter.write(privinceSqlTxt+citySqlTxt+countySqlTxt);

         fileWriter.close();

     }

}

 

G、附件

链接:https://pan.baidu.com/s/1_h1sozR0ESeLtFu2qxOtdQ 
提取码:yx05 

三、2019年5月份实现结果(前台实现完全是想怎么做就怎么做的事,我们只要完成数据结构化就okay)

 

四、个人水平有限,文中如有错误或不当之处,请联系本人修改或删除,个人邮箱jaylinn@126.com ,另外个人爱好技术,如有兴趣可加QQ群377147577

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值