0

from this question I managed to create a SpatRaster object that has proper colors plotted. But now I am in a bind as to how to add markers in the map that show specific points in space from coordinates.

library(terra)
img <- rast("https://i.stack.imgur.com/ZyyaD.jpg")
crs(img) <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m"
ext(img) <- c(-9390939.6587, -9327758.7787, 1095038.6805, 1127491.1389)

What would be the simplest way to add points to positions parting from coordinates? I have in mind something like this:

plot(img)
coordinates<-data.frame(lat=c(10.00236,9.93333),
           long=c(-84.11651,-84.08333),place=c("heredia","San José"))
points(coordinates)

This does not work, but I hope it gets the message if what I need to do.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
GEX_HEX_420
  • 105
  • 6
  • I have discovered that my problem can be solved if I somehow apply a conversion formula [like this](https://gis.stackexchange.com/questions/20686/mercator-projection-problem-with-latitude-formula) to the table to convert the coordinates to pixels. However I still can't wrap my head around it. – GEX_HEX_420 May 18 '22 at 01:35
  • instead of commenting, you could edit your question. – Robert Hijmans May 18 '22 at 02:02

1 Answers1

0

You can first create a SpatVector of points from your coordinates, and transform that to the coordinate reference system (crs) of the SpatRaster.

v <- vect(coordinates, c("long", "lat"), crs="+proj=longlat")
vv <- project(v, crs(img))
plot(img)
points(vv, cex=3, col="light blue")

In this case you could also get the coordinates in the Mercator crs of the SpatRaster by doing:

plot(img)
xy <- click()

And click on the map at the locations of interest.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63