2

我正在尝试使用 RgoogleMaps 保存一个点叠加到 png 文件的地图。我能够在绘图设备中成功创建地图和覆盖点,但无法弄清楚如何将点写入我的地图的 png 文件。

我使用的是 Mac OS X Lion 10.7.5 R 版本 2.15.0

使用 R 文档中的示例代码,我得到了相同的结果:我打开的绘图设备上有行,但没有写入 png 文件。

这是我正在使用的示例代码:

lat = c(40.702147,40.718217,40.711614);
lon = c(-74.012318,-74.015794,-73.998284);
center = c(mean(lat), mean(lon));
zoom <- min(MaxZoom(range(lat), range(lon)));
#this overhead is taken care of implicitly by GetMap.bbox();              
MyMap <- GetMap(center=center, zoom=zoom,markers = '&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|40.718217,-73.998284', destfile = "MyTile1.png");

tmp <- PlotOnStaticMap(MyMap, lat = c(40.702147,40.711614,40.718217), lon = c(-74.015794,-74.012318,-73.998284), destfile = "MyTile1.png", cex=1.5,pch=20,col=c('red', 'blue', 'green'), add=FALSE);
#and add lines:
PlotOnStaticMap(MyMap, lat = c(40.702147,40.711614,40.718217), lon = c(-74.015794,-74.012318,-73.998284), lwd=1.5,col=c('red', 'blue', 'green'), FUN = lines, add=TRUE)

我知道 png 设备正在工作,因为它有效:

png('my.png')
plot(seq(1:10))
dev.off()

谢谢你的帮助。

4

2 回答 2

1

要使用 PlotOnStaticMap 向 png 文件添加点和线,可能需要在调用 PlotOnStaticMap 之前为绘图设备显式设置 type='cairo-png'。在我的机器和我的 R 环境中,这有效:

png('MyTile1.png',type='cairo-png')
dev.cur()
png 3 # returned value

但这不是

png('MyTile1.png')
dev.cur()
quartz_off_screen 2 # returned value

下面的代码将点和线写入 MyTile1.png:

lat = c(40.702147,40.718217,40.711614);
lon = c(-74.012318,-74.015794,-73.998284);
center = c(mean(lat), mean(lon));
zoom <- min(MaxZoom(range(lat), range(lon)));             

MyMap <- GetMap(center=center, zoom=zoom,markers = '&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|40.718217,-73.998284')

# explicitly set device to png with type='cairo-png'
png('MyTile1.png',type='cairo-png')     
# add points            
tmp <- PlotOnStaticMap(MyMap, lat = c(40.702147,40.711614,40.718217), lon = c(-74.015794,-74.012318,-73.998284), cex=1.5,pch=20,col=c('red', 'blue', 'green'));
#and add lines:
PlotOnStaticMap(MyMap, lat = c(40.702147,40.711614,40.718217), lon = c(-74.015794,-74.012318,-73.998284), lwd=1.5,col=c('red', 'blue', 'green'), FUN = lines, add=TRUE)
dev.off()
于 2012-10-16T17:50:42.117 回答
0

你可以按照你的最后一行

dev.print(png, width = 800, file = "MyTile2.png")

或类似的东西

于 2012-10-16T07:08:42.147 回答