A subset of the SpatialPolygonsDataFrame

I have SpatialPolygonsDataFrameone that I created by reading in a shapefile using readOGRin a package rgdal. I am trying to use it to create a selection grid, using spsamplein a package spthat will be used to interpolate from the survey data collected in the area. However, the SpatialPointsDataFrame covers a much larger area than the survey, and as a result, the interpolation predicts values ​​that are far from where any surveys were conducted. The survey data and shapefile were designed using the same proj4 line.

I would like to multiply the SpatialPolygonsDataFrame using the coordinates set by the survey stations, but I'm not sure where the corresponding values ​​are stored in the object.

I am afraid that I cannot provide the relevant data because the shapefile is not hosted on the Internet. However, I will get the code from Paul Hiemstra's answer 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 polls were conducted in this area:

bbox(surveys)
    >         min      max
    >r1     4.000    7.000
    >r2    51.000   53.000

How can I crop this area with a SpatialPolygonsDataFrame?


EDIT: This question seems to answer me. I apologize for not looking quite a bit (although the comments gave me some idea of ​​where to go with rgeos). However gIntersection, R ... crashes.

+3
source share
1 answer

Depending on the size of the polygons, you can do something like

range = cbind(c(4,7), c(51,53))
centroids <- coordinates(spdf)

spdf.subset <- spdf[centroids[,1] > range[1,1] &
                    centroids[,1] < range[2,1] &
                    centroids[,2] > range[1,2] &
                    centroids[,2] < range[2,2],]
0
source

All Articles