Last active
March 2, 2022 11:07
-
-
Save jrosell/b4afcdba309b521a09b3107192a77cb8 to your computer and use it in GitHub Desktop.
Obtenció de dades de "Municipi en xifres" de idescat via API en R.
This file contains 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
library(tidyverse) | |
library(rvest) | |
library(xml2) | |
get_attrs <- function(df) { | |
ret <- | |
map(df, ~xml_attrs(df)) %>% | |
bind_rows %>% | |
rename(tipus = scheme) | |
ret | |
} | |
get_nodes <- function(nodes_url) { | |
com <- read_xml(nodes_url) %>% html_nodes("v[scheme=com]") %>% get_attrs %>% distinct | |
comarques <- read_xml(nodes_url) %>% as_list %>% as_tibble %>% unnest_longer(fitxes) %>% filter(fitxes_id == "v") %>% select(-fitxes_id) %>% unnest_longer(fitxes) %>% filter(fitxes_id != "v") %>% unnest(fitxes) %>% transmute(nom = fitxes) | |
mun <- read_xml(nodes_url) %>% html_nodes("v[scheme=mun]") %>% get_attrs %>% distinct | |
municipis <- read_xml(nodes_url) %>% as_list %>% as_tibble %>% unnest_longer(fitxes) %>% filter(fitxes_id == "v") %>% select(-fitxes_id) %>% unnest_longer(fitxes) %>% filter(fitxes_id == "v") %>% unnest(fitxes) %>% unnest(fitxes) %>% transmute(nom = fitxes) | |
ret <- bind_rows( | |
bind_cols(com, comarques), | |
bind_cols(mun, municipis) | |
) | |
ret | |
} | |
get_taula <- function(id, tipus){ | |
ret <- read_xml(paste0("https://api.idescat.cat/emex/v1/dades.xml?id=", id,"&tipus=", tipus)) %>% | |
as_list %>% | |
as_tibble %>% | |
unnest_longer(fitxes) %>% | |
filter(fitxes_id == "g") %>% | |
unnest_wider(fitxes) %>% | |
unnest(cols = names(.)) %>% | |
unnest(cols = names(.)) %>% | |
rowwise() %>% | |
mutate(tt_len = length(unlist(tt))) %>% | |
filter(tt_len > 1) %>% | |
select(tt) %>% | |
unnest_longer(tt) %>% | |
unnest_wider(tt) %>% | |
rowwise() %>% | |
mutate( | |
c = unlist(c), | |
calt = unlist(calt), | |
v = unlist(v), | |
u = ifelse(length(unlist(u)) > 0, unlist(u), ""), | |
updated = ifelse(length(unlist(updated)) > 0, unlist(updated), ""), | |
r = ifelse(length(unlist(r)) > 0, unlist(r), ""), | |
tipus = tipus, | |
id = id | |
) %>% | |
select(-tt_id) | |
Sys.sleep(10) | |
ret | |
} | |
get_dades <- function(nodes) { | |
res <- nodes %>% | |
group_split(id) %>% | |
map_df(function(el){ | |
id = el[["id"]] | |
tipus = el[["tipus"]] | |
nom = el[["nom"]] | |
tibble( | |
nom = nom, | |
nested = list(get_taula(id, tipus)) | |
) %>% | |
unnest(nested) | |
}) | |
ret | |
} | |
get_emex <- function(lang = "ca", enc = "utf-8") { | |
if (!file.exists("emex-nodes.csv")) { | |
nodes <- get_nodes(paste0('https://api.idescat.cat/emex/v1/nodes.xml?lang=',ca,'&enc=',enc)) | |
write_csv(nodes, "emex-nodes.csv") | |
} | |
nodes <- read_csv("emex-nodes.csv") | |
if (!file.exists("emex-dades.csv")) { | |
dades <- get_dades(nodes) | |
write_csv(dades, "emex-dades.csv") | |
} | |
dades <- read_csv("emex-dades.csv") | |
dades | |
} | |
if(interactive()){ | |
get_emex() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment