
R version 3.2.2 (2015-08-14) -- "Fire Safety"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(sp)
> 
> g = SpatialGrid(GridTopology(c(0,0), c(1,1), c(3,3)))
> p = as(g, "SpatialPolygons")
> over(g,g)
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
> over(p,p)
g1.g1 g2.g1 g3.g2 g4.g1 g5.g1 g6.g2 g7.g4 g8.g4 g9.g5 
    1     1     2     1     1     2     4     4     5 
> over(p,p, minDimension = 0) # orders; different names
g1 g2 g3 g4 g5 g6 g7 g8 g9 
 1  2  3  4  5  6  7  8  9 
> over(p,g)
g1 g2 g3 g4 g5 g6 g7 g8 g9 
 1  2  3  4  5  6  7  8  9 
> over(g,p)
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
> 
> over(p,p,returnList=TRUE)
$g1
g1 g2 g4 g5 
 1  2  4  5 

$g2
g1 g2 g3 g4 g5 g6 
 1  2  3  4  5  6 

$g3
g2 g3 g5 g6 
 2  3  5  6 

$g4
g1 g2 g4 g5 g7 g8 
 1  2  4  5  7  8 

$g5
g1 g2 g3 g4 g5 g6 g7 g8 g9 
 1  2  3  4  5  6  7  8  9 

$g6
g2 g3 g5 g6 g8 g9 
 2  3  5  6  8  9 

$g7
g4 g5 g7 g8 
 4  5  7  8 

$g8
g4 g5 g6 g7 g8 g9 
 4  5  6  7  8  9 

$g9
g5 g6 g8 g9 
 5  6  8  9 

> over(p,p,returnList=TRUE, minDimension=0)
$g1
[1] 1 2 4 5

$g2
[1] 2 1 3 5 4 6

$g3
[1] 3 2 6 5

$g4
[1] 4 1 5 7 2 8

$g5
[1] 5 2 4 6 8 1 3 7 9

$g6
[1] 6 3 5 9 2 8

$g7
[1] 7 4 8 5

$g8
[1] 8 5 7 9 4 6

$g9
[1] 9 6 8 5

> over(p,p,returnList=TRUE, minDimension=1)
$g1
[1] 1 2 4

$g2
[1] 2 1 3 5

$g3
[1] 3 2 6

$g4
[1] 4 1 5 7

$g5
[1] 5 2 4 6 8

$g6
[1] 6 3 5 9

$g7
[1] 7 4 8

$g8
[1] 8 5 7 9

$g9
[1] 9 6 8

> over(p,p,returnList=TRUE, minDimension=2)
$g1
[1] 1

$g2
[1] 2

$g3
[1] 3

$g4
[1] 4

$g5
[1] 5

$g6
[1] 6

$g7
[1] 7

$g8
[1] 8

$g9
[1] 9

> over(p,p[1:6],minDimension=2)
g1 g2 g3 g4 g5 g6 g7 g8 g9 
 1  2  3  4  5  6 NA NA NA 
> 
> x2 = x1 = cbind(c(0,1,1,0,0), c(0,0,1,1,0))
> x1[,1] = x1[,1]+0.5
> x1[,2] = x1[,2]+0.25
> sp = SpatialPolygons(list(
+  Polygons(list(Polygon(x1)), "x1"),
+  Polygons(list(Polygon(x2)), "x2")))
> pt = SpatialPoints(cbind(0.5,0.5)) # on border of x1
> row.names(pt) = "pt1"
> over(pt,sp)
pt1 
  2 
> over(pt,sp,returnList=TRUE)
$pt1
[1] 1 2

> 
> rgeos::overGeomGeom(pt,sp)
pt1.x1 
     1 
> rgeos::overGeomGeom(pt,sp,returnList=TRUE)
$pt1
x1 x2 
 1  2 

> 
> plot(sp)
> plot(pt,add=TRUE,col='red',pch=16)
> #    x1     x2 
> #	"F0FF" "0FFF" 
> # it would be nice to have these sorted "2, 1" instead of "1, 2" - use
> rgeos::overGeomGeom(pt,sp,returnList=TRUE, minDimension = 0)
$pt1
[1] 2 1

> 
> rgeos::overGeomGeom(pt,pt,minDimension=2)
[1] NA
> rgeos::overGeomGeom(pt,pt,minDimension=1)
[1] NA
> rgeos::overGeomGeom(pt,pt,minDimension=0)
pt1 
  1 
> 
> proc.time()
   user  system elapsed 
  0.587   0.295   0.559 
