data-raw/elabora_facebook.R

#################################################################
# Popolazione a livello microterritoriale - Facebook - 30 metri #
#################################################################
# https://data.humdata.org/dataset/italy-high-resolution-population-density-maps-demographic-estimates
# totali: 53.200.892, non nulli: 50.455.472, italia validi "general": 29.774.007

masteRfun::load_pkgs(master = FALSE, 'data.table', 'sf')
in_path <- file.path(ext_path, 'facebook')
out_path <- file.path(data_path, 'gridpop', 'facebook')

# for(fn in list.files(in_path, 'zip', full.names = TRUE)) unzip(fn, exdir = in_path)

yb <- readRDS(file.path(bnd_path, 'CMN', 's20', '0')) |> st_transform(crs.it.ed)

fns <- list.files(in_path, 'csv$', full.names = TRUE)
tns <- gsub('.*ita_(.*)_2020.*', '\\1', fns) 
for(idx in 1:length(fns)){
    message('\n===============================================')
    message('\nELABORAZIONE: ', toupper(tns[idx]))
    y <- fread(fns[idx], col.names = c('x_lon', 'y_lat', 'pop'))
    y[, `:=`( id = 1:.N, x_lon = x_lon + 0.00025 )]
    y <- y[!(x_lon < bbox.it[1, 1] | x_lon > bbox.it[1, 2] | y_lat < bbox.it[2, 1] | y_lat > bbox.it[2, 2]) & pop > 0]
    yt <- rbindlist(lapply(
            yb$CMN,
            \(x){
                message('Elaboro: ', x)
                ybx <- yb |> subset(CMN == x) 
                ybb <- ybx |> st_transform(4326) |> st_bbox()
                yx <- y[x_lon %between% c(ybb[1], ybb[3]) & y_lat %between% c(ybb[2], ybb[4])] |> 
                        st_as_sf(coords = c('x_lon', 'y_lat'), crs = 4326) |> 
                        st_transform(crs.it.ed) |> 
                        st_join(ybx, join = st_within) |> 
                        st_transform(4326) |> 
                        subset(!is.na(CMN))
                y <<- y[!id %in% yx$id]
                data.table(x, yx |> subset(select = pop) |> st_drop_geometry(), yx |> st_coordinates())
            }
    ))
    setnames(yt, c('CMN', 'pop', 'x_lon', 'y_lat'))
    setcolorder(yt, c('CMN', 'x_lon', 'y_lat'))
    write_fst_idx(yt, 'CMN', tns[idx], out_path)
}

# write_fst_idx(coords, c('ref', 'CMN'), 'centroidi', out_path)
# 
# fn <- 'fb_centroidi'
# assign(fn, coords)
# save( list = fn, file = file.path('data', paste0(fn, '.rda')), version = 3, compress = 'gzip' )
# dbm_do('gridpop', 'w', fn, coords)
# fwrite(coords, './data-raw/csv/fb_centroidi.csv')
# 
# dbm_do('gridpop', 'w', 'fb_totale', fst::read_fst(file.path(out_path, 'general')))
# dbm_do('gridpop', 'w', 'fb_anziani', fst::read_fst(file.path(out_path, 'elderly_60_plus')))
# dbm_do('gridpop', 'w', 'fb_giovani', fst::read_fst(file.path(out_path, 'youth_15_24')))
master-info/masteRgridpop documentation built on July 31, 2023, 1:37 p.m.