I am new to using Python scripts to execute ArcGIS tools. In my first script I am attempting to loop through many Select Layer by Location operations on overlapping feature classes. I want to create a new feature class that contains all of the "Block" features that have their center in the "Coverage" feature. All selecting polygon feature classes are in a "Coverage" feature dataset and all input feature classes are in a "Blocks" feature dataset. Both feature datasets are in a File Geodatabase. The selection completes with no unexepected behavior in a batch run of a functionally similar ModelBuilder Model, but the Python script below produces empty feature classes for the selected features.

My code is below:

#Import arcpy module
import os
import arcpy
from arcpy import env

#Define workspace
env.workspace = "C:\\ArcPrj\\NFHL_Data.gdb"

#Local variables
#Feature dataset containing blocks
Block_fd = "C:\\ArcPrj\\NFHL_Data.gdb\\CensusBlocks"
#Feature class containing coverage polygons
Coverage_fd = "C:\\ArcPrj\\NFHL_Data.gdb\\NFHLCoveragePolys"
#Output location
Output_fd = "C:\\ArcPrj\\NFHL_Data.gdb\\NFHLBLocks"

#Create lists from feature datasets to select from
env.workspace = Block_fd
blockList = arcpy.ListFeatureClasses()
env.workspace = Coverage_fd
coverageList = arcpy.ListFeatureClasses()

#Iterate through each feature class in the Block dataset and the Coverage
#dataset and perform operations

#Change workspace to main gdb
env.workspace = "C:\\ArcPrj\\NFHL_Data.gdb"

for i in range(len(blockList)):  
    #Change workspace to main gdb
    env.workspace = "C:\\ArcPrj\\NFHL_Data.gdb"

    #Select the ith feature class in the feature dataset
    blocks = blockList[i]
    coverage = coverageList[i]

    #Make feature classes into feature layers
    arcpy.MakeFeatureLayer_management(blocks, "blocks_lyr")
    arcpy.MakeFeatureLayer_management(coverage, "coverage_lyr")

    #Select blocks that have centroids in the coverage polygon
    #(select layer by loc)
    arcpy.SelectLayerByLocation_management("blocks_lyr", "HAVE_THEIR_CENTER_IN", "coverage_lyr", selection_type="NEW_SELECTION")

    env.workspace = Output_fd
    #Copy selection to a feature class
    arcpy.CopyFeatures_management("blocks_lyr", blocks + "_NFHL")

    #Clear previous layers

As a Python beginner I appreciate any help you can give me.

  • Is this script the result of exporting your model to .py?
    – Roy
    Apr 23, 2012 at 17:51
  • No, for this script I mostly copied and pasted the python syntax from the ArcGIS documentation on the individual tools.
    – MTerry
    Apr 23, 2012 at 17:57
  • 1
    When you call arcpy.SelectLayerByLocation() you left in selection_type= too.
    – Roy
    Apr 23, 2012 at 18:00
  • 3
    Have you checked that the blocks layers and coverage layers are being listed in the expected order, and that each block layer spatially overlaps the coverage layer having the same index? Presumably there is a 1:1 relationship between the blocks layers and the coverage layers, but since we don't know your data this is going to be difficult to answer. I suggest placing debug/print statements or stepping through your code in a debugger and checking the state of your variables at each iteration.
    – blah238
    Apr 23, 2012 at 18:11
  • 1
    @blah238 - After closer inspection of how the feature classes are indexed within the feature dataset, it would appear that they are not indexed in alphabetical order as I anticipated. I sorted both lists using .sort() prior to iterating and this solved the problem. Thank you both for your prompt replies.
    – MTerry
    Apr 23, 2012 at 20:07

1 Answer 1


The "Blocks" features and "Coverage" features were not indexed alphabetically as anticipated. Thus the iteration selected feature classes that did not overlap as expected. Adding blockList.sort() and coverageList.sort() after defining each list made sure that the correct feature classes were selected in the for loop.

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.