[英]Finding nearest grid value in spatial dataframe in R
I am learning about spatial dataframes. 我正在学习空间数据框。 I have created a simple example that uses inverse distance weighting (IDW) to interpolate values across a grid, based on an initial set of points.
我创建了一个简单的示例,该示例基于一组初始点,使用反距离权重(IDW)在整个网格内插值。 I would now like to obtain the interpolated value for each of the initial points.
我现在想获得每个初始点的插值。 I would presume that this should be relatively easy, but struggle to find an elegant way of doing so.
我认为这应该相对容易一些,但是很难找到一种优雅的方式。 Put differently, I would like to find the nearest grid value in "geog2.idw" for each of the 40 points (x,y).
换句话说,我想在“ geog2.idw”中为40个点(x,y)中的每一个找到最接近的网格值。
Code 码
#adjusted from example created by Brian J. Knaus
#packages
library(gstat)
require(gstat)
require(lattice)
require(raster)
require(sp)
##### ##### ##### ##### #####
# Generate data.
set.seed(1)
x <- runif(40, -123, -110)
set.seed(2)
y <- runif(40, 40, 48)
set.seed(3)
z <- runif(40, 0, 1)
# Create spatial data.frame.
geog2 <- data.frame(x,y,z)
# Assigning coordinates results in spdataframe.
coordinates(geog2) = ~x+y
#create grid
pixels <- 100
geog.grd <- expand.grid(x=seq(floor(min(coordinates(geog2)[,1])),
ceiling(max(coordinates(geog2)[,1])),
length.out=pixels),
y=seq(floor(min(coordinates(geog2)[,2])),
ceiling(max(coordinates(geog2)[,2])),
length.out=pixels))
#
grd.pts <- SpatialPixels(SpatialPoints((geog.grd)))
grd <- as(grd.pts, "SpatialGrid")
##### ##### ##### ##### #####
# IDW interpolation.
geog2.idw <- idw(z ~ 1, geog2, grd, idp=6)
spplot(geog2.idw["var1.pred"])
##### ##### ##### ##### #####
# Image.
par(mar=c(2,2,1,1))
#
image(geog2.idw, xlim=c(-125, -110), ylim=c(40, 47))
#
contour(geog2.idw, add=T, lwd=0.5)
#
map("state", lwd=1, add=T)
map("county", lty=3, lwd=0.5, add=T)
#
points(x, y, pch=21, bg=c(1:4))
#######
You can turn x
and y
into SpatialPoints
: SpatialPoints(cbind(x,y))
and use the function over
: 您可以将
x
和y
转换为SpatialPoints
: SpatialPoints(cbind(x,y))
并在以下函数over
使用:
pts <- SpatialPoints(cbind(x, y))
pts.idw <- over(pts, geog2.idw["var1.pred"])
# Writing extracted values on the map:
image(geog2.idw, xlim=c(-125, -110), ylim=c(40, 47))
contour(geog2.idw, add=T, lwd=0.5)
map("state", lwd=1, add=T)
map("county", lty=3, lwd=0.5, add=T)
text(pts, round(pts.idw[,"var1.pred"],4), col = "blue")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.