I want to find the values of row and column present in the cell. I have written a code to fetch value as given below:
x<-raster()
values(x)<-1:ncell(x)
class : RasterLayer
dimensions : 180, 360, 64800 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : layer
values : 1, 64800 (min, max)
There is another file as given below which contains lat and long information. So, we have to find these points in the raster object and then store values in the format given at last.
long<-c(106.61291,-81.97224,-84.4277,-97.66631,-72.68604,-73.1247,-119.05662,
-86.75413,-108.5377,-86.67737,-116.22231,-71.00956,-91.15146,-73.1516)
lat<-c(35.04333,33.37378,33.64073,30.19743,41.93887,41.1642,35.43291,33.56243,
45.8037,36.12632,43.56582,42.36561,30.53236,44.4707)
xy<-data.frame(long,lat)
coordinates(xy)=c('long','lat')
proj4string(xy)=proj4string(x)
points<-cellFromXY(x,xy)
so , points contains all the cell number where xy is present in raster object x.
As given below:
[1] 19727 20259 20256 21323 17388 17387 19501 20254 15912 19174 16624 17029 21329 16307
I want to find the value of row and column for every cell given in points as I have done below: For example taking cell number 19727 from "points"
rowColFromCell(x,19727)
row col
[1,] 55 287
getValues(x,row=55)[287]
value:16109
And I want values of 9*9 grid where the given row and column i.e 55,287 is in centre.
And I want to do the same for all the cells given in the points.
Final output should look like this:
long lat cell_1 cell_2 cell_3 cell _4 ......... cell_9
106.61291 35.04333 16109 ... .... .... ....
Thanks in advance!