The aim of this topic is to deal with spatial data and open data in R. The first task is to read and plot the border of the government of vienna. Furthermore we will use open data to plot a particular area of interest.
The package rdgal provides some import functions like readOGR, which makes an import of shapefiles possible.
#Import data. Download from: https://open.wien.gv.at/
library(rgdal) bordervienna <- readOGR(".","LANDESGRENZEOGDPolygon")
# Transformation: Shape file to a data frame library(ggmap) vienna_bor <- fortify(bordervienna) # ggplot can deal with a data frame
#Plot Vienna as polyline (left image) ggplot(data=vienna_bor,aes(x=long,y=lat,group=group))+ geom_path(size=0.9)
#Plot Vienna as filled polygon (right image) ggplot(vienna_bor, aes(x = long, y = lat, group = group)) + geom_polygon()
Now we create a map of the campus called “Türkenschanze” and the neighborhood of the University of Natural Resources an Applied Life Sciences in Vienna. We need data from OpenStreetMap which can be downloaded via the package osmar.
#Import data install.packages("osmar") library(osmar) src <- osmsource_api() bb <- center_bbox(16.33402, 48.23669,500,500) #you have to define a bounding box for the area of interest boku <- get_osm(bb, source = src) # and download them # First preview of the data plot(boku)
The downloaded data set has a complex structure and to better understanding I recommend this article written by Eugster & Schlesinger.
Every single tree, bus stop or building can be selected via constructed attribute queries.
#Construct query: All buildings in bounding box b_ids <- find(boku, way(tags(k == "building"))) b_ids <- find_down(boku, way(b_ids)) b<- subset(boku,ids=b_ids) #To plot my query result I transform it to a data frame b_poly <- as_sp(b,what="polygons") building <- fortify(b_poly) # Plot of all buildings in the neighborhood of the University of Natural Resources an Applied Life Sciences in Vienna ggplot(building, aes(x = long, y = lat,group=group)) + geom_path( )
# After all you can see a plot of the area of interest.
Points present trees (darkgreen= conifer; lightgreen=broadleaved). Roads are read lines; Black polygons are all buildings and blue filled polygons are buildings which are part of the university.