Learn R Programming

sensors4plumes (version 0.9.3)

points2polygrid: Turn points (and data) into a SpatialPolygridDataFrame

Description

points2polygrid determines a fine grid, such that each point is represented by a set of grid cells (nearest neighbourhood); to each grid cell it assigns the values of the nearest point. It may also assign cells of a given grid to the nearest neighbour points.

Usage

points2polygrid(points, grid, index, data, 
tolerance = signif(mean(diff(t(bbox(points))))/1000, digits = 1))

Arguments

points

coordinates of the original points: data.frame, matrix with coordinates in columns 1 and 2, or SpatialPoints

grid

target grid: GridTopology or SpatialGrid

index

target index indicating for each grid cell the associated data: integer, length must fit number of cells in grid (order: top left to right, then to bottom)

data

data associated with points, to be assigned to grid cells: data.frame or vector of same length as points

tolerance

lower limit of the resolution of the generated grid, default generates about 1000 x 1000 grid cells; if points lie on a regular grid, the resulting resolution is automatically adapted

Value

A SpatialPolygridDataFrame. If no data is given, the returned data consists of one column 'Index' with the indices of the associated points. proj4string of points is kept if available, else the one of grid is taken.

Details

It is sufficient to provide points, they can be used to generate a grid and associate the cells to the closest points. The generated grid covers all points; resolution is chosen such that there are cell boundaries between the x-coordinates and y-coordinates of all points respectively. If points lie on a regular grid, resolution is chosen to fit this grid. A grid can also be provided which saves computational effort.

If no index is given, grid cells are assigned to the nearest point (based on get.knnx). If index, grid and points are given, points are ignored. If points are at the same location or so close that they are merged, the associated data of only one point is used.

Examples

Run this code
# NOT RUN {
# prepare
data(SPolygridDF)
regularPoints = coordinates(SPolygridDF)
irregularPoints = regularPoints + runif(12, 0.1)

# grid for regular points
newSPolygrid_reg = points2polygrid(points = regularPoints, 
                                   data = data.frame(a = 12:1 * 5))
spplot(newSPolygrid_reg, 
       sp.layout = list(list("sp.points", regularPoints, col = 3)))
       
# generate grid for irregular points with given resolution
newSPolygrid_irreg = points2polygrid(points = irregularPoints, tolerance = 0.5)
spplot(newSPolygrid_irreg, sp.layout = list("sp.points", irregularPoints, col = 3))

# with given grid
newSPolygrid_grid = points2polygrid(points = irregularPoints,
  grid = GridTopology(c(2,2), c(1,1), c(10,10)))
spplot(newSPolygrid_grid, sp.layout = list("sp.points", irregularPoints, col = 3))

# }

Run the code above in your browser using DataLab