RgoogleMaps PlotOnStaticMap not adding points to png fiile

2.3k views Asked by At

I'm trying to save a map with points overlayed to a png file use RgoogleMaps. I'm able to successfully create maps and overlay points in a plot device but can't figure out how to get the points written to a png file of my map.

I'm on a Mac OS X Lion 10.7.5 R version 2.15.0

Using example code from R docs I get the same results: lines on my open plot device, but not written to png file.

Here is the example code I'm using:

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)

I know that png device is working because this works:

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

Thanks for any help.

2

There are 2 answers

0
glynnsc On BEST ANSWER

To add points and lines to png file using PlotOnStaticMap one may need to explicitly set type='cairo-png' for the plotting device prior to calling PlotOnStaticMap. On my machine and in my R environment this works:

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

but this doesn't

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

The code below writes points and lines to 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()
1
Henry On

You could follow your final line with

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

or something similar