0

I have a PNG image wich I have opened in R and in order to give coordinations converted into a SpatRast object with the following code:

library(terra)
r <- rast("yanomamis.png")
#Warning: [rast] unknown extent

## drop the alpha channel 
r <- r[[1:3]]
## declare the R,G,B channels
RGB(r) <- 1:3
ext(r) <- c( -70.18066, -55.6897, -0.5009, 4.89839 )
crs(r) <- "+proj=longlat +datum=WGS84" 

Which resulted in:

r
#class       : SpatRaster 
#dimensions  : 503, 1328, 3  (nrow, ncol, nlyr)
#resolution  : 0.01091187, 0.01073417  (x, y)
#extent      : -70.18066, -55.6897, -0.5009, 4.89839  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs  
#source      : yanomamis.png 
#colors RGB  : 1, 2, 3 
#names       : yanomamis_1, yanomamis_2, yanomamis_3 

My goal is to draw over this plot and save the drawing as a shape file, which I thought I could accomplish with the function draw from the terra package.

However, when I try to execute the code, Im not able to draw anything over the plot.

enter image description here

Can anyone see what am I missing?

1
  • Cannot reproduce this with terra 1.6-33. Setting ext and crs on a random png without any georeference works just fine, without any warning. Which version are you working with?
    – dimfalk
    Commented Nov 20, 2022 at 13:32

1 Answer 1

1

To be able to draw on a map in R-Studio, it needs to be plotted in the plot window.

Since you are using a ".Rmd" (Rmarkdown) file, you need to select "Chunk Output in Console" (select the "gear" menu next to "knitr" menu).

An Rmarkdown file seems an odd choice if you are looking for this type of interaction. You could use a simple R code (".R") file instead. Or you could run plot(r) in the R console.

1
  • You are a life savior @Robert Hijmas. As I learned to code to Rmd, sometimes I get stuck because of it.
    – Aniel
    Commented Nov 21, 2022 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.