-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-updates.php
76 lines (57 loc) · 2.57 KB
/
db-updates.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
<?php
use MapasCulturais\App;
use CreateGeoDivisions\Plugin;
return [
'Cria novas geometrias baseado em divisões em uma planilha ' => function () {
/** @var App $app */
$app = App::i();
/**
* Array para armazenar os municípios separados por região
*/
foreach (scandir(Plugin::IMPORT_FILES_PATH) as $file_name) {
if (strtolower(substr($file_name, -4)) != ".csv") {
continue;
}
echo "\n \n ========================================================";
echo "\n Processando arquivo: $file_name";
$file_name = Plugin::IMPORT_FILES_PATH . $file_name;
$target_file = Plugin::IMPORTED_FILES_PATH . md5_file($file_name);
if (file_exists($target_file)) {
continue;
}
copy($file_name, $target_file);
$file = fopen($file_name, 'r');
$regioes = [];
$nova_regiao = '';
$filtro = '';
$count = 0;
while (($line = fgetcsv($file)) !== false) {
if ($count == 0) {
$filtro = $line[1];
} else if ($count == 1) {
$nova_regiao = $line[1];
} else {
if (!array_key_exists($line[1], $regioes)) {
$regioes[$line[1]] = [];
}
$regioes[$line[1]][] = $line[0];
}
++$count;
}
fclose($file);
$conn = $app->em->getConnection();
$conn->executeQuery("SELECT setval('geo_division_id_seq', (SELECT max(id) FROM geo_division))");
foreach($regioes as $regiao => $municipios) {
echo "\n Criando $nova_regiao: $regiao - Municípios: " . implode(', ', $municipios);
$keys = [];
foreach ($municipios as $municipio) {
$keys[uniqid()] = $municipio;
}
$in_municipios = 'lower(unaccent(:' . implode(')), lower(unaccent(:', array_keys($keys)) . '))';
$sql = "INSERT INTO geo_division (type, cod, name, geom) VALUES ('{$nova_regiao}', '{$regiao}', '{$regiao}', (SELECT ST_Union(geom) AS poligono_unido FROM geo_division WHERE lower(unaccent(name)) IN($in_municipios) AND cod LIKE '$filtro%'))";
$conn->executeQuery($sql, $keys);
}
}
return false;
},
];