You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.4 KiB
PHP
90 lines
2.4 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Import a CSV file generated by osmpois.jar
|
|
* https://github.com/MorbZ/OsmPoisPbf
|
|
*
|
|
* Expected CSV format:
|
|
65|N357939737|48.5841285|-114.9976581|Swamp Mountain
|
|
65|N357939740|47.7252186|-113.6412010|Swan Glaciers
|
|
65|N357939743|47.7193851|-113.6414787|Swan Peak
|
|
37|N357939746|47.7688309|-113.8928792|Swan River State Forest
|
|
21|N357939752|47.4904920|-113.6795331|Swan Valley School
|
|
65|N357939758|48.4088516|-114.7782011|Mount Swaney
|
|
65|N357939762|48.3680110|-115.4601645|Swede Mountain
|
|
65|N357939764|46.6479788|-114.2214988|Sweeney Peak
|
|
65|N357939778|45.1641103|-109.6046133|Sylvan Peak
|
|
*/
|
|
|
|
require __DIR__ . '/settings.php';
|
|
|
|
if (!$SETTINGS['debug']) {
|
|
error_reporting(0);
|
|
} else {
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 'On');
|
|
}
|
|
|
|
//
|
|
// Composer
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
date_default_timezone_set($SETTINGS['timezone']);
|
|
|
|
use Medoo\Medoo;
|
|
|
|
$database;
|
|
try {
|
|
$database = new Medoo([
|
|
'database_type' => $SETTINGS['database']['type'],
|
|
'database_name' => $SETTINGS['database']['name'],
|
|
'server' => $SETTINGS['database']['server'],
|
|
'username' => $SETTINGS['database']['user'],
|
|
'password' => $SETTINGS['database']['password'],
|
|
'charset' => $SETTINGS['database']['charset']
|
|
]);
|
|
} catch (Exception $ex) {
|
|
die("Database error: $ex\n");
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists($argv[1])) {
|
|
die("Please supply the filename of the CSV file to import.\n");
|
|
}
|
|
|
|
$current = 0;
|
|
|
|
$handle = fopen($argv[1], "r");
|
|
if ($handle) {
|
|
$batchinsert = [];
|
|
while (($line = fgets($handle)) !== false) {
|
|
$fields = explode("|", trim($line), 5);
|
|
|
|
$batchinsert[] = [
|
|
"osmid" => substr($fields[1], 1),
|
|
"typeid" => $fields[0],
|
|
"latitude" => $fields[2],
|
|
"longitude" => $fields[3],
|
|
"eletype" => $fields[1][0],
|
|
"name" => $fields[4]
|
|
];
|
|
|
|
$current++;
|
|
if ($current % 100 == 0) {
|
|
$database->insert("poi", $batchinsert);
|
|
$error = $database->error();
|
|
if ($error[1] != 0 && $error[0] != "23000") {
|
|
die("\nDatabase error $error[0]: $error[2]\n");
|
|
}
|
|
$batchinsert = [];
|
|
echo " $current ";
|
|
}
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
echo "\nDone: $current records processed.\n";
|
|
} else {
|
|
die("Error: could not open the file.\n");
|
|
} |