This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.php
154 lines (128 loc) · 5.16 KB
/
route.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
145
146
147
148
149
150
151
152
153
154
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
$options = getopt('', array('from:', 'to:', 'api:', 'profile::', 'mode::', 'full', 'no-header'));
$profile = (isset($options['profile']) ? $options['profile'] : 'car');
$mode = (isset($options['mode']) && in_array($options['mode'], array('closest', 'fastest')) ? $options['mode'] : 'fastest');
$header = !(isset($options['no-header']));
$full = (isset($options['full']));
$fileFrom = $options['from'];
$fileTo = $options['to'];
$api = $options['api'];
$cursorFrom = 0;
$cursorTo = 0;
echo '--------------------------------------------------'.PHP_EOL;
echo 'Profile : '.$profile.PHP_EOL;
echo 'Mode : '.$mode.PHP_EOL;
echo '--------------------------------------------------'.PHP_EOL;
$client = new Client(['base_uri' => $api]);
if (isset($fileFrom) && file_exists($fileFrom)) {
$fname = pathinfo($fileFrom, PATHINFO_FILENAME);
$dir = 'data/'.$fname;
if (!file_exists($dir) || !is_dir($dir)) {
mkdir($dir);
} else {
$i = 1;
while (file_exists($dir) && is_dir($dir)) {
$dir = 'data/'.$fname.' ('.$i.')';
$i++;
}
mkdir($dir);
}
if (($handleFrom = fopen($fileFrom, 'r')) !== FALSE && ($handleTo = fopen($fileTo, 'r')) !== FALSE) {
$fpResult = fopen($dir.'/result.csv', 'w');
if ($full === TRUE) {
$fpResultFull = fopen($dir.'/result-full.csv', 'w');
}
if ($header === TRUE && ($headerFrom = fgetcsv($handleFrom, 1000)) !== FALSE && ($headerTo = fgetcsv($handleTo, 1000)) !== FALSE) {
$headerFrom = array_map(function ($column) { return 'from_'.$column; }, $headerFrom);
$headerTo = array_map(function ($column) { return 'to_'.$column; }, $headerTo);
fputcsv($fpResult, array_merge($headerFrom, $headerTo, array('mode','distance','time')));
if ($full === TRUE) {
fputcsv($fpResultFull, array_merge($headerFrom, $headerTo, array('mode','distance','time')));
}
rewind($handleTo);
}
while (($dataFrom = fgetcsv($handleFrom, 1000)) !== FALSE) {
$min = NULL;
$minTo = NULL;
$minResult = NULL;
while (($dataTo = fgetcsv($handleTo, 1000)) !== FALSE) {
if ($header === TRUE && $cursorTo === 0) {
$cursorTo++;
continue;
}
$response_error = FALSE;
try {
$url = sprintf('routing?profile=%s&loc=%f,%f&loc=%f,%f&sort=true', $profile, $dataFrom[1], $dataFrom[0], $dataTo[1], $dataTo[0]);
$response = $client->get($url, [
'headers' => [
'Accept' => 'application/json',
'User-Agent' => 'PHP/'.phpversion()
]
]);
$json = json_decode((string)$response->getBody());
$last = end($json->features);
if ($full === TRUE) {
$result = array_merge(
$dataFrom,
$dataTo,
array($mode, $last->properties->distance, $last->properties->time)
);
fputcsv($fpResultFull, $result);
}
if ($mode === 'closest' && (is_null($min) || $last->properties->distance < $min)) {
$min = $last->properties->distance;
$minTo = $dataTo;
$minResult = array($last->properties->distance, $last->properties->time);
}
else if ($mode === 'fastest' && (is_null($min) || $last->properties->time < $min)) {
$min = $last->properties->time;
$minTo = $dataTo;
$minResult = array($last->properties->distance, $last->properties->time);
}
} catch (ClientException $e) {
$request = $e->getRequest();
$response_error = $e->getResponse();
echo $cursorFrom.' | '.$dataFrom[2].' | ERROR: [client] '.$request->getUri().' '.$response_error->getStatusCode().' '.$response_error->getReasonPhrase().PHP_EOL;
} catch (ServerException $e) {
$request = $e->getRequest();
$response_error = $e->getResponse();
echo $cursorFrom.' | '.$dataFrom[2].' | ERROR: [server] '.$request->getUri().' '.$response_error->getStatusCode().' '.$response_error->getReasonPhrase().PHP_EOL;
} catch (Exception $e) {
$request = $e->getRequest();
$response_error = $e->getResponse();
echo $cursorFrom.' | '.$dataFrom[2].' | ERROR: '.$request->getUri().' '.$response_error->getStatusCode().' '.$response_error->getReasonPhrase().PHP_EOL;
}
$cursorTo++;
if ($response_error !== FALSE) {
$fpError = fopen($dir.'/errors.csv', 'a');
fputcsv($fpError, $dataFrom);
fclose($fpError);
break;
}
}
if ($response_error === FALSE) {
echo $cursorFrom.' | '.$dataFrom[2].' | '.$minTo[2].' | Distance: '.$minResult[0].' - Time: '.$minResult[1].PHP_EOL;
$result = array_merge(
$dataFrom,
$minTo,
array($mode),
$minResult
);
fputcsv($fpResult, $result);
}
rewind($handleTo);
$cursorTo = 0;
$cursorFrom++;
}
fclose($fpResult);
if ($full === TRUE) {
fclose($fpResultFull);
}
}
}
exit();