forked from edzer/rpubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracks2.Rmd
103 lines (93 loc) · 3.63 KB
/
Tracks2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
New Trajector classes in spacetime
============================
Along the lines of classes `SpatialPolygonsDataFrame` and `SpatialLinesDataFrame` in package `sp`, I implemented Trajectories in spacetime as (typed) lists of lists. The reason for this is that trajectory data sets usually contain sets of tracks, or trips, per person (or animal, or object), and then groups these.
Sources of this document are found at <http://github.com/edzer/rpubs>, spacetime under development is also found on github.
The class will be demonstrated with a small excerpt of the geolife data.
```{r}
library(sp)
library(spacetime)
```
The next class, `Tracks`, takes a collection (list) of `Track` objects, along with a metadata data.frame `tracksData` containing (summary) information, one record per `Track` (e.g. total length, duration, average speed). It is assumed that each `Tracks` object refers to a single person, animal or object.
`TracksCollection` finally collects `Tracks` collections for several persons, animals or objects, and has a slot `tracksCollectionData` with a summary record for each person/animal/object.
We will demonsrate useage by reading a number of geolife trajectories, for the following persons:
```{r}
IDS = c("079", "095", "111", "127", "143", "159", "175")
```
```{r}
# Geolife trajectories,
# documentation: http://research.microsoft.com/apps/pubs/?id=152176 :
# data: http://research.microsoft.com/en-us/projects/geolife/
# or http://ftp.research.microsoft.com/downloads/b16d359d-d164-469e-9fd4-daa38f2b2e13/Geolife%20Trajectories%201.2.zip
setwd("/home/edzer/Downloads/Geolife Trajectories 1.3/Data/")
#sel = 1:2
#sel = TRUE
i = j = 1
#dirs = list.files("Data")[sel]
crs = CRS("+proj=longlat +datum=WGS84")
#pb = txtProgressBar(style = 3, max = length(IDS))
elev = numeric(0)
lst0 = list()
for (d in IDS) {
dir = paste(d, "Trajectory", sep = "/")
print(dir)
lst = list()
files = list.files(dir, pattern = "*plt", full.names = TRUE)
i = 1
for (f in files) {
tab = read.csv(f, skip = 6, stringsAsFactors=FALSE)
tab$time = as.POSIXct(paste(tab[,6],tab[,7]))
tab[tab[,4] == -777, 4] = NA # altitude
tab = tab[,-c(3,5,6,7)]
names(tab) = c("lat", "long", "elev", "time")
if (all(tab$lat > -90 & tab$lat < 90 & tab$long < 360
& tab$long > -180)) {
stidf = STIDF(SpatialPoints(tab[,2:1], crs), tab$time, tab)
conn = tab[-1,"elev",drop=FALSE]
lst[[i]] = Track(stidf, conn)
i = i+1
}
}
names(lst) = f
n = sapply(lst, length)
lst0[[j]] = Tracks(tracks = lst, tracksData = data.frame(length = n))
#setTxtProgressBar(pb, j)
j = j+1
}
names(lst0) = IDS
df = data.frame(IDS=IDS)
TR = TracksCollection(tracksCollection = lst0, tracksCollectionData = df)
object.size(TR)
```
We defined a few utility functions to convert the track data into `data.frame` objects, with `NA` separated trips:
and we use this:
```{r}
df = as(TR, "data.frame")
# focus on Bejing:
```
then, we can plot the trajectories read:
```{r}
plot(TR)
```
```{r}
stplot(TR, xlim=c(116.3,116.5),ylim=c(39.8,40), col = 1:20)
```
```{r}
ggm = function(obj) {
bb <- bbox(obj)
location = c(bb[1,1], bb[1,2], bb[2,1], bb[2,2])
require(ggmap)
map4ao <- get_map(location = location)
bbMap <- attr(map4ao, "bb")
latCenter <- with(bbMap, ll.lat + ur.lat)/2
lonCenter <- with(bbMap, ll.lon + ur.lon)/2
height <- with(bbMap, ur.lat - ll.lat)
width <- with(bbMap, ur.lon - ll.lon)
list("grid.raster", map4ao, x = lonCenter,
y = latCenter, width = width, height = height,
default.units = "native")
}
## Another component of the sp.layout in spplot
stplot(TR, scales = list(draw = TRUE), sp.layout = ggm(TR),
col = "red", lwd = 2,
xlim=c(116.3,116.5),ylim=c(39.8,40))
```