0

I'm trying to ''cut'' a spatial lines data frame object by a raster grid using R,

raster::intersect(SLDF, grid)

However, when I run this code I get,

Error in as.vector(x) : no method for coercing this S4 class to a vector

Here is my session info,

> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.4 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
 [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] bindrcpp_0.2.2    viridis_0.5.1     viridisLite_0.3.0 flexsurv_1.1     
 [5] survival_2.42-3   raster_2.6-7      rgeos_0.3-27      rgdal_1.3-1      
 [9] sp_1.3-1          magrittr_1.5      dplyr_0.7.5      

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.17       pillar_1.2.3       compiler_3.4.4     RColorBrewer_1.1-2
 [5] plyr_1.8.4         bindr_0.1.1        tibble_1.4.2       gtable_0.2.0      
 [9] lattice_0.20-35    pkgconfig_2.0.1    rlang_0.2.1        Matrix_1.2-14     
[13] mvtnorm_1.0-8      gridExtra_2.3      grid_3.4.4         tidyselect_0.2.4  
[17] deSolve_1.21       mstate_0.2.11      glue_1.2.0         R6_2.2.2          
[21] purrr_0.2.5        ggplot2_2.2.1      scales_0.5.0       splines_3.4.4     
[25] assertthat_0.2.0   colorspace_1.3-2   quadprog_1.5-5     muhaz_1.2.6       
[29] lazyeval_0.2.1     munsell_0.5.0     

Another strange thing, if I type raster::intersect into the console I get

> raster::intersect
standardGeneric for "intersect" defined from package "base"

function (x, y) 
standardGeneric("intersect")
<environment: 0x5cc78f8>
Methods may be defined for arguments: x, y
Use  showMethods("intersect")  for currently available ones.
3
  • To work through the rgeos functions that raster::intersect provides a wrapper for, take a look at my answer here: gis.stackexchange.com/questions/276928/… Commented Jun 28, 2018 at 20:05
  • Are you trying to get the cells that your lines pass through? Then rasterize your lines and then use that as a mask.
    – Spacedman
    Commented Jun 29, 2018 at 13:10
  • The "strange" result from typing raster::intersect correctly shows that intersect is a generic function defined in the base package. You can do showMethods("intersect") to see implementations. Commented Jun 29, 2018 at 15:13

2 Answers 2

2

I think you are looking for crop:

raster::crop(SLDF, grid)
1

There is no method in the raster package for intersect for SpatialLinesDataFrame and raster in that order.

help(raster::intersect) lists the following method, and none of the signatures are SpatialLinesDataFrame, Raster:

 ## S4 method for signature 'Extent,ANY'
 intersect(x, y)

 ## S4 method for signature 'Raster,ANY'
 intersect(x, y)

 ## S4 method for signature 'SpatialPoints,ANY'
 intersect(x, y)

 ## S4 method for signature 'SpatialPolygons,SpatialPolygons'
 intersect(x, y)

 ## S4 method for signature 'SpatialPolygons,SpatialLines'
 intersect(x, y)

 ## S4 method for signature 'SpatialPolygons,SpatialPoints'
 intersect(x, y)

 ## S4 method for signature 'SpatialLines,SpatialPolygons'
 intersect(x, y)

 ## S4 method for signature 'SpatialLines,SpatialLines'
 intersect(x, y)
1
  • Just converted the raster to a SpatialPolygons object and that did it!
    – user13317
    Commented Jul 10, 2018 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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