I have a SpatialPolygonsDataFrame
that I created by reading in a shapefile using readOGR
in the rgdal
package. I am attempting to use it to generate a sampling grid using spsample
in the sp
package to be used for interpolation from survey data collected in the area. However, the SpatialPointsDataFrame encompasses a much larger area than the survey, and as a result, the interpolation is predicting values far away from where any surveys were conducted. The survey data and the shapefile were both projected using the same proj4string.
I would like to subset the SpatialPolygonsDataFrame using the coordinates set by the survey stations, but I'm not sure where the relevant values are stored in the object.
I'm afraid that I can't provide the relevant data as the shapefile is not hosted online. However, I will borrow some code from Paul Hiemstra's response to this post for the Netherlands:
library(ggplot2)
library(sp)
library(automap)
library(rgdal)
#get the spatial data for the Netherlands
con <- url("http://gadm.org/data/rda/NLD_adm0.RData")
print(load(con))
close(con)
class(gadm)
bbox(gadm)
> min max
>r1 3.360782 7.29271
>r2 50.755165 53.55458
Let's say that the surveys were conducted in this area:
bbox(surveys)
> min max
>r1 4.000 7.000
>r2 51.000 53.000
How can I crop out that area of the SpatialPolygonsDataFrame?
EDIT: This question appears to answer mine. Apologies for not searching hard enough (although the comments did give me some sense of where to turn with rgeos). However, gIntersection
causes R to crash...