genTile.R 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #' Generate Global Tiling System
  2. #'
  3. #' @description
  4. #' This function generates a matrix with bounding box information for a
  5. #' global tiling system (based on Lat/Lon).
  6. #'
  7. #' @param tileSize \code{numeric}, size of a single tile in degrees (EPSG:4326).
  8. #' @param offset \code{numeric}, shifts the tiling system in upper-left
  9. #' direction.
  10. #' @param StartNameFrom \code{numeric}. \code{c(Lat-Direction,Lon-Direction)}
  11. #' start number in the naming of the tiles.
  12. #' @param extent \code{list}. Tile system extent information, basically the
  13. #' coverage of the data on server.
  14. #'
  15. #' @return
  16. #' A \code{matrix}.
  17. #'
  18. #' @author
  19. #' Matteo Mattiuzzi
  20. #'
  21. #' @seealso
  22. #' \code{\link{getTile}}.
  23. #'
  24. #' @examples
  25. #' # 1x1 degree tiling system
  26. #' e1 <- genTile()
  27. #' head(e1)
  28. #'
  29. #' # 10x10 degree tiling system with offset to be aligned to Geoland2 Dataset
  30. #' e2 <- genTile(tileSize = 10, offset = (1/112) / 2)
  31. #' head(e2)
  32. #'
  33. #' # Tiling system for SRTMv4 data (CGIAR-CSI)
  34. #' e3 <- genTile(tileSize = 5, StartNameFrom = c(1, 1),
  35. #' extent = list(xmin = -180, xmax = 180, ymin = -60,ymax = 60))
  36. #' head(e3)
  37. #'
  38. #' @export genTile
  39. #' @name genTile
  40. genTile <- function(tileSize = 1, offset = 0, StartNameFrom = c(0, 0),
  41. extent = list(xmin = -180, xmax = 180, ymin = -90, ymax = 90))
  42. {
  43. # offset is used in case of pixel centrum reference. In such case the offset is res/2
  44. if (offset!=0) {cat("Warning! Tiles crossing LAT extremas (-90 and +90) are not meaningful for now! For those tiles the resulting shift in LON is not computed!\n")}
  45. # set origin in UL
  46. LON <- seq(extent$xmin,extent$xmax,by=tileSize)
  47. LON <- LON[-length(LON)]
  48. LAT <- seq(extent$ymax,extent$ymin,by=-tileSize)
  49. LAT <- LAT[-length(LAT)]
  50. LON <- LON - offset
  51. LAT <- LAT + offset
  52. tiles <- expand.grid(LON,LAT)
  53. colnames(tiles) <- c("xmin","ymax")
  54. iv <- (0:(length(LON)-1)) + StartNameFrom[2]
  55. ih <- (0:(length(LAT)-1)) + StartNameFrom[1]
  56. vh <- expand.grid(iv,ih)
  57. tiles$iv <- vh[,2]
  58. tiles$ih <- vh[,1]
  59. tiles$xmax <- tiles$xmin + tileSize
  60. tiles$ymin <- tiles$ymax - tileSize
  61. tiles[tiles$xmin < -180,"xmin"] <- tiles[tiles$xmin < -180,"xmin"] + 2*180
  62. tiles[tiles$xmin > 180,"xmin"] <- tiles[tiles$xmin > 180,"xmin"] - 2*180
  63. tiles[tiles$xmax < -180,"xmax"] <- tiles[tiles$xmax < -180,"xmax"] + 2*180
  64. tiles[tiles$xmax > 180,"xmax"] <- tiles[tiles$xmax > 180,"xmax"] - 2*180
  65. #TODO: EXTREMAS IN LAT are changing LON +- 180
  66. # tiles[tiles[,"lat"] < -90,"lon"] <- tiles[tiles[,"lat"] < -90,"lon"] + 180 #
  67. tiles[tiles$ymax < -90,"ymax"] <- -90 + abs(90 + tiles[tiles$ymax < -90,"ymax"])
  68. tiles[tiles$ymax > 90,"ymax"] <- (2*90) - tiles[tiles$ymax > 90,"ymax"]
  69. tiles[tiles$ymin < -90,"ymin"] <- -90 + abs(90 + tiles[tiles$ymin < -90,"ymin"])
  70. tiles[tiles$ymin > 90,"ymin"] <- (2*90) - tiles[tiles$ymin > 90,"ymin"]
  71. tiles <- tiles[,c(3,4,1,2,5,6)]
  72. return(tiles)
  73. }