-
Notifications
You must be signed in to change notification settings - Fork 8
/
count-avg.php
118 lines (107 loc) · 4.26 KB
/
count-avg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
class CountAVG
{
public function main($year)
{
$year = intval($year);
$output = fopen('php://output', 'w');
if (!$year) {
throw new Exception("Usage: php count-avg.php <year>");
}
// 先抓縣市升格資料,因為原始用電資料的代碼都是用最舊代碼 (台北縣、高雄縣時代...)
$url = 'https://sheethub.com/area.reference.tw/中華民國行政區/?sql=SELECT+*+FROM+this+WHERE+type%3D%27town%27+AND+note+%21%3D+%27%27&format=array_json';
$json = json_decode(file_get_contents($url));
if (!is_array($json)) {
throw new Exception("找不到 {$year} 年人口");
}
$upgrade_map = array();
foreach ($json as $record) {
if (preg_match('/從 #([0-9]*) 升格成 #([0-9]*)/', $record->note, $matches)) {
$upgrade_map[$matches[1]] = $matches[2];
}
}
// 從 sheethub 抓人口
$url = 'https://sheethub.com/ronnywang/全國人口統計_鄉鎮市區_' . ($year - 1911) . '年12月/?sql=SELECT+"鄉鎮市區代碼".town_id+%2C+戶數%2C+人口數+FROM+this&format=array_json';
$json = json_decode(file_get_contents($url));
if (!is_array($json)) {
throw new Exception("找不到 {$year} 年人口");
}
$populations = array();
foreach ($json as $record) {
$populations[$record->town_id] = $record;
}
$files = glob(__DIR__ . '/outputs/town/' . $year . '-*.csv');
$town_stats = array();
foreach ($files as $file) {
if (!preg_match('#^([0-9]*)-([0-9]*)\.csv$#', basename($file), $matches)) {
continue;
}
list(, $year, $month) = $matches;
$id = ceil($month / 2);
$fp = fopen($file, 'r');
while ($rows = fgetcsv($fp)) {
list($county_id, $county_name, $town_id, $town_name, , $count) = $rows;
if (!$town_id) continue;
if (!array_key_exists($town_id, $town_stats)) {
$town_stats[$town_id] = array(
$county_name,
$town_name,
array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0),
);
}
$town_stats[$town_id][2][$id] += $count;
}
fclose($fp);
}
fputcsv($output, array(
'town_id',
'縣市',
'鄉鎮',
'人口數',
'家戶數',
'1-2月總用電',
'1-2月家戶用電',
'3-4月總用電',
'3-4月家戶用電',
'5-6月總用電',
'5-6月家戶用電',
'7-8月總用電',
'7-8月家戶用電',
'9-10月總用電',
'9-10月家戶用電',
'11-12月總用電',
'11-12月家戶用電',
));
foreach ($town_stats as $town_id => $stat) {
if (array_key_exists($town_id, $populations)) {
$population_data = $populations[$town_id];
} elseif (array_key_exists($town_id, $upgrade_map) and array_key_exists($upgrade_map[$town_id], $populations)) {
$population_data = $populations[$upgrade_map[$town_id]];
} else {
error_log($town_id . ' ' . $stat[0] . ' ' . $stat[1]);
continue;
}
fputcsv($output, array(
$population_data->town_id,
$stat[0],
$stat[1],
$population_data->{'人口數'},
$population_data->{'戶數'},
$stat[2][1],
$stat[2][1] / $population_data->{'戶數'},
$stat[2][2],
$stat[2][2] / $population_data->{'戶數'},
$stat[2][3],
$stat[2][3] / $population_data->{'戶數'},
$stat[2][4],
$stat[2][4] / $population_data->{'戶數'},
$stat[2][5],
$stat[2][5] / $population_data->{'戶數'},
$stat[2][6],
$stat[2][6] / $population_data->{'戶數'},
));
}
}
}
$c = new CountAVG;
$c->main($_SERVER['argv'][1]);