The new Github repository OpenGovernmentVienna has already been created including a very nice addition by Christian.
The script executes the following steps:
- Download Vienna map including district boundaries from http://data.wien.gv.at.
- Download population and district size from https://www.wien.gv.at/statistik, calculate population density.
- Plot Vienna map coloured by population density.
library(rgdal)
library(rgeos)
library(XML)
library(RCurl)
<- function(tablename, skip.row = 3) {
download.vienna.bydistrict <- "https://www.wien.gv.at/statistik"
baseurl <- sprintf("%s/%s.html", baseurl, tablename)
popurl
<- readHTMLTable(getURL(popurl))[[1]]
poptable <- poptable[-c(1:skip.row), ]
poptable <- poptable[, -1]
poptable row.names(poptable) <- NULL
<- sapply(poptable, function(x) gsub(".", "", x, fixed = TRUE))
poptable <- gsub(",", ".", poptable, fixed = TRUE)
poptable <- matrix(as.numeric(poptable), nrow = nrow(poptable))
poptable
poptable
}
# Download Data Shape Data District Boundaries
<- "http://data.wien.gv.at/daten/geo?service=WFS&request=GetFeature&version=1.1.0&typeName=ogdwien:BEZIRKSGRENZEOGD&srsName=EPSG:4326&outputFormat=shape-zip"
mapdata
dir.create("data", showWarnings = FALSE)
<- "data/BEZIRKSGRENZEOGD.zip"
destfile download.file(mapdata, destfile = destfile)
unzip(destfile, exdir = "data/BEZIRKSGRENZEOGD")
file.remove(destfile)
## [1] TRUE
# Read District Boundaries
<- 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
# Download Population per district
<- download.vienna.bydistrict("bevoelkerung/tabellen/bevoelkerung-alter-geschl-bez")
distpop <- rowSums(as.matrix(distpop))
distpopsum
# Download Size of Each district
<- download.vienna.bydistrict("lebensraum/tabellen/nutzungsklassen-bez", skip.row = 2)
distsize <- distsize[, 1] / 100
distsizekm2
<- data.frame(distpopsum / distsizekm2)
wd
<- gCentroid(wmap, byid=TRUE)
centroids <- colorRampPalette(c("lightblue", "darkred"))
colfunc colnames(wd) <- "inh"
$district <- seq(1,23)
wd<- wd$district[order(wd$inh)]
anstieg_pop <- colfunc(23)[order(anstieg_pop)]
colsort layout(matrix(c(1,2), byrow = TRUE),height=c(1.3, 0.7))
par(mar=c(0,0,0,0))
plot(wmap,col=colsort[wmap$BEZNR])
text(as.character(wmap$BEZ_RZ), x = centroids@coords[,1], y = centroids@coords[,2],cex=0.8)
par(mar=c(3,4,4,2),mgp=c(2,0.7,0))
barplot(wd$inh,main="Population Density Vienna 2014",yaxt="n",col=colsort,xlab="District",beside=T, ylab=expression(paste("1.000 Inhabitants km"^-2)),names.arg=as.roman(wd$district),las=2)
axis(2,labels=c("0","5","10","15","20"),at=c(0,5000,10000,15000,20000),las=1)
abline(h=c(5000,10000,15000,20000),lty=2)
legend("topright",pch=c(15,15),col=c("darkred","lightblue"),c("high","low"),bg="white")
Authors: Christian Brandstaetter with small additions by Mario Annau