AnsweredAssumed Answered

Is there a way to use python to select features by location from selected features?

Question asked by RP.CObb on Oct 8, 2019
Latest reply on Oct 9, 2019 by RP.CObb



I have a script that seems to work but is unfortunately returning incorrect results. I am trying to figure out the best way to go about it. I have tried several other alternatives but those alternatives either return an incorrect number of results or doesn't have the data I need to work with. What I am trying to do is:


  • simply iterate through a set of features that have like fields
  • iterate through each null value in the fields, selecting another layer based on location to that null record in the feature class field
  • derive a certain field value from the feature that is selected based on the location of the feature class with null field value


import arcpy
import os

arcpy.env.overwriteOutput = True

workspace = r'Database Connections\......sde'
fcs = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass")

#make district landlot layer
Dist_LL = "Database Connections\.....sde\......Landlots"
DistLL_layer = arcpy.MakeFeatureLayer_management(Dist_LL, 'DLL_layer')

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        fcs.append(os.path.join(dirpath, filename))

for fc in fcs:
    geometryType = arcpy.Describe(fc).shapeType

    #Get feature class name
    fcsname = os.path.basename(fc)
    name = os.path.splitext(fcsname)
    y = name[1].lstrip('.')
    print y

    #Create lists (OID)
    OID = []
    if geometryType == 'Point':
        print geometryType
        for row in arcpy.da.SearchCursor(fc, ["OID@", "LANDDISTRICT", "LANDLOT"], """LANDDISTRICT IS NULL OR LANDLOT IS NULL"""):
            appendOID = OID.append(row[0])
            OIDstrnum = str(len(OID))
            #Create unique naming conventions
            a = '{}{}'.format(y, OIDstrnum)
            if (row[1] is None or row[2] is None):

                #Create temp fc copy
                b = arcpy.MakeFeatureLayer_management(fc, a)
                #Select layer by location
                selection = arcpy.SelectLayerByLocation_management(DistLL_layer, "INTERSECT", b, '', "NEW_SELECTION")
                print '{} District is {} and Land Lot is {} '.format(row[0], row[1], row[2])
                for selected in arcpy.da.SearchCursor(selection, ["LAND_DIST", "LAND_LOT"]):
                    print selected
            elif (row[1] is None):
                print '{} Land District is {} '.format(row[0], row[1])
                for selected_Lot in arcpy.da.SearchCursor(selection, ["LAND_DIST"]):
                    print selected_Lot
            elif (row[2] is None):
                print '{} Land Lot is {}'.format(row[0], row[2])
                for selected_District in arcpy.da.SearchCursor(selection, ["LAND_LOT"]):
                    print selected_District
                print 'No null values'

            del OID[:]


I have something that works(partially) but does not return the correct features with a corresponding spatial location. If anyone could assist with this I would greatly appreciate it.