Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
    arcpy.Delete_management("blocks_lyr")
    arcpy.Delete_management("coverage_lyr")

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

share|improve this question
    
Is this script the result of exporting your model to .py? –  Roy Apr 23 '12 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 '12 at 17:57
1  
When you call arcpy.SelectLayerByLocation() you left in selection_type= too. –  Roy Apr 23 '12 at 18:00
2  
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 '12 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 '12 at 20:07
show 3 more comments

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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