-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapPointsToLines_enviroCar.Rmd
71 lines (64 loc) · 2.52 KB
/
snapPointsToLines_enviroCar.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
Map-Matching enviroCar trajectories to OSM roads
========================================================
```{r}
importEnviroCar = function(file) {
require(rjson) # fromJSON
require(maptools) # spCbind
# read data as spatial object:
features = readOGR(file, layer = "OGRGeoJSON")
# convert time from text to POSIXct:
features$time = as.POSIXct(features$time, format = "%Y-%m-%dT%H:%M:%SZ")
# the third column is JSON, we want it in a table (data.frame) form: 1. form
# a list of lists
l1 = lapply(as.character(features$phenomenons), fromJSON)
# 2. parse the $value elements in the sublist:
l2 = lapply(l1, function(x) as.data.frame(lapply(x, function(X) X$value)))
# bind these value elements, row by row, together
ret = do.call(rbind, l2)
# read the units:
units = lapply(l1[1], function(x) as.data.frame(lapply(x, function(X) X$unit)))
# add a units attribute to layer
features$phenomenons = NULL
# add the table as attributes to the spatial object
if (length(features) == nrow(ret)) {
features = spCbind(features, ret)
attr(features, "units") = units[[1]]
features
} else NULL
}
require(RCurl)
require(rgdal)
require(maptools) #snapPointsToLines function
url <- "https://giv-car.uni-muenster.de/stable/rest/tracks/5207d871e4b058cd3d669afe"
layer = readOGR(getURL(url, ssl.verifypeer = FALSE), layer = "OGRGeoJSON")
# feature extracted OSM shape file containing subset of roads in Muenster
shape <- readOGR(dsn=".", layer="road_selection")
# points
points <- importEnviroCar(url)
res <- snapPointsToLines(points, shape)
# plotting raw points
plot(points, col="deepskyblue")
# adding OSM roads
plot(shape, add=TRUE)
# adding map matched points
plot(res, add=TRUE, col="red")
# sample code for using OSM and R (OSMAR) package
#library("osmar")
#src <- osmsource_api()
# center of Muenster with a 2km * 2km bounding box
#bb <- center_bbox(7.667724, 51.928308, 1500, 1500)
#bb <- corner_bbox(7.650558,51.927938,7.690977,51.935649)
# 7.557603,52.004258,7.689218,51.923227
# bbb <- corner_bbox(7.473833,52.06002,7.774362,51.840151)
# bbbb <- corner_bbox(7.557603,52.004258,7.689218,51.923227)
# creating osmar object from bounding box
#ms <- get_osm(bb, source = src)
# subset data: choose only
# ms_hw_ids <- find(ms, way(tags(k == "highway")))
# transform from osmar object to sp object
# ms_as_sp <- as_sp(ms, what = c("points", "lines", "polygons"), crs = osm_crs(), simplify = TRUE)
# plot(ms_as_sp)
# create spatial lines data frame from ms_as_sp
# Sldf <- ms_as_sp$lines
# url to track in geosjon format
```