-
Notifications
You must be signed in to change notification settings - Fork 0
Upload e integridade dos dados originais
Peter edited this page Aug 4, 2020
·
14 revisions
A seguir passo a passo didático que deu origem ao algoritmo "automatizador de ingestão".
Os arquivos precisam ser distribuídos, via FTP ou SFTP, por pastas do município.
Por exemplo, com ls /home/igor/ obtemos a listagem das pastas organizadas pelo Igor em junho de 2020:
BR-ES-Serra BR-PR-Cascavel BR-RS-PortoAlegre BR-SP-LaranjalPaulista BR-SP-SaoVicente
BR-ES-VilaVelha BR-PR-Pinhais BR-RS-SantaMaria BR-SP-Osasco BR-SP-Sorocaba
BR-ES-Vitoria BR-PR-SaoJosePinhais BR-SC-JaraguaSul BR-SP-Santos
BR-MG-BeloHorizonte BR-RJ-Niteroi BR-SP-Itu BR-SP-SaoBernardoCampo
BR-PE-Recife BR-RS-Gravatai BR-SP-Jundiai BR-SP-SaoPaulo
Dentro de cada uma delas são mantidos os arquivos de input. Por exemplo:
ls /home/igor/BR-RS-PortoAlegre/input
# SMF-XLSX-ORIGINAIS.zipPara garantir que todos os arquivos de carga foram verificados, a informação pode ser transferida para um arquivo temporário com
CREATE or replace FUNCTION ingest.cityfolder_input_files(
p_fpath text DEFAULT '/tmp/pg_io/'
) RETURNS TABLE (fid int, cityname text, fname text, is_validext boolean, fmeta jsonb) AS $f$
WITH t0 AS ( SELECT rtrim(p_fpath,'/') AS fpath )
, t1 AS (
SELECT f as cityname,
t0.fpath ||'/'|| f as f
FROM pg_ls_dir((SELECT fpath FROM t0)) t(f), t0
WHERE f ~ '^BR\-[A-Z]{2,2}\-[A-Za-z]+$'
)
SELECT (row_number() OVER ())::int id,
cityname, fname,
fname ~* '\.(zip|gz|rar|geojson|csv|dwg)$' as is_validext,
to_jsonb( pg_stat_file(fpath||'/'||fname) ) || jsonb_build_object('fpath',fpath)
FROM (
SELECT cityname,
f||'/'||'input' as fpath,
pg_ls_dir(f||'/'||'input') as fname
FROM t1
ORDER BY 1,3
) t2
$f$ LANGUAGE SQL IMMUTABLE;
-- Exemplo:
SELECT cityname, fname, is_validext, fmeta->>'size' as bytes
FROM ingest.cityfolder_input_files('/home/igor');No exemplo, rodando em 1 de julho de 2020, foram listados 243 arquivos válidos:
| cityname | fname | is_validext | bytes |
|---|---|---|---|
| BR-ES-Serra | 2018-01-05 BaseCartográfica.gdb.zip | t | 24218623 |
| BR-ES-VilaVelha | BAIRROS.zip | t | 68069 |
| BR-ES-VilaVelha | LOTES.zip | t | 6282461 |
| BR-ES-VilaVelha | QUADRAS.zip | t | 2327148 |
| ... | ... | ... | ... |
| BR-SP-SaoPaulo | SIRGAS_SHP_quadraviariaed.zip | t | 70291605 |
| BR-SP-SaoPaulo | SIRGAS_SHP_subprefeitura.zip | t | 1188797 |
| BR-SP-SaoVicente | MAPA DE LOTEAMENTOS.dwg | t | 22665728 |
| BR-SP-Sorocaba | lotes_prediais.zip | t | 14476669 |
| BR-SP-Sorocaba | shape_eixos_sorocaba.zip | t | 1880173 |
(wiki temporária!)