##############################################
# Elaborazione Codici e Dati Agenzia Entrate #
##############################################
# https://www.agenziaentrate.gov.it/portale/web/guest/schede/fabbricatiterreni/omi/forniture-dati-omi
masteRfun::load_pkgs('masteRomi', 'data.table')
message('Lettura e Compilazione file "Zone"')
yz <- rbindlist(lapply(
list.files(file.path(omi_path$input, 'QI'), 'ZONE', full.names = TRUE),
\(x) data.table( termine = gsub('.*_([0-9]{5})_.*$', '\\1', x), fread(x) )
))
yz[LinkZona == 'RG00001390', `:=`( LinkZona = 'RG00001430', Comune_cat = 'U7AA', Comune_amm = 'H163', Fascia = 'C', Zona = 'C5' )]
yz[LinkZona == 'RG00001351', `:=`( LinkZona = 'RG00001412', Comune_cat = 'U7AA', Comune_amm = 'H163', Zona = 'E16' )]
yz[LinkZona == 'RG00001352', `:=`( LinkZona = 'RG00001413', Comune_cat = 'U7AA', Comune_amm = 'H163', Zona = 'R2' )]
message('Lettura e Compilazione file "Quotazioni"')
yv <- rbindlist(lapply(
list.files(file.path(omi_path$input, 'QI'), 'VALORI', full.names = TRUE),
\(x) data.table( periodo = as.integer(gsub('.*_([0-9]{5})_.*$', '\\1', x)), fread(x, select = c(5, 9:21)) )
))
yv[LinkZona == 'RG00001390', `:=`( LinkZona = 'RG00001430', Comune_cat = 'U7AA', Fascia = 'C', Zona = 'C5' )]
yv[LinkZona == 'RG00001351', `:=`( LinkZona = 'RG00001412', Comune_cat = 'U7AA', Zona = 'E16' )]
yv[LinkZona == 'RG00001352', `:=`( LinkZona = 'RG00001413', Comune_cat = 'U7AA', Zona = 'R2' )]
message('Elaborazione Storico Comuni da "Zone"')
yc <- unique(yz[, c(1, 4, 6, 8)])
setnames(yc, c('termine', 'PRVs', 'nazionale', 'catasto'))
# yc[, CMNs := as.numeric(substring(CMNs, nchar(CMNs) - 5))]
yc[, inizio := min(termine), catasto]
yc <- yc[yc[, .I[which.max(termine)], catasto]$V1]
yc[termine == max(termine), termine := NA]
yc <- masteRgeo::comuni_storico[, .(catasto, CMN)][yc, on ='catasto'] |> unique()
# FIX PER EX MONTIGLIO MONFERRATO, PISTOLESA, LONATO, ORDONA
# yc[catasto == 'C831', CMNs := 5043]
# yc[catasto == 'F678', CMNs := 5078]
# yc[catasto == 'I495', CMNs := 5102]
# yc[catasto == 'G714', CMNs := 96045]
# yc[catasto == 'M266', CMNs := CMN ]
yc[catasto == 'E667', CMN := 17092]
# CHECK
yc[CMN %in% yc[,.N,CMN][N>1, CMN]][is.na(termine)][,.N,CMN][N>1]
# 5122 e 12144 sono i due nuovi comuni 2023, controllare a 20231 che siano stati inseriti
setcolorder(yc, c('CMN', 'catasto', 'nazionale', 'inizio'))
setorderv(yc, c('CMN', 'termine', 'catasto'), c(1, -1, 1))
message(' - salvataggio...\n')
salva_dts_pkg(yc, 'comuni', omi_path$output, dbn = 'omi')
message('\n\nElaborazione Storico "Zone"')
yt <- yz[, .(termine, Comune_amm, LinkZona, Zona_Descr, Fascia, Zona, Cod_tip_prev, Stato_prev, Microzona)]
setnames(yt, c('termine', 'catasto', 'OMIk', 'OMId', 'fascia', 'zona', 'utilizzo', 'stato', 'microzona'))
yt[, OMI := paste0(catasto, aggiungi_caratteri(zona, 4))]
yt[, inizio := min(termine), OMI]
ytna <- yt[OMI %chin% unique(yt[, .(OMI, OMIk)])[, .N, OMI][N>1, OMI], .(catasto, OMI, OMIk)][order(OMIk)][] |> unique()
yt <- yt[yt[, .I[which.max(termine)], OMI]$V1]
ytna <- ytna[OMIk %chin% yt$OMIk, .(OMI, OMIk)][ytna[!OMIk %chin% yt$OMIk, .(catasto, OMI, OMIko = OMIk)], on = 'OMI']
setcolorder(ytna, c('catasto', 'OMI', 'OMIk', 'OMIko'))
salva_dts_pkg(ytna, 'omik_variati', as_db = FALSE)
yt[microzona == 999, microzona := NA]
yt[termine == max(termine), termine := NA]
yt[, OMId := trimws(gsub("^'|'$|^-{0,2}", '', OMId))][, OMId := trimws(gsub("^-", '', OMId))]
yt[yt == ''] <- NA
setcolorder(yt, c('catasto', 'OMI', 'inizio'))
setorder(yt, 'OMI')
# CHECK
yc[CMN %in% masteRgeo::comuni_storico[catasto %chin% yt[is.na(termine)][!catasto %chin% comuni[is.na(termine), catasto], catasto], CMN]]
# 5122 e 12144 sono i due nuovi comuni 2023, controllare a 20231 che siano stati inseriti
message(' - salvataggio...\n')
salva_dts_pkg(yt, 'zone', omi_path$output, dbn = omi_path$dbn)
message('Elaborazione Storico "Valori"')
yt <- yv[, c('periodo', 'LinkZona', 'Cod_Tip', 'Stato', 'Compr_min', 'Compr_max', 'Loc_min', 'Loc_max')]
setnames(yt, c('periodo', 'OMIk', 'tipologia', 'stato', 'min_acq', 'max_acq', 'min_loc', 'max_loc'))
yk <- fst::read_fst(file.path(omi_path$output, 'zone'), columns = c('OMIk', 'OMI'), as.data.table = TRUE)
yt <- yk[yt, on = 'OMIk']
for(x in 1:nrow(ytna)) yt[OMIk == ytna[x, OMIko], `:=`( OMIk = ytna[x, OMIk], OMI = ytna[x, OMI] )]
yt[, OMIk := NULL]
setcolorder(yt, c('periodo'))
yt[yt == '' | yt == 0] <- NA
yt[, `:=`(
stato = substr(stato, 1, 1),
min_loc = as.numeric(gsub(',', '.', min_loc)),
max_loc = as.numeric(gsub(',', '.', max_loc))
)]
yt[is.na(stato), stato := 'N']
yt[periodo == 20141 & OMI == 'A9440C10' & tipologia == 13 & stato == 'N', max_loc := 14.3]
yt[periodo == 20142 & OMI == 'F10600D1' & tipologia == 5 & stato == 'N', max_loc := 5.9]
yt <- unique(yt)
setorderv(yt)
message(' - salvataggio...\n')
salva_dts_pkg(yt[!is.na(OMI)], 'quotazioni', omi_path$output, dbn = omi_path$dbn, csv2zip = TRUE)
message('Lettura e Compilazione file "Transazioni"')
y <- rbindlist(lapply(
list.files(file.path(omi_path$input, 'VC'), 'VALORI', full.names = TRUE),
\(x){
px <- gsub('.*_([0-9]{4})_.*$', '\\1', x)
strx <- paste0('NTN_', px)
yt <- fread(x, drop = c('Area', 'AREA', 'Regione', 'REGIONE', 'prov', 'PROVINCIA'))
names(yt) <- gsub('CodFitt', 'CodCom', names(yt))
setnames(yt, 1, 'catasto')
names(yt) <- gsub(paste0(strx, '_|_mq'), '', gsub(' {1,3}', '_', names(yt)))
if(length(grep(strx, names(yt))) > 0) setnames(yt, strx, 'TOT_RES')
yt <- unique(yt)
yt <- melt(yt, id.vars = 1, variable.factor = FALSE, variable.name = 'sigla', value.name = 'valore')
yt[yt == '' | yt == 0] <- NA
data.table(anno = px, yt)
}
))
message('Elaborazione Storico "Transazioni"')
y[, valore := as.numeric(gsub(',', '.', valore))]
y <- y[!is.na(valore)][valore > 0]
y[, sigla := gsub('NTN_', '', sigla)]
y[, sigla := gsub('_-|_', '-', sigla, fixed = TRUE)]
y[sigla == 'TOTALE', sigla := 'TOT_RES']
y[grepl('Depositi_Comm', sigla), sigla := 'Autorimesse']
y <- tipologie[, .(sigla, tipologia = codice)][y, on = 'sigla'][, sigla := NULL]
y[is.na(tipologia), tipologia := 255]
setcolorder(y, c('anno', 'catasto', 'tipologia'))
setorderv(y)
message(' - salvataggio...\n')
salva_dts_pkg(y, 'transazioni', omi_path$output, dbn = omi_path$dbn)
message('Elaborazioni Terminate. Pulizia Ambiente ed Uscita')
rm(list = ls())
gc()
.rs.restartR()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.