[R-sig-Geo] Which dimension should a matrix returned by a binary topological operaor have?

Roger Bivand Roger.Bivand at nhh.no
Wed Sep 7 14:23:55 CEST 2016

On Wed, 7 Sep 2016, Edzer Pebesma wrote:

> In sp and rgeos, we currently have
>> library(sp)
>> library(rgeos)
>> p1 = SpatialPoints(matrix(1:4,2))
>> p2 = SpatialPoints(matrix(1:6,3))
>> gRelate(p1,p2,byid=TRUE)
>  1           2
> 1 "FF0FFF0F2" "FF0FFF0F2"
> 2 "FF0FFF0F2" "FF0FFF0F2"
> 3 "FF0FFF0F2" "FF0FFF0F2"
>> dim(gRelate(p1,p2,byid=TRUE))
> [1] 3 2
>> length(p1)
> [1] 2
>> length(p2)
> [1] 3
> Meaning: arguments with lengths 2 and 3 result in a 3 x 2 matrix. This
> is true for all binary operators (covers, intersects, etc) and byid=TRUE.

In src/rgeos_predicate_binary.c, you'll see that rgeos_binpredfunc() can 
return a matrix or a list. For rgeos_relate() the list option should be 
unused (it will be trapped in the next release), because the output must 
be a dense matrix by design. In other cases where the output is a logical 
matrix with many FALSE entries, a sparse list output object is perhaps 
more logical. sf should not return dense matrices where sparse binary 
matrices would suffice. However, the "relate" binary predicate is 

We could try to add an option to t() the output matrix, and see what got 
broken among dependent packages. The returned list is the length of 
spgeom1, by the way, with components 1-base indexing spgeom2. These would 
be like the columns in a data frame if the numbers of indexed components 
were equal, but a matrix is more natural.

Shall we test the dependencies for breakage? Scripts will break too I 
expect, but we can't check them.


> I always found this counterintuitive, and consider implementing it the
> other way around (2 and 3 -> 2 x 3) in package sf. Or am I overlooking
> something obvious?

Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no

More information about the R-sig-Geo mailing list