
R version 4.4.2 (2024-10-31) -- "Pile of Leaves"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> options("rgdal_show_exportToProj4_warnings"="none")
> library(sp)
> data(meuse.grid)
> x = meuse.grid
> coordinates(x) = c("x", "y")
> gridded(x) = TRUE
> gridded(x)
[1] TRUE
> image(x["dist"])
> 
> fullgrid(x) = TRUE
> fullgrid(x)
[1] TRUE
> # summary(x)
> gridparameters(x)
  cellcentre.offset cellsize cells.dim
x            178460       40        78
y            329620       40       104
> class(as(x, "matrix"))
[1] "matrix" "array" 
Warning message:
In as.matrix.SpatialGridDataFrame(from) :
  as.matrix.SpatialGridDataFrame uses first column;
 use subset or [] for other columns
> 
> fullgrid(x) = FALSE
> fullgrid(x)
[1] FALSE
> # summary(x)
> class(as(x, "matrix"))
[1] "matrix" "array" 
Warning message:
In as.matrix.SpatialGridDataFrame(x, ...) :
  as.matrix.SpatialGridDataFrame uses first column;
 use subset or [] for other columns
> gridparameters(x)
  cellcentre.offset cellsize cells.dim
x            178460       40        78
y            329620       40       104
> 
> df = data.frame(z = c(1:6,NA,8,9), 
+ 	xc = c(1,1,1,2,2,2,3,3,3), 
+ 	yc = c(rep(c(0, 1.5, 3),3)))
> 
> coordinates(df) = ~xc+yc
> gridded(df) = TRUE
> gridparameters(df)
   cellcentre.offset cellsize cells.dim
xc                 1      1.0         3
yc                 0      1.5         3
> as(df, "matrix")
     [,1] [,2] [,3]
[1,]    3    2    1
[2,]    6    5    4
[3,]    9    8   NA
> 
> # get grid topology:
> grd = points2grid(as(df, "SpatialPoints"), 1e-31) 
> grd
                  xc  yc
cellcentre.offset  1 0.0
cellsize           1 1.5
cells.dim          3 3.0
> getGridIndex(coordinates(df), grd)
[1] 7 4 1 8 5 2 9 6 3
> 
> g = SpatialGrid(grid = grd)
> fullgrid(g)
[1] TRUE
> fullgrid(g) = TRUE
> class(g)
[1] "SpatialGrid"
attr(,"package")
[1] "sp"
> # the next one has to fail:
> fullgrid(g) <- FALSE
> class(g)
[1] "SpatialPixels"
attr(,"package")
[1] "sp"
> 
> # summary(df)
> image(df["z"])
> as.image.SpatialGridDataFrame(df)
$x
[1] 1 2 3

$y
[1] 0.0 1.5 3.0

$z
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]   NA    8    9

> as.image.SpatialGridDataFrame(df["z"])
$x
[1] 1 2 3

$y
[1] 0.0 1.5 3.0

$z
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]   NA    8    9

> coordinatevalues(getGridTopology(df))
$xc
[1] 1 2 3

$yc
[1] 3.0 1.5 0.0

> 
> as.data.frame(df)
   z xc  yc
1  1  1 0.0
2  2  1 1.5
3  3  1 3.0
4  4  2 0.0
5  5  2 1.5
6  6  2 3.0
7 NA  3 0.0
8  8  3 1.5
9  9  3 3.0
> 
> fullgrid(df) = TRUE
> as.data.frame(df)
  z xc  yc
1 3  1 3.0
2 6  2 3.0
3 9  3 3.0
4 2  1 1.5
5 5  2 1.5
6 8  3 1.5
7 1  1 0.0
8 4  2 0.0
> 
> fullgrid(df) = FALSE  
> as.data.frame(df)
  z xc  yc
1 3  1 3.0
2 6  2 3.0
3 9  3 3.0
4 2  1 1.5
5 5  2 1.5
6 8  3 1.5
7 1  1 0.0
8 4  2 0.0
> 
> fullgrid(df) = TRUE
> fullgrid(df) = FALSE
> as.data.frame(df)
  z xc  yc
1 3  1 3.0
2 6  2 3.0
3 9  3 3.0
4 2  1 1.5
5 5  2 1.5
6 8  3 1.5
7 1  1 0.0
8 4  2 0.0
> 
> df = as.data.frame(df)
> set.seed(133331)
> df$xc = df$xc + rep(.001*rnorm(3), 3)[1:8]
> df.sp = SpatialPoints(df[c("xc", "yc")])
> df.grd = SpatialPixels(df.sp, tolerance = .01)
> df.grd[1:4,,tolerance=.01,drop=TRUE]
Object of class SpatialPixels
Grid topology:
   cellcentre.offset  cellsize cells.dim
xc          1.000433 0.9995197         3
yc          1.500000 1.5000000         2
SpatialPoints:
        xc  yc
1 1.000433 3.0
2 1.997926 3.0
3 2.999472 3.0
4 1.000433 1.5
Coordinate Reference System (CRS) arguments: NA 
> df.grd[1:4,,tolerance=.01]
Object of class SpatialPixels
Grid topology:
   cellcentre.offset  cellsize cells.dim
xc          1.000433 0.9995197         3
yc          0.000000 1.5000000         3
SpatialPoints:
        xc  yc
1 1.000433 3.0
2 1.997926 3.0
3 2.999472 3.0
4 1.000433 1.5
Coordinate Reference System (CRS) arguments: NA 
> 
> proc.time()
   user  system elapsed 
  0.566   1.398   0.459 
