library(ggplot2)
library(ggthemes)
library(viridis) # devtools::install_github("sjmgarnier/viridis)
## Loading required package: viridisLite
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:viridis':
## 
##     viridis_pal
library(grid)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(leaflet.extras)
## Loading required package: leaflet
# rodar mapa_acidentes.R
setwd('~/OneDrive/r-files/sem-foros/')
list.files()
##  [1] "endpoints.csv"       "endpoints.html"      "endpoints.Rmd"      
##  [4] "index.html"          "LICENSE"             "README.md"          
##  [7] "rsconnect"           "run.R"               "semaforos_jul18.csv"
## [10] "semaforos_jul18.xls" "semaforos.R"         "teste.R"
semaforos = read.csv('semaforos_jul18.csv', header = TRUE, sep = ";")
head(semaforos, 10)
class(semaforos$Longitude)
## [1] "numeric"
endpoints = read.csv('~/OneDrive/r-files/sem-foros/endpoints.csv', header = TRUE, sep = ";")
head(endpoints, 10)
sub( '(?<=.{3})', '.', endpoints$LONGITUDE[1], perl=TRUE )
## [1] "-51.100178"
endpoints$LONGITUDE = sub( '(?<=.{3})', '.', endpoints$LONGITUDE, perl=TRUE )
endpoints$LATITUDE = sub( '(?<=.{3})', '.', endpoints$LATITUDE, perl=TRUE )
head(endpoints)
class(endpoints$LONGITUDE)
## [1] "character"
endpoints$LONGITUDE = as.numeric(as.character(endpoints$LONGITUDE))
endpoints$LATITUDE = as.numeric(as.character(endpoints$LATITUDE))
library(wesanderson)

pal <- colorFactor(
  #palette = 'Darjeeling', #topo.colors(5),
  palette =  c("black", "yellow","Blue", "green","red"),
  domain = semaforos$TIPO
)
head(dados)

Criando heatmap de acidentes e plotando pontos semafóricos:

#utilizando o dataset dados de mapa_acidentes.R
leaflet(semaforos) %>%
  addTiles(group="OSM") %>%
  addCircles(~Longitude, ~Latitude, popup=~paste("ID: ", ID,  
                                                 "<br>Local: ", LOG1, "N?: ", PREDIAL,  
                                                 "<br>Tipo: ", TIPO, 
                                                 "<br>ObsS: ", OBSERVACAO, 
                                                 sep = ""),  
             weight = 1, radius=20, color= 'red', stroke = TRUE, fillOpacity = 0.8) %>% 
  addHeatmap(group=dados$LOG1, lng=dados$LONGITUDE, lat=dados$LATITUDE, radius = 15 , max=10, blur = 20, intensity  =1) %>%
  addLegend("bottomright", colors= "red", labels="JULHO 2018", title="Semáforos:")

Criando heatmap de acidentes e plotando pontos de endpoits:

#utilizando o dataset dados de mapa_acidentes.R
leaflet(endpoints) %>%
  addTiles(group="OSM") %>%
  addCircles(~LONGITUDE, ~LATITUDE, weight = 1, radius=15, color= '#085BD3', stroke = TRUE, fillOpacity = 0.8) %>% 
  addHeatmap(group=dados$LOG1, lng=dados$LONGITUDE, lat=dados$LATITUDE, radius = 15 , max=10, blur = 20, intensity  =1) %>%
  addLegend("bottomright", colors= "#085BD3", labels="JULHO 2018", title="EndPoints:")
#https://www.rdocumentation.org/packages/leaflet.extras/versions/0.2/topics/addHeatmap
pal <- colorFactor(
  #palette = 'Darjeeling', #topo.colors(5),
  palette =  c("black", "yellow","Blue", "green","red"),
  domain = semaforos$TIPO
)



leaflet(semaforos) %>%
  addTiles(group="OSM") %>%
  addHeatmap(group=dados$LOG1, lng=dados$LONGITUDE, lat=dados$LATITUDE, radius = 10 , max=30, blur = 25, intensity  =1) %>%
  addCircles(~Longitude, ~Latitude, popup=~paste("ID: ", ID,  
                                                 "<br>Local: ", LOG1, "N?: ", PREDIAL,  
                                                 "<br>Tipo: ", TIPO, 
                                                 "<br>ObsS: ", OBSERVACAO, 
                                                 sep = ""),  
             weight = 1, radius=20, color= ~pal(TIPO), stroke = TRUE, fillOpacity = 0.8) %>% 
  addLegend("bottomright", colors= "#ffa500", labels="JULHO 2018", title="SEMÁFOROS:")
pal2 <- colorFactor(
  palette = c('red', 'blue', 'green', 'orange'),
  domain = semaforos$TIPO
)

leaflet(semaforos) %>%
  addTiles(group="OSM") %>%
  addCircles(~Longitude, ~Latitude, popup=~paste("ID: ", ID,  "<br>Local: ", CDL1,  "<br>Tipo: ", TIPO,   sep = " "),  
             weight = 1, radius=20, color= ~pal2(TIPO), stroke = TRUE, fillOpacity = 0.8) %>% 
  addLegend("bottomright", colors= "#ffa500", labels="JULHO 2018", title="SEMÁFOROS:")
leaflet(semaforos) %>%
  addProviderTiles(providers$OpenStreetMap) %>%
  addWebGLHeatmap(lng = ~Longitude, lat = ~Latitude, size = 60) %>%
  addCircles(lng = ~Longitude, lat = ~Latitude, weight = 1, fillOpacity = 0.5,
           radius = 20, color= ~pal(TIPO), group = "cities")

Acidentes em Porto Alegre de 2013:2017

dim(dados)
## [1] 81172    41
#https://www.rdocumentation.org/packages/leaflet.extras/versions/0.2/topics/addHeatmap
pal <- colorFactor(
  #palette = 'Darjeeling', #topo.colors(5),
  palette =  c("black", "yellow","Blue", "green","red"),
  domain = dados$TIPO_ACID
)
leaflet(dados) %>%
  addTiles(group="OSM") %>%
  addHeatmap(group=dados$LOG1, lng=dados$LONGITUDE, lat=dados$LATITUDE, radius = 10 , max=30, blur = 25, intensity  =1) %>%
  addCircles(~LONGITUDE, ~LATITUDE, popup=~paste("ID: ", ID,  
                                                 "<br>Local: ", LOCAL_VIA,
                                                 "<br>Tipo: ", TIPO_ACID, 
                                                 "<br>Região: ", REGIAO, 
                                                 sep = ""),  
             weight = 1, radius=20, color= ~pal(TIPO_ACID), stroke = TRUE, fillOpacity = 0.8) %>% 
  addLegend("bottomright", colors= "", labels="2013-2017", title="Acidentes:")
atropelamentos = dados[dados$TIPO_ACID == "ATROPELAMENTO",]
head(atropelamentos)
dim(atropelamentos)
## [1] 5063   41
pal <- colorFactor(
  #palette = 'Darjeeling', #topo.colors(5),
  palette =  c("red", "Blue", "green"),
  domain = atropelamentos$FATAIS
)
leaflet(dados) %>%
  addTiles(group="OSM") %>%
  addHeatmap(group=dados$LOG1, lng=dados$LONGITUDE, lat=dados$LATITUDE, radius = 10 , max=30, blur = 25, intensity  =1) %>%
  addCircles(~atropelamentos$LONGITUDE, ~atropelamentos$LATITUDE, popup=~paste("ID: ", atropelamentos$ID,  
                                                 "<br>Local: ", atropelamentos$LOCAL_VIA,
                                                 "<br>Tipo: ", atropelamentos$TIPO_ACID, 
                                                 "<br>Região: ", atropelamentos$REGIAO, 
                                                 sep = ""),  
             weight = 1, radius=20, color= ~pal(FATAIS), stroke = TRUE, fillOpacity = 0.8) %>% 
  addLegend("bottomright", colors= "", labels="2013-2017", title="Atropelamentos:")
## Warning in pal(FATAIS): Some values were outside the color scale and will
## be treated as NA

## Warning in pal(FATAIS): Some values were outside the color scale and will
## be treated as NA
atropelamentos = atropelamentos[atropelamentos$FATAIS > 0,]
head(atropelamentos)
dim(atropelamentos)
## [1] 221  41

Atropelamentos fatais em Porto Alegre de 2013 a 2017

leaflet(dados) %>%
  addTiles(group="OSM") %>%
  addHeatmap(group=dados$LOG1, lng=dados$LONGITUDE, lat=dados$LATITUDE, radius = 10 , max=30, blur = 25, intensity  =1) %>%
  addCircles(~atropelamentos$LONGITUDE, ~atropelamentos$LATITUDE, popup=~paste("ID: ", atropelamentos$ID,  
                                                 "<br>Local: ", atropelamentos$LOCAL_VIA,
                                                 "<br>Tipo: ", atropelamentos$TIPO_ACID, 
                                                 "<br>Região: ", atropelamentos$REGIAO, 
                                                 "<br>Vitimas fatais: ", atropelamentos$FATAIS, 
                                                 sep = ""),  
             weight = 1, radius=20, color= "red", stroke = TRUE, fillOpacity = 0.8) %>% 
  addLegend("bottomright", colors= "red", labels="2013-2017", title="Atropelamentos:")

Acidentes Fatais:

fatais = dados[dados$FATAIS > 0,]
head(fatais)
dim(fatais)
## [1] 513  41
pal <- colorFactor(
  palette =  c("red", "Blue", "green", "black", "orange", "pink", "brown", "magenta", "yellow", "#00ff00", "#660033"),
  domain = fatais$TIPO_ACID
)
leaflet(fatais) %>%
  addTiles(group = "Mapa") %>%
  addHeatmap(group="Calor", lng=dados$LONGITUDE, lat=dados$LATITUDE, radius = 10 , max=30, blur = 25, intensity  =1) %>%
  addCircles(~fatais$LONGITUDE, ~fatais$LATITUDE, popup=~paste("ID: ", fatais$ID,  
                                                                               "<br>Local: ", fatais$LOCAL_VIA,
                                                                               "<br>Tipo: ", fatais$TIPO_ACID, 
                                                                               "<br>Região: ", fatais$REGIAO, 
                                                                               "<br>Vitimas fatais: ", fatais$FATAIS, 
                                                                               sep = ""),  
             weight = 1, radius=10, color= ~pal(TIPO_ACID), stroke = TRUE, fillOpacity = 0.8, group = "Fatais") %>% 
  addLegend("bottomright", colors= "", labels="Fatais", title="Acidentes:") %>% 
  addLayersControl(
  overlayGroups = c("Fatais", "Calor", "Mapa"),
  options = layersControlOptions(collapsed = TRUE)
)

end….