Use rasterToPoints
- for example:
> r = raster(matrix(1:12,3,4))
> rasterToPoints(r)
x y layer
[1,] 0.125 0.8333333 1
[2,] 0.375 0.8333333 4
[3,] 0.625 0.8333333 7
[4,] 0.875 0.8333333 10
[5,] 0.125 0.5000000 2
[6,] 0.375 0.5000000 5
[7,] 0.625 0.5000000 8
[8,] 0.875 0.5000000 11
[9,] 0.125 0.1666667 3
[10,] 0.375 0.1666667 6
[11,] 0.625 0.1666667 9
[12,] 0.875 0.1666667 12
you'll end up with 86 million rows, although cells with NA aren't returned.
you can use the raster::xyFromCell
function to get a matrix of coordinates from a vector of cell numbers, which you can cbind onto the extract (as the xyFromCell result is in exactly the same order as the vector provided)
I've used sf
but of course this works with readOGR etc.;
require(raster)
require(sf)
# make 2 dummy rasters for a dummy stack
r <- raster(xmn=0,xmx=10,ymn=0,ymx=10,res=1)
r[] <- sample(1:4,100,replace=T)
r2 <- raster(xmn=0,xmx=10,ymn=0,ymx=10,res=1)
r2[] <- sample(8:10,100,replace=T)
s <- stack(r,r2)
# make a triangle simple feature
s2 <- rbind(c(2.2,2.2), c(3.3,8.5), c(5.3,0.8), c(2.2,2.2))
s2.sf <-st_sfc(st_polygon(list(s2)))
s2.pol = st_sf(ID = "tr", s2.sf)
# extract values into data frame
ex.df <- as.data.frame(extract(s,s2.pol,cellnumbers=T))
# create coordinate columns using xyFromCell
ex.df.coords <- cbind(ex.df, xyFromCell(s,ex.df[,1]))
Best Answer
Yes, it is possible to extract a shapefile but it is very difficult to handle it because you need a lot a memory. It is preferable to "burn" these coordinates in two raster (one for each coordinates values; x and y).
However, I tried to produce the "shapefile" (in fact an "event theme" from a text file) with next approach. First, I selected an arbitrary area (at Brazil) with your exact cell number (2105x1388 cells) and helped with my two plugins (as it can be observed at the next image).
With the next code ran at the Python Console of QGIS, I got a text file to be loaded in QGIS.
At the next image, you can observe a point layer (cell centres in lon/lat) created as delimited text file but, it is very difficult to handle. It is possible but inconvenient.
Editing Note:
Next code add the raster values to the text file: