Another addition to the OpenGovernmentVienna repo by Christian Brandstaetter showing the density of bike parking lots in Vienna.
The script executes the following steps:
- Download Vienna map including district boundaries and bike parking lots from http://data.wien.gv.at.
- Plot two Vienna maps showing bike parking lots and density per district.
library(rgdal)
library(rgeos)
library(XML)
library(RCurl)
library(ggplot2)
library(plotrix)
library(maptools)
<- function(shapename, outdir = "data") {
download.vienna.shape <- "http://data.wien.gv.at/daten/geo?service=WFS&request=GetFeature&version=1.1.0&typeName=ogdwien:"
baseurl <- "&srsName=EPSG:4326&outputFormat=shape-zip"
urlparam <- sprintf("%s%s%s", baseurl, shapename, urlparam)
url
dir.create(outdir, showWarnings = FALSE)
= file.path(outdir, sprintf("%s.zip", shapename))
destfile download.file(url, destfile = destfile)
unzip(destfile, exdir = file.path(outdir, shapename))
invisible(file.remove(destfile))
}
Retrieve Data
## Read District Boundaries
download.vienna.shape("BEZIRKSGRENZEOGD")
<- readOGR("data/BEZIRKSGRENZEOGD", layer="BEZIRKSGRENZEOGDPolygon") wmap
## OGR data source with driver: ESRI Shapefile
## Source: "data/BEZIRKSGRENZEOGD", layer: "BEZIRKSGRENZEOGDPolygon"
## with 23 features
## It has 15 fields
## Read Streets
download.vienna.shape("STRASSENGRAPHOGD")
<- readOGR("data/STRASSENGRAPHOGD", layer="STRASSENGRAPHOGD") smap
## OGR data source with driver: ESRI Shapefile
## Source: "data/STRASSENGRAPHOGD", layer: "STRASSENGRAPHOGD"
## with 28293 features
## It has 18 fields
## Read bike parking lots
download.vienna.shape("FAHRRADABSTELLANLAGEOGD")
<- readOGR("data/FAHRRADABSTELLANLAGEOGD", layer="FAHRRADABSTELLANLAGEOGDPoint") bmap
## OGR data source with driver: ESRI Shapefile
## Source: "data/FAHRRADABSTELLANLAGEOGD", layer: "FAHRRADABSTELLANLAGEOGDPoint"
## with 4061 features
## It has 5 fields
## Download Size of Each district
<- download.vienna.bydistrict("lebensraum/tabellen/nutzungsklassen-bez", skip.row = 2)
distsize <- distsize[, 1] / 100 distsizekm2
Calculate stuff
## Number of bike parking lots per district
<- table(bmap$BEZIRK)
bikelotsperdistrict <- bikelotsperdistrict / distsizekm2
lotdensity
## Normalization of Colour Scaling
<- round(lotdensity/max(lotdensity)*50,digits=0)
normdichte <- colorRampPalette(c("lightblue", "darkgreen"))
colfunc <- colfunc(100)
colfunc100 <- colfunc100[normdichte]
bezirksfarben
# Transformations 111.1
# km to degree (einfache Umrechnung)
<- (1/111.1)/10
hundm # 100 m for Radius of Punkte
<- gCentroid(wmap, byid=TRUE) # Mittelpunkt/Bezirk centroids
Plots
Plot 1 - Vienna Map with Bike Parking Lots (Discrete Density)
layout(1:2, heights=c(5,1))
par(mar=c(0.5,0.5,1,0.5), oma=rep(3, 4), las=1)
plot(wmap, main="Bike Parking Lots in Vienna", col=bezirksfarben[wmap$BEZNR])
# add bike parking lots
plot(smap, add=TRUE, col = "grey")
draw.circle(coordinates(bmap)[,1], coordinates(bmap)[,2],hundm,border=rgb(255,255,0,maxColorValue=255),col=rgb(255,255,0,maxColorValue=255))
text(as.character(wmap$BEZ_RZ), x = centroids@coords[,1], y = centroids@coords[,2], col="orangered",cex=0.8,font=2)
# Legend
par(mar=c(1,0.5,3,0.5))
<- seq(range(lotdensity)[1],range(lotdensity)[2],20)
colseq image(x=colseq,y=1,z=matrix(seq_along(colseq)), col=colfunc(10), main=expression(paste("Bike parking lot density per district km"^-2)),axes=F)
axis(1)
Plot 2 - Vienna Map with Bike Parking Lots (Continous Density)
# Transformation of bike parking lots shape to data.frame for ggplot2
<- data.frame(coordinates(bmap))
RK colnames(RK) <- c("long","lat")
<- fortify(smap,region="OBJECTID")
ws2 <- wmap
wmap2 # assign ID
@data$id <- rownames(wmap2@data)
wmap2# transform to data.frame
<- fortify(wmap2, region="id")
test1 # This may take a while...
ggplot(data=test1) +
aes(x=long,y=lat) +
geom_polygon(aes(group=group),col="black",fill=NA) +
geom_point(data=RK,aes(x=long,y=lat)) +
geom_line(data=ws2,aes(group=group))+ xlab("longitude")+ylab("latitude") +
stat_density2d(data=RK,aes(fill = ..level..),size=1,bins=200,alpha=0.1, geom="polygon",n=100) +
ggtitle("Bike Parking Lots in Vienna 2015")+ scale_fill_continuous(name = "Kernel Density") +
theme_bw()
Comments/Pull Requests welcome!
Authors: Christian Brandstaetter with minor modifications by Mario Annau