Last active
November 16, 2020 09:41
-
-
Save giocomai/9bb8510c39aff5e5ab4494ad4b8c88a8 to your computer and use it in GitHub Desktop.
population grid to kml example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#remotes::install_github("giocomai/latlon2map") | |
library("latlon2map") | |
ll_set_folder(path = "~/R") | |
library("ggplot2") | |
library("dplyr") | |
library("sf") | |
# recupera griglia di esempio come oggetto sf | |
if (file.exists("base_grid.rds")==FALSE) { | |
### Trentino | |
# download.file(url = "https://testzone.giorgiocomai.eu/files/pop_grids/2011-it_trento_nuts3_1km_pop_grid_eu.rds", | |
# destfile = "base_grid.rds") | |
#Venezia | |
download.file(url = "https://testzone.giorgiocomai.eu/files/pop_grids/it_venezia_nuts3_pop_grid.rds", | |
destfile = "base_grid.rds") | |
} | |
base_grid <- readr::read_rds(file = "base_grid.rds") | |
### oppure genera dall'originale, anche cambiando provincia (funzione scarica da sola i dati, ma serve un po' di RAM per filtrare) | |
# base_grid <- ll_get_population_grid(match_sf = ll_get_nuts_it(level = 3, name = "Trento"), | |
# match_name = "it_trento_nuts3_pop_grid") | |
### o per venezia che è città metropolitana | |
# base_grid <- ll_get_population_grid(match_sf = ll_get_nuts_it(level = 3) %>% | |
# filter(DEN_CM=="Venezia"), | |
# match_name = "it_venezia_nuts3_pop_grid") | |
# versione base | |
sf <- base_grid %>% | |
mutate(L2 = row_number(), # serve per fare il join sotto | |
custom_colour = scales::col_numeric(domain = NULL, # se no qui si può dare c(min, max) | |
palette = c("#eff3ff", # colore minimo e massimo per il gradiente | |
"#08306b"))(TOT_P)) | |
# versione usata per l'esempio (meno "precisa", ma più chiara, da valutare altre formule) | |
sf <- base_grid %>% | |
filter(TOT_P>20) %>% # lascia solo celle con piu di venti persone | |
mutate(TOT_P = TOT_P + 10) %>% # dai a ogni poligono 10 metri di altezza in più, per evitare che restino sotto case e alture | |
mutate(L2 = row_number(), | |
custom_colour = scales::col_numeric(domain = NULL, | |
palette = c("#eff3ff", | |
"#08306b"))(sqrt(TOT_P))) # radice quadrata del valore per determinare il colore, per evidenziare differenza (tante celle con valori bassi, poche con valori altissimi) | |
sf_pre_kml <- sf %>% | |
st_coordinates() %>% | |
as_tibble() %>% | |
left_join(y = sf, by = "L2") %>% | |
mutate(Z = TOT_P) %>% | |
st_as_sf(coords = c("X", "Y", "Z"), dim = "XYZ") %>% | |
group_by(L2, .drop = TRUE) %>% | |
summarise(geometry = st_combine(geometry), .groups = "drop") %>% | |
st_cast("POLYGON") %>% | |
mutate(altitudeMode="relativeToGround", | |
extrude = TRUE) | |
ll_export_sf_to_kml(sf = sf_pre_kml, | |
path = "pop_grid.kml", | |
fill_colour = sf$custom_colour, | |
line_width = "0.5px") #questa è lo spessore della righina bianca di contorno dei poligoni. puoi anche cambiare colore con parametro `line_colour = ` | |
#output: https://testzone.giorgiocomai.eu/files/pop_grids/pop_grid_venezia.kml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment