I've been working on a map showing lines running across Europe. Each line is coming from a GPX file, parsed in like so:
spdf <- readOGR("data/file.gpx", layer = "track_points")
Now, if I plot the map using plot()
, I can simplify each of these lines without any issue:
spdf_simple <- gSimplify(spdf, tol = 0.1)
However, in this particular instance I am using ggplot2
and need to keep a dataframe structure, as I'm doing some filtering (see below):
# load each file
for (file in list.files('data/') {
# parse each file
track_points <- readOGR(paste("data/", file, sep = ""), layer = "track_points")
track_points@data$time_clean <- ymd_hms(track_points@data$time)
# we need to preserve @data to filter the time value later on
df1 <- data.frame(track_points@data)
df2 <- data.frame(track_points@coords)
df <- bind_cols(df1, df2)
# filter out all records that happened after FINISH time
df.filter <- df %>%
filter(time_clean < FINISH) %>%
select(time, coords.x1, coords.x2) %>%
mutate(group = file)
colnames(df.filter) <- c('time', 'lon', 'lat', 'group')
paths <- rbind(paths, df.filter)
}
Subsequently I can plot my paths like so:
ggplot() +
geom_path(data = paths, aes(lon, lat, group = group))
Now the result of gSimplify
is a sp::SpatialPoints
object which doesn't preserve what's in @data
.
I'd like to simplify these paths, although I'm at a loss as to where to do it in ggplot world.
layer="track_points"
and then plot it withplot(spdf)
I gt points, not a track. Then runninggSimplify
on points doesn't actually do any simplification - it returns the points without the attributes. Maybe you need to read thetracks
layer?