[R-sig-Geo] map algebra with ascii grid

Philip Riggs priggs at cnr.colostate.edu
Sun Jul 15 15:40:12 CEST 2007


>> Dear R Users,
>>
>> could you please advise me on doing map algebra with spatial  
>> grids? It's
>> the first time I am using spatial objects in R. I have imported an  
>> ascii
>> grid file and wanted to round the values in it or sum with a  
>> value, but it
>> gives an error message:
>>
>>> require(maptools)
>>> x <- readAsciiGrid(fname="xxxx.asc")
>>> x+3
>> Error in x + 3 : non-numeric argument to binary operator
>>> round(x,2)
>> Error in round(x, digits) : Non-numeric argument to mathematical  
>> function
>>
>
> Arithmetic operations are not defined for SpatialGridDataFrame  
> operations,
> as the columns of the data frame (here a single column) may contain  
> data
> of different classes. Do the operations on the columns directly:
>
> names(x)
>
> If there is a column called "band1", then
>
> x$band1 + 3
>
> will print "band1" + 3, and
>
> x$band1 <- x$band1 + 3
>
> will add 3 to the "band1" column. Think what would happen if band1 was
> categorical or logical to see why doing arithmetic directly isn't  
> such a
> good idea.
>
> Hope this helps,
>
> Roger
>
>> Thank you for any suggestions.
>>
>> Best wishes,
>>
>> Michal
>>

I use Python with Numpy/GDAL python bindings all the time to do  
spatial algebra and manipulate grids. It has all the math functions,  
and in the case of categorical data you can reassign values. Then I  
use rpy to do any statistical analyses in R. You could also do your  
manipulation in Python, then save the grid in an appropriate format  
for import into R to do the statistical analyses.

Philip




More information about the R-sig-Geo mailing list