Skip to content

Instantly share code, notes, and snippets.

@DATAUNIRIO
Created February 22, 2024 18:53
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 DATAUNIRIO/d10b81b0f4f7dedcdf64d2ea756e4d47 to your computer and use it in GitHub Desktop.
Save DATAUNIRIO/d10b81b0f4f7dedcdf64d2ea756e4d47 to your computer and use it in GitHub Desktop.
geocoding_com_o_google.R
rm(list=ls(all=TRUE)) # Limpar memória do R
# ---------------------------------------#
# Georreferenciamente e Mapas #
# dos Locais dos Núcleos - Qualifica RJ #
# Por Vinicius Israel #
# Em 22/04/2023 #
# ---------------------------------------#
library(ggplot2) # Pacote gráfico
library(googleway) # Pacote para o Google Maps
library(ggmap) # Ferramentas para fazer mapas
library(sf) # Pacote para georreferenciamento
library(maptools) # Contorno do mapa
library(dplyr) # Pacote de manipulaçãoo de bd
library(mapview) # Visualização dinâmica de mapas
library(readxl)
# ---------------------------
# Georeferenciando os núcleos
# Chave do Google
#Perguntar para o Vinicius
# Núcleos ---
bd <- read_excel("24 Polos de Ellos Qualifica RJ - Cursos e Endereços.xlsx")
View(bd)
locais <- bd$ENDEREÇO
latlon=array(NA, c(length(locais), 2))
for(i in 1:length(locais)){
print(paste('Polo:', bd$LOCAL[i], ', ID =', i))
an.error.occured <- FALSE
tryCatch( { res = google_geocode(locais[i]);
res = access_result(res, "coordinates"); print(res) },
error = function(e) {an.error.occured <<- TRUE})
print(an.error.occured)
if(!(an.error.occured)){
latlon[i,] = c(mean(res$lat), mean(res$lng))
}
}
bd$lat= jitter(latlon[,1])
bd$lon= jitter(latlon[,2])
save(bd, file="Polos.RData")
# Beneficiários ---
bd <- read_excel("ELLOS_QUALIFICAÇÃO___INÍCIO_DE_TURMA_(respostas)_Final(1)COM DATAS.xlsx")
View(bd)
# Chave do Google
set_key("AIzaSyBZ8KuS23N1LqTNBFbAnLgV0sSTvoK61to")
locais <- paste0(bd$`Endereço:`,', ', bd$`Bairro:`,', ', bd$`Município:`, ", Rio de Janeiro")
latlon=array(NA, c(length(locais), 2))
for(i in 1:length(locais)){
print(i)
an.error.occured <- FALSE
tryCatch( { res = google_geocode(locais[i]);
res = access_result(res, "coordinates"); print(res) },
error = function(e) {an.error.occured <<- TRUE})
print(an.error.occured)
if(!(an.error.occured)){
latlon[i,] = c(mean(res$lat), mean(res$lng))
}
}
bd$lat= jitter(latlon[,1])
bd$lon= jitter(latlon[,2])
names(bd) = c('Carimbo','E-mail', 'Nome', 'CPF','Sexo',
'Nascimento', 'Data', 'Idade', 'Escolaridade', 'Expectativa', 'CorRaça', 'Ocupação',
'Participou de projetos', 'Como soube', 'Motivo', 'Renda familiar', 'RG',
'Órgão', 'Tel1', 'Tel2', 'e-mail','Endereço',
'Bairro', 'CEP', 'Município', 'Polo', 'Curso',
'Estuda', 'Curso superior', 'Estado civil','lat','lon')
save(bd, file="Beneficiarios.RData")
# -------------------------
# Fazendo os mapas
# Fazendo os mapas - Núcleo ----
load("Polos.RData")
# Todos os núcleos
Núcleos <- st_as_sf(bd, coords = c("lon", "lat"), crs = 4326)
mp = mapview(Núcleos, color='red', col.regions='red')
mp
# Cursos iniciais
table(bd$CURSO)
ggplot(bd, aes(x = CURSO))+
geom_bar(stat='count', fill='lightblue')+coord_flip()+
ylab('Quantidade')+xlab('Cursos')+
theme_minimal()+ggtitle("Quantidade de cursos oferecidos")
# Fazendo os mapas - Beneficários ---
load("Beneficiarios.RData")
# Limites do Estado do Rio de Janeiro
bdb = bd[!is.na(bd$lat) & bd$lat> -23.198314 & bd$lat< -20.866388, ]
bdb = bd[!is.na(bd$lon) & bd$lon> -44.975911 & bd$lon< -41.604066, ]
bdb = subset(bdb, select=c("Curso","lat","lon"))
ncls <- st_as_sf(bdb, coords = c("lon", "lat"), crs = 4326)
mb = mapview(ncls, zcol=NULL, legend=F)
mb
mp + mb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment