This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
update.php
127 lines (109 loc) · 3.76 KB
/
update.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
<?php
require_once "config.php";
require_once "geometry_utils.php";
// connect to database
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_DATABASE);
if ($mysqli->connect_errno) {
echo $mysqli->connect_error;
exit(1);
}
// prepare for updating database
if (!($mysqli->query("DROP TABLE IF EXISTS oneway_new"))) {
echo $mysqli->error;
exit(1);
}
if (!($mysqli->query("CREATE TABLE IF NOT EXISTS oneway_new(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
wayId BIGINT UNSIGNED,
fromNodeId BIGINT UNSIGNED,
toNodeId BIGINT UNSIGNED,
fromLongitude DOUBLE(10,7),
fromLatitude DOUBLE(10,7),
toLongitude DOUBLE(10,7),
toLatitude DOUBLE(10,7),
latitude DOUBLE(10,7),
longitude DOUBLE(10,7),
INDEX pos (latitude, longitude)
)")))
{
echo $mysqli->error;
exit(1);
}
if (!($mysqli->query("CREATE TABLE IF NOT EXISTS oneway LIKE oneway_new"))) {
echo $mysqli->error;
exit(1);
}
// prepare insert statement
if (!($stmt = $mysqli->prepare("INSERT INTO oneway_new(wayId, fromNodeId, toNodeId, fromLongitude, fromLatitude, toLongitude, toLatitude, latitude, longitude) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")))
{
echo $mysqli->error;
exit(1);
}
if (!($stmt->bind_param("iiidddddd", $wayId, $fromNodeId, $toNodeId, $fromLongitude, $fromLatitude, $toLongitude, $toLatitude, $latitude, $longitude))) {
echo $stmt->error;
exit(1);
}
// using transaction and commit is crucial for acceptable performance during db population
if (!($mysqli->query("START TRANSACTION"))) {
echo $mysqli->error;
exit(1);
}
// get new data from server
$ymd = date("Ymd");
$file = gzfile("https://dumps.improveosm.org/ExistingDumps/OneWays/" . $ymd . "/directionOfFlow_" . $ymd . ".csv.gz");
$headline = str_getcsv($file[0], ";");
$pos_wayId = array_search("wayId", $headline);
$pos_fromNodeId = array_search("fromNodeId", $headline);
$pos_toNodeId = array_search("toNodeId", $headline);
$pos_status = array_search("status", $headline);
$pos_theGeom = array_search("theGeom", $headline);
$pos_numberOfTrips = array_search("numberOfTrips", $headline);
if ($pos_wayId === FALSE || $pos_fromNodeId === FALSE || $pos_toNodeId === FALSE || $pos_status === FALSE || $pos_theGeom === FALSE || $pos_numberOfTrips === FALSE) {
echo "Error: Input data format changed in an unpredictable manner!";
exit(1);
}
// convert data into desired format and prepare db operations
foreach ($file as $line) {
$csv = str_getcsv($line, ";");
if (isset($csv[$pos_status]) && $csv[$pos_status] == "OPEN" && isset($csv[$pos_numberOfTrips]) && $csv[$pos_numberOfTrips] > 10) {
$geom = parse_lineString_points($csv[$pos_theGeom]);
if($geom === FALSE) continue;
$center = get_center($geom);
$wayId = $csv[$pos_wayId];
$fromNodeId = $csv[$pos_fromNodeId];
$toNodeId = $csv[$pos_toNodeId];
$fromLongitude = $geom[0][0];
$fromLatitude = $geom[0][1];
$toLongitude = $geom[count($geom)-1][0];
$toLatitude = $geom[count($geom)-1][1];
$latitude = $center[1];
$longitude = $center[0];
if (!($stmt->execute())) {
echo $stmt->error;
exit(1);
}
}
}
// actually do db operations
if (!($stmt->close())) {
echo $stmt->error;
exit(1);
}
if (!($mysqli->query("COMMIT"))) {
echo $mysqli->error;
exit(1);
}
// make new data available and clean up old data
if (!($mysqli->query("RENAME TABLE oneway TO oneway_old, oneway_new TO oneway;"))) {
echo $mysqli->error;
exit(1);
}
if (!($mysqli->query("DROP TABLE IF EXISTS oneway_old"))) {
echo $mysqli->error;
exit(1);
}
if (!($mysqli->close())) {
echo $mysqli->error;
exit(1);
}
?>