2

I have rasterized a SpatialPointsDataFrame object, but the created RasterLayer returns NA.

> #create spatial attributes
> xmin <- 624748.7
> xmax <- 669548.7
> ymin <- 640557.8
> ymax <-  712757.8 
> goef.col <- 448.00
> geof.row <- 722.00
> spa.res <- 100.00
> 
> #Points
> points <- frame.val
> points
          X        Y   Z DATE
1  100.1855 6.448029 3.9  2.5
2  100.3491 6.348555 3.9 18.0
3  100.3125 6.318388 3.9  4.0
4  100.3852 6.285333 3.9  3.0
5  100.3587 6.242500 3.9  6.0
6  100.3221 6.238888 3.9  3.0
7  100.2445 6.217528 3.9  2.0
8  100.3990 6.149028 3.9  1.0
9  100.4592 6.175778 3.9  1.0
10 100.4010 6.085000 3.9  1.0
11 100.4447 5.973361 3.9  1.0
12 100.4113 6.022528 3.9  0.0
13 100.3281 6.046112 3.9  1.0
14 100.3299 6.097472 3.9  1.0
15 100.3648 5.850138 3.9  0.0
> 
> coordinates(points) <- c("X", "Y")
> points
class       : SpatialPointsDataFrame 
features    : 15 
extent      : 100.1855, 100.4592, 5.850138, 6.448029  (xmin, xmax, ymin, ymax)
crs         : NA 
variables   : 2
names       :   Z, DATE 
min values  : 3.9,    0 
max values  : 3.9,   18 
> 
> # An empty SpatialGrid 
> basin.grid <- SpatialGrid(GridTopology(c(X = xmin, Y = ymin), 
+                                        c(spa.res, spa.res), 
+                                        c(goef.col, geof.row))) 
> basin.grid <- SpatialPoints(basin.grid)  
> gridded(basin.grid) <- TRUE  
> 
> # The grid is converted into RasterLayer
> raster.georef <- raster(basin.grid)  
> ext.raster <- points[2]  
> 
> point.raster <- rasterize(ext.raster, raster.georef, 'DATE')  
> point.raster
class      : RasterLayer 
dimensions : 722, 448, 323456  (nrow, ncol, ncell)
resolution : 100, 100  (x, y)
extent     : 624698.7, 669498.7, 640507.8, 712707.8  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : layer 
values     : NA, NA  (min, max)

Any way I can solve this to get real values in the created RasterLayer?

1 Answer 1

1

ext.raster is:

> ext.raster
class       : SpatialPointsDataFrame 
features    : 15 
extent      : 100.1855, 100.4592, 5.850138, 6.448029  (xmin, xmax, ymin, ymax)
crs         : NA 
variables   : 1
names       : DATE 
min values  :    0 
max values  :   18 

and raster.georef is:

> raster.georef
class      : RasterLayer 
dimensions : 722, 448, 323456  (nrow, ncol, ncell)
resolution : 100, 100  (x, y)
extent     : 624698.7, 669498.7, 640507.8, 712707.8  (xmin, xmax, ymin, ymax)
crs        : NA 

and if you look at the extents, you can see that they don't overlap. So there's no way you can rasterize one onto the other.

I'd bet these are actually different coordinate systems and you need to transform one of them to the other.

If you need help doing that, then start another question and we'll attack it there.

1
  • Thanks, Spacedman, your suggestion to transform the coordinates of one of them to another has really helped and now both objects are in the projected coordinate reference system in unit meters. I can get my results now in the required way.
    – Abdul
    Commented Nov 8, 2021 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.