C API for sp classes?
On Tue, 7 Mar 2006, Edzer J. Pebesma wrote:
Wouter, if you want to use the .C interface, you need to pass the maps as simple vectors. I would pass one with the map values, and one with the topology. Below is some example code how to get them. An R function should be used to do the coercion to double arrays, and wrap the .C call.
Is the overhead of learning and using the .Call() interface too great? As Edzer says, since the AttributeList can contain multiple vectors of possibly different types, it may be helpful to get a better grip on them that way. The geometry slots are more complicated, and would need more thought, though, but would be needed to make a raster, as Edzer shows. Interesting issue. What would be returned? I guess in fact that a .C() interface with a more complicated R function to pick everything apart and check types is the way to go for prototyping. What would you use as baseline for comparison to see if your output is plausible? Roger
Please note that a SpatialGridDataFrame can actually hold more than one attribute (map). -- Edzer For getting the values:
> library(sp) > data(meuse.grid) > gridded(meuse.grid)=~x+y > fullgrid(meuse.grid)=T > meuse.grid at data@att[[1]] # first attribute
[1] NA NA NA NA NA NA NA NA NA NA ...
>
Getting the topology:
> meuse.grid at grid
x y cellcentre.offset 178460 329620 cellsize 40 40 cells.dim 78 104
> class(meuse.grid at grid)
[1] "GridTopology" attr(,"package") [1] "sp"
> unlist(gridtopology(meuse.grid))
Error in unlist(gridtopology(meuse.grid)) :
couldn't find function "gridtopology"
> unlist(gridparameters(meuse.grid))
cellcentre.offset1 cellcentre.offset2 cellsize1 cellsize2
178460 329620 40 40
cells.dim1 cells.dim2
78 104
> as.double(unlist(gridparameters(meuse.grid)))
[1] 178460 329620 40 40 78 104
>
Wouter Buytaert wrote:
Hi, I want to write some R functions for calculating hydrological catchment characteristics from a DEM (topographic index, overland flow delay function etc.). Looks like using a DEM with SpatialGridDataFrame class as an input is a good idea. Then what is the best way to extract the data matrix from this class to pass it on to a .C() function which does the actual calculations? cheers, wouter
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no