0
votes

I have three rasters for the same geographical location loaded in R.

> ndvi
class       : RasterLayer 
dimensions  : 1138, 1171, 1332598  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : 766867.4, 801997.4, 1420228, 1454368  (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       : layer 
values      : -0.4103095, 0.7972555  (min, max)


> temp_celsius_lst
class       : RasterLayer 
dimensions  : 1138, 1171, 1332598  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : 766867.4, 801997.4, 1420228, 1454368  (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       : layer 
values      : 21.56528, 40.01204  (min, max)


> landuse_raster
class       : RasterLayer 
dimensions  : 1138, 1171, 1332598  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : 766867.4, 801997.4, 1420228, 1454368  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 0.93, 0.98  (min, max)

I also have a polygon layer as follows.

> urbangreen_buffer
class       : SpatialPolygonsDataFrame 
features    : 884 
extent      : 774055.5, 791282.7, 1421905, 1446710  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 
+towgs84=0,0,0 
variables   : 6
names       : Id     
min values  :  1 
max values  :  9

What i am trying to do is extract data from the raster that intersect with the polygon regions. I use the following code:

extract(ndvi, urbangreen_buffer)
extract(temp_celsius_lst, urbangreen_buffer)
extract(landuse_raster, urbangreen_buffer)

It works for the ndvi and temp_celsius_lst rasters. The code returns a list with 884 elements with each element having values of all pixels in the raster that lie within the corresponding polygon.

However, the code for the landuse_raster just returns a list with 884 elements filled with NULL values. I am unable to figure out a reason for the same. Any help would be greatly appreciated.

I'd even be fine if there any any alternate methods of extraction that return all pixel values for the polygons.

Regards,

1
Can you provide the raster files and the shapefile? Your landuse_raster file has a different projection (longlat) than the urbangreen_buffer (utm).Christopher Stephan
Yeah the crs was the issue. Robert's solution solved the problem!Arpit
You could consider to accept his answer then.Christopher Stephan
Thanks! I hadnt known about accepting an answer earlier...still new to the site.Arpit

1 Answers

1
votes

landuse_raster has this crs: +proj=longlat +datum=WGS84 +ellps=WGS84 which is clearly wrong given the extent. Supposedly it really has the same crs as the other data. So I would try:

crs(landuse_raster) <- "+proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84"
e <- extract(landuse_raster, urbangreen_buffer)

Or all of them together:

s <- stack(ndvi, temp_celsius_lst, landuse_raster)
ee <- extract(s, urbangreen_buffer)