|
| 1 | +#> DATASET: schools 2020 |
| 2 | +#> Source: INEP - |
| 3 | +#> https://www.gov.br/inep/pt-br/acesso-a-informacao/dados-abertos/inep-data/catalogo-de-escolas |
| 4 | +#> |
| 5 | +#: scale |
| 6 | +#> Metadata: |
| 7 | +# Titulo: schools |
| 8 | +#' Frequencia de atualizacao: anual |
| 9 | +#' |
| 10 | +#' Forma de apresentação: Shape |
| 11 | +#' Linguagem: Pt-BR |
| 12 | +#' Character set: Utf-8 |
| 13 | +#' |
| 14 | +#' Resumo: Pontos com coordenadas gegráficas das escolas do censo escolar |
| 15 | +#' Informações adicionais: Dados produzidos pelo INEP. Os dados de escolas e sua |
| 16 | +#' geolocalização são atualizados pelo INEP continuamente. Para finalidade do geobr, |
| 17 | +#' esses dados precisam ser baixados uma vez ao ano |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +update_schools <- function(){ |
| 23 | + |
| 24 | + |
| 25 | + # If the data set is updated regularly, you should create a function that will have |
| 26 | + # a `date` argument download the data |
| 27 | + update <- 2023 |
| 28 | + date_update <- Sys.Date() |
| 29 | + |
| 30 | + # date shown to geobr user |
| 31 | + geobr_date <- gsub('-', '' , date_update) |
| 32 | + geobr_date <- substr(geobr_date, 1, 6) |
| 33 | + |
| 34 | + |
| 35 | + # download manual |
| 36 | + # https://www.gov.br/inep/pt-br/acesso-a-informacao/dados-abertos/inep-data/catalogo-de-escolas |
| 37 | + dt <- fread('C:/Users/r1701707/Downloads/Análise - Tabela da lista das escolas - Detalhado.csv', |
| 38 | + encoding = 'UTF-8') |
| 39 | + head(dt) |
| 40 | + |
| 41 | + |
| 42 | + ##### 4. Rename columns ------------------------- |
| 43 | + head(dt) |
| 44 | + |
| 45 | + df <- dplyr::select(dt, |
| 46 | + abbrev_state = 'UF', |
| 47 | + name_muni = 'Município', |
| 48 | + code_school = 'Código INEP', |
| 49 | + name_school = 'Escola', |
| 50 | + education_level = 'Etapas e Modalidade de Ensino Oferecidas', |
| 51 | + education_level_others = 'Outras Ofertas Educacionais', |
| 52 | + admin_category = 'Categoria Administrativa', |
| 53 | + address = 'Endereço', |
| 54 | + phone_number = 'Telefone', |
| 55 | + government_level = 'Dependência Administrativa', |
| 56 | + private_school_type = 'Categoria Escola Privada', |
| 57 | + private_government_partnership = 'Conveniada Poder Público', |
| 58 | + regulated_education_council = 'Regulamentação pelo Conselho de Educação', |
| 59 | + service_restriction ='Restrição de Atendimento', |
| 60 | + size = 'Porte da Escola', |
| 61 | + urban = 'Localização', |
| 62 | + location_type = 'Localidade Diferenciada', |
| 63 | + date_update = 'date_update', |
| 64 | + y = 'Latitude', |
| 65 | + x = 'Longitude' |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + head(df) |
| 72 | + |
| 73 | + |
| 74 | + # add update date columns |
| 75 | + df[, date_update := as.character(date_update)] |
| 76 | + |
| 77 | + |
| 78 | + # deal with points with missing coordinates |
| 79 | + head(df) |
| 80 | + df[is.na(x) | is.na(y),] |
| 81 | + df[x==0,] |
| 82 | + |
| 83 | + # identify which points should have empty geo |
| 84 | + df[is.na(x) | is.na(y), empty_geo := T] |
| 85 | + |
| 86 | + df[code_school=='11000180', x] |
| 87 | + |
| 88 | + |
| 89 | + # replace NAs with 0 |
| 90 | + data.table::setnafill(df, |
| 91 | + type = "const", |
| 92 | + fill = 0, |
| 93 | + cols=c("x","y") |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + # Convert originl data frame into sf |
| 99 | + temp_sf <- sf::st_as_sf(x = df, |
| 100 | + coords = c("x", "y"), |
| 101 | + crs = "+proj=longlat +datum=WGS84") |
| 102 | + |
| 103 | + |
| 104 | + # convert to point empty |
| 105 | + # 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 |
| 106 | + temp_sf$geometry[temp_sf$empty_geo == T] = sf::st_point() |
| 107 | + |
| 108 | + subset(temp_sf, code_school=='11000180') |
| 109 | + |
| 110 | + |
| 111 | + # Change CRS to SIRGAS Geodetic reference system "SIRGAS2000" , CRS(4674). |
| 112 | + temp_sf <- harmonize_projection(temp_sf) |
| 113 | + |
| 114 | + |
| 115 | + # create folder to save the data |
| 116 | + dest_dir <- paste0('./data/schools/', update,'/') |
| 117 | + dir.create(path = dest_dir, recursive = TRUE, showWarnings = FALSE) |
| 118 | + |
| 119 | + |
| 120 | + # Save raw file in sf format |
| 121 | + sf::st_write(temp_sf, |
| 122 | + dsn= paste0(dest_dir, 'schools_', update,".gpkg"), |
| 123 | + overwrite = TRUE, |
| 124 | + append = FALSE, |
| 125 | + delete_dsn = T, |
| 126 | + delete_layer = T, |
| 127 | + quiet = T |
| 128 | + ) |
| 129 | + |
| 130 | +} |
0 commit comments