Hi Herry,

ok I think this should be pretty simple. 

you should be able to do that with overlay(). Just use your raster and overlay it with every polygon (in which the value is its ID) singly. 

If you use a loop (for n in your.poly@plotOrder) and

overlay(raster, (as.SpatialPolygons.PolygonsList(your.poly@polygons[n])) 

you can go through all polygons and get TRUE for the cell coordinates which are in the specified polygon, then assign the value of that polygon to those points which according to the overlay() are in the polygon. 


I hope this is not too cryptic. Good luck,

Kami




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

I try to explain my problem a bit better.

1) Vector and raster have the same extent.

2) Vector data:
Several polygones with different attribute values
3) raster data:
A raster with points over the areas (and also NAs for areas where it is not possible to have a value from polygones). These points need to get the polygon attribute values assigned

Vector                    Raster                  after assiging polygon values to raster
-----------             ------------          ------------
|   3   |  1    |          | +na + + na |        |3na 3 1 na  |
|-----------|          | na na  na na|        |na na  na na|
|    7  | 5     |          |++na  na +  |        |7 7na na 5  |
-----------            -------------        -------------

+ denotes a raster value

Does this make more sense?

In gdal/fwtools is a function rgdal_rasterize, which could potentially do this, but I'd rather to all in R.

Cheers and thanks
Herry

________________________________________
From: Tomislav Hengl [T.Hengl@uva.nl]
Sent: Wednesday, January 28, 2009 7:56 PM
To: r-sig-geo@stat.math.ethz.ch
Cc: Herr, Alexander Herr - Herry (CSE, Gungahlin)
Subject: RE: [R-sig-Geo] calculate raster values based on vector regions

Dear Herry,

If I understand what you problem, one solution is to use R+SAGA. You should first convert the
polygon map to the same grid, and then you can load it to R and do any type of aggregation:

> library(maptools)
> library(rgdal)
> library(RSAGA)
# load the gridded map:
> rastermap <- readGDAL("rastermap.asc")
# load the shapefile:
> rsaga.esri.to.sgrd(in.grids=" rastermap.asc", out.sgrds=" rastermap.sgrd", in.path=getwd())
# convert the polygon map to a raster map:
> cellsize <- rastermap@grid@cellsize[1]
> rsaga.geoprocessor(lib="grid_gridding", module=3, param=list(GRID="polygons.sgrd",
INPUT="polygons.shp", FIELD=1, LINE_TYPE=0, USER_CELL_SIZE=cellsize,
USER_X_EXTENT_MIN=rastermap@bbox[1,1]+cellsize, USER_X_EXTENT_MAX=rastermap@bbox[1,2]-cellsize,
USER_Y_EXTENT_MIN=rastermap@bbox[2,1]+cellsize, USER_Y_EXTENT_MAX=rastermap@bbox[2,2]-cellsize))
> rsaga.sgrd.to.esri(in.sgrds="polygons.sgrd", out.grids="polygons.asc", out.path=getwd(), prec=0)
> rastermap$polygons <- as.factor(readGDAL("polygons.asc"))
# summary statistics per polygon class:
> raster.polygons <- boxplot(band1 ~ polygons, rastermap @data,
col=rainbow(length(levels(negotingrid$SOIL))))
> str(raster.polygons)

You will of course need to adjust the "FIELD" position in the attribute table and the precision
"prec".

To obtain and use SAGA, check this article:
http://spatial-analyst.net/wiki/index.php?title=Software

cheers,

How is the weather now in Canberra? In Amsterdam is freezing brrr.

T. Hengl


> -----Original Message-----
> From: r-sig-geo-bounces@stat.math.ethz.ch [mailto:r-sig-geo-bounces@stat.math.ethz.ch] On Behalf
> Of Alexander.Herr@csiro.au
> Sent: Wednesday, January 28, 2009 2:10 AM
> 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
_______________________________________________
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}}


