Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lvalnegri committed Jul 23, 2022
1 parent 2273ad1 commit 8d0e23f
Show file tree
Hide file tree
Showing 30 changed files with 1,118,418 additions and 1 deletion.
27 changes: 27 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Package: it_hydrogeo_risk
Type: Package
Date: 2022-07-31
Title: Hazards and Risks from Floods and Landslides In Italy
Version: 0.1.0
Authors@R: person("Luca", "Valnegri", email = "l.valnegri@datamaps.co.uk", role = c("aut", "cre"))
Description: Data and Digital Boundaries for the Analysis of Hazards and Risks from Floods and Landslides In Italy.
License: file LICENSE
URL: https://github.com/lvalnegri/it_hydrogeo_risk
BugReports: https://github.com/lvalnegri/it_hydrogeo_risk/issues
Encoding: UTF-8
Depends: R (>= 4.1)
Imports:
data.table,
leaflet,
rmapshaper,
qs,
sf
Suggests:
dplyr,
fst,
shiny,
shinyjs
Remotes:
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.0
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(dpath)
export(palette.lst)
export(risks.lst)
103 changes: 103 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#' #' @importFrom data.table data.table
#' NULL
#'
#' #' @import sf
#' NULL
#'
#' #' @import leaflet
#' NULL
#'
#' #' dataset
#' #'
#' #' Tabella dati territoriali di rischio idrogeologico
#' #'
#' #' @format Una data.table con i campi seguenti:
#' #' \describe{
#' #' \item{\code{CMN}}{ Codice ISTAT del Comune }
#' #' \item{\code{var_id}}{ Identificativo della Variabile (chiave esterna per la tabella metadati `mdts`) }
#' #' \item{\code{value}}{ Valore assunto dalla Variabile sul Comune }
#' #' }
#' #'
#' 'dts'
#'
#' #' metadata
#' #'
#' #' Tabella di spiegazione delle variabili contenute nella tabella dati `dts`
#' #'
#' #' @format Una data.table con i campi seguenti:
#' #' \describe{
#' #' \item{\code{var_id}}{ Identificativo della Variabile }
#' #' \item{\code{id_orig}}{ Identificativo della Variabile nel file ISPRA }
#' #' \item{\code{description}}{ Descrizione della Variabile }
#' #' \item{\code{label}}{ Etichetta della Variabile }
#' #' \item{\code{source}}{ Sorgente dati }
#' #' \item{\code{year}}{ Anno di misurazione }
#' #' \item{\code{risk}}{ Tipologia di Rischio }
#' #' \item{\code{level}}{ Descrizione del Livello di Rischio }
#' #' \item{\code{clevel}}{ Codice del Livello di Rischio }
#' #' \item{\code{measure}}{ Dato Territoriale di riferimento }
#' #' \item{\code{metric}}{ Specifica se Conteggio o Percentuale }
#' #' }
#' #'
#' 'mdts'
#'
#' #' cmn_lid
#' #'
#' #' Tabella di mappatura fra Codici Comuni ed Aree a Rischio
#' #'
#' #' @format Una data.table con i campi seguenti:
#' #' \describe{
#' #' \item{\code{risk}}{ Tipologia di Rischio: `G`eologico (Frana) o `I`drologico (Alluvione) }
#' #' \item{\code{lid}}{ Identificativo unico formato come concatenazione fra `livello` e `id` }
#' #' \item{\code{level}}{ Valori assunti dal Rischio (si veda la lista `livelli.lst`) }
#' #' \item{\code{id}}{ Identificativo di Livello per ogni tipologia di Rischio }
#' #' \item{\code{CMN}}{ Codice ISTAT del Comune }
#' #' }
#' #'
#' 'cmn_lid'
#'
#' #' geoCMN
#' #'
#' #' Dataset containing the complete list of Municipalities (as of 2022), and corresponding Province
#' #'
#' #' @format An `data.table` object with the following fields:
#' #' \describe{
#' #' \item{\code{CMN}}{ ISTAT code for the Municipality }
#' #' \item{\code{CMNd}}{ Name of the Municipality }
#' #' \item{\code{PRV}}{ ISTAT code for the Province }
#' #' \item{\code{PRVd}}{ Name of the Province }
#' #' \item{\code{PRVs}}{ Road Code of the Province }
#' #' }
#' #'
#' 'geoCMN'
#'
#' #' bndCMN
#' #'
#' #' Digital Boundaries for Italian Municipalities
#' #'
#' #' @format An `sf` object with the following fields:
#' #' \describe{
#' #' \item{\code{CMN}}{ ISTAT code for the Municipality }
#' #' \item{\code{CMNd}}{ Name of the Municipality }
#' #' }
#' #'
#' 'bndCMN'
#'
#' #' bndPRV
#' #'
#' #' Digital Boundaries for Italian Provinces
#' #'
#' #' @format An `sf` object with the following fields:
#' #' \describe{
#' #' \item{\code{PRV}}{ ISTAT code for the Province }
#' #' \item{\code{PRVd}}{ Name of the Province }
#' #' \item{\code{PRVs}}{ Road Code of the Province }
#' #' }
#' #'
#' 'bndPRV'
#'
#' #' mps
#' #'
#' #' `Leaflet` object to be used as initial map for shiny apps
#' #'
#' 'mps'
7 changes: 7 additions & 0 deletions R/it_hydrogeo_risk.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' it_hydrogeo_risk
#'
#' Data and Digital Boundaries for the Analysis of Hazards and Risks from Floods and Landslides In Italy.
#'
#' @docType package
#' @name it_hydrogeo_risk
NULL
59 changes: 59 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' dpath
#'
#' Folder for the `sf` files
#'
#' @export
#'
dpath <- file.path(Rfuns::datait_path, 'ispra')

#' #' cmn.lst
#' #'
#' #' Complete List of Italian Municipalities (`CMN`), with Zip Codes and Provinces Roads Codes.
#' #'
#' #' @export
#' #'
#' cmn.lst <- create_cmn_lst()

#' palette.lst
#'
#' List of default colours to associate with the various Levels of Hazards and Risks
#'
#' @export
#'
palette.lst <- list(
'geo' = list('P4' = '#6E0000', 'P3' = '#DC0000', 'P2' = '#DE6700', 'P1' = '#F1CA2E', 'AA' = '#FEFF68'),
'idro' = list('H' = '#0C2775', 'M' = '#2D72FF', 'L' = '#C3E8FF')
)

#' risks.lst
#'
#' Description of the various Levels of Hazards and Risks
#'
#' @export
#'
risks.lst <- list(
'geo' = list('P4' = 'Very High', 'P3' = 'High', 'P2' = 'Medium', 'P1' = 'Moderate', 'AA' = 'Attention'),
'idro' = list('H' = 'High', 'M' = 'Medium', 'L' = 'Low')
)


# .onAttach <- function(libname, pkgname) {
# packageStartupMessage(
# )
# }

# .onLoad <- function(libname, pkgname) {
# oop <- options()
# opts <- list(
# pkgname.optname = " option value goes here "
# )
# toset <- !(names(opts) %in% names(oop))
# if(any(toset)) options(opts[toset])
#
# invisible()
# }

# .onUnload <- function(libname, pkgname) {
#
# }

3 changes: 3 additions & 0 deletions R/zzzGV.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
utils::globalVariables(c(
'id', 'x_lon', 'y_lat' # column names in data.table "table_name"
))
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# it_hydrogeo_risk
## Hydrogeologic Risk in Italy




### Resources
- [Vector Data (shapefiles) for Landslide Risk (v.4.0, 2020-2021)](https://idrogeo.isprambiente.it/opendata/wms/Mosaicatura_ISPRA_2020_2021_aree_pericolosita_frana_PAI.zip)
- [Vector Data (shapefiles) for Flooding Risk (v.5.0, 2020)](https://idrogeo.isprambiente.it/opendata/wms/Mosaicatura_ISPRA_2020_aree_pericolosita_idraulica.zip)
- Metrics:
[Country](https://idrogeo.isprambiente.it/api/pir/italia/export?outputFormat=csv),
[Regions](https://idrogeo.isprambiente.it/api/pir/regioni/export?outputFormat=csv),
[Provinces](https://idrogeo.isprambiente.it/api/pir/province/export?outputFormat=csv),
[Municipalities](https://idrogeo.isprambiente.it/api/pir/comuni/export?outputFormat=csv),
([Metadata](https://idrogeo.isprambiente.it/assets/data/Metadati_open_data_PIR.csv))
- []()

- [Interactive App Web](https://idrogeo.isprambiente.it/app/)
55 changes: 55 additions & 0 deletions data-raw/01-create_database.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#########################################################################
# ISPRA RISCHIO IDROGEO - Creazione databases e tabelle in MySQL server #
#########################################################################

library(Rfuns)

dbn <- 'ispra'
dd_create_db(dbn)

## TABLE <dataset> ----------------
x <- "
`CMN` MEDIUMINT UNSIGNED NOT NULL,
`var_id` CHAR(14) NOT NULL,
`value` DECIMAL(10, 3) NOT NULL,
PRIMARY KEY (`CMN`, `var_id`)
"
dd_create_dbtable('dataset', dbn, x)

## TABLE <metadata> ----------------
x <- "
`var_id` CHAR(14) NOT NULL,
`id_orig` CHAR(12) NOT NULL,
`description` VARCHAR(120) NOT NULL,
`label` VARCHAR(50) NULL DEFAULT NULL,
`source` CHAR(5) NULL DEFAULT NULL,
`year` SMALLINT UNSIGNED NULL DEFAULT NULL,
`risk` CHAR(10) NULL DEFAULT NULL,
`level` CHAR(15) NULL DEFAULT NULL,
`clevel` CHAR(5) NULL DEFAULT NULL,
`measure` CHAR(15) NULL DEFAULT NULL,
`metric` CHAR(11) NULL DEFAULT NULL,
PRIMARY KEY (`var_id`),
KEY `level` (`level`),
KEY `clevel` (`clevel`),
KEY `measure` (`measure`),
KEY `metric` (`metric`)
"
dd_create_dbtable('metadata', dbn, x)

## TABLE <lookups> ----------------
x <- "
`risk` CHAR(1) NOT NULL,
`lid` CHAR(6) NOT NULL,
`level` CHAR(3) NOT NULL,
`id` SMALLINT UNSIGNED NOT NULL,
`CMN` MEDIUMINT UNSIGNED NOT NULL,
PRIMARY KEY (`risk`, `lid`, `CMN`),
KEY `livello` (`level`),
KEY `id` (`id`)
"
dd_create_dbtable('lookups', dbn, x)

## FINE -------------------------------
rm(list = ls())
gc()
26 changes: 26 additions & 0 deletions data-raw/06-download_files.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#######################################################
# ISPRA HYDROGEO RISK - download and decompress files #
#######################################################

Rfuns::load_pkgs()

opath <- file.path(ext_path, 'it', 'ispra')
tmpf <- tempfile()

# FLOODING (hydro risk)
download.file('https://idrogeo.isprambiente.it/opendata/wms/Mosaicatura_ISPRA_2020_aree_pericolosita_idraulica.zip', tmpf)
out_path <- file.path(opath, 'hydro')
unzip(tmpf, exdir = out_path)
file.remove(grep('pdf$', list.files(out_path, full.names = TRUE), value = TRUE))
file.rename(list.files(out_path, full.names = TRUE), file.path(out_path, gsub('(.{1}).*(\\..*$)', '\\1\\2', list.files(out_path))))

# LANDSLIDES (geo risk)
download.file('https://idrogeo.isprambiente.it/opendata/wms/Mosaicatura_ISPRA_2020_2021_aree_pericolosita_frana_PAI.zip', tmpf)
out_path <- file.path(opath, 'geo')
unzip(tmpf, exdir = out_path)
file.remove(grep('pdf$', list.files(out_path, full.names = TRUE), value = TRUE))
file.rename(list.files(out_path, full.names = TRUE), file.path(out_path, gsub('(.{1}).*(\\..*$)', 'geo_ori\\2', list.files(out_path))))

unlink(tmpf)
rm(list = ls())
gc()
78 changes: 78 additions & 0 deletions data-raw/11-hydro_risk.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
###########################################
# ISPRA HYDROGEO RISK - Polygons Flooding #
###########################################

Rfuns::load_pkgs('data.table', 'dplyr', 'qs', 'sf')

in_path <- file.path(ext_path, 'it', 'ispra', 'hydro')
out_path <- file.path(datait_path, 'ispra', 'hydro')

# LETTURA e PULIZIA SHAPEFILES CON AGGIUNTA id
yb <- rbind(
st_read(file.path(in_path, 'L.shp'), quiet = TRUE) |> select(-1) |> mutate(level = 'L', id = 1:n()),
st_read(file.path(in_path, 'M.shp'), quiet = TRUE) |> select(-1) |> mutate(level = 'M', id = 1:n()),
st_read(file.path(in_path, 'H.shp'), quiet = TRUE) |> select(-1) |> mutate(level = 'H', id = 1:n())
) |> dplyr::mutate(lid = paste0(id, level))
st_write(yb, file.path(in_path, 'idro.shp'))
qsave(yb, file.path(out_path, 'idro.qs'), nthreads = 10)

# CREA TABELLA LEGAMI id-CMN (intersezione con comuni)
ybc <- RbndIT::CMN |> st_transform(3035)
ye <- st_intersects(yb, ybc)
yt <- yb |> st_drop_geometry()
yc <- ybc |> st_drop_geometry()
ye <- rbindlist(lapply( 1:nrow(ye), \(x) data.table( yt[x,], yc[ye[[x]],] ) )) |> setnames('V2', 'CMN')
ye[, lid := paste0(id, livello)]
fwrite(ye, './data-raw/csv/idro_comuni.csv')
fst::write_fst(ye, file.path(out_path, 'idro.fst'))

# ESTRAI PROVINCIE
ybp <- RbndIT::PRV |> st_transform(3035)
yc <- RgeoIT::comuni[, .(CMN = as.integer(CMN), PRV)][order(PRV, CMN)]
y <- list()
for(p in ybp$PRV){
message('Provincia ', p)
ycp <- yc[PRV == p, CMN]
y[[p]] <- st_intersection( yb |> subset(lid %in% ye[CMN %in% ycp, lid]), ybp |> subset(PRV == p) ) |> select(-PRV)
qsave(y[[p]], file.path(out_path, p), nthreads = 10)
}
qsave(y, file.path(out_path, 'idro.prv'), nthreads = 10)

# ESTRAI COMUNI e CALCOLO DIFFERENZE
st_stuff <- \(x){
if(is.na(sum(st_bbox(x)))) return(NULL)
# if(!(st_is(x, 'MULTIPOLYGON') | st_is(x, 'POLYGON'))) x <- x |> st_collection_extract('POLYGON')
x |> st_collection_extract('POLYGON') |> st_union() |> st_transform(4326)
}
yy <- list()
for(p in ybp$PRV){
message('Provincia ', p)
for(pc in yc[PRV == p, CMN]){
yt <- st_intersection(ybc |> subset(CMN == pc), y[[p]] ) |> select(livello)
if(nrow(yt) > 0){
yy[[as.character(pc)]] <- list(
'bbx' = st_bbox(yt |> st_transform(4326)),
'H' = yt |> subset(livello == 'H') |> st_stuff(),
'M' = st_difference(yt |> subset(livello == 'M') |> st_union(), yt |> subset(livello == 'H') |> st_union()) |> st_stuff(),
'L' = st_difference(yt |> subset(livello == 'L') |> st_union(), yt |> subset(livello %in% c('H', 'M')) |> st_union()) |> st_stuff(),
'CMN' = masteRconfini::CMN |> subset(CMN == pc)
)
} else {
yy[[as.character(pc)]] <- list( 'bbx' = NULL, 'CMN' = masteRconfini::CMN |> subset(CMN == pc) )
}
qsave(yy[[as.character(p)]], file.path(out_path, pc), nthreads = 10)
}
}
qsave(yy, file.path(out_path, 'idro.cmn'), nthreads = 10)

# library(leaflet)
# leaflet() |>
# aggiungi_tessera(tiles.lst[[19]]) |>
# addPolygons(data = yt |> subset(livello == 'H'), group = 'H', fillColor = colori.lst$idro$H, opacity = 0, fillOpacity = 1) |>
# addPolygons(data = yt |> subset(livello == 'M'), group = 'M', fillColor = colori.lst$idro$M, opacity = 0, fillOpacity = 1) |>
# addPolygons(data = yt |> subset(livello == 'L'), group = 'L', fillColor = colori.lst$idro$L, opacity = 0, fillOpacity = 1) |>
# addLayersControl(overlayGroups = c('H', 'M', 'L'))

# ESCI
rm(list = ls())
gc()
Loading

0 comments on commit 8d0e23f

Please sign in to comment.