lat long
7.16 124.21
8.6 123.35
8.43 124.28
8.15 125.08
Consider these coordinates, these coordinates correspond to weather stations th
@yzw and @Edzer bring up good points for creating a regular rectangular grid, but sometimes, there is the need to create an irregular grid over a defined polygon, usually for kriging.
This is a sparsely documented topic. One good answer can be found here. I expand on it with code below:
Consider the the built in meuse dataset. meuse.grid is an irregularly shaped grid. How do we make an grid like meuse.grid for our unique study area?
library(sp)
data(meuse.grid)
ggplot(data = meuse.grid)+geom_point(aes(x=x, y=y))
Imagine an irregularly shaped SpatialPolygon or SpatialPolygonsDataFrame, called spdf. You first build a regular rectangular grid over it, then subset the points in that regular grid by the irregularly-shaped polygon.
SpatialPolygonsDataFramegrd <- makegrid(spdf, n = 100)
colnames(grd) <- c('x','y')
SpatialPoints and subset these points by the polygon.grd_pts <- SpatialPoints(coords = grd,
proj4string=CRS(proj4string(spdf)))
# find all points in `grd_pts` that fall within `spdf`
grd_pts_in <- grd_pts[spdf, ]
# transform grd_pts_in back into a data frame
gdf <- as.data.frame(coordinates(grd_pts_in))
ggplot(gdf) +
geom_point(aes(x=x,y=y))