/
geocode.php
144 lines (142 loc) · 6.2 KB
/
geocode.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
$config = require __DIR__ . '/config.php';
/*
* Array
(
[service_request_id] => UN201504150001
[requested_datetime] => 2015-04-15 00:02:00.0
[status] => 未處理
[keyword] => 0
[area] => 柳營區
[service_name] => 髒亂及污染
[agency] => 0
[subproject] => 空氣汙染
[description] => 柳營奇美醫院附近 空氣汙染
[address_string] => 柳營奇美醫院附近
[lat] => 0
[long] => 0
[service_notice] =>
[updated_datetime] => null
[expected_datetime] => 2015-04-22 00:02:00.0
)
*/
$replacments = array(
'巿' => '市',
'0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5',
'6' => '6', '7' => '7', '8' => '8', '9' => '9',
'F' => 'F', 'B' => 'B', '-' => '-'
);
$weekly = array();
foreach (glob($config['paths']['data'] . '/requests/*/*/*/*.json') AS $jsonFile) {
$json = json_decode(file_get_contents($jsonFile), true);
foreach ($json AS $k => $v) {
if (is_array($v)) {
$json[$k] = '';
}
}
$requestTime = strtotime($json['requested_datetime']);
$key = date('o-W', $requestTime);
if (!isset($weekly[$key])) {
$dataPath = __DIR__ . '/data/' . date('o', $requestTime);
if (!file_exists($dataPath)) {
mkdir($dataPath, 0777, true);
}
$weekly[$key] = array(
'key' => date('o/W', $requestTime),
'fh' => fopen($dataPath . '/' . date('W', $requestTime) . '.csv', 'w'),
'begin' => $requestTime,
'end' => $requestTime,
'count' => 1,
);
} else {
if ($requestTime > $weekly[$key]['end']) {
$weekly[$key]['end'] = $requestTime;
} elseif ($requestTime < $weekly[$key]['begin']) {
$weekly[$key]['begin'] = $requestTime;
}
++$weekly[$key]['count'];
}
if ($json['lat'] > 23.413740929 || $json['lat'] < 22.887506857 || $json['long'] > 120.65626753 || $json['long'] < 120.02304376) {
$json['lat'] = 0;
}
if (is_string($json['address_string']) && empty($json['lat'])) {
$pos = strpos($json['address_string'], '號');
if (false !== $pos) {
$json['address_string'] = substr($json['address_string'], 0, $pos) . '號';
$json['address_string'] = preg_replace('/\\((.*)\\)/', '', $json['address_string']);
$json['address_string'] = strtr($json['address_string'], $replacments);
if (!empty($json['area'])) {
$areaPos = strpos($json['address_string'], $json['area']);
if (false !== $areaPos) {
$json['address_string'] = $json['area'] . substr($json['address_string'], $areaPos + strlen($json['area']));
}
}
$json['address_string'] = '臺南市' . $json['address_string'];
$sha = sha1($json['address_string']);
$path = implode('/', array(
$config['paths']['sha'],
substr($sha, 0, 3),
substr($sha, 3, 3),
));
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
$shaFile = $path . '/' . $sha;
if (!file_exists($shaFile)) {
$apiUrl = $config['tgos']['url'] . '?' . http_build_query(array(
'oAPPId' => $config['tgos']['APPID'], //應用程式識別碼(APPId)
'oAPIKey' => $config['tgos']['APIKey'], // 應用程式介接驗證碼(APIKey)
'oAddress' => $json['address_string'], //所要查詢的門牌位置
'oSRS' => 'EPSG:4326', //回傳的坐標系統
'oFuzzyType' => '2', //模糊比對的代碼
'oResultDataType' => 'JSON', //回傳的資料格式
'oFuzzyBuffer' => '0', //模糊比對回傳門牌號的許可誤差範圍
'oIsOnlyFullMatch' => 'false', //是否只進行完全比對
'oIsLockCounty' => 'true', //是否鎖定縣市
'oIsLockTown' => 'false', //是否鎖定鄉鎮市區
'oIsLockVillage' => 'false', //是否鎖定村里
'oIsLockRoadSection' => 'false', //是否鎖定路段
'oIsLockLane' => 'false', //是否鎖定巷
'oIsLockAlley' => 'false', //是否鎖定弄
'oIsLockArea' => 'false', //是否鎖定地區
'oIsSameNumber_SubNumber' => 'true', //號之、之號是否視為相同
'oCanIgnoreVillage' => 'true', //找不時是否可忽略村里
'oCanIgnoreNeighborhood' => 'true', //找不時是否可忽略鄰
'oReturnMaxCount' => '0', //如為多筆時,限制回傳最大筆數
));
file_put_contents($shaFile, file_get_contents($apiUrl));
error_log("getting {$json['address_string']}");
}
$content = file_get_contents($shaFile);
$pos = strpos($content, '{');
$posEnd = strrpos($content, '}') + 1;
$jsonResult = json_decode(substr($content, $pos, $posEnd - $pos), true);
if (isset($jsonResult['AddressList'][0])) {
$json['lat'] = $jsonResult['AddressList'][0]['Y'];
$json['long'] = $jsonResult['AddressList'][0]['X'];
}
}
}
if (!empty($json['lat'])) {
fputcsv($weekly[$key]['fh'], array(
$json['service_request_id'],
date('Y-m-d H:i:s', $requestTime),
$json['area'],
$json['service_name'],
$json['subproject'],
$json['description'],
$json['address_string'],
$json['lat'],
$json['long'],
));
}
}
$listJson = array();
krsort($weekly);
foreach ($weekly AS $week) {
unset($week['fh']);
$week['begin'] = date('Y-m-d', $week['begin']);
$week['end'] = date('Y-m-d', $week['end']);
$listJson[] = $week;
}
file_put_contents(__DIR__ . '/data/list.json', json_encode($listJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));