library(leaflet.extras)
## Loading required package: leaflet
library(apcluster)
##
## Attaching package: 'apcluster'
## The following object is masked from 'package:stats':
##
## heatmap
library(magrittr)
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(leaflet)
library(rgdal)
## Loading required package: sp
## 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-27, (SVN revision 1148)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/fagne/Documents/R/win-library/4.1/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/fagne/Documents/R/win-library/4.1/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was C:/Users/fagne/Documents/R/win-library/4.1/rgdal/proj
library(rgeos)
## rgeos version: 0.5-8, (SVN revision 679)
## GEOS runtime version: 3.9.1-CAPI-1.14.2
## Please note that rgeos will be retired by the end of 2023,
## plan transition to sf functions using GEOS at your earliest convenience.
## GEOS using OverlayNG
## Linking to sp version: 1.4-5
## Polygon checking: TRUE
library(geojsonio)
## Registered S3 method overwritten by 'geojsonsf':
## method from
## print.geojson geojson
##
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
##
## pretty
library(mapview)
library(contoureR)
## Loading required package: geometry
library(geosphere)
##
## Attaching package: 'geosphere'
## The following object is masked from 'package:geojsonio':
##
## centroid
library(cluster)
library(sp)
library(rgdal)
library(RColorBrewer)
library(sp)
library(foreign)
dados = read.dbf("/Users/fagne/OneDrive/r-files/CIET/acidentes2020/_Base/acidentes_2014a2020_WGS84.dbf")
dados = dados[dados$ANO > 2014, ]
sort(unique(dados$ANO))
## [1] 2015 2016 2017 2018 2019 2020
anos = length(unique(dados$ANO))
anos
## [1] 6
class(dados)
## [1] "data.frame"
x2 <- cbind(dados$LONGITUDE, dados$LATITUDE)
x2 <- x2[complete.cases(x2), ]
dim(x2)
## [1] 75102 2
head(x2)
## [,1] [,2]
## [1,] -51.01213 -30.19741
## [2,] -51.03732 -30.21369
## [3,] -51.05466 -30.23513
## [4,] -51.05470 -30.23517
## [5,] -51.05470 -30.23517
## [6,] -51.05470 -30.23517
x1 <- x2
#x2 <- x2[sample(nrow(x2), round(nrow(dados)*0.30, 0)), ]
load("data/AZURE/x2-20000-99933.rda")
load("data/AZURE/apres2-20000-99933.rda")
names(x2) = c("LONGITUDE", "LATITUDE" )
head(x2)
dim(x1)
## [1] 75102 2
dim(x2)
## [1] 25009 2
#apres <- apcluster(negDistMat(r=2), x2, q=0.999)
plot(apres, x2)
A caption
summary(apres)
## Length Class Mode
## 1210 APResult S4
#save(apres, file = "data/apres2-20000-99933.rda")
centroides = unique(apres@exemplars)
poly = data.frame()
centr_indice = 0
for (i in centroides){
centr_indice = centr_indice + 1
centr_lat=x2[i,1]
centr_lon=x2[i,2]
poly = rbind(poly, c(centr_lat, centr_lon, centr_indice))
}
names(poly) = c("Lat", "Lon", "Cluster")
head(poly)
dim(poly)
## [1] 1210 3
exemplars = poly
#save(exemplars, file = "data/exemplars-20000-99933.rda")
predict.apcluster <- function(s, exemplars, newdata){
simMat <- s(rbind(exemplars, newdata), sel=(1:nrow(newdata)) + nrow(exemplars))[1:nrow(exemplars), ]
unname(apply(simMat, 2, which.max))
}
resultado <- list()
dados$cluster = 0
for(i in seq(from=1, to=length(dados$ID)-1000, by=1000)){
inicio = i
final = i+999
resultado = predict.apcluster(negDistMat(r=2), x2[apres@exemplars, ], dados[inicio:final, 2:3])
dados$cluster[inicio:final] = resultado
}
controle = length(dados$cluster) - final
resultado = predict.apcluster(negDistMat(r=2), x2[apres@exemplars, ], dados[(final + 1):length(dados$cluster), 2:3])
dados$cluster[(final + 1):length(dados$cluster)] = resultado
head(dados)
tail(dados)
#save(dados, file = "data/acidentes-20000-99933.rda")
pal <- colorFactor(
palette = 'Dark2',
domain = dados$cluster
)
leaflet(dados) %>%
addTiles(group="Mapa") %>%
addCircles(group="Acidentes", ~LONGITUDE, ~LATITUDE, weight = 0.1, radius=7, color=~pal(cluster),
stroke = TRUE, fillOpacity = 0.8, popup=~paste("Cluster Nº: ", cluster,
"<br>Ano: ", ANO, "<br>Tipo: ", TIPO_ACID, "<br>Local: ", LOG1, "<br>UPS: ", UPS, sep = " ")) %>%
addLegend(group="Legenda", "topright", colors= "", labels=paste("Classificados em meio a ", summary(apres)[1], "Clusters"), title="Acidentes em Porto Alegre") %>%
addLayersControl(overlayGroups = c("Mapa", "Acidentes", "Legenda"),
options = layersControlOptions(collapsed = FALSE)) %>%
addProviderTiles(providers$CartoDB.DarkMatter)