0

Last year I got assistance from StackExchange to write a Python script for ArcGIS 10.0 which loops through SelectLayerByLocation with buffer until it returns no new rows. Then it uses the selection for further processes.

We've just upgraded to ArcGIS 10.2.1 and I've discovered that the script no longer works :(

I can see in the Python window that the SelectLayerByLocation is running and the count increases, but the process stalls as soon as no new rows are returned. For example with the dummy data below, I get ...Row Count = 16.... ...Row Count = 18... ...Row Count = 18... The selected points are highlighted on screen, but the process hangs at that point. The script completes if the SelectByLocation detects no other sites within the distance (inDis2) of the selected site (fcFarmInterest), so I suspect the loop is the problem.

## Title: Autozone
## Function: Expanding selection of sites from a given site.
## Version: ArcGIS 10.0
## Author: Liam Mason, with assistance from dklassen @ GIS StackExchange
## Date: 5 August 2014

#Import ArcPy site package
import arcpy

# Overwrite files
arcpy.env.overwriteOutput = True

# Parameters
# Workspace setting
workSpace = r"C:\GIS\Autozone"
# Site ID as string
inSite = 'FS0501'
# Distance as string
inDis = '800'
# Name of farm data feature layer
farmData = "farms"
# Location of clipping shape shapefile
clipShape = "ClippingShape"
# Location for buffer to be saved into
outBuffer = r"C:\GIS\Autozone\Outputs\buffer.shp"
# Add meters to distance value
inDis1 = inDis + " meters"
# Convert distance value into integer, multiply by two, then remove a single     unit. Convert back to string and add meters.
inDis2 = str((int(inDis)*2)-1) + " meters"
# Where query, adding inSite string as criteria.
where = '"Site_No" = ' + "'%s'" %inSite

# Set the workspace environment setting
arcpy.env.workspace = workSpace

try:

    # Make a feature layer with all the farms
    fcFarmsAll = farmData

    #Select only marine farms, create output, then clear selection.
    fcFarmsMarine = arcpy.SelectLayerByLocation_management(fcFarmsAll,"WITHIN", clipShape)
    fcFarmsMarineOut = arcpy.CopyFeatures_management(fcFarmsMarine,r"\Working\marineFarms.shp")
    arcpy.SelectLayerByAttribute_management(fcFarmsAll,"CLEAR_SELECTION")


    # Make a feature layer with all the marine farms
    fcFarms = arcpy.MakeFeatureLayer_management(r"\Working\marineFarms.shp", "MarineFarms")

    #Select site of interest and create shapefile
    fcFarmInterest = arcpy.Select_analysis (fcFarms, r"\Working\farms_Select.shp", where)

    #Apply a selection to farms layer
    fcFarms1 = arcpy.SelectLayerByLocation_management(fcFarms,"WITHIN_A_DISTANCE", fcFarmInterest, inDis2)

    #Apply a further selection to farms layer
    fcFarms2 = arcpy.SelectLayerByLocation_management(fcFarms1,"WITHIN_A_DISTANCE", fcFarms1, inDis2, "ADD_TO_SELECTION")

    #Count selection
    initial_getCount = int(arcpy.GetCount_management(fcFarms2).getOutput(0))

    #Add one to selection count
    getCount = initial_getCount + 1

    #Loop through selection 
    while getCount > initial_getCount:
         fcFarms3 = arcpy.SelectLayerByLocation_management(fcFarms,"WITHIN_A_DISTANCE", fcFarms2, inDis2, "ADD_TO_SELECTION")
      initial_getCount = getCount
      getCount = int(arcpy.GetCount_management(fcFarms3).getOutput(0))

    #Buffer selection
    arcpy.Buffer_analysis(fcFarms3,r"\Working\farms_buffer.shp", inDis1,"FULL","ROUND","ALL")

    #Clip buffer
    arcpy.Clip_analysis(r"\Working\farms_buffer.shp",clipShape,r"\Working\buffer_clip.shp")

    #Break clipped buffer into multiparts
    arcpy.MultipartToSinglepart_management(r"\Working\buffer_clip.shp",r"\Working\buffer_multi.shp")

    #Make a feature layer for multipart buffer
    fcBuffer = arcpy.MakeFeatureLayer_management(r"\Working\buffer_multi.shp","BufferTemp")

    #Select buffer part with site of interest
    fcBufferSelect = arcpy.SelectLayerByLocation_management(fcBuffer,"INTERSECT", fcFarmInterest)

    #Export buffer to file, reselect sites within this buffer
    fcBufferSites = arcpy.CopyFeatures_management(fcBufferSelect, r"\Working\buffer_sites.shp")
    fcBufferSites1 = arcpy.SelectLayerByLocation_management(fcFarms,"INTERSECT", fcBufferSites)

    #Buffer new selection, clip, break into multiparts, select and extract buffer part with sites
    arcpy.Buffer_analysis(fcBufferSites1,r"\Working\farms_buffer1.shp", inDis1,"FULL","ROUND","ALL")
    arcpy.Clip_analysis(r"\Working\farms_buffer1.shp",clipShape,r"\Working\buffer_clip1.shp")
    arcpy.MultipartToSinglepart_management(r"\Working\buffer_clip1.shp",r"\Working\buffer_multi1.shp")
    fcBuffer1 = arcpy.MakeFeatureLayer_management(r"\Working\buffer_multi1.shp","BufferTemp")
    fcBufferSelect1 = arcpy.SelectLayerByLocation_management(fcBuffer1,"INTERSECT", r"\Working\buffer_sites.shp")

    # Check for existence of old buffer in ArcMap before deleting
    ##if arcpy.Exists(outBuffer):
       ##arcpy.Delete_management(outBuffer)

    #Export final Buffer to file
    fcFinal = arcpy.CopyFeatures_management(fcBufferSelect1, outBuffer)    

    #Select sites within buffer
    fcFarmsFinal = arcpy.SelectLayerByLocation_management(fcFarmsAll,"INTERSECT", outBuffer)

    #Delete working files
    arcpy.Delete_management(r"\Working\farms_buffer.shp")
    arcpy.Delete_management(r"\Working\farms_buffer1.shp")
    arcpy.Delete_management(r"\Working\buffer_clip.shp")
    arcpy.Delete_management(r"\Working\buffer_clip1.shp")
    arcpy.Delete_management(r"\Working\buffer_multi.shp")
    arcpy.Delete_management(r"\Working\buffer_multi1.shp")
    arcpy.Delete_management(r"\Working\farms_Select.shp")
    arcpy.Delete_management(r"\Working\buffer_sites.shp")
    arcpy.Delete_management(r"\Working\marineFarms.shp")

except:
    print arcpy.GetMessages()
1
  • The while loop at "#Loop through selection" comment isn't formatted correctly with the indents, so its hard to tell what is part of the loop. I am assuming that the 3 lines under "while getCount > initial_getCount:" are part of the loop, but if you could fix the code, that would be helpful.
    – TsvGis
    Commented Jun 19, 2015 at 6:54

1 Answer 1

0

Through testing and trial-and-error, I have discovered it's not the loop but the dissolve function within buffer - if you remove the ALL from the buffers it works.

I need the buffers to dissolve so will raise as separate issue.

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.