[R-sig-Geo] calculate raster values based on vector regions
Alexander.Herr at csiro.au
Alexander.Herr at csiro.au
Wed Jan 28 11:37:32 CET 2009
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 at uva.nl]
Sent: Wednesday, January 28, 2009 7:56 PM
To: r-sig-geo at 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 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