Select to view content in your preferred language

Select by location for the row in a search cursor

11679
4
Jump to solution
01-06-2015 07:46 AM
ThomasCaruso
New Contributor III

Hi folks,

I'm trying to use a search cursor to iterate through a table and select the nearest centroids to the feature in the cursor, but when I try this instead of only selecting the centroids nearest the point in the cursor, it instead selects all of the centroids that distance from all of the points, then iterates to the next feature and selects all of them again. Is it possible to make the SelectLayerByLocation_management function only select features based on a single point at a time?

Here's the code I'm using:

# Create search cursor which will iterate through wells, selecting all centroids within a distance 
with arcpy.da.SearchCursor(wells, ["OBJECTID"]) as cursor:
        for row in cursor:
                # select by location
                arcpy.SelectLayerByLocation_management("orps09_FL", "WITHIN_A_DISTANCE", wells, "0.5 kilometers", "NEW_SELECTION")

Thanks in advance,

Tom

Tags (1)
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

There are a couple or more ways to get this working.  One of the quickest to implement, although it might not be the most peformant, is to include a SelectLayerByAttribute_management statement before the select by location:

# Create search cursor which will iterate through wells, selecting all centroids within a distance   
with arcpy.da.SearchCursor(wells, ["OBJECTID"]) as cursor:  
    for row in cursor:
        # select by attribute
        SelectLayerByAttribute_management (wells,
                                           "NEW_SELECTION",
                                           "OBJECTID = {}".format(row[0]))
        # select by location  
        arcpy.SelectLayerByLocation_management("orps09_FL",
                                               "WITHIN_A_DISTANCE",
                                               wells,
                                               "0.5 kilometers",
                                               "NEW_SELECTION")

The above assumes that OBJECTID is numeric and not a string.

View solution in original post

0 Kudos
4 Replies
JoshuaBixby
MVP Esteemed Contributor

There are a couple or more ways to get this working.  One of the quickest to implement, although it might not be the most peformant, is to include a SelectLayerByAttribute_management statement before the select by location:

# Create search cursor which will iterate through wells, selecting all centroids within a distance   
with arcpy.da.SearchCursor(wells, ["OBJECTID"]) as cursor:  
    for row in cursor:
        # select by attribute
        SelectLayerByAttribute_management (wells,
                                           "NEW_SELECTION",
                                           "OBJECTID = {}".format(row[0]))
        # select by location  
        arcpy.SelectLayerByLocation_management("orps09_FL",
                                               "WITHIN_A_DISTANCE",
                                               wells,
                                               "0.5 kilometers",
                                               "NEW_SELECTION")

The above assumes that OBJECTID is numeric and not a string.

0 Kudos
ThomasCaruso
New Contributor III

Hi Joshua,

Thanks for answering. Sadly, this did not actually work for me. I still have the issue of selecting all centroids that distance from all wells - and OBJECTID is indeed numeric. What other ways might I implement this, if you don't mind?

This is the full code I am using. I'm basically trying to compare last names of well owners and nearby centroids to see if there is a match.

import arcpy
arcpy.env.overwriteOutput = 1
# define a workspace
arcpy.env.workspace = r"C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb"
# Define input files
orps09 = r"C:\Users\tmc18\Desktop\comp_orps\centroids\madirps_point1.shp"
wells = r"C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb\Madison_DEC_Well_Logs_3_14_14_MASTER_COPY1"
# Make a feature layer
arcpy.MakeFeatureLayer_management(orps09, "orps09_FL")
arcpy.MakeFeatureLayer_management(wells, "wells1")
wells1 = "wells1"
# Create dictionary of last names of all well owners
well_owners = {}  
rows = arcpy.da.SearchCursor(wells1, ["OBJECTID", "owner_last"])  
for row in rows:  
    well_owners[row[0]] = [row[1]]        
del row,rows
# Create search cursor which will iterate through wells
with arcpy.da.SearchCursor(wells1, ["OBJECTID"]) as cursor:
        for row in cursor:
            # set well owner name for this record
            owner = well_owners[row[0]]
            
            # select by attribute  
            arcpy.SelectLayerByAttribute_management(wells1,"NEW_SELECTION","OBJECTID = {}".format(row[0]))
            
            # select by location
            arcpy.SelectLayerByLocation_management("orps09_FL", "WITHIN_A_DISTANCE", wells, "0.5 kilometers", "NEW_SELECTION")
            # Create dictionary of last names of selected orps
            orps_owners = {}
            rows = arcpy.da.SearchCursor("orps09_FL", ["FID", "owner_last"])  
            for row in rows:  
                orps_owners[row[0]] = [row[1]]
            del row,rows
            
            # create search cursor which will iterate through selected orps owners
            with arcpy.da.SearchCursor("orps09_FL", ["FID"]) as orpscur:
                for row in orpscur:
                    # set orps owner name
                    orpsowner = orps_owners[row[0]]
                    if owner != orpsowner:
                        print owner, orpsowner
                        continue
                    else:
                        with arcpy.da.UpdateCursor(wells, ["match"]) as cur:
                            for row in cur:
                                row[0] = "YES"
                                cur.updateRow(row)
                            del cur
                            continue
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

On line 28, try replacing wells with wells1.  The SelectLayerByLocation needs to have the selected feature(s) passed to it and not the original feature class.

0 Kudos
ThomasCaruso
New Contributor III

ah! Many thanks, my friend. I appreciate your help!

0 Kudos