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.
Any guidance much appreciated!
Liam
## 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 packageimport arcpy# Overwrite filesarcpy.env.overwriteOutput = True# Parameters# Workspace settingworkSpace = r"C:\GIS\Autozone"# Site ID as stringinSite = 'FS0501'# Distance as stringinDis = '800'# Name of farm data feature layerfarmData = "farms"# Location of clipping shape shapefileclipShape = "ClippingShape"# Location for buffer to be saved intooutBuffer = r"C:\GIS\Autozone\Outputs\buffer.shp"# Add meters to distance valueinDis1 = 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 settingarcpy.env.workspace = workSpacetry: # 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()
أكثر...
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.
Any guidance much appreciated!
Liam
## 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 packageimport arcpy# Overwrite filesarcpy.env.overwriteOutput = True# Parameters# Workspace settingworkSpace = r"C:\GIS\Autozone"# Site ID as stringinSite = 'FS0501'# Distance as stringinDis = '800'# Name of farm data feature layerfarmData = "farms"# Location of clipping shape shapefileclipShape = "ClippingShape"# Location for buffer to be saved intooutBuffer = r"C:\GIS\Autozone\Outputs\buffer.shp"# Add meters to distance valueinDis1 = 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 settingarcpy.env.workspace = workSpacetry: # 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()
أكثر...