[R-sig-Geo] overlay polygon with raster

Frede Aakmann Tøgersen frtog at vestas.com
Thu Dec 4 07:36:34 CET 2014


Hi Simone

If I understand you correctly, then have a look at the man page of raster(). Here is the relevant sections:

     ## S4 method for signature 'Raster,SpatialPolygons'
     extract(x, y, fun=NULL, na.rm=FALSE, weights=FALSE, cellnumbers=FALSE,
          small=FALSE, df=FALSE, layer, nl, factors=FALSE, sp=FALSE, ...)

Where
       x: Raster* object

       y: points represented by a two-column matrix or data.frame, or
          'SpatialPoints*'; 'SpatialPolygons*'; 'SpatialLines';
          'Extent'; or a numeric vector representing cell numbers

and

weights: logical. If 'TRUE', the function returns, for each polygon, a
          matrix with the cell values and the approximate fraction of
          each cell that is covered by the polygon(rounded to 1/100).
          The weights can be used for averaging; see examples. This
          option can be useful (but slow) if the polygons are small
          relative to the cells size of the Raster* object.

I just googled for ' R area of raster cells within polygon' and got this hit with a reproducible small example:

http://stackoverflow.com/questions/17766989/extract-data-from-raster-with-small-polygons-rounded-weights-too-small 

See that page for a solution to some rounding errors in the case of the polygon being very small compared to raster cell size.

Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 

> -----Original Message-----
> From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of
> Simone Ruzza
> Sent: 4. december 2014 01:15
> To: r-sig-geo at r-project.org
> Subject: [R-sig-Geo] overlay polygon with raster
> 
> Dear R-sig-geo users,
> 
> apologies for the simplicity of my question, but I have been
> struggling with this for a bit I have not managed to find a solution.
> Basically, I would like to overlay a polygon onto raster grid and
> extract some statistics for the polygon in question i.e. the area of
> every raster cell covered by the polygon. Here is a figure that might
> make it clearer
> <https://dl.dropboxusercontent.com/u/33966347/example.JPG> . I have
> been fiddling around with the extract function from the raster
> package, which works to some extent, as I am able to retrieve
> information on the categories of each of the raster cells covered by
> the polygon. However, as I said, I am interested in the area covered
> by the polygon within each raster cell. I know that perhaps I should
> try and convert my polygon into another raster and then overlay the
> two rasters, but this perhaps would mean that the polygon should be
> converted into a raster at a high resolution? If this was case, it is
> not computationally feasible for me as some polygons I have cover a
> huge area and it would take forever to overlay all those rasters. Any
> help, suggestions would be highly appreciated!
> 
> thanks in advance!
> 
> Simone
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list