tylermorganwall / rayshader

R Package for 2D and 3D mapping and data visualization
https://www.rayshader.com/
2.05k stars 211 forks source link

Annotate using lat/long coordinates #30

Closed bhogan-mitre closed 4 years ago

bhogan-mitre commented 5 years ago

Could you explain your workflow for determining the matrix indices used in the render_label examples?

render_label(montereybay,x=220,y=330,z=10000,zscale=50,text = "Santa Cruz")

It would be more natural for some applications to specify Santa Cruz for example by latitude=37,longitude=-122 rather than x=220,y=330.

Do you see a role in this package for helper functions to assist with that translation?

This was mentioned by @YsoSirius in https://github.com/tylermorganwall/rayshader/issues/25 as well (who gave an example using raster's colFromX and colFromY), but I think this is deserving of its own issue.

The use case would be for placing labels by their lat/long coordinates, but also for other types of overlays such as a GPS track. Presumably some input data types such as GeoTIFF would have the necessary information to facilitate this.

ojabri commented 5 years ago

My work around was to convert point data lat/long to xy using the elevation maps extent/bbox. It seems to put the labels close enough to right place.

p1 = (x-extent(elevation)@xmin)  / (extent(elevation)@xmax-extent(elevation)@xmin) * ncol(elevation)
p2 = (y-extent(elevation)@ymin)  / (extent(elevation)@ymax-extent(elevation)@ymin) * nrow(elevation)
neilcharles commented 5 years ago

That work around is what the package 'geoviz' does in latlong_to_rayshader_coords(). You can also make GPS overlays with it if you want to try @bhogan-mitre

ratnanil commented 4 years ago

@ojabri's approach (https://github.com/tylermorganwall/rayshader/issues/30#issuecomment-463794935) does not quite work for me, I need to add a 1-... in the second line. This is my adopted function:

coords_to_xy <- function(x, y, elevation){
  require(raster)
  ex <- extent(elevation)
  x = (x-ex@xmin)/(ex@xmax-ex@xmin)*ncol(elevation)
  y = (1-(y-ex@ymin)/(ex@ymax-ex@ymin))*nrow(elevation)
  out <- c(x,y)
  names(out) <- c("x","y")
  return(out)
}
tylermorganwall commented 4 years ago

Added in 7052b91dd73fe9dc745f6139bcc589c65050c4ec