map algebra with ascii grid
Thank you very much to All , Roger's hint doest the thing I need. I am working with DEMs and instead of processing them partially in a GIS, I wanted to do everything in R as most of the analyses are carried out in R. I'm sure I will have more questions in the future:) I appreciate all the advice. Michal
On Jul 15 2007, Agustin Lobo wrote:
Roger, I think that having Arithmetic operations defined for SpatialGridDataFrame objects would be very useful. May be I'm missing something, but the argument that "columns of the data frame (here a single column) may contain data of different classes" does not seem a valid one to me, as this is the case for any dataframe object in R while arithmetic operations are defined for them, just yielding the proper results and warnigs,i.e.:
> d <- data.frame(cbind(x=1, y=1:10), fac=sample(L3, 10, repl=TRUE))
x y fac 1 1 1 C 2 1 2 A 3 1 3 A 4 1 4 A 5 1 5 A 6 1 6 A 7 1 7 A 8 1 8 C 9 1 9 A 10 1 10 C
> d+3
x y fac 1 4 4 NA 2 4 5 NA 3 4 6 NA 4 4 7 NA 5 4 8 NA 6 4 9 NA 7 4 10 NA 8 4 11 NA 9 4 12 NA 10 4 13 NA Warning message: + not meaningful for factors in: Ops.factor(left, right) So I would be most happy with such a feature added to spatial objects. The "solution" of using other tools (i.e., python, cf. message by Philip, but this can also be done with grass and many other GIS) is not optimal at all, as we keep having copies of the same information in different formats for different tools. Agus Roger Bivand escribi?:
On Sat, 14 Jul 2007, Michal Gallay wrote:
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
Michal Gallay Postgraduate Research Student School of Geography, Archaeology and Palaeoecology Queen's University Belfast BT7 1NN Northern Ireland Tel: +44(0)2890 273929 Fax: +44(0)2890 973212 email: mgallay01 at qub.ac.uk www: www.qub.ac.uk/geog