Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ipeaGIT/geobr
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Jul 29, 2024
2 parents 606cfb2 + 8a8dd9b commit 81c4fd6
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The package is currently available in [**R**](https://CRAN.R-project.org/package

| ***R*** | ***Python*** | ***Repo***|
|-----|-----|----|
| [![CRAN/METACRAN Version](https://www.r-pkg.org/badges/version/geobr)](https://CRAN.R-project.org/package=geobr) <br /> [![CRAN/METACRAN Total downloads](http://cranlogs.r-pkg.org/badges/grand-total/geobr?color=blue)](https://CRAN.R-project.org/package=geobr) <br /> [![CRAN/METACRAN downloads per month](http://cranlogs.r-pkg.org/badges/geobr?color=yellow)](https://CRAN.R-project.org/package=geobr) <br /> [![Codecov test coverage](https://codecov.io/gh/ipeaGIT/geobr/branch/master/graph/badge.svg)](https://app.codecov.io/gh/ipeaGIT/geobr?branch=master) <br /> [![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://www.tidyverse.org/lifecycle/#stable) <br /> [![R build status](https://github.com/ipeaGIT/geobr/workflows/R-CMD-check/badge.svg)](https://github.com/ipeaGIT/geobr/actions) | [![PyPI version](https://badge.fury.io/py/geobr.svg)](https://badge.fury.io/py/geobr) <br /> [![Downloads](https://static.pepy.tech/badge/geobr)](https://pepy.tech/project/geobr) <br /> [![Downloads](https://static.pepy.tech/badge/geobr/month)](https://pepy.tech/project/geobr) <br /> [![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing) <br /> [![Python build status](https://github.com/ipeaGIT/geobr/workflows/Python-CMD-check/badge.svg)](https://github.com/ipeaGIT/geobr/actions) |<img alt="GitHub stars" src="https://img.shields.io/github/stars/ipeaGIT/geobr.svg?color=orange"> <br /> <br /> [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) |
| [![CRAN/METACRAN Version](https://www.r-pkg.org/badges/version/geobr)](https://CRAN.R-project.org/package=geobr) <br /> [![CRAN/METACRAN Total downloads](https://cranlogs.r-pkg.org/badges/grand-total/geobr?color=blue)](https://CRAN.R-project.org/package=geobr) <br /> [![CRAN/METACRAN downloads per month](https://cranlogs.r-pkg.org/badges/geobr?color=yellow)](https://CRAN.R-project.org/package=geobr) <br /> [![Codecov test coverage](https://codecov.io/gh/ipeaGIT/geobr/branch/master/graph/badge.svg)](https://app.codecov.io/gh/ipeaGIT/geobr?branch=master) <br /> [![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://www.tidyverse.org/lifecycle/#stable) <br /> [![R build status](https://github.com/ipeaGIT/geobr/workflows/R-CMD-check/badge.svg)](https://github.com/ipeaGIT/geobr/actions) | [![PyPI version](https://badge.fury.io/py/geobr.svg)](https://badge.fury.io/py/geobr) <br /> [![Downloads](https://static.pepy.tech/badge/geobr)](https://pepy.tech/project/geobr) <br /> [![Downloads](https://static.pepy.tech/badge/geobr/month)](https://pepy.tech/project/geobr) <br /> [![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing) <br /> [![Python build status](https://github.com/ipeaGIT/geobr/workflows/Python-CMD-check/badge.svg)](https://github.com/ipeaGIT/geobr/actions) |<img alt="GitHub stars" src="https://img.shields.io/github/stars/ipeaGIT/geobr.svg?color=orange"> <br /> <br /> [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) |



Expand Down
31 changes: 21 additions & 10 deletions data_prep/R/census_tract_2022.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dir.create(dest_dir, recursive = T)

if(year == 2022){

ftp <- 'https://ftp.ibge.gov.br/Censos/Censo_Demografico_2022/Agregados_por_Setores_Censitarios_preliminares/malha_com_atributos/setores/gpkg/BR/BR_Malha_Preliminar_2022.zip'
dest_file <- download_file(file_url = ftp, dest_dir = raw_dir)

}
Expand All @@ -28,21 +29,22 @@ if(year == 2022){
temp_dir <- tempdir()

unzip(dest_file, exdir = temp_dir)
local_file <- list.files(temp_dir, full.names = T, pattern = 'gpkg')

local_file <- unzip_fun(dest_file)


# read and save original raw data
df <- sf::st_read(local_file)
saveRDS(df, paste0(raw_dir,'/BR_Malha_Preliminar_2022.rds'))

df <- sf::st_read('./data_raw/census_tracts/2022/BR_Malha_Preliminar_2022.gpkg')
saveRDS(df, './data_raw/census_tracts/2022/BR_Malha_Preliminar_2022.rds')



#### 1. clean and save data -----------------

df$AREA_KM2 <- NULL
#### 1. clean and save data -----------------
df <- readRDS(paste0(raw_dir,'/BR_Malha_Preliminar_2022.rds'))

temp_sf <- dplyr::rename(df,
temp_sf <- dplyr::select(df,
code_tract = CD_SETOR,
code_muni = CD_MUN,
name_muni = NM_MUN,
Expand Down Expand Up @@ -73,11 +75,15 @@ temp_sf <- mutate(temp_sf, code_tract = gsub("P","", code_tract))
head(temp_sf)


# make all columns as character
char_cols <- names(temp_sf)
char_cols <- char_cols[char_cols %like% 'code_|name_']
# make all name columns as character
all_cols <- names(temp_sf)
char_cols <- all_cols[all_cols %like% 'name_']
temp_sf <- mutate(temp_sf, across(all_of(char_cols), as.character))

# make all columns as character
num_cols <- all_cols[all_cols %like% 'code_']
temp_sf <- mutate(temp_sf, across(all_of(char_cols), as.numeric))


# Use UTF-8 encoding
temp_sf <- use_encoding_utf8(temp_sf)
Expand All @@ -91,14 +97,19 @@ gc()

save_state <- function(code_uf){ # code_uf <- 33

message(code_uf)

temp_sf2 <- subset(temp_sf, code_state == code_uf)
# temp_sf2 <- subset(temp_sf2, code_muni == '3304557')
# temp_sf2 <- subset(temp_sf, code_muni == '3304557')

# convert to MULTIPOLYGON
temp_sf2 <- to_multipolygon(temp_sf2)

temp_sf2 <- fix_topoly(temp_sf2)

# simplify
temp_sf_simplified <- simplify_temp_sf(temp_sf2, tolerance = 10)
temp_sf_simplified <- fix_topoly(temp_sf_simplified)

# Save cleaned sf in the cleaned directory
sf::st_write(temp_sf2, paste0(dest_dir,'/', code_uf,'census_tract_', year, '.gpkg'))
Expand Down
50 changes: 50 additions & 0 deletions data_prep/R/cnefe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
library(arrow)
library(dplyr)
library(sf)
library(mapview)
library(sfheaders)

dir_coord <- 'R:/Dropbox/git/import_cnefe/data/2022/coordenadas/parquet/'
dir_addrs <- 'R:/Dropbox/git/import_cnefe/data/2022/arquivos/parquet/'



#coord <- arrow::read_parquet(file = paste0(dir_coord,'53_DF.parquet'))
arqui <- arrow::read_parquet(file = paste0(dir_addrs,'53_DF.parquet'))


c <- subset(arqui, cep == 70355030) # 71665015
c

c <- sfheaders::sf_point(obj = c, x='lon', y='lat', keep = T)

st_crs(c) <- 4674
plot(c)

mapview(c)

# build polygons with {concaveman}
temp <- concaveman::concaveman(c)
plot(temp)


n <- unique(arqui$desc_localidade)
n <- sort(n)

get_neig_poly <- function(name){ # name = 'ASA SUL'


c <- filter(arqui, desc_localidade == name)
c <- sfheaders::sf_point(obj = c, x='lon', y='lat', keep = F)
st_crs(c) <- 4674
# mapview(c)

temp <- concaveman::concaveman(c)
# mapview(temp)
# saveRDS(temp, file = paste0('./test_cnefe/', name, '.rds'))
sf::st_write(temp, dsn = paste0('./test_cnefe/', name, '.gpkg'),quiet = T)

}

pbapply::pblapply(X = n, FUN = get_neig_poly)

23 changes: 23 additions & 0 deletions data_prep/R/regions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
library(geobr)
library(collapse)
library(sf)
library(s2)
library(geos)

i = 2020

uf <- read_state(code_state = 'all',
year= i,
simplified = FALSE)

r <- uf |>
fmutate(geom = s2::as_s2_geography(geom)) |>
fgroup_by(code_region, name_region) |>
fsummarise(geom = s2::s2_union_agg(geom)) |>
fmutate(geom = st_as_sfc(geom))

r

sf::st_is_valid(r)

plot(r['name_region'])
56 changes: 56 additions & 0 deletions data_prep/R/topology.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,59 @@ df <- sf::st_make_valid(df)
df <- sf::st_buffer(df, dist = -1)

mapview(df)









library(sf)
library(geobr)
library(dplyr)

biomes <- read_biomes(
year = 2019,
simplified = FALSE,
showProgress = FALSE
)

fix_topology_brute <- function(poly_sf){ # poly_sf = biomes

# check which polys are valid
is_valid <- sf::st_is_valid(poly_sf)

# keep hold of valid polys
poly_valid <- poly_sf[is_valid, ]

# let's work on invalid ones
poly_invalid <- poly_sf[!is_valid, ]

# detect problematic edges
problem_edges <- st_is_valid(poly_invalid, reason=TRUE)
problem_edges <- sub(".*:\\D*(\\d+).*", "\\1", problem_edges) |> as.numeric()

# convert to point
poly_invalid2 <- sf::st_cast(poly_invalid, "POINT") |> suppressWarnings()

# drop problem edges
poly_invalid2 <- poly_invalid2[-c(problem_edges), ]

poly_invalid2 <- sf::st_combine(poly_invalid2)
poly_invalid2 <- sf::st_cast(poly_invalid2, "POLYGON")
poly_invalid2 <- sf::st_sf(poly_invalid2)

poly_invalid2 <- rename(poly_invalid2, geom = poly_invalid2)
poly_invalid2$code_biome <- 1
poly_invalid2$name_biome <- 'Amazônia'
poly_invalid2$year <- 2019
poly_invalid2 <- dplyr::select(poly_invalid2, names(poly_valid))

manchas_consertadas <- rbind(poly_valid, poly_invalid2)
manchas_consertadas <- sf::st_union(manchas_consertadas)
manchas_consertadas <- sf::st_make_valid(manchas_consertadas)

return(manchas_consertadas)
}

0 comments on commit 81c4fd6

Please sign in to comment.