Esta é uma aplicação Markdown R Markdown Notebook. Quando uma aplicação é executada com esta tecnologia o código da aplicação e suas saidas são exibidas em um arquivo estátco.

Iniciamos a aplicação setando o diretório de trabalho e carregando a biblioteca ggmap para rodar o código. Esta biblioteca requer uma chave de API do Google MAps setada em da seguite forma: register_google(“SUA_API_KEY”).

setwd("~/OneDrive/r-files/TCC/")
#if(!requireNamespace("devtools")) install.packages("devtools")
#devtools::install_github("dkahle/ggmap", ref = "tidyup")
library(ggmap)
## Loading required package: ggplot2
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
#register_google("SUA_API_KEY")

Carga de dados

origAddress1 <- read.csv("servico/servico.csv", header = TRUE, sep = ";")
origAddress2 <- read.csv("comercio/comercio.csv", header = TRUE, sep = ";")
names(origAddress1)
## [1] "Empresa"   "Endereco"  "CEP"       "Bairro"    "Atividade"
dim(origAddress1)
## [1] 108565      5
names(origAddress2)
## [1] "Empresa"   "Endereco"  "CEP"       "Bairro"    "Atividade"
dim(origAddress2)
## [1] 69954     5
origAddress = rbind(origAddress1, origAddress2)
## Warning in `[<-.factor`(`*tmp*`, ri, value = c(90150052L, 90150052L,
## 91330000L, : invalid factor level, NA generated
summary(origAddress)
##                                     Empresa      
##  TECNOLOGIA BANCARIA S.A.               :    83  
##  PAQUETA CALCADOS LTDA                  :    61  
##  WMS SUPERMERCADOS DO BRASIL LTDA       :    60  
##  DIMED S/A DISTRIBUIDORA DE MEDICAMENTOS:    56  
##  ITA\x81EUNIBANCO S/A                   :    44  
##  BANCO SANTANDER (BRASIL) S/A           :    43  
##  (Other)                                :178172  
##                              Endereco            CEP        
##  LE PERCORRENDO BAIRROS - SMIC, 1:   371   91900000:  2150  
##  AV LAGEADO, 991                 :   220   91340001:  1846  
##  R ITABORAI, 1090                :   210   90560003:  1542  
##  AV SOLEDADE, 569/912            :   189   90610000:  1324  
##  AV JOAO WALLIG, 1800/1102       :   185   91910001:  1288  
##  AV JOAO WALLIG, 1800/1263       :   159   (Other) :169162  
##  (Other)                         :177185   NA's    :  1207  
##              Bairro      
##  CENTRO HISTORIC: 25076  
##  FLORESTA       :  8708  
##  PETROPOLIS     :  7802  
##  RIO BRANCO     :  7291  
##  PASSO DA AREIA :  6971  
##  MOINHOS VENTO  :  6841  
##  (Other)        :115830  
##                                                       Atividade     
##  PROFISSIONAL LIBERAL,TECNICO E UNIVERSITARIO              : 13373  
##  PROFISSIONAL AUTONOMO                                     : 11753  
##  SALAO DE BELEZA                                           :  8585  
##  CONSULTORIO MEDICO                                        :  6377  
##  ARTIGOS DO VESTUARIO                                      :  4501  
##  ESCRITORIO ADMINISTRATIVO (SEDE ADMINISTRATIVA DE EMPRESA):  4254  
##  (Other)                                                   :129676

Verificamos o tamanho do data set

dim(origAddress)
## [1] 178519      5

View(origAddress)

head(origAddress)

Removemos os duplicados

#origAddress <- origAddress[!duplicated(c(origAddress$Empresa, origAddress$Endereco)), ]

Verificamos o tamanho do data set

dim(origAddress)
## [1] 178519      5

criamos a variavel de pesquisa

origAddress$addresses <- origAddress$Endereco

agregamosao endereço o bairro e cidade

origAddress$addresses <- paste(origAddress$Endereco,  origAddress$Bairro, "Porto Alegre", sep = " ")

Eliminamos a virgula

origAddress$addresses <- gsub(",", "", origAddress$addresses)

fazemos o tipecast

#origAddress$addresses <- as.character(origAddress$geoAddress)

verificamos o resultado final

definimoso range da pesquisa

origAddress<- origAddress[1:10,]

fazemos a restricao

addresses <- origAddress$addresses

verificamos o tamanho

length(addresses)
## [1] 10

Criamos a funcao que geocodifica e incremente os datos ao data frame

getGeoDetails <- function(address){
  geo_reply = geocode(address, output='all', messaging=TRUE, override_limit=TRUE)
  answer <- data.frame(lat=NA, long=NA, accuracy=NA, formatted_address=NA, address_type=NA, status=NA)
  answer$status <- geo_reply$status
  
  while(geo_reply$status == "OVER_QUERY_LIMIT"){
    print("OVER QUERY LIMIT - Pausando as:") 
    time <- Sys.time()
    print(as.character(time))
    Sys.sleep(1)
    geo_reply = geocode(address, output='all', messaging=TRUE, override_limit=TRUE)
    answer$status <- geo_reply$status
  }
  
  if (geo_reply$status != "OK"){
    return(answer)
  }
  answer$lat <- geo_reply$results[[1]]$geometry$location$lat
  answer$long <- geo_reply$results[[1]]$geometry$location$lng   
  if (length(geo_reply$results[[1]]$types) > 0){
    answer$accuracy <- geo_reply$results[[1]]$types[[1]]
  }
  answer$address_type <- paste(geo_reply$results[[1]]$types, collapse=',')
  answer$formatted_address <- geo_reply$results[[1]]$formatted_address
  
  return(answer)
}
geocoded <- data.frame()
class(geocoded)
## [1] "data.frame"

Criamos o loop para geocodificar line by line

startindex <- 1

for (ii in seq(startindex, length(addresses))){
  print(paste("Trabalhando no item ", ii, " de", length(addresses)))
  
  result = getGeoDetails(addresses[ii]) 
  print(result$status)
  result$index <- ii
  result$tipo <-  origAddress$Atividade[ii]
  result$default <-  origAddress$Endereco[ii]
  
  geocoded <- rbind(geocoded, result)
  
}
## [1] "Trabalhando no item  1  de 10"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=R+PORTUGAL+417+HIGIENOPOLIS+Porto+Alegre&key=xxx-GW1AUDc
## [1] "OK"
## [1] "Trabalhando no item  2  de 10"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=R+JOSE+DO+PATROCINIO+263+CIDADE+BAIXA+Porto+Alegre&key=xxx-GW1AUDc
## [1] "OK"
## [1] "Trabalhando no item  3  de 10"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=R+DR+MARIO+TOTTA+625/306+TRISTEZA+Porto+Alegre&key=xxx-GW1AUDc
## [1] "OK"
## [1] "Trabalhando no item  4  de 10"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=R+GEN+ANDRADE+NEVES+90/61+CENTRO+HISTORIC+Porto+Alegre&key=xxx-GW1AUDc
## [1] "OK"
## [1] "Trabalhando no item  5  de 10"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=TRAV+SAUDE+22/201+AUXILIADORA+Porto+Alegre&key=xxx-GW1AUDc
## [1] "OK"
## [1] "Trabalhando no item  6  de 10"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=TRAV+SAUDE+22/201+AUXILIADORA+Porto+Alegre&key=xxx-GW1AUDc
## [1] "OK"
## [1] "Trabalhando no item  7  de 10"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=AV+BALTAZAR+DE+OLIVEIRA+GARCIA+2215/2+ZONA+INDEFIN.+3+Porto+Alegre&key=xxx-GW1AUDc
## [1] "OK"
## [1] "Trabalhando no item  8  de 10"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=R+SAO+LUIZ+300+SANTANA+Porto+Alegre&key=xxx-GW1AUDc
## [1] "OK"
## [1] "Trabalhando no item  9  de 10"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=R+SAO+LUIZ+300+SANTANA+Porto+Alegre&key=xxx-GW1AUDc
## [1] "OK"
## [1] "Trabalhando no item  10  de 10"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=AV+PRESIDENTE+FRANKLIN+ROOSEVELT+647+SAO+GERALDO+Porto+Alegre&key=xxx-GW1AUDc
## [1] "OK"

Tratamos a string de coordenadas

geocoded$address_type<-NULL
geocoded$status<-NULL
geocoded$lat <- gsub(" ", ".", geocoded$lat)
geocoded$long<- gsub(" ", ".", geocoded$long)

Salvamos os dados

write.csv(geocoded, "saida2.csv", row.names=FALSE)
geocoded$address_type<-NULL
geocoded$status<-NULL
geocoded$lat <- gsub(" ", ".", geocoded$lat)
geocoded$long<- gsub(" ", ".", geocoded$long)

data <- paste("geo_", as.character(as.numeric(Sys.time())), ".csv", sep = "")
write.table(geocoded,file=data,sep=";",dec = " ", row.names=FALSE)
data <- paste("./", data, sep = "")
write.table(geocoded,file=data,sep=";",dec = " ", row.names=FALSE)
head(geocoded)

Ir para Fase 2.

Fim