1

I have a SpatialPointsDataFrame and a raster for a region. I would like to retain only those points that occur within the elevational limits of the raster. Unfortunately, no points are being filtered. I still see points that occur outside of the 'masked extent' of the raster, although they occur within the overall extent of the raster.

Raster below:

> newelev
 class       : RasterLayer 
 dimensions  : 642, 382, 245244  (nrow, ncol, ncell)
 resolution  : 1000, 1000  (x, y)
 extent      : 461951, 843951, 892583.3, 1534583  (xmin, xmax, ymin, ymax)
 coord. ref. : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
 data source : in memory
 names       : alt 
 values      : 1400, 2585.524  (min, max)

Below is my SpatialPointsDataFrame

> pr
class       : SpatialPointsDataFrame 
features    : 262 
extent      : 529866.4, 798884.4, 945186.2, 1498039  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables   : 2
names       :   X, pr 
min values  :   1,         1 
max values  : 899,         1 

I use raster::intersect to obtain only points that occur within the elevational limits of the above raster.

> trial <- raster::intersect(pr,newelev)
> trial
class       : SpatialPointsDataFrame 
features    : 262 
extent      : 529866.4, 798884.4, 945186.2, 1498039  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables   : 2
names       :   X, pr 
min values  :   1,         1 
max values  : 899,         1 

Below, you can see the plot, where points occur outside the masked extent of the elevation raster. Any suggestions?

enter image description here

1

intersect uses the extent of the RasterLayer to select the points. You can get what you are after like this:

v <- extract(newelev, pr)
p <- pr[!is.na(v), ]
  • Thanks @RobertH. That works. I realized that the intersect function simply ended up using the extent and was wondering if it excluded locations where values == NA. – Vijay Ramesh Dec 3 '17 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.