Skip to content

Instantly share code, notes, and snippets.

@kguidonimartins
Created March 29, 2023 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kguidonimartins/74a272b6fd42be6d5429d43585578cb5 to your computer and use it in GitHub Desktop.
Save kguidonimartins/74a272b6fd42be6d5429d43585578cb5 to your computer and use it in GitHub Desktop.
Obtenção das coordenadas geográficas da sede das capitais do Brasil via `{geobr}`
## -*- mode: ess-r; coding: utf-8-*-
## scratch-mode: ess-r-mode
## created-at: 20230329-111607
### Obtenção das coordenadas geográficas da sede das capitais do Brasil via `{geobr}`
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("sf")) install.packages("sf")
if (!require("geobr")) install.packages("geobr")
options(
tibble.print_min = 30,
tibble.print_max = 30
)
capitais_uf <-
tibble::tribble(
~codigo_ibge, ~capital, ~uf,
2800308L , "Aracaju", "SE",
3106200L , "Belo Horizonte", "MG",
1501402L , "Belém", "PA",
1400100L , "Boa Vista", "RR",
5300108L , "Brasília", "DF",
5002704L , "Campo Grande", "MS",
5103403L , "Cuiabá", "MT",
4106902L , "Curitiba", "PR",
4205407L , "Florianópolis", "SC",
2304400L , "Fortaleza", "CE",
5208707L , "Goiânia", "GO",
2507507L , "João Pessoa", "PB",
1600303L , "Macapá", "AP",
2704302L , "Maceió", "AL",
1302603L , "Manaus", "AM",
2408102L , "Natal", "RN",
1721000L , "Palmas", "TO",
4314902L , "Porto Alegre", "RS",
1100205L , "Porto Velho", "RO",
2611606L , "Recife", "PE",
1200401L , "Rio Branco", "AC",
3304557L , "Rio De Janeiro", "RJ",
2927408L , "Salvador", "BA",
2111300L , "São Luís", "MA",
3550308L , "São Paulo", "SP",
2211001L , "Teresina", "PI",
3205309L , "Vitória", "ES"
)
sedes_capitais_sf <-
geobr::read_municipal_seat(year = 2010) %>%
dplyr::filter(code_muni %in% capitais_uf$codigo_ibge)
sedes_capitais_df <-
sedes_capitais_sf %>%
dplyr::mutate(
longitude = sf::st_coordinates(.)[, "X"],
latitude = sf::st_coordinates(.)[, "Y"],
.before = geom
) %>%
sf::st_drop_geometry() %>%
dplyr::arrange(name_muni) %>%
dplyr::as_tibble()
sedes_capitais_sf %>%
sf::st_write("./sedes-capitais-geom.geojson")
sedes_capitais_df %>%
readr::write_csv("./sedes-capitais-coords.csv")
code_muni name_muni code_state abbrev_state code_region name_region year longitude latitude
2800308 Aracaju 28 SE 2 Nordeste 2010 -37.048212639512236 -10.907215797883499
3106200 Belo Horizonte 31 MG 3 Sudeste 2010 -43.92645317353045 -19.93752429377505
1501402 Belém 15 PA 1 Norte 2010 -48.48782568748754 -1.459845
1400100 Boa Vista 14 RR 1 Norte 2010 -60.67053267296504 2.81668191910439
5300108 Brasília 53 DF 5 Centro Oeste 2010 -47.88790547803133 -15.794087361890998
5002704 Campo Grande 50 MS 5 Centro Oeste 2010 -54.61574356645883 -20.4580299878639
5103403 Cuiabá 51 MT 5 Centro Oeste 2010 -56.07325203819113 -15.569989013120598
4106902 Curitiba 41 PR 4 Sul 2010 -49.271847885077406 -25.432955999999997
4205407 Florianópolis 42 SC 4 Sul 2010 -48.54763737819332 -27.58779554854995
2304400 Fortaleza 23 CE 2 Nordeste 2010 -38.589927555043175 -3.723805035
5208707 Goiânia 52 GO 5 Centro Oeste 2010 -49.25581427581575 -16.673309773838945
2507507 João Pessoa 25 PB 2 Nordeste 2010 -34.87338481353855 -7.14938202
1600303 Macapá 16 AP 1 Norte 2010 -51.05740545703594 0.03895101
2704302 Maceió 27 AL 2 Nordeste 2010 -35.701629913489484 -9.66082215163114
1302603 Manaus 13 AM 1 Norte 2010 -60.02333518106102 -3.134691491201945
2408102 Natal 24 RN 2 Nordeste 2010 -35.2522547280543 -5.750898537612059
1721000 Palmas 17 TO 1 Norte 2010 -48.35104370824618 -10.163253326792749
4314902 Porto Alegre 43 RS 4 Sul 2010 -51.228660463702326 -30.030036774766394
1100205 Porto Velho 11 RO 1 Norte 2010 -63.83144565436112 -8.768891790000001
2611606 Recife 26 PE 2 Nordeste 2010 -34.88894194457771 -8.062762483052406
1200401 Rio Branco 12 AC 1 Norte 2010 -67.81052870042349 -9.97829875272498
3304557 Rio De Janeiro 33 RJ 3 Sudeste 2010 -43.22787512499518 -22.87665211818645
2927408 Salvador 29 BA 2 Nordeste 2010 -38.488061484007844 -13.01477191153275
2111300 São Luís 21 MA 2 Nordeste 2010 -44.29791856424437 -2.5318859850833895
3550308 São Paulo 35 SP 3 Sudeste 2010 -46.57038318211274 -23.567386499999998
2211001 Teresina 22 PI 2 Nordeste 2010 -42.80527045822334 -5.0863419523217
3205309 Vitória 32 ES 3 Sudeste 2010 -40.322208726726615 -20.3201537928533
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment