-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlinear_interpolation.R
More file actions
46 lines (40 loc) · 1.45 KB
/
linear_interpolation.R
File metadata and controls
46 lines (40 loc) · 1.45 KB
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
# Linear interpolation
# Configuration
TRACKS_PATH = "mallorca.csv"
OUTPUT_PATH = "mallorca_interpolated.csv"
N_POINTS_INTERPOLATION = 30
DELIMITER = ";"
HEADER = TRUE
# Read tracks
tracks <- read.table(TRACKS_PATH, sep=DELIMITER, header=HEADER)
# Initialize output
write.table(data.frame(Points=character()), file=OUTPUT_PATH, sep=";", quote=FALSE, col.names=TRUE)
tracks.format <- list()
for (i in 1:50) {
print(i)
track <- as.character(tracks$Points[i])
points <- strsplit(track, "], \\[")[[1]]
points[1] <- strsplit(points[1], "\\[\\[")[[1]][2]
points[length(points)] <- strsplit(points[length(points)], "]]")[[1]][1]
lat <- c()
lon <- c()
for (j in 1:length(points)) {
point <- strsplit(points[j], ",")[[1]]
lat <- c(lat, as.numeric(point[1]))
lon <- c(lon, as.numeric(point[2]))
}
points.int <- approx(lat, lon, method="linear", n=N_POINTS_INTERPOLATION)
if (length(lat) > 1) {
track.format <- list()
str <- "["
for (j in 1:N_POINTS_INTERPOLATION) {
track.format[[j]] <- c(points.int$x[j], points.int$y[j])
str <- paste(str, paste("[", paste(points.int$x[j], paste(", ", paste(points.int$y[j], "], ", sep=""), sep=""), sep=""), sep=""), sep="")
}
str <- substr(str, 1, nchar(str)-1)
str <- substr(str, 1, nchar(str)-1)
str <- paste(str, "]", sep="")
write.table(str, file=OUTPUT_PATH, sep=";", col.names=FALSE, row.names=FALSE, quote=FALSE, append=TRUE)
tracks.format[[i]] <- track.format
}
}