I have a satellite image as a PNG with known coordinates (bottom, left, top, right). I want to draw a shapefile on it (the limits of a county) in R.
My first thought was to convert the PNG (it's read as a three-layered matrix, for red, green and blue) to a raster. But creating a three-layered raster may be an overkill.
And simpler solution to do this?
I found the function rasterImage(), and I'm doing like that:
Map0 <- readPNG('myMap0.png')
cM <- c(xmin,ymin,xmax,ymax)
png('myMap.png',10240,8610)
par(mar=c(0,0,0,0))
plot(myShapeFile,border=NA,xlim=c(cM[1],cM[3]),ylim=c(cM[2],cM[4]))
rasterImage(Map0,cM[1],cM[2],cM[3],cM[4])
plot(myShapeFile,add=T,border='white',lwd=5)
dev.off()
It's almost fine, except for a white border around the image. Since I used the dimensions of the original image (10240x8610), this border is probably distorting the new image, because the painted area is smaller than the original. I had this problem before, but could never solve it.
I thought the problem was with png(), so I did the following:
par(mar=c(0,0,0,0),bg=NA)
plot(myShapeFile,border='white',xlim=c(cM[1],cM[3]),ylim=c(cM[2],cM[4]),lwd=5)
dev.print(width=10240,height=8610,'myBorder.png',dev=png)
So I could try to merge the two images as layers in Gimp, but I see that the border remains, causing the shapefile to be drawn in the wrong position (it aligns perfectly with myMap.png, but not with myMap0.png). In Google Earth I know that the coordinates in cM are precise, so, since I'm giving mar=0, and xlim and ylim should refer to the limits of the plot, why this margin keeps on disturbing the image created?
Following @mdsumner comment, I tried the following:
map0 <- brick('myMap0.png')
extent(map0) <- c(cM[1],cM[3],cM[2],cM[4])
png('myMap.png',10240,8610)
par(mar=c(0,0,0,0))
plotRGB(map0,maxpixels=10240*8610)
plot(myShapeFile,add=T,border='white',lwd=5)
dev.off()
Now it's almost right, except for the left and right white margins, each 27 pixels wide).