如何创建空间点的网格 [英] How to create a grid of spatial points

查看:204
本文介绍了如何创建空间点的网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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中使用克里金法,但由于无法为我的数据找到网格而陷入困境.我的数据占用了美国科罗拉多州的所有州,我希望能够获得一个网格来对我的数据进行交织.类似于我在下面的示例中使用的meuse.grid.

I am trying to use kriging methods in R but i am stuck due to not being able to find a grid for my data. My data takes up all the US state of Colorado and i would like to be able to get a grid to interlope my data over. Similar to the meuse.grid that is used in the example i am following.

任何帮助将不胜感激

推荐答案

使用sp

的方法

请参见下面的sf版本!

Approach using sp

see sf version below!

您可以使用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)

另一个例子是奥地利(GADM代码'AUT').

And another example with Austria (GADM code 'AUT').

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

To extract only the points within the polygon, use `[` to subset the points based on the location like this:

grid <- grid[colorado, ]


使用sf

的方法


Approach using 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)

这篇关于如何创建空间点的网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆