title: extract raster pixel value as well as pixel xy coordinates using a SpatialLine object in R
body: <p>I have a Spatialline that I converted from a polygon shapefile (digitized manually based on features in "imagebrick" - this means that spatially the "polyline" and "imagebrick" are overlapped as I wanted)</p>
  
  <pre><code>polyline &lt;- as(shapefiles_data[1,],"SpatialLines")
  
  &gt; polyline
  class       : SpatialLines 
  features    : 1 
  extent      : 357714.3, 357719, 4076030, 4076035  (xmin, xmax, ymin, ymax)
  coord. ref. : +proj=utm +zone=11 +datum=NAD27 +units=m +no_defs +ellps=clrk66 +nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat 
  </code></pre>
  
  <p>And a Rasterbrick</p>
  
  <pre><code>&gt; imagebrick
  class       : RasterBrick 
  dimensions  : 29180, 14940, 435949200, 4  (nrow, ncol, ncell, nlayers)
  resolution  : 0.6, 0.6  (x, y)
  extent      : 354038.4, 363002.4, 4066992, 4084500  (xmin, xmax, ymin, ymax)
  coord. ref. : +proj=utm +zone=11 +datum=NAD27 +units=m +no_defs +ellps=clrk66 +nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat 
  names       : band1, band2, band3, band4 
  min values  :     0,     0,     0,     0 
  max values  : 65535, 65535, 65535, 65535 
  </code></pre>
  
  <p>I was trying to extract pixels in the Rasterbrick that touched by the Line(SpatialLine). But I also wanted to pull out xy coordinates associated with those extracted pixels and store them together in a SpatialPointsDataFrame. I've tried:</p>
  
  <pre><code>extract_polyline_test &lt;- extract(imagebrick, polyline)
  extract_polyline_test &lt;- as.data.frame(extract_polyline_test)
  &gt; extract_polyline_test
  band1 band2 band3 band4
  1    125   156    73   392
  2    129   161    80   366
  3    156   208   122   374
  4    142   175    97   330
  5    102   117    31   389
  6    117   143    57   381
  7    162   221   127   413
  8    213   302   204   405
  ..   ...   ...   ...   ...
  </code></pre>
  
  <p>Then, I was trying to use RasterToPoints function to pull out the xy coordinates. This requires a raster layer that contains all the pixels that I want xy from. I've tried to use "crop" to get that raster "line" but it does not work. </p>
  
  <pre><code>rastercrop_polyline &lt;-crop(imagebrick,polyline)
  rastercrop_polyline
  
  &gt; rastercrop_polyline
  class       : RasterBrick 
  dimensions  : 8, 7, 56, 4  (nrow, ncol, ncell, nlayers)
  resolution  : 0.6, 0.6  (x, y)
  extent      : 357714.6, 357718.8, 4076030, 4076035  (xmin, xmax, ymin, ymax)
  coord. ref. : +proj=utm +zone=11 +datum=NAD27 +units=m +no_defs +ellps=clrk66 +nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat 
  data source : in memory
  names       : band1, band2, band3, band4 
  min values  :    72,    63,     0,   156 
  max values  :   274,   415,   299,   497 
  
  s.polyline &lt;- rasterToPoints(rastercrop_polyline)
  &gt; s.polyline
           x       y band1 band2 band3 band4
   [1,] 357714.9 4076035    93    96    17   342
   [2,] 357715.5 4076035    72    63     0   377
   [3,] 357716.1 4076035    90    93    17   371
   [4,] 357716.7 4076035   125   156    73   392
   [5,] 357717.3 4076035   129   161    80   366
   [6,] 357717.9 4076035   156   208   122   374
   [7,] 357718.5 4076035   142   175    97   330
   [8,] 357714.9 4076034   100   108    25   326
   ..  ........  .......   ...   ..     ..    ...
  </code></pre>
  
  <p>It seems that the "crop" function does not crop a "line" of raster but a whole "polygon". Is there any other way to get the xy coordiantes of the pixels that I extracted? </p>
  
  <p>p.s. I was also trying to extract surrounding pixels of each extracted pixels. How should I do this? I notice that there is a function parameter in extract function. Should I work on writing a small function within the extraction function or there are any other ways to do this? </p>
  
  <p>p.p.s I was also thinking to use Rasterize function to rasterize the "SpatialLine" and then use the RasterToPoints to pull out the xy coordinates. But I can not control each pixel of the "rasterized SpatialLine" equals to the pixels that touched by the "line" in the original imagebrick. </p>
  
  <p>Thanks everyone for your time viewing my question and thanks in advance for your help.</p>