[R-sig-Geo] problem exporting map including both google map image and shape polygon

Bastien.Ferland-Raymond at mrnf.gouv.qc.ca Bastien.Ferland-Raymond at mrnf.gouv.qc.ca
Thu Oct 27 15:44:48 CEST 2011


Dear R list,

I have a problem I've been working on for a while now and I'll try to explain it clearly.  It's highly visual and it would be easier to just import images, however I'm not sure I can do that on the list so I'll try to explain it well.

Here is what I'm trying to do:
1) import a satellite image from google map,
2) Put field station locations on the map,
3) Draw a polygon shape around the territory studied,
4) Automatically export the image as a (preferably) png file.

Point 4 is where my problem is.  I succeed at doing a pretty nice image using:

###
range<-qbbox(donn2$X$LATITUDE,donn2$X$LONGITUDE)
mamap<-GetMap.bbox(range$lonR,range$latR,destfile="mamap.png",maptype="satellite", size=c(500,640))
windows(width=6,height=7.68)
PlotOnStaticMap(mamap,lat=donn2$X$LATITUDE,lon=donn2$X$LONGITUDE,pch=21,col=1,bg="white",cex=1.3)

nc2 <- readShapePoly("shape/contour_carto06152.shp")
par(new=TRUE)
plot(nc2,border="blue",lwd=3)
###

I can then save this image directly in the menu : file -> save as -> png.  Which does work.  However, it's not automatic.

So I tried this way (line 3 and 9 are changed):

###
###
range<-qbbox(donn2$X$LATITUDE,donn2$X$LONGITUDE)
mamap<-GetMap.bbox(range$lonR,range$latR,destfile="mamap.png",maptype="satellite", size=c(500,640))
png(filename = "map.png",width=500, height=640 )
PlotOnStaticMap(mamap,lat=donn2$X$LATITUDE,lon=donn2$X$LONGITUDE,pch=21,col=1,bg="white",cex=1.3)

nc2 <- readShapePoly("shape/contour_carto06152.shp")
par(new=TRUE)
plot(nc2,border="blue",lwd=3)
dev.off()
###

It does save an image in my directory, however the image includes only the shape file (the territory polygon) and not the satellite image and field station location behind it.  Any idea why?

Another interesting point is that I had to use the option --  par(new=TRUE)  -- to get both the shape and the picture on the same plot.  Using the option  -- add=TRUE  --  like it is done in most example available on the internet didn't work for me (with plot(nc2,border="blue",lwd=3, add=TRUE)).  It didn't display any error message, but simply didn't had the polygon to the image.  I have a feeling the par(new=TRUE) statement is the problem.


I'm using R 2.11.0 on windows XP.


Any help would be appreciated.
Thanks!


Bastien Ferland-Raymond, M.Sc. Stat., M.Sc. Biol.
Division des orientations et projets spéciaux
Direction des inventaires forestiers
Ministère des Ressources naturelles et de la Faune
880, chemin Sainte-Foy, 5e étage
Québec (Québec) G1S 4X4



More information about the R-sig-Geo mailing list