Skip to content

Instantly share code, notes, and snippets.

@patperu
Created August 8, 2016 10:08
Show Gist options
  • Save patperu/e277005a3950e8d2593c991134559df0 to your computer and use it in GitHub Desktop.
Save patperu/e277005a3950e8d2593c991134559df0 to your computer and use it in GitHub Desktop.
Incidenti stradali rilevati - Roma 2015
---
title: "Incidenti stradali rilevati - Roma 2015"
author: "Patrick Hausmann"
date: "`r format(Sys.time(), '%a %b %d %X %Y')`"
output:
html_document:
fig_caption: yes
highlight: tango
keep_md: yes
number_sections: yes
theme: united
toc: yes
toc_depth: 3
github_document:
html_preview: no
toc: yes
toc_depth: 3
always_allow_html: yes
---
```{r fun}
library("xml2")
library("dplyr")
library("httr")
library("ggplot2")
library("viridis")
library("gplots")
library("leaflet")
options(stringsAsFactors = FALSE)
```
# Incidenti a Roma 2015
Source: [http://dati.comune.roma.it/cms/it/dettaglio_incidente_stradale.page?contentId=DTS3563](http://dati.comune.roma.it/cms/it/dettaglio_incidente_stradale.page?contentId=DTS3563)
```{r fetch_data, warning = FALSE}
tmp <- tempfile()
url <- "http://dati.comune.roma.it/cms/do/jacms/Content/incrementDownload.action?contentId=DTS3563&filename=2015.zip"
pg <- httr::GET(url, write_disk(tmp))
unzip(tmp)
pg_xml <- read_xml("2015/Incidenti2016318183652.xml")
# get all the <z:row>s
# sempre Bob Rudis su Stackoverflow
recs <- xml_find_all(pg_xml, "//z:row")
x <- xml_attrs(recs, "z:row")
x <- lapply(x, function(m) data.frame(t(m)))
x <- bind_rows(x)
x$Latitudine <- round(as.numeric(x$Latitudine), 5)
x$Longitudine <- round(as.numeric(x$Longitudine), 5)
write.csv2(x, file = "incidenti.csv", row.names = FALSE)
x <- readr::read_csv2("incidenti.csv")
x
colSums(select(x, NUM_FERITI, NUM_RISERVATA, NUM_MORTI, NUM_ILLESI))
```
# Heatmap
Number of indicents by hour and month
```{r heatmap, fig.width=9, fig.height=7}
color_palette <- viridis(9, alpha = 0.8, option = "C")
(v1 <- table(as.POSIXlt(x$DataOraIncidente)$hour, as.POSIXlt(x$DataOraIncidente)$mon + 1))
heatmap.2(v1, col = color_palette, trace = "none", scale = "none", srtCol = 0,
Rowv = FALSE, Colv = FALSE, dendrogram = "none",
cellnote = v1, notecex = 0.7, notecol = "black",
xlab = "Month", ylab = "Hour")
heatmap.2(v1, col = color_palette, trace = "none", scale = "none", srtCol = 0,
#Rowv = FALSE, Colv = FALSE, dendrogram = "none",
cellnote = v1, notecex = 0.7, notecol = "black",
xlab = "Month", ylab = "Hour")
```
```{r leaflet, fig.width=9, fig.height=7}
x2 <- filter(x, !is.na(x$Longitudine) | !is.na(x$Latitudine))
x2 <- x2 %>%
mutate(lat = round(Latitudine, 4),
lon = round(Longitudine, 4)) %>%
group_by(lat, lon) %>%
summarise(incidenti = n(),
pa = sum(NUM_FERITI + NUM_RISERVATA + NUM_MORTI + NUM_ILLESI)) %>%
ungroup()
x2 <- sp::SpatialPointsDataFrame(cbind(x2$lon, x2$lat), x2)
leaflet(x2) %>%
setView(lng = mean(x2$lon), lat = mean(x2$lat), zoom = 12) %>%
addTiles() %>%
addCircleMarkers(
radius = ~sqrt(pa),
color = "blue",
stroke = FALSE,
fillOpacity = 0.8)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment