1

I have an old ArcView 3 tool which I've been tasked with converting to Python. I'm breaking it down into stages, but there's a "walking selection" I can't trap.

To put the query in context, the user selects a site (point data) - selection A. All sites within X kilometres of selection A are then selected (selection B), then all sites within X of kilometres of selection B are selected, and so forth until no new selections are made.

I've tried to use a While... loop with a Get Count to no avail. The problem I think is because the Get Count will always have a value, so I think I need to do something like Get Count <> Previous Get Count?

#Import ArcPy site package
import arcpy

# Overwrite files
arcpy.env.overwriteOutput = True

 try:
     # Make a feature layer with all the farms
     arcpy.MakeFeatureLayer_management(r"shapefile.shp", "Farms")

    # Make a feature layer containing only the farm of interest
     arcpy.MakeFeatureLayer_management(r"shapefile_select.shp","Farms_select")

    # Apply a selection to farms layer
    fcFarms = arcpy.SelectLayerByLocation_management ("Farms","WITHIN_A_DISTANCE","Farms_select", "2000 Meters")

    # Apply a further selection to farms layer
    fcFarms2 = arcpy.SelectLayerByLocation_management("Farms","WITHIN_A_DISTANCE",fcFarms, "2000 Meters", "ADD_TO_SELECTION")

    while getCount > 0:
        fcFarms3 = arcpy.SelectLayerByLocation_management("Farms","WITHIN_A_DISTANCE", fcFarms2, "2000 Meters", "ADD_TO_SELECTION")
        fcFarms4 = arcpy.SelectLayerByLocation_management("Farms","WITHIN_A_DISTANCE", fcFarms3, "2000 Meters", "NEW_SELECTION")
        getCount = int(arcpy.GetCount_management(fcFarms4))

except:
    print arcpy.GetMessages()
1
  • What I think you want to do is after your first SelectByLocation do a arcpy.GetCount. then for your while loop - do while newGetCount# > oldGetCount# and re-calculate it within the loop.
    – dklassen
    May 12, 2014 at 15:40

2 Answers 2

0

Try something like this:

before you while loop:

initial_getCount = int(arcpy.GetCount_management(fcFarms2).getOutput(0))
getCount = initial_getCount + 1

then for you loop

while getCount > initial_getCount:
    if arcpy.Exists(fcFarms3):
        arcpy.Delete_management(fcFarms3)
    fcFarms3 = arcpy.SelectLayerByLocation_management("Farms", "WITHIN_A_DISTANCE", fcFarms2, "2000 Meters", "ADD_TO_SELECTION")
    initial_getCount = getCount
    getCount = int(arcpy.GetCount_management(fcFarms2).getOutput(0))
2
  • Thanks for advice @dklassen. I'd been trying something similar, but alas it doesn't work. It just runs the first two selections (before the loop) then stops.
    – Liam M
    May 13, 2014 at 10:08
  • Messed about a bit more and it turns out the if arcpy.Exists was causing the process to fail. Removed that line and it works. Thanks @dklassen.
    – Liam M
    May 13, 2014 at 13:58
0

From my understanding of your question, I modified your script. This will serve your requirement.

#Import ArcPy site package
import arcpy

# Overwrite files
arcpy.env.overwriteOutput = True

def select_by_location(in_fc, target_fc, distance, selection_count=0):
    """ takes farms layer, selected layer, distance for selection"""
    selected_farms = arcpy.SelectLayerByLocation_management (in_fc, "WITHIN_A_DISTANCE", target_fc, distance, "ADD_TO_SELECTION")
    # check the previous and current value should not same
    # if they are same that means no furthur selections has been made
    if arcpy.GetCount_management(selected_farms).getOutput(0) != selection_count:
        selection_count = arcpy.GetCount_management(selected_farms).getOutput(0)
        return select_by_location(in_fc, selected_farms, distance, selection_count)
    return selected_farms

def main():
    try:
        # Make a feature layer with all the farms
        farms = arcpy.MakeFeatureLayer_management(r"path_of_farms.shp", "farms")

        # Make a feature layer containing only the farm of interest
        farms_select = arcpy.MakeFeatureLayer_management(r"path_of_farm_interest.shp","farms_select")

        # Make selection on the farms_select layer
        farms_select = arcpy.SelectLayerByAttribute_management(farms_select, "NEW_SELECTION", "Id = 0")

        # all_selected_farms contain all the selected farms within the given distance
        all_selected_farms = select_by_location(farms, farms_select, "80 Kilometer")

        # Save the all_selected_farms feature class to workspace
        arcpy.FeatureClassToFeatureClass_conversion(all_selected_farms , r"path_to_workspace if workspace is not defined earlier", "name of feature class")
    except:
        print arcpy.GetMessages()

if __name__ == "__main__":
    main()
2
  • Thanks @Surya. I can't seem to get this code to work. The main module works, but the select_by_location doesn't produce any selections. I presumed I had to change the in_fc etc, but I get a syntax error if I do that.
    – Liam M
    May 13, 2014 at 10:30
  • @Liam Mason: I changed the script for select_by_location function. Just for clarification farms is one feature class/shapefile and farms_select is another. I selected one feature from farms_select and performed the further selections on farms feature class. The result comes in all_selected_farms, which is in in_memory, so used FeatureClassToFeatureClass_conversion to save it to disk. Hope this will work for you now. Otherwise drop a comment for further improvements. Thanks
    – Surya
    May 13, 2014 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.