Commit cc8d232d authored by Hopf, Konstantin's avatar Hopf, Konstantin
Browse files

Added OpenStreetMap example and examples for the `lubridate` package (L05)

parent 2f02c938
......@@ -7,6 +7,11 @@ editor_options:
# Practicals for geographic data
```{r install packages}
install.packages("sp", "rgdal", "osmar", "lubridate")
```
Germany publishes geographic data as ESRI shapefiles on the Website of the [Bundesamt für Kartographie](http://www.geodatenzentrum.de/geodaten/gdz_rahmen.gdz_div?gdz_spr=deu&gdz_akt_zeile=5&gdz_anz_zeile=1&gdz_user_id=0)
For illusrration, the political borders (Verwaltungsgrenzen) are used on a scale of 1:250 000 (VG250)
......@@ -41,16 +46,11 @@ ger.shp <- readOGR("../data/DE_Admin_Geodata/VG250_Ebenen/", "VG250_LAN")
#basic plot of the data - looks similar to previous plot
plot(ger.shp)
axis(1)
axis(2)
#Here, we have polygons that we can colorate
plot(ger.shp, col=1:23)
ger.shp@data
?plot.sp
proj4string(ger.shp)
head(ger.shp@)
plot(ger.shp, axit="t")
```
As an example, the housing statistics from the German Federal Ministry of Statistics is used
......@@ -111,11 +111,144 @@ axis(2)
# the WGS84 coordinates of Bamberg are
bamberg <- SpatialPoints(list(x=10.891667, y=49.891667))
#display the projection and coordinate system of the spatial data
proj4string(ger.shp)
proj4string(bamberg)
# transform the german UTM coordinates to WGS84 coordinates
ger.shp.wgs84 <- spTransform(ger.shp, CRS("+proj=longlat +datum=WGS84"))
plot(ger.shp.wgs84)
axis(1)
axis(2)
points(bamberg, col=2)
```
Here is an example on how to access OpenStreetMap
**Please** do not query OSM with more than 1 request per second!!!
The API is not made for large read queries
There is a special [Overpass API](https://wiki.openstreetmap.org/wiki/Overpass_API)
to retrieve much data from the database.
```{r accessing OpenStreetMap data}
library(osmar)
erba_data <- get_osm(x = center_bbox(center_lon = 10.86832,
center_lat = 49.90300,
width=300,
height=300),
source = osmsource_api(url = "https://api.openstreetmap.org/api/0.6/"))
#plot the data
plot(as_sp(erba_data, what = "lines"))
points(as_sp(erba_data, what = "points"), pch=16, col=2, cex=0.5)
axis(1)
axis(2)
erba_data$nodes$attrs
```
# Practicals for the date and time handling
```{r parsing data}
library(lubridate)
#for the case you get a warning with "unknown timezone"
Sys.setenv(TZ="CET")
dmy(13112018)
ymd("20181113")
mdy("November 13th, 2018")
dmy_hms("13.11.2018 11:15")
dmy_hms("13.11.2018 11:15", tz = "CET")
today()
now()
ymd(c("2010-10-10", "bananas"))
```
```{r getting and setting components}
t <- dmy_hms("13.11.2018 11:15:01", tz = "CET")
second(t)
minute(t)
day(t)
week(t)
month(t)
month(t, label = T)
wday(t)
wday(t, label = T)
year(t) <- 2017
print(t)
#override time zone
tz(t) <- "EST"
t
#converts the time zone
with_tz(t, "CET")
```
```{r date time aritmetics}
t <- c("13.11.2018 11:15:01",
"12.11.2018 23:16:09",
"09.11.2018 07:01:01",
"08.11.2018 10:19:10")
t <- dmy_hms(t)
mean(t)
floor_date(t, unit = "week")
```
There are **three types of time spans**:
* durations, which represent an exact number of seconds.
* periods, which represent human units like weeks and months.
* intervals, which represent a starting and ending point.
`difftime` objects are hard to work with as they store the difference in seconds, minutes, ...
```{r time spans}
#how old is Hannah?
h_age <- today() - dmy(21031985)
h_age
class(h_age) #duration stored as a difftim-object
h_age_dur <- as.duration(h_age) #lubridate offers a duration object
h_age_dur
#durations can also be created
dweeks(3)
dyears(2)
#mind the fact:
dmy_hms("27.10.2018 10:00:00", tz="CET")
dmy_hms("27.10.2018 10:00:00", tz="CET")+ddays(1) #time shift for DST
#to solve this, lubridate offers periods
dmy_hms("27.10.2018 10:00:00", tz="CET")+days(1)
# an example with leap years
ymd("2016-01-01") + dyears(1)
ymd("2016-01-01") + years(1)
```
```{r intervals}
next_year <- today() + years(1)
#lubridate brings a new operation for time intervals
today() %--% next_year
(today() %--% next_year) / ddays(1)
#there is also an operation to check how many periods fall into an interval
(today() %--% next_year) %/% dweeks(1)
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment