# NOT RUN {
# prepare
index1 = as.integer(
c( 6, 6, 7, 7, 8, 8,
6, 6, 7, 7, 8, 8,
5, 5, 1, 2, 9, 9,
5, 5, 4, 3, 9, 9,
12,12,11,11,10,10,
12,12,11,11,10,10))
dataFrame1 = data.frame(a = 1:12 * 10, b = 2^(12:1))
grid1 = GridTopology(c(1,1), c(2,2), c(6,6))
spatialGrid1 = SpatialGrid(grid1, CRS("+proj=longlat +datum=WGS84"))
#- - create object by function SpatialPolygridDataFrame - -#
SPolygridDF1 = SpatialPolygridDataFrame(
grid = grid1,
data = dataFrame1,
index = index1,
proj4string = CRS("+proj=longlat +datum=WGS84"))
#- - - - coerce from SpatialGridDataFrame - - - - - - - -#
spatialGridDataFrame1 = SpatialGridDataFrame(
grid = spatialGrid1, data = dataFrame1[index1,])
# coerce, each cell keeps individual values
s = as(spatialGridDataFrame1, "SpatialPolygridDataFrame")
# coerce back (name of coordinates may change)
#spatialGridDataFrame2 = as(SPolygridDF2, "SpatialGridDataFrame")
spatialGridDataFrame2 = as(s, "SpatialGridDataFrame")
#- - - - coerce from SpatialPointsDataFrame - - - - - - - #
# from irregular points, grid is created, cells are assigned values of nearest point
spatialPoints1 = SpatialPoints(coordinates(SPolygridDF1) + runif(24))
spatialPointsDataFrame1 = SpatialPointsDataFrame(
coords = spatialPoints1, data = dataFrame1)
# }
# NOT RUN {
## takes some seconds
SPolygridDF3 = as(spatialPointsDataFrame1, "SpatialPolygridDataFrame")
# from regular points: one cell per point
spatialPointsDataFrame2 = as(spatialGridDataFrame1, "SpatialPointsDataFrame")
SPolygridDF4 = as(spatialPointsDataFrame2, "SpatialPolygridDataFrame")
# - - - - - - - - - - - - - coordinates - - - - - - - - - - - - #
# compare irregular input points and centroids of output
plot(spatialPoints1)
points(coordinates(SPolygridDF3))
# - - - - - - - - - - - - - proj4string - - - - - - - - - - - - #
# only to retrieve projection attributes
#proj4string(SPolygridDF2)
proj4string(s)
proj4string(SPolygridDF3)
# - - - - - - - - - - - - - proj4string - - - - - - - - - - - - #
bbox(SPolygridDF3)
# - - - - - - - - - - - spplot - - - - - - - - - - - - - - - - #
## takes some time
# plot map with original points
spplot(SPolygridDF3, zcol = "a",
sp.layout = list(list("sp.points", spatialPoints1, col = 8)))
# plot map with centres of polygrid cells
spplot(SPolygridDF4, zcol = "a", col.regions = rainbow(16),
sp.layout = list("sp.points", spatialPointsDataFrame2@coords, col = 8))
# - - - - - - - - - - - - cbind - - - - - - - - - - - - - - - - #
#SPolygridDF5 = cbind(SPolygridDF2, SPolygridDF4)
SPolygridDF5 = cbind(s, SPolygridDF4)
# }
Run the code above in your browser using DataLab