Hi Herry,

I am not entirely sure I understand your problem, but I suggest to do the following:

1st alternative
1. load your shape files
2. set the bounding box of the polygon to match your grid scheme
3. use spsample() to sample the polygon in a regular fashion
4. assign to those points that are returned the value of the polygon
Potential problems: polygons with odd shapes or very small size may not be hit by the grid cell centres.
Solution: check for "no hits" and assign the value of the polygon (centroid coordinates) to the nearest grid cell

2nd alternative
1. load your shape file
2. create by hand regular coordinates (within the bounding box of the polygon, saves memory, but you have to remember to use floor() and ceiling()) in a data frame (tip: use rep() and seq() with order())
        as an example a grid between -180 and 180 and -90 and 90 with a cell size of 0.5
        X <- rep(seq((-180 + (0.5/2)),(180 - (0.5/2)),0.5), length(seq((-90 + (0.5/2)),(90 - (0.5/2)),0.5)))
        Y <- rep(seq((-90 + (0.5/2)),(90 - (0.5/2)),0.5), length(seq((-180 + (0.5/2)),(180 - (0.5/2)),0.5)))
        grid <- cbind(X, Y[order(Y)])
3. use coordinates() and make a spatialpoints out of the X/Y coordinates
4. overlay() with your polygon
remember you will have the same problems as mentioned above.

Hope this helps. If you need more details, please give me a shout. You could also check out this sample code, as it does essentially the same thing: http://spatialr.googlepages.com/code13


good luck,


Kami
------------------------
Kamran Safi

Postdoctoral Research Fellow
Institute of Zoology
Zoological Society of London
Regent's Park
London NW1 4RY

http://www.zoo.cam.ac.uk/ioz/people/safi.htm

http://spatialr.googlepages.com
http://asapi.wetpaint.com



-----Original Message-----
From: r-sig-geo-bounces@stat.math.ethz.ch on behalf of Alexander.Herr@csiro.au
Sent: Wed 28.01.2009 01:10
To: r-sig-geo@stat.math.ethz.ch
Subject: [R-sig-Geo] calculate raster values based on vector regions
 
 
Hi List,

How would I go about assigning values to a raster based on polygon regions in R. Examples would be most appreciated.

I have (vector) regions assigned to a specific value. The raster has NAs and some pixels where these values are likely to occur on ground. I need to assign these values to the raster and can do this in ArcInfo through vectors and rasterizing but only to a limited raster size. And R is much more preferable anyway...

Any help appreciated.
Cheers
Herry  

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo




Click https://www.mailcontrol.com/sr/wQw0zmjPoHdJTZGyOCrrhg==  to report this email as spam.



The Zoological Society of London is incorporated by Royal Charter
Principal Office England. Company Number RC000749
Registered address: 
Regent's Park, London, England NW1 4RY
Registered Charity in England and Wales no. 208728 

_________________________________________________________________________
This e-mail has been sent in confidence to the named add...{{dropped:20}}


