-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgmap.R
42 lines (36 loc) · 1.05 KB
/
gmap.R
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
# Google map generator
library(plotKML)
library(ggmap)
library(maps)
# Configuration
TRACKS_PATH = "/mallorca.csv"
DELIMITER = ";"
HEADER = TRUE
LOCATION = "Mallorca"
RESOLUTION = c(1920, 1080)
# Get map
map <- get_map(location=LOCATION, zoom=9, maptype="satellite", source="google")
map <- ggmap(map)
# Read tracks
tracks <- read.table(TRACKS_PATH, sep=DELIMITER, header=HEADER)
# Add tracks to the map
for (i in 1:dim(tracks)[1]) {
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]))
}
track <- data.frame(lat, lon)
map <- map + geom_point(data=track, aes(x=lon, y=lat), size=0.5, color="red", alpha=0.5)
}
# Save as png
png(filename="map.png", width=RESOLUTION[1], height=RESOLUTION[2])
plot(map)
dev.off()