Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan committed Aug 4, 2020
1 parent 1e96d38 commit 528a779
Show file tree
Hide file tree
Showing 8 changed files with 3,490 additions and 6,680 deletions.
3,317 changes: 0 additions & 3,317 deletions log.txt

This file was deleted.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* [中华人民共和国国家统计局-统计用区划和城乡划分代码](http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/)
* [中华人民共和国国家统计局-统计用区划代码和城乡划分代码编制规则](http://www.stats.gov.cn/tjsj/tjbz/200911/t20091125_8667.html)
* 已更新至:
* [最新县及县以上行政区划代码(截止时间:2016-07-31,发布时间:2017-03-10)](http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t20170310_1471429.html)
* [2016年统计用区划代码和城乡划分代码(截止时间:2016-07-31,发布时间:2017-05-16)](http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/index.html)

* 2019年统计用区划代码和城乡划分代码(截止时间:2019-10-31,发布时间:2020-02-25)
* 经纬度更新时间 2020-08-04
* 参考 [行政区划代码 adcode 映射表](http://mapopen-pub-webserviceapi.bj.bcebos.com/geocoding/%E8%A1%8C%E6%94%BF%E5%8C%BA%E5%88%92%E6%B8%85%E5%8D%95%20V3.0%209.03.xlsx)

## 下载
Expand Down
19 changes: 10 additions & 9 deletions db.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
DROP TABLE IF EXISTS `region`;
CREATE TABLE IF NOT EXISTS `region` (
`code` int NOT NULL PRIMARY KEY,

CREATE TABLE `region` (
`id` varchar(32) NOT NULL DEFAULT '',
`name` varchar(50) NOT NULL,
`parent_code` int NOT NULL,
`lng` varchar(50) NOT NULL DEFAULT '', -- lng 经度值
`lat` varchar(50) NOT NULL DEFAULT '', -- lat 纬度值
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8 COMMENT='区域';
`parent_id` varchar(32) NOT NULL DEFAULT '',
`lng` varchar(50) NOT NULL DEFAULT '',
`lat` varchar(50) NOT NULL DEFAULT '',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='区域';
18 changes: 7 additions & 11 deletions load-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,36 @@
$db = new \PFinal\Database\Builder($config);

foreach ($all as $p) {
if (!isset($p['childs'])) {
if (!isset($p['children'])) {
echo 'error 1';
exit;
}

//省
$data = ['code' => $p['code'], 'name' => $p['name'], 'parent_code' => 0];
$data = ['id' => $p['code'], 'name' => $p['name'], 'parent_id' => ''];
if (!$db->table('region')->insert($data)) {
echo 'db error 1';
exit;
}

echo '<div style="color:red">' . $p['code'] . ' ' . $p['name'] . '</div>';

foreach ($p['childs'] as $c) {
//echo '<div style="color:blue;">' . $c['code'] . ' ' . $c['name'] . '</div>';

foreach ($p['children'] as $c) {
//市
$data = ['code' => $c['code'], 'name' => $c['name'], 'parent_code' => $p['code']];
$data = ['id' => $c['code'], 'name' => $c['name'], 'parent_id' => $p['code']];
if (!$db->table('region')->insert($data)) {
echo 'db error 2';
exit;
}

if (!isset($c['childs'])) {
if (!isset($c['children'])) {
echo 'error 2';
exit;
}

//区
foreach ($c['childs'] as $a) {
//echo $a['code'] . ' ' . $a['name'] . '<br>';

$data = ['code' => $a['code'], 'name' => $a['name'], 'parent_code' => $c['code']];
foreach ($c['children'] as $a) {
$data = ['id' => $a['code'], 'name' => $a['name'], 'parent_id' => $c['code']];
if (!$db->table('region')->insert($data)) {
echo 'db error 3';
exit;
Expand Down
21 changes: 12 additions & 9 deletions location.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
$db = new \PFinal\Database\Builder($config);

$all = $db->table('region')->findAll();
$all = array_column($all, null, 'code');
$all = array_column($all, null, 'id');

foreach ($all as $item) {
if(!empty($item['lng'])){
continue;
}

$ak = '';
$address = getFullName($item['code'], $all);
$ak = 'xxx';
$address = getFullName($item['id'], $all);
echo $address . "\r\n";
$url = 'http://api.map.baidu.com/geocoder/v2/?address=' . $address . '&output=json&ak=' . $ak;

Expand Down Expand Up @@ -45,11 +48,11 @@

$arr = json_decode($json, true);

file_put_contents(' log.txt', $address . ' ' . $json . "\n", FILE_APPEND);
file_put_contents('log.txt', $address . ' ' . $json . "\n", FILE_APPEND);

if (isset($arr['status']) && $arr['status'] == 0) {
$row = $db->table('region')
->wherePk($item['code'])
->wherePk($item['id'])
->update($arr['result']['location']);

if ($row != 1) {
Expand All @@ -74,18 +77,18 @@ function getFullName($code, $all)

$name = $one['name'];

if ($one['parent_code'] == 0) {
if ($one['parent_id'] == '') {
return $name;
}

$parent = $all[$one['parent_code']];
$parent = $all[$one['parent_id']];

$name = $parent['name'] . $name;

if ($parent['parent_code'] == 0) {
if ($parent['parent_id'] == '') {
return $name;
}

$pprent = $all[$parent['parent_code']];
$pprent = $all[$parent['parent_id']];
return $pprent['name'] . $name;
}
2 changes: 1 addition & 1 deletion pca-code.json

Large diffs are not rendered by default.

Loading

0 comments on commit 528a779

Please sign in to comment.