Atividade Processos Pontuais

Carregando pacotes necessários

require(maptools)
## Carregando pacotes exigidos: maptools
## Carregando pacotes exigidos: sp
## Checking rgeos availability: TRUE
## Please note that 'maptools' will be retired by the end of 2023,
## plan transition at your earliest convenience;
## some functionality will be moved to 'sp'.
require(sp)           
require(spdep)        
## Carregando pacotes exigidos: spdep
## Carregando pacotes exigidos: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
## Carregando pacotes exigidos: sf
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
require(classInt)     
## Carregando pacotes exigidos: classInt
require(RColorBrewer)
## Carregando pacotes exigidos: RColorBrewer
library(maps)
library(mapproj)
library(rgdal)
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.3, released 2022/04/22
## Path to GDAL shared files: C:/Users/Gabriel/AppData/Local/R/win-library/4.2/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/Gabriel/AppData/Local/R/win-library/4.2/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.5-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
require(spatstat)
## Carregando pacotes exigidos: spatstat
## Carregando pacotes exigidos: spatstat.data
## Carregando pacotes exigidos: spatstat.geom
## spatstat.geom 2.4-0
## Carregando pacotes exigidos: spatstat.random
## spatstat.random 2.2-0
## Carregando pacotes exigidos: spatstat.core
## Carregando pacotes exigidos: nlme
## Carregando pacotes exigidos: rpart
## spatstat.core 2.4-4
## Carregando pacotes exigidos: spatstat.linnet
## spatstat.linnet 2.3-2
## 
## spatstat 2.3-4       (nickname: 'Watch this space') 
## For an introduction to spatstat, type 'beginner'
require(readxl)
## Carregando pacotes exigidos: readxl

Carregando dados

library(readr)
dados <- read_excel("dadosdetran2018.xlsx")
## New names:
## • `` -> `...31`
#View(dados)

head(dados)
## # A tibble: 6 × 31
##       id data_inversa        dia_semana   horario             uf    br    km   
##    <dbl> <dttm>              <chr>        <dttm>              <chr> <chr> <chr>
## 1 133472 2018-05-11 00:00:00 sexta-feira  1899-12-31 12:40:00 AC    317   20   
## 2 101809 2018-01-07 00:00:00 domingo      1899-12-31 17:30:00 AC    317   22   
## 3 173487 2018-11-23 00:00:00 sexta-feira  1899-12-31 18:30:00 AC    317   45.8 
## 4 158232 2018-09-08 00:00:00 sábado       1899-12-31 23:40:00 AC    317   67   
## 5 102948 2018-01-12 00:00:00 sexta-feira  1899-12-31 12:40:00 AC    317   81   
## 6 134777 2018-05-16 00:00:00 quarta-feira 1899-12-31 19:15:00 AC    317   88   
## # … with 24 more variables: municipio <chr>, causa_acidente <chr>,
## #   tipo_acidente <chr>, classificacao_acidente <chr>, fase_dia <chr>,
## #   sentido_via <chr>, condicao_metereologica <chr>, tipo_pista <chr>,
## #   tracado_via <chr>, uso_solo <chr>, pessoas <dbl>, mortos <dbl>,
## #   feridos_leves <dbl>, feridos_graves <dbl>, ilesos <dbl>, ignorados <dbl>,
## #   feridos <dbl>, veiculos <dbl>, latitude <chr>, longitude <chr>,
## #   regional <chr>, delegacia <chr>, uop <chr>, ...31 <dbl>
attach(dados)

names(dados)
##  [1] "id"                     "data_inversa"           "dia_semana"            
##  [4] "horario"                "uf"                     "br"                    
##  [7] "km"                     "municipio"              "causa_acidente"        
## [10] "tipo_acidente"          "classificacao_acidente" "fase_dia"              
## [13] "sentido_via"            "condicao_metereologica" "tipo_pista"            
## [16] "tracado_via"            "uso_solo"               "pessoas"               
## [19] "mortos"                 "feridos_leves"          "feridos_graves"        
## [22] "ilesos"                 "ignorados"              "feridos"               
## [25] "veiculos"               "latitude"               "longitude"             
## [28] "regional"               "delegacia"              "uop"                   
## [31] "...31"
head(dados$latitude)
## [1] "-9.755836"   "-9.77922649" "-9.917211"   "-10.051794"  "-10.10742"  
## [6] "-10.134775"

Construindo um novo conjunto de dados

newdata = dados[dados$uf=="BA", c(26,27,19)]
head(newdata)
## # A tibble: 6 × 3
##   latitude   longitude  mortos
##   <chr>      <chr>       <dbl>
## 1 -13.7734   -46.1607        0
## 2 -13.694868 -46.142708      1
## 3 -13.688    -46.141         0
## 4 -13.5907   -46.118         0
## 5 -13.612104 -46.125139      0
## 6 -13.587615 -46.117381      0
names(newdata)
## [1] "latitude"  "longitude" "mortos"
attach(newdata)
## The following objects are masked from dados:
## 
##     latitude, longitude, mortos

Create matrix of coordinates

xy_vetor <- newdata[,c("longitude","latitude")]
xy_vetor$longitude <- as.numeric(xy_vetor$longitude)
xy_vetor$latitude <- as.numeric(xy_vetor$latitude)
head(xy_vetor)
## # A tibble: 6 × 2
##   longitude latitude
##       <dbl>    <dbl>
## 1     -46.2    -13.8
## 2     -46.1    -13.7
## 3     -46.1    -13.7
## 4     -46.1    -13.6
## 5     -46.1    -13.6
## 6     -46.1    -13.6

Primeira plotagem

plot(xy_vetor, pch=16, cex=.5,axes=TRUE) 

proj <- CRS("+proj=longlat +datum=WGS84 +no_defs")
sp.points <- SpatialPointsDataFrame(coords=xy_vetor,data=newdata, 
                                    proj4string=proj)
class(sp.points)
## [1] "SpatialPointsDataFrame"
## attr(,"package")
## [1] "sp"
names(sp.points)
## [1] "latitude"  "longitude" "mortos"
## Bounding box of data points
bbox(sp.points)
##                 min        max
## longitude -46.31493 -37.780325
## latitude  -18.12160  -8.652825
plot(sp.points, pch=16, cex=.5, axes=T,cex.axis=.8)

Obtendo a visualização completa

shape_ba <-readShapeSpatial("BA_UF_2021.shp")
## Warning: readShapeSpatial is deprecated; use rgdal::readOGR or sf::st_read
## Warning: readShapePoly is deprecated; use rgdal::readOGR or sf::st_read
plot(sp.points, pch=16, cex=.5, axes=T,cex.axis=.8)
plot(shape_ba,add=TRUE)
class(shape_ba)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
names(shape_ba)
## [1] "CD_UF"     "NM_UF"     "SIGLA"     "NM_REGIAO"
title("Mortes por acidente nas BR do Estado da Bahia")