如何创建空间点网格

时间:2017-01-22 04:21:52

标签: r geospatial r-grid desctools

library(reshape2)
library(data.table)
library(dplyr)
library(magrittr)
library(ggplot2)
library(scales)
library(gstat)
library(DescTools)
library(sp)



#I want a colorado grid#
data("colorado.grid")

#making cordinates into spatial points dataframe#
coordinates(Gold_tracer_kri) <- ~ long_orig + lat_orig
#attempt at kriging but no grid#
lzn.kriged <- krige(Au ~ 1, Gold_tracer_kri, colorado.grid, model=lzn.fit)

lzn.kriged %>% as.data.frame %>%
ggplot(aes(long_orig=long_orig, lat_orig=lat_orig)) +     geom_tile(aes(fill=var1.pred)) + coord_equal() +
scale_fill_gradient(low = "yellow", high="red") +
scale_x_continuous(labels=comma) + scale_y_continuous(labels=comma) +
theme_bw()

加载空间域以在

上插值
data("meuse.grid")

我正在尝试在R中使用kriging方法,但由于无法为我的数据找到网格而陷入困境。我的数据占据了美国所有的科罗拉多州,我希望能够获得一个网格来中断我的数据。与我正在关注的示例中使用的meuse.grid类似。

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:15)

使用sp

的方法

请参阅下面的sf版本!

您可以使用sp::makegrid

创建网格
library(sp)
library(rgdal)
library(raster)

# load some spatial data. Administrative Boundary
us <- getData('GADM', country = 'US', level = 1)
us$NAME_1
colorado <- us[us$NAME_1 == "Colorado",]


# check the CRS to know which map units are used
proj4string(colorado)
# "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

# Create a grid of points within the bbox of the SpatialPolygonsDataFrame 
# colorado with decimal degrees as map units
grid <- makegrid(colorado, cellsize = 0.1) # cellsize in map units!

# grid is a data.frame. To change it to a spatial data set we have to
grid <- SpatialPoints(grid, proj4string = CRS(proj4string(colorado)))

plot(colorado)
plot(grid, pch = ".", add = T)

Example with Colorado

奥地利的另一个例子(GADM代码'AUT')。 Example with Austria

网格仅指向多边形:

要仅提取多边形内的点,请使用`[`根据以下位置对点进行子集化:

grid <- grid[colorado, ]

使用sf

的方法
library(sf)
library(raster)
library(ggplot2)

# load some spatial data. Administrative Boundary
aut <- getData('GADM', country = 'aut', level = 0)
aut <- st_as_sf(aut)
# ggplot() + 
  # geom_sf(data = aut)

grid <- aut %>% 
  st_make_grid(cellsize = 0.1, what = "centers") %>% # grid of points
  st_intersection(aut)                               # only within the polygon

# ggplot() + 
  # geom_sf(data = aut) + 
  # geom_sf(data = grid)