The R
Package sf
has an amazing set of functions under the title "Geometric binary predicates" which are described in detail here.
As stated in the link, the functions are recursively applied to all geometries within the same dataset if only one sf
-object is provided (see example below)
If y is missing,
st_predicate(x, x)
is effectively called, and a square matrix is returned with diagonal elementsst_predicate(x[i], x[i])
.
Right now however, I'm building some tools where I am bound to arcpy
from ArcGIS. What would be a fast way to get a square matrix of all features within the same dataset indicating whether the respective features overlap or not?
arcpy.SpatialJoin_analysis()
only compares two datasets and arcpy.GenerateNearTable_analysis()
as well as arcpy.Near_analysis()
only calculate distances between features.
This is how st_overlaps()
works in R
:
library(sf)
#> Warning: Paket 'sf' wurde unter R Version 3.5.2 erstellt
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
b0 = st_polygon(list(rbind(c(-1,-1), c(1,-1), c(1,1), c(-1,1), c(-1,-1))))
a0 = b0 * 0.8
a1 = a0 * 0.5 + c(2, 0.7)
a2 = a0 + 1
a3 = b0 * 0.5 + c(2, -0.5)
y = st_sfc(a0,a1,a2,a3)
plot(y)
st_overlaps(y,sparse = F)
#> [,1] [,2] [,3] [,4]
#> [1,] FALSE FALSE TRUE FALSE
#> [2,] FALSE FALSE TRUE FALSE
#> [3,] TRUE TRUE FALSE FALSE
#> [4,] FALSE FALSE FALSE FALSE
Created on 2019-04-16 by the reprex package (v0.2.1)
One way to accomplish this:
Sample python: