0

我获得了许多不同的 GPS 轨迹,并试图使用 ggmap 将所有路径绘制到一张地图上。我正在尝试制作类似于Flowing Data所做的工作的东西,其中作者使用 plotkml 而不是 ggmap (我更喜欢)。不幸的是,在添加了几条路径后,我的 R 脚本显然由于内存过多而停止运行。有人可以帮忙吗?

我在 MacOS 上使用 R 版本 3.0.2。

这是一个功能齐全的脚本。我手动放入了三个示例数据帧,其中一帧来自 GPS 坐标的一个跟踪文件。实际上,我有超过4000 个这样的数据框。

library(ggmap)

printf <- function(...) cat(sprintf(...))

# ---------------------------------------------------------
# Create three example data frames
df1 <- structure(list(latitude = c(37.789165, 37.827332, 37.851501),
    longitude = c(-122.411667, -122.210167, -122.265831)), .Names = c("latitude",
    "longitude"), class = "data.frame", row.names = c(4634L, 5415L,
    30777L))

df2 <- structure(list(latitude = c(37.331951, 37.332291), longitude = c(-122.029579,
-122.028625)), .Names = c("latitude", "longitude"), class = "data.frame", row.names = c(18781L,
18787L))

df3 <- structure(list(latitude = c(37.789333, 37.780834, 37.622833,
37.7775), longitude = c(-122.408669, -122.423668, -122.330666,
-122.43383)), .Names = c("latitude", "longitude"), class = "data.frame", row.names = c(44107L,
44182L, 44237L, 44277L))

allDataFrames <- list(df1, df2, df3)
numDataFrames <- length(allDataFrames)
# ---------------------------------------------------------


# Get the background map
map <-get_googlemap(center=c(lon=-122.237, lat=37.753),maptype="roadmap", color="bw", zoom=9)
p <- ggmap(map)

dfCounter <- 0

# Loop over all the data frames.
for (df in allDataFrames)
{
    # Plot this data frame as a path.
    p <- p + geom_path(aes(x=longitude, y=latitude), data=df, colour="#ff0000", size=0.5)
    printf("%d / %d\n", dfCounter, numDataFrames)
    dfCounter <- dfCounter+1
}

print(p)

现在,当循环完成第 300 次迭代时,脚本会停止,没有任何进展。我怀疑是这条线:

p <- p + geom_path(aes(x=longitude, y=latitude), data=df, colour="#ff0000", size=0.5)

但我不知道如何改进。我需要达到 4000 个数据帧,它已经停止在 300 个。

在我的 MacOS 上“top”的输出显示 RSIZE(物理内存)和 VSIZE(虚拟内存)已经脱钩。问题是什么?

在运行脚本之前:

PID    COMMAND      %CPU  TIME     #TH   #WQ  #POR #MREG RPRVT  RSHRD  RSIZE  VPRVT  VSIZE  PGRP  PPID  STATE    UID  FAULTS
42708  R            0.0   00:00.18 1     0    21   126   30M    244K   34M    47M    2440M  42708 448   sleeping 501  9464 

在运行脚本时,在迭代 150 左右:

PID    COMMAND      %CPU  TIME     #TH   #WQ  #POR #MREG RPRVT  RSHRD  RSIZE  VPRVT  VSIZE  PGRP  PPID  STATE    UID  FAULTS
42708  R            100.1 00:45.87 1/1   0    22   544+  2002M+ 244K   2018M+ 2034M+ 4429M+ 42708 448   running  501  773737+
4

1 回答 1

0

创建单个数据框,就像在上面链接到的 Flowing Data 站点上所做的那样。该数据框应包含三列:索引、纬度和经度。lat 和 long 是不言自明的。索引为每条路径取一个唯一值。同样,这已经在上面流动数据站点的代码中完成了。对于其余的:

#experiment with zoom and other ggmap options as you see fit. Random lat/long chosen here

baseMap <- get_googlemap(center=c(lon = -80, lat = 38), maptype="roadmap", color="bw", zoom=11)

map <- ggmap(baseMap) +
geom_path(aes(x=longitude, y=latitude, group=index), data=routes, alpha=0.2, color="red")

map
于 2014-03-11T05:31:35.363 回答