Select to view content in your preferred language

Intersections - Biggest Overlap Methods - Query

139
1
a week ago
Labels (3)
benedictmueller
Emerging Contributor

Hey there!

In ArcPy you have the option to choose 'biggest overlap' when performing a spatial join. 

I was wondering if there is any possible way to perform the same operation with the arcgis api. You have different options to perform eg a query on a feature layer with geometry filters, same for sdf.spatial.join() method. But I can only find interesect, touches, within, etc. Is there a possible workaround to maybe interesect first and then compare the geometry to calculate the bigger overlap and then only join the features of the biggest overlap?

I havent figured it out yet and would appreciate any kind of help!

Thank you in advance.

Benni xo

0 Kudos
1 Reply
Clubdebambos
MVP Regular Contributor

Hi @benedictmueller,

This should get you on your way. The result is a dictionary with the key as the target oid and the value as the source oid. Code is comment, hopefully easy to follow.

from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from arcgis.geometry import Geometry

################################################################################
## USER INPUTS #################################################################

## the target polygon feature layer url
ply_fl_1_url = "FEATURE_LAYER_URL"

## the source polygon feature layer
## this is the one that has multiple overlaps with the one above
ply_fl_2_url = "FEATURE_LAYER_URL"

################################################################################
## REQUIRED OBJECTS ############################################################

## we will store the target and source object ids in here
results_dict = {}

################################################################################
## ACCESS ARCGIS ONLINE ########################################################

agol = GIS("home")

################################################################################
## CREATE FEATURE LAYER OBJECTS ################################################

## get the target polygon feature layer as a FeatureLayer object
ply_fl_1 = FeatureLayer(
    url = ply_fl_1_url,
    gis = agol
)

## get the source polygon feature layer as a FeatureLayer object
ply_fl_2 = FeatureLayer(
    url = ply_fl_2_url,
    gis = agol
)

################################################################################
## GET OID FIELDS ##############################################################

## get the target polygon feature layer oid field
ply_1_oid = ply_fl_1.properties.objectIdField

## get the source polygon feature layer oid field
ply_2_oid = ply_fl_2.properties.objectIdField

################################################################################
## CREATE FEATURE SET OBJECT ###################################################

## get the target polygon feature layer as a feature set
ply_1_fset = ply_fl_1.query(out_fields=[ply_1_oid]).features

## get the source polygon feature layer oid field
ply_2_fset = ply_fl_2.query(out_fields=[ply_2_oid]).features

################################################################################
## GET THE ID WITH BIGGEST OVERLAPS ############################################

## for each polygon in the target feature layer
for ply_1_f in ply_1_fset:
    ## create a Geometry object
    ply_1_geom = Geometry(ply_1_f.geometry)
    ## set overlap area to zero and id to None
    max_overlap_area = 0
    max_overlap_id = None

    ## for each polygon in the source feature layer
    for ply_2_f in ply_2_fset:
        ## create a Geometry object
        ply_2_geom = Geometry(ply_2_f.geometry)
        ## if the target geometry overlaps
        if ply_1_geom.overlaps(ply_2_geom):
            ## then get the area of intersection
            overlap_area = ply_1_geom.intersect(second_geometry = ply_2_geom, dimension = 4).area
            ## if the overlap area is greater than previous
            if overlap_area > max_overlap_area:
                ## set the overlap area as the max
                max_overlap_area = overlap_area
                ## set the source id as the id with biggest overlap
                max_overlap_id = ply_2_f.attributes[ply_2_oid]

    ## if a max_overlap_id was found
    if max_overlap_id:
        ## add to the results dictionary {target_id : source_id}
        results_dict[ply_1_f.attributes[ply_1_oid]] = max_overlap_id

################################################################################
## PRINT RESULTS ###############################################################

for key,value in results_dict.items():
    print(f"Target ID: {key}", f"Source ID: {value}")

 

Clubdebambos_0-1757159613369.png

Clubdebambos_1-1757159659466.png

 

All the best,

Glen

 

~ learn.finaldraftmapping.com
0 Kudos