[R-sig-Geo] calculate raster values based on vector regions

Tomislav Hengl T.Hengl at uva.nl
Wed Jan 28 09:56:01 CET 2009


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 at 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 at bbox[1,1]+cellsize, USER_X_EXTENT_MAX=rastermap at bbox[1,2]-cellsize,
USER_Y_EXTENT_MIN=rastermap at bbox[2,1]+cellsize, USER_Y_EXTENT_MAX=rastermap at 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 at stat.math.ethz.ch [mailto:r-sig-geo-bounces at stat.math.ethz.ch] On Behalf
> Of Alexander.Herr at csiro.au
> Sent: Wednesday, January 28, 2009 2:10 AM
> To: r-sig-geo at 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 at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo




More information about the R-sig-Geo mailing list