-
Notifications
You must be signed in to change notification settings - Fork 1
/
import_lt.php
71 lines (58 loc) · 1.54 KB
/
import_lt.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
<?php
include_once('config.php');
include_once('tmcfile.php');
$tables = array(
'countries',
'locationdatasets',
'languages',
'locationcodes',
'names',
'administrativearea',
'otherareas',
'roads',
'segments',
'soffsets',
'points',
'poffsets',
'intersections'
);
$pdo = new PDO($config_pdo_connection, $config_pdo_write_user, $config_pdo_write_password, $config_pdo_attributes);
foreach($config_countries as $cc => $country)
{
$charset = get_charset($country);
echo "Importing location tables for $country from $charset...\n";
foreach($tables as $table)
{
echo "Filling $table: ";
$csv = fopen($config_lt_dir . '/' . $country . '/' . strtoupper($table) . '.DAT', 'r');
$header = explode(";", trim(remove_utf8_bom(fgets($csv))));
$cols = strtolower("(" . implode(", ", $header) . ")");
$vals = strtolower("(" . implode(", ", array_map(function ($x) { return ':' . $x; }, $header)) . ")");
$stmt = $pdo->prepare("INSERT INTO $table $cols VALUES $vals;");
$total = 0;
$success = 0;
for(;;)
{
$text = iconv($charset, 'UTF-8', trim(fgets($csv)));
if(!$text)
break;
$data = array_combine($header, explode(";", $text, count($header)));
$total++;
foreach($data as $key => $value)
{
if(($key == 'XCOORD') || ($key == 'YCOORD'))
$value = ((float)$value) / 1.0e5;
$stmt->bindValue(':' . strtolower($key), $value);
}
if($stmt->execute() === false)
{
print_r($data);
print_r($stmt->errorInfo());
}
else
$success++;
}
echo "$success of $total entries imported.\n";
}
}
?>