问题
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)
回答1:
One way to accomplish this:
- use the 'intersect' tool to break all the polygons apart where they overlap.
- Use the 'find duplicates' tool to generate a list of polygons that have the same shape.
- Join the results of these processes to the original table using the object Ids. Once the joins are in place you'll see overlapping polygons marked with the same value in the 'Feat_Seq' field.
Sample python:
arcpy.analysis.Intersect("test.shp", "test_Intersect", "ONLY_FID", None, "INPUT")
arcpy.management.FindIdentical("test_Intersect", r"test_Intersect_FindIdentical", "Shape", None, 0, "ONLY_DUPLICATES")
arcpy.management.AddJoin("test", "FID", "test_Intersect", "FID_test", "KEEP_ALL")
arcpy.management.AddJoin("test", "test_Intersect.OBJECTID", "test_Intersect_FindIdentical", "IN_FID", "KEEP_ALL")
来源:https://stackoverflow.com/questions/55716263/overlaps-of-all-objects-within-the-same-dataset-in-arcpy-st-overlaps