diff --git a/data_prep/R/schools.R b/data_prep/R/schools.R new file mode 100644 index 00000000..eb56bfbf --- /dev/null +++ b/data_prep/R/schools.R @@ -0,0 +1,130 @@ +#> DATASET: schools 2020 +#> Source: INEP - +#> https://www.gov.br/inep/pt-br/acesso-a-informacao/dados-abertos/inep-data/catalogo-de-escolas +#> +#: scale +#> Metadata: +# Titulo: schools +#' Frequencia de atualizacao: anual +#' +#' Forma de apresentação: Shape +#' Linguagem: Pt-BR +#' Character set: Utf-8 +#' +#' Resumo: Pontos com coordenadas gegráficas das escolas do censo escolar +#' Informações adicionais: Dados produzidos pelo INEP. Os dados de escolas e sua +#' geolocalização são atualizados pelo INEP continuamente. Para finalidade do geobr, +#' esses dados precisam ser baixados uma vez ao ano + + + + +update_schools <- function(){ + + + # If the data set is updated regularly, you should create a function that will have + # a `date` argument download the data + update <- 2023 + date_update <- Sys.Date() + + # date shown to geobr user + geobr_date <- gsub('-', '' , date_update) + geobr_date <- substr(geobr_date, 1, 6) + + + # download manual + # https://www.gov.br/inep/pt-br/acesso-a-informacao/dados-abertos/inep-data/catalogo-de-escolas + dt <- fread('C:/Users/r1701707/Downloads/Análise - Tabela da lista das escolas - Detalhado.csv', + encoding = 'UTF-8') + head(dt) + + + ##### 4. Rename columns ------------------------- + head(dt) + + df <- dplyr::select(dt, + abbrev_state = 'UF', + name_muni = 'Município', + code_school = 'Código INEP', + name_school = 'Escola', + education_level = 'Etapas e Modalidade de Ensino Oferecidas', + education_level_others = 'Outras Ofertas Educacionais', + admin_category = 'Categoria Administrativa', + address = 'Endereço', + phone_number = 'Telefone', + government_level = 'Dependência Administrativa', + private_school_type = 'Categoria Escola Privada', + private_government_partnership = 'Conveniada Poder Público', + regulated_education_council = 'Regulamentação pelo Conselho de Educação', + service_restriction ='Restrição de Atendimento', + size = 'Porte da Escola', + urban = 'Localização', + location_type = 'Localidade Diferenciada', + date_update = 'date_update', + y = 'Latitude', + x = 'Longitude' + ) + + + + + head(df) + + + # add update date columns + df[, date_update := as.character(date_update)] + + + # deal with points with missing coordinates + head(df) + df[is.na(x) | is.na(y),] + df[x==0,] + + # identify which points should have empty geo + df[is.na(x) | is.na(y), empty_geo := T] + + df[code_school=='11000180', x] + + + # replace NAs with 0 + data.table::setnafill(df, + type = "const", + fill = 0, + cols=c("x","y") + ) + + + + # Convert originl data frame into sf + temp_sf <- sf::st_as_sf(x = df, + coords = c("x", "y"), + crs = "+proj=longlat +datum=WGS84") + + + # convert to point empty + # solution from: https://gis.stackexchange.com/questions/459239/how-to-set-a-geometry-to-na-empty-for-some-features-of-an-sf-dataframe-in-r + temp_sf$geometry[temp_sf$empty_geo == T] = sf::st_point() + + subset(temp_sf, code_school=='11000180') + + + # Change CRS to SIRGAS Geodetic reference system "SIRGAS2000" , CRS(4674). + temp_sf <- harmonize_projection(temp_sf) + + + # create folder to save the data + dest_dir <- paste0('./data/schools/', update,'/') + dir.create(path = dest_dir, recursive = TRUE, showWarnings = FALSE) + + + # Save raw file in sf format + sf::st_write(temp_sf, + dsn= paste0(dest_dir, 'schools_', update,".gpkg"), + overwrite = TRUE, + append = FALSE, + delete_dsn = T, + delete_layer = T, + quiet = T + ) + +} diff --git a/data_prep/R/support_fun.R b/data_prep/R/support_fun.R index 6d741213..ccbc6a6f 100644 --- a/data_prep/R/support_fun.R +++ b/data_prep/R/support_fun.R @@ -146,7 +146,7 @@ add_region_info <- function(temp_sf, column){ code_region==2, 'Nordeste', code_region==3, 'Sudeste', code_region==4, 'Sul', - code_region==5, 'Centro Oeste', + code_region==5, 'Centro-Oeste', default = NA)) return(temp_sf) } diff --git a/r-package/NEWS.md b/r-package/NEWS.md index 70893e03..be4cec52 100644 --- a/r-package/NEWS.md +++ b/r-package/NEWS.md @@ -5,6 +5,10 @@ - Function `read_health_facilities()` now has a new parameter `date`, which will allow users to access data for different dates of reference. The plan is to have at least one update of this data set per year. +**New data** +- schools for 2023 +- health facilities for 202303 + # geobr v1.8.2 diff --git a/r-package/prep_data/prep_schools.R b/r-package/prep_data/prep_schools.R deleted file mode 100644 index 88213335..00000000 --- a/r-package/prep_data/prep_schools.R +++ /dev/null @@ -1,156 +0,0 @@ -#> DATASET: schools 2020 -#> Source: INEP - -#> https://www.gov.br/inep/pt-br/acesso-a-informacao/dados-abertos/inep-data/catalogo-de-escolas -#> -#: scale -#> Metadata: -# Titulo: schools -#' Frequencia de atualizacao: anual -#' -#' Forma de apresentação: Shape -#' Linguagem: Pt-BR -#' Character set: Utf-8 -#' -#' Resumo: Pontos com coordenadas gegráficas das escolas do censo escolar -#' Informações adicionais: Dados produzidos pelo INEP. Os dados de escolas e sua -#' geolocalização são atualizados pelo INEP continuamente. Para finalidade do geobr, -#' esses dados precisam ser baixados uma vez ao ano -# - -### Libraries (use any library as necessary) - -library(RCurl) -library(stringr) -library(sf) -library(dplyr) -library(readr) -library(data.table) -library(magrittr) -library(lwgeom) -library(stringi) -library(sfheaders) -library(mapview) -library(ggplot2) - -mapviewOptions(platform = 'leafgl') -# mapviewOptions(platform = 'mapdeck') - -####### Load Support functions to use in the preprocessing of the data - -source("./prep_data/prep_functions.R") - -# Root directory -root_dir <- "L:////# DIRUR #//ASMEQ//geobr//data-raw" -setwd(root_dir) - - - -###### 0. Create folders to save the data ----------------- - -# If the data set is updated regularly, you should create a function that will have -# a `date` argument download the data -update <- 2020 -date_update <- '2020-10-18' - - -# Root directory -root_dir <- "L:\\# DIRUR #\\ASMEQ\\geobr\\data-raw" -setwd(root_dir) - -# Directory to keep raw zipped files -dir.create("./schools") -destdir_raw <- paste0("./schools/",update) -dir.create(destdir_raw) - - - - -#### 1. Download manual do dado ----------------- - - -# download manual do dado a partir de - - -# leitura do dado bruto -df <- fread('C:/Users/r1701707/Downloads/Análise - Tabela da lista das escolas - Detalhado (1).csv', - encoding = 'UTF-8') - -head(df) - - - - - - -##### 4. Rename columns ------------------------- -df$date_update <- date_update - - -df2 <- - dplyr::select(df, - abbrev_state = 'UF', - name_muni = 'Município', - code_school = 'Código INEP', - name_school = 'Escola', - education_level = 'Etapas e Modalidade de Ensino Oferecidas', - education_level_others = 'Outras Ofertas Educacionais', - admin_category = 'Categoria Administrativa', - address = 'Endereço', - phone_number = 'Telefone', - government_level = 'Dependência Administrativa', - private_school_type = 'Categoria Escola Privada', - private_government_partnership = 'Conveniada Poder Público', - regulated_education_council = 'Regulamentação pelo Conselho de Educação', - service_restriction ='Restrição de Atendimento', - size = 'Porte da Escola', - urban = 'Localização', - location_type = 'Localidade Diferenciada', - date_update = 'date_update', - y = 'Latitude', - x = 'Longitude' - ) - -head(df2) - - - - -# fix spatial coordinates -summary(df2$x) -temp_sf <- sfheaders::sf_point(df2, x='x', y='y', keep = T) -# temp_sf <- sfheaders::sf_point(subset(df2, !is.na(x)), x='x', y='y', keep = T) - - -# temp_sf = st_as_sf(subset(df2, !is.na(x)), coords = c("x", "y")) - - - -country <- geobr::read_country() -sirgas <- st_crs(country) -st_crs(temp_sf) <- sirgas -st_crs(temp_sf) <- 4674 - -# st_crs(temp_sf) -# head(temp_sf) -# -# a <- temp_sf[1:100,] -# -# plot(a) -mapview(temp_sf) - -ggplot() + - geom_sf(data= country) + - geom_sf(data= temp_sf) - - -##### Save file ------------------------- - -# save raw file -fwrite(df, paste0(destdir_raw, '/schools_', update, '_raw.csv')) - -# Save sf -sf::st_write(temp_sf, dsn= paste0(destdir_raw ,"/schools_", update,".gpkg"), update = TRUE) - - - -