-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.php
executable file
·47 lines (44 loc) · 1.84 KB
/
import.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
#!/usr/bin/env php
<?php
$conn = pg_connect("dbname=stargazing");
function sanitize($raw) {
return "'" . pg_escape_string(trim(preg_replace("/ +/", ' ', $raw))) . "'";
}
$input_file = file('SAC_DeepSky_Ver80_Fence.TXT', FILE_SKIP_EMPTY_LINES);
array_shift($input_file);
foreach ($input_file as $input_line) {
$input = explode('|', $input_line);
$r = array();
$r['name'] = sanitize($input[1]);
$r['name_alt'] = sanitize($input[2]);
$r['type'] = sanitize($input[3]);
$r['con'] = sanitize($input[4]);
$r['ra'] = (int)substr($input[5], 0, 2) * 15 +
(float)substr($input[5], 3) / 60;
$r['dec'] = (int)substr($input[6], 0, 3) +
(float)substr($input[6], 4) / 60;
$r['mag'] = (float)$input[7];
$r['subr'] = (float)$input[8];
$r['size_max'] = (trim($input[11]) == '') ? 'NULL' :
(float)substr($input[11], 0, -1) /
((substr($input[11], -1) == 's') ? 60 : 1);
$r['size_min'] = (trim($input[12]) == '') ? 'NULL' :
(float)substr($input[12], 0, -1) /
((substr($input[12], -1) == 's') ? 60 : 1);
$r['pa'] = (trim($input[13]) == '') ? 'NULL' :
(int)$input[13];
$r['class'] = (trim($input[14]) == '') ? 'NULL' :
sanitize($input[14]);
$r['ngc_descr'] = (trim($input[18]) == '') ? 'NULL' :
sanitize($input[18]);
$r['notes'] = (trim($input[19]) == '') ? 'NULL' :
sanitize($input[19]);
$r['geom'] = "'POINT({$r['ra']} {$r['dec']})'";
$keys = array_keys($r);
foreach ($keys as $i => $k) $keys[$i] = "\"$k\"";
$cols = implode(', ', $keys);
$values = implode(', ', array_values($r));
pg_exec($conn, "INSERT INTO sky ($cols) VALUES ($values);");
print $r['name']."\n";
}
?>