I have a spatial grid of dimension 72 col × 24 rows. For this question, I tried to create a small polygon grid (3×3) which I hope you can run in your PC too.
As you can see it is a 3×3 grid, however the numbering of the grid cell starts from bottom left of the last row towards bottom right & again continues from left side of the middle row towards right.
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0; sf_use_s2() is TRUE
url = "https://github.com/Ohm-Np/ex-data/raw/main/vector/test_grid.gpkg"
download.file(url, "../grid.gpkg")
grid <- read_sf("../grid.gpkg")
plot(grid)
Created on 2022-02-25 by the reprex package (v2.0.0)
So, my question is: How to create another column (say) column_row
and store the grid cell id which starts from top left towards right.
Result like this (col_number × row_number):
df <- data.frame(column_row = c("01_01", "02_01", "03_01", "01_02", "02_02", "03_02", "01_03", "02_03", "03_03"))
df
#> column_row
#> 1 01_01
#> 2 02_01
#> 3 03_01
#> 4 01_02
#> 5 02_02
#> 6 03_02
#> 7 01_03
#> 8 02_03
#> 9 03_03
Created on 2022-02-25 by the reprex package (v2.0.0)
This could be one way you could get to your solution.
tile = 7
col <- tile%%3
row <- 3 - floor(tile/3)
if (tile%%3 == 0) {
col = 3
row = row + 1
}
sprintf("%02d_%02d", col, row)
#> [1] "01_01"
Created on 2022-02-28 by the reprex package (v2.0.0)