[R-sig-Geo] C API for sp classes?
Wouter Buytaert
wouter at paramo.be
Tue Mar 7 17:04:11 CET 2006
Thanks for the info!
Learning the .Call interface is certainly no problem of course. I just
thought about going with .C() because:
- the "writing R extensions" manual says you should consider .C before
using .Call
- it is easy to pass the map values as a double vector, and only a few
additional parameters have to be passed on (resolution, number of
rows/cols etc...), so using .C() is quite straightforward.
- I prefer doing a datacheck in R before passing the data to .C()
The resulting R function doesn't seem very complicated. I'm a newbie
though, so all recommendations welcome:
topidx <- function(map) {
if(!(class(map) == "SpatialGridDataFrame"))
printf("Map should be of SpatialGridDataFrame class")
values <- map at data@att[[1]]
ew_res <- map at grid@cellsize[1]
ns_res <- map at grid@cellsize[2]
cols <- map at grid@cells.dim[1]
rows <- map at grid@cells.dim[2]
# here comes al sorts of data check and NA handling...
.C("topidx",
PACKAGE = "mypackage",
as.double(values),
as.integer(rows),
as.integer(cols),
as.double(ew_res),
as.double(ns_res),
topidxmap = double(rows*cols))$topidxmap
# return is a double vector which can be postprocessed here...
}
cheers
wouter
On Tue, 7 Mar 2006, Roger Bivand wrote:
> 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
>
>
>
More information about the R-sig-Geo
mailing list