Create Grid in R for kriging in gstat

前端 未结 3 1031
青春惊慌失措
青春惊慌失措 2020-12-05 12:26
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

3条回答
  •  鱼传尺愫
    2020-12-05 13:02

    @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.

    1. Make a rectangular grid over your SpatialPolygonsDataFrame

    grd <- makegrid(spdf, n = 100)
    colnames(grd) <- c('x','y')
    

    2. Convert the grid to 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, ]
    

    3. Visualize your clipped grid, which can be used for kriging.

    # 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))
    

提交回复
热议问题