简体   繁体   中英

Confining an IDW interpolation to a polygon in R{Spatstat}

I'm a fairly new R user working with spatial data for places embedded within US metro areas. I'm working on generating IDW maps for a number of variables and am having trouble defining the boundaries of the IDW. I would like to interpolate data only within the confines of the metro area boundary, while my current approach fills the entire plot.

I begin by creating a ppp object from a dataframe as such:

x.points<-SpatialPointsDataFrame(coords =x.data[,c("LON10","LAT10")],
  data=x.data[,c("v1","v2")],
  proj4string=CRS("+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs"))

x.proj<-spTransform(x.points,CRSobj = CRS("+proj=aea
  +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 
  +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))    

x.ppp<-as.ppp(x.proj)

Projecting this ppp object using plot(x.ppp) displays a plot for each of the variables ("v1" and "v2" in the code above). However, the plot fills the entire rectangular frame. I limit this frame when plotting my ppp object by assigning an owin object to it, as follows:

y.spdf<-spTransform(y.spdf,CRSobj = CRS("+proj=aea 
  +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 
  +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))

y.owin<-as.owin(y.spdf)

x.ppp<-x.ppp[y.owin]

Plotting this using plot(x.ppp) now gives me a plot of my data points correctly framed within the polygon of their metro area. While this is a good start, I want to continue to pass this spatial frame onto an IDW map. Consulting the spatstat handbook leads me to believe that I could pass my owin object as an as.mask argument, but whenever I do, I get an error. Example code follows:

idw<-idw(x.ppp, power=4, as.mask(y.owin))

This gives me the error "Error in switch(at, pixels = { : EXPR must be a length 1 vector". Using at="points" does not work, as I instead get a resulting dataframe object rather than an imlist . If I leave out the as.mask argument I get a correct idw, but it interpolates beyond the metro area boundaries that I was previously able to set using the owin object.

I figure this is a fairly common task, but consuting the spatstat documentation left me at a bit of a loss. The as.mask argument seems like it would work, and I'm not sure where I'm going wrong. Any input or advice on how to solve this problem, or a source to which I could turn to try and better understand the issue would be a great help. Thanks!

I see how this is a bit confusing. Basically idw calculates the result in the entire rectangular box containing the data (the frame of the owin ). This is the result you get if you do rslt <- idw(x.ppp) . To restrict the result to the polygonal window we need to subset the resulting image by this window (and set drop=FALSE to get the resulting irregular image rather than the pixel values inside the polygonal region). Explained in code:

Simple case (one numeric mark value)

library(spatstat)
# Fake data with one numeric mark
X <- humberside
marks(X) <- runif(npoints(X))
# Window is a polygonal region:
W <- Window(X)
W
# IDW in the frame of W
rslt <- idw(X)
# Subset of IDW inside W
rslt <- rslt[W, drop = FALSE]
plot(rslt)

一个变量的IDW

Multivariate case (two or more numeric mark values)

library(spatstat)
# Fake data with more numeric marks
X <- humberside
marks(X) <- cbind(x = runif(npoints(X)), y = runif(npoints(X)))
# Window is a polygonal region:
W <- Window(X)
W
# IDW in the frame of W for each mark variable (imlist object)
rslt <- idw(X)
# Subset of IDW inside W for each mark variable (imlist object)
rslt <- solapply(rslt, "[.im", W, drop = FALSE)
plot(rslt)

IDW两个变量

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM