Last active
December 8, 2016 10:48
-
-
Save larryli/363de1b59385953e04a224f004128416 to your computer and use it in GitHub Desktop.
转换 http://www.mca.gov.cn/article/sj/tjbz/a/ 的文本数据为 PHP 数据和 SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$directly = ['11', '12', '31', '50', '81', '82']; | |
$names = [ | |
'拉祜族佤族布朗族傣族自治县', '保安族东乡族撒拉族自治县', '彝族哈尼族拉祜族自治县', '傣族拉祜族佤族自治县', | |
'哈尼族彝族傣族自治县', '彝族回族苗族自治县', '布依族苗族自治县', '布依族苗族自治州', '白族普米族自治县', | |
'苗族瑶族傣族自治县', '傣族景颇族自治州', '独龙族怒族自治县', '哈尼族彝族自治县', '哈尼族彝族自治县', '哈尼族彝族自治州', | |
'满族蒙古族自治县', '蒙古族藏族自治州', '苗族布依族自治县', '苗族土家族自治县', '土家族苗族自治县', '壮族苗族自治州', | |
'土家族苗族自治州', '仡佬族苗族自治县', '藏族羌族自治州', '傣族佤族自治县', '傣族彝族自治县', '哈萨克族自治县', | |
'回族土族自治县', '回族彝族自治县', '柯尔克孜自治州', '黎族苗族自治县', '苗族侗族自治县', '苗族侗族自治州', | |
'彝族傣族自治县', '彝族回族自治县', '彝族苗族自治县', '壮族瑶族自治县', '朝鲜族自治县', '朝鲜族自治州', | |
'哈尼族自治县', '哈萨克自治县', '哈萨克自治州', '拉祜族自治县', '傈僳族自治县', '傈僳族自治州', '毛南族自治县', | |
'蒙古族自治县', '仫佬族自治县', '纳西族自治县', '撒拉族自治县', '塔吉克自治县', '土家族自治县', '维吾尔自治区', | |
'斡尔族自治旗', '裕固族自治县', '白族自治州', '藏族自治县', '藏族自治州', '达斡尔族区', '傣族自治州', | |
'侗族自治县', '各族自治县', '回族自治区', '回族自治县', '回族自治州', '黎族自治县', '满族自治县', '蒙古自治县', | |
'蒙古自治州', '苗族自治县', '羌族自治县', '畲族自治县', '水族自治县', '特别行政区', '土族自治县', '佤族自治县', | |
'锡伯自治县', '瑶族自治县', '彝族自治县', '彝族自治州', '壮族自治区', '族自治县', '回族区', '自治旗', '联合旗', | |
'自治区', '地区', '矿区', '特区', '新区', '盟', '区', '省', '市', '县', | |
]; | |
$special = [ | |
'本溪满族自治县' => '本溪县', '神农架林区' => '神农架', '阜新蒙古族自治县' => '阜新县', '清新区' => '清新', | |
]; | |
$except = [ | |
'上饶县', '东营区', '临夏市', '临夏县', '乌鲁木齐县', '九江县', '井陉县', '井陉矿区', '伊宁县', '伊宁市', | |
'伊春区', '克拉玛依区', '南昌县', '吉安县', '吉林市', '和田市', '和田县', '喀什市', '塔城市', '大同县', | |
'大理市', '安阳县', '宜宾县', '宣化县', '宣化区', '岳阳县', '广安区', '恩施市', '承德县', '抚顺县', '新乡县', | |
'昌吉市', '朝阳县', '株洲县', '楚雄市', '淮安区', '湘潭县', '濮阳县', '玉树市', '甘孜县', '白银区', '红河县', | |
'芜湖县', '荆州区', '衡阳县', '许昌县', '辽阳县', '通化县', '邢台县', '那曲县', '邵阳县', '铁岭县', '长沙县', | |
'长治县', '阿克苏市', '阿勒泰市', '阿坝县', '黄山区', '文山市', | |
]; | |
$districts = [ | |
'东莞', '中山', '三沙', '儋州', '嘉峪关', | |
]; | |
$data = file_get_contents('districts.txt'); | |
$php = fopen('districts.php', 'w'); | |
$sql = fopen('districts.sql', 'w'); | |
fputs($php, "<?php\n\n"); | |
fputs($php, "// `id`, `name`, `title`, `parent_id`, `is_city`, `is_district`\n"); | |
fputs($php, "return [\n"); | |
$header = <<<EOF | |
/*!40101 SET NAMES utf8 */; | |
DROP TABLE IF EXISTS `districts`; | |
/*!40101 SET character_set_client = utf8 */; | |
CREATE TABLE `districts` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', | |
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '名称', | |
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '全称', | |
`parent_id` int(11) DEFAULT NULL COMMENT '上级', | |
`is_city` tinyint(1) NOT NULL COMMENT '是否城市', | |
`is_district` tinyint(1) NOT NULL COMMENT '是否县区', | |
PRIMARY KEY (`id`), | |
KEY `idx-districts-parent_id` (`parent_id`), | |
KEY `idx-districts-is_city` (`is_city`), | |
KEY `idx-districts-is_district` (`is_district`), | |
CONSTRAINT `fk-districts-parent_id` FOREIGN KEY (`parent_id`) REFERENCES `districts` (`id`) ON DELETE CASCADE | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; | |
LOCK TABLES `districts` WRITE; | |
EOF; | |
fputs($sql, $header); | |
$insert = []; | |
foreach (explode("\n", $data) as $row) { | |
$row = trim($row); | |
if (!empty($row)) { | |
list($code, $name, $title, $parent, $is_city, $is_district) = call_user_func_array('row', array_map(function ($d) { | |
return trim($d); | |
}, explode(' ', $row, 2))); | |
fputs($php, "\t[{$code}, '{$name}', '{$title}', {$parent}, {$is_city}, {$is_district}],\n"); | |
$insert[] = "({$code}, '{$name}', '{$title}', {$parent}, {$is_city}, {$is_district})"; | |
} | |
} | |
fputs($php, "];\n"); | |
fclose($php); | |
foreach (array_chunk($insert, 500) as $d) { | |
fputs($sql, "INSERT INTO `districts` VALUES\n" . implode(",\n", $d) . ";\n"); | |
} | |
fputs($sql, "UNLOCK TABLES;\n"); | |
fclose($sql); | |
function row($code, $title) | |
{ | |
global $directly, $districts; | |
$name = name($title); | |
if (in_array(substr($code, 0, 2), $directly)) { | |
if (substr($code, 2, 4) == '0000') { | |
// 直辖市/特别行政区 | |
return [code2id($code), $name, $title, 'null', 1, 0]; | |
} | |
// 直辖市下辖区县市 | |
return [code2id($code), $name, $title, code2id(substr($code, 0, 2) . '0000'), 0, 1]; | |
} elseif (substr($code, 2, 4) == '0000') { | |
// 省/自治区 | |
return [code2id($code), $name, $title, 'null', 0, 0]; | |
} elseif (substr($code, 4, 2) == '00') { | |
if (in_array($name, $districts)) { | |
// 不设区的地级市 | |
return [code2id($code), $name, $title, code2id(substr($code, 0, 2) . '0000'), 1, 1]; | |
} | |
// 地级市 | |
return [code2id($code), $name, $title, code2id(substr($code, 0, 2) . '0000'), 1, 0]; | |
} elseif (substr($code, 2, 2) == '90') { | |
// 省直管县市 | |
return [code2id($code), $name, $title, code2id(substr($code, 0, 2) . '0000'), 1, 1]; | |
} | |
// 区县市 | |
return [code2id($code), $name, $title, code2id(substr($code, 0, 4) . '00'), 0, 1]; | |
} | |
function code2id($code) | |
{ | |
static $n = 1, $codes = []; | |
if (isset($codes[$code])) { | |
return $codes[$code]; | |
} | |
return $codes[$code] = $n++; | |
} | |
function name($title) | |
{ | |
global $names, $special, $except; | |
$len = mb_strlen($title); | |
if ($len < 3 || in_array($title, $except)) { | |
// 单名区县和不处理的数据 | |
return $title; | |
} | |
if (isset($special[$title])) { | |
// 特殊处理的数据 | |
return $special[$title]; | |
} | |
foreach ($names as $end) { | |
$len = strlen($end); | |
if (substr($title, -$len) == $end) { | |
// 截断处理 | |
return substr($title, 0, -$len); | |
} | |
} | |
// 旗 | |
return $title; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment