On Fri, Mar 28, 2014 at 4:11 PM, dododave <dododave@gmail.com> wrote:

> I am now able to convert the hdf files into GeoTiffs. I have a follow-up
> question though.
>
> I now need to extract the mean ndvi value from within a polygon overlaid on
> that GeoTiff. The issue I am having is that the code is returning NULL when
> I do.
>
> >       rast <- raster("2011_P01.tif")
> >       mean.ndvi <- extract(rast, poly, fun=mean)
> > mean.ndvi
> [[1]]
> NULL
>
>
> Here is the output when I call rast;
> > rast
> class       : RasterLayer
> dimensions  : 904, 2500, 2260000  (nrow, ncol, ncell)
> resolution  : 1, 1  (x, y)
> extent      : 0, 2500, 0, 904  (xmin, xmax, ymin, ymax)
> coord. ref. : NA
> data source : /Volumes/GOLONDRINA/tiff/2011_P01.tif
> names       : X2011_P01
> values      : -32768, 32767  (min, max)
>
> Help appreciated!
>

Are you sure your polygon is in the right place?

r=raster()
r[]=runif(180*360)
pts=data.frame(x=c(-50,50,50,-50,-50),y=c(-50,-50,50,50,-50))
poly = SpatialPolygons(list(Polygons(list(Polygon(pts)),ID=1)))
extract(r,poly,fun=mean)
[1] 0.4997085

 - so that;s all good, but if I move the polygon way off the raster:

poly2 = SpatialPolygons(list(Polygons(list(Polygon(pts+200)),ID=1)))
extract(r,poly2,fun=mean)
[[1]]
NULL

 I get what you're getting.  Check your polygon coordinates and CRS string
is correct...

Barry

	[[alternative HTML version deleted]]

