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.

EDIT: Too big a question, I think - so, to refine it:

Please help me define (and therefore, research/learn how to use) the Pythonic concept for writing a script that can take one input variable, compare it against a list of other variables, and then holding onto both those variables, apply those IDs to an Arc function.

In this case, I need to take a FC, and with it's ID#, compare it to another FC with matching ID#. Then I need to read each record in the first FC, and then find and select the matching record in the matching FC. My script below does this but compares ALL the features in each FC to every other feature, in every other FC, in the fGDB.

I've left the initial question format below for context....


Thanks to the GIS StackExchange community helping with this challenge, I was able to develop the script below that almost accomplishes my goals.

However, I think I need a little guidance in the logic and theory behind the structure of this script. The script matches, within the fGDB, the featureID from one feature class to a matching featureID in another feature class. Something about the structure results in only creating rings for features in one feature class, while the goal is to create rings for each feature of each feature class in the fGDB.

I don't mean to request a script re-write, but I would appreciate someone helping point out the flaws in my logic structure for the script I've added here. It's a mess of for-loops and if-statements, that would be improved with defining functions, I just don't know how. Thanks very much!

#Test script: select record and erase it from another, matching selected record

import os
import sys
import arcpy
from arcpy import env
import time

#Set overwrote output
arcpy.env.overwriteOutput = True
print arcpy.env.overwriteOutput

workspace = "C:\\BOEM\\pt_buff3\\"

fileList = []
buffersd4 = []
buffersd3 = []

buffs = workspace+"BUFFTEST.gdb"
directorypath = buffs

#Create new results_feature class
for dirpath, dirnames, buffs in arcpy.da.Walk(directorypath,
                                              datatype="FeatureClass",
                                              type="Polygon"):
#For all feature classes in the Buffer fGDB...
    for buff in buffs:
        buffbits=buff.split('_')
        if buffbits[4] == str('d4'):
            buffersd4.append(buff)
            field = ['APR16_ID']
            #For Feature Classes that end in 'd4', read through Features for the APR16_ID...
            with arcpy.da.SearchCursor(directorypath+"\\"+buff, ['APR16_ID']) as cursor:
                for ID4 in cursor:
                    print ID4
                #Create a list called APR16_ID4 of APR16IDs for the records in the d4 feature
                APR16_ID4 = [row[0] for row in arcpy.da.SearchCursor(workspace+"BUFFTEST.gdb\\"+buff,['APR16_ID'])]

        elif buffbits[4] == str('d3'):
            #For Feature Classes that end in 'd3', read through Features for the APR16_ID...
           buffersd3.append(buff)
           field = ['APR16_ID']
           with arcpy.da.SearchCursor(directorypath+"\\"+buff, ['APR16_ID']) as cursor:
               for ID3 in cursor:
                   print ID3
               #Create a list called APR16_ID3 of APR16IDs for the records in the d3 feature    
               APR16_ID3 = [row[0] for row in arcpy.da.SearchCursor(workspace+"BUFFTEST.gdb\\"+buff,['APR16_ID'])]
        elif ID4 == ID3:
            print "early on, they match" 
        else:
            print "no interesting files here"
#New For-Loop:
#If the feature ID matches, make feature layer and select feature by the ID4 and ID3
#from the feature class with a matching buffbits[4] value. 

    for buff in buffs:            
        for ID4 in APR16_ID4:
            for ID3 in APR16_ID3:
                if ID4 == ID3:
                    print "They really really match!" 

                    #make feature layer of feature d4 for FCs in buffersd4:
                    for fc4 in buffersd4:
                            in_feat=workspace+"BUFFTEST.gdb\\"+fc4
                            out_layer4= fc4+"_lyr"
                            arcpy.MakeFeatureLayer_management(in_feat,out_layer4)
                            print "Make Feature Layer for " + fc4
                            print "___"
                            #print arcpy.GetMessages()

                    #select record in fc_layer
                            where_clause4=""" "APR16_ID" = """ + "'"+ID4+"'"
                            arcpy.SelectLayerByAttribute_management(out_layer4,"NEW_SELECTION",where_clause4)
                            #print arcpy.GetMessages()
                            print "SelectLayer for "+ID4+" and "+out_layer4
                            print "______"

                    #make feature layer of feature d3
                    for fc3 in buffersd3:
                            in_feat=workspace+"BUFFTEST.gdb\\"+fc3
                            out_layer3= fc3+"_lyr"
                            arcpy.MakeFeatureLayer_management(in_feat,out_layer3)
                            #print arcpy.GetMessages()
                            print "Make Feature Layer for " + fc3
                            print "___"

                    #select record in fc_layer
                            where_clause3=""" "APR16_ID" = """ + "'"+ID3+"'"
                            arcpy.SelectLayerByAttribute_management(out_layer3,"NEW_SELECTION",where_clause3)
                            #print arcpy.GetMessages()
                            print "SelectLayer for " +ID3+" and " + out_layer3
                            print "______"

                    #execute Erase function(erase d3 from d4)
                    out_feat = "C:\\BOEM\\Donuts\\Donuts_output.gdb\\"+"don_d4_"+ID4
                    arcpy.Erase_analysis(out_layer4,out_layer3,out_feat,"")
                    #print arcpy.GetMessages()
                    print "Erase "+ out_layer3 +" from "+out_layer4+"to produce "+out_feat
                    print "___"
                    print "___"
                else:
                    print "they don't match, next."

            print "done checking features for this feature class."

    print "done going through the buffers inthe fGDB"    

(Hi, Moderators - Although this may be a StackOverflow-type question, I think it's still dealing with arcpy-specific syntax/script issues, so I left it in GIS StackExchange. If you disagree than please go ahead and migrate the conversation.)

share|improve this question
    
@Chad Cooper, would you just note what edits you made? Thank you! –  SharonB Jun 27 '13 at 15:27
1  
You should be able to see the revision history via the link edited 1 hour ago , but all I did was some capitalizations in the title and I added a formatting tag to the code block to color-code it. –  Chad Cooper Jun 27 '13 at 16:30
    
@Chad - thanks for edits and coloring in my python script, it helps. –  SharonB Jun 27 '13 at 17:50
add comment

1 Answer

After several drafts, I finally figured out I was over thinking my strategy. The main core of the solution is to take advantage of the matching feature class names, which greatly simplifies the work. I've copied the working, final script below. It includes three iterations of the task, resulting in a 'donut' each. from (d4-d3),(d3-d2), and (d2-d1).

import os
import sys
import arcpy
from arcpy import env
import time

#Set overwrote output
arcpy.env.overwriteOutput = True
print arcpy.env.overwriteOutput

workspace = "C:\\BOEM\\pt_buff2\\"

fileList = []
buffersd4 = []
buffersd3 = []

buffs = workspace+"buffer.gdb"
directorypath = buffs
for dirpath, dirnames, fgdb in arcpy.da.Walk(directorypath,
                                              datatype="FeatureClass",
                                              type="Polygon"):
    for fc in fgdb:
        #fc_split=fc.split('_')
        StartTime=time.clock()
        if fc.endswith("d4"):
         #   if str(fc_split[1]) != str(100):
                #list features by APR16_ID in that fc
            APR16_ID4 = [row[0] for row in arcpy.da.SearchCursor(buffs+"\\"+fc,['APR16_ID'])]

            # make feat layer of fc_endswith_d4
            in_feat4 = buffs+"\\"+fc
            out_layer4 = fc+"_lyr"
            arcpy.MakeFeatureLayer_management(in_feat4,out_layer4)
            print "Make Feature Layer for " + fc

            #make feat layer of fc_endswith_d3
            in_feat3 = buffs +"\\"+ fc[:-2]+"d3"
            out_layer3 = fc[:-2]+"d3"
            arcpy.MakeFeatureLayer_management(in_feat3, out_layer3)
            print "Make Feature Layer for " + in_feat3

            for ID4 in APR16_ID4:
                where_clause4=""" "APR16_ID" = """ + "'"+ID4+"'"
                arcpy.SelectLayerByAttribute_management(out_layer4,"NEW_SELECTION",where_clause4)
                arcpy.SelectLayerByAttribute_management(out_layer3,"NEW_SELECTION",where_clause4)
                erase_out= "C:\\BOEM\\Donuts\\Donuts_d4_output.gdb\\"+"don_d4_"+ID4
                arcpy.Erase_analysis(out_layer4, out_layer3,erase_out,"")
                print "Erase "+ out_layer3 +" from "+out_layer4+" to produce "+erase_out
                print "___"

        if fc.endswith("d3"):
            #list features by APR16_ID in that fc
            APR16_ID3 = [row[0] for row in arcpy.da.SearchCursor(buffs+"\\"+fc,['APR16_ID'])]

            # make feat layer of fc_endswith_d3
            in_feat3 = buffs+"\\"+fc
            out_layer3 = fc+"_lyr"
            arcpy.MakeFeatureLayer_management(in_feat3,out_layer3)
            print "Make Feature Layer for " + fc

            #make feat layer of fc_endswith_d2
            in_feat2 = buffs+"\\"+ fc[:-2]+"d2"
            out_layer2 = fc[:-2]+"d2"
            arcpy.MakeFeatureLayer_management(in_feat2, out_layer2)
            print "Make Feature Layer for " + in_feat2

            for ID3 in APR16_ID3:
                where_clause3=""" "APR16_ID" = """ + "'"+ID3+"'"
                arcpy.SelectLayerByAttribute_management(out_layer3,"NEW_SELECTION",where_clause3)
                arcpy.SelectLayerByAttribute_management(out_layer2,"NEW_SELECTION",where_clause3)
                erase_out= "C:\\BOEM\\Donuts\\Donuts_d3_output.gdb\\"+"don_d3_"+ID3
                arcpy.Erase_analysis(out_layer3, out_layer2,erase_out,"")
                print "Erase "+ out_layer2 +" from "+out_layer3+" to produce "+erase_out
                print "___"

        if fc.endswith("d2"):
            #list features by APR16_ID in that fc
            APR16_ID2 = [row[0] for row in arcpy.da.SearchCursor(buffs+"\\"+fc,['APR16_ID'])]

            # make feat layer of fc_endswith_d2
            in_feat2 = buffs+"\\"+fc
            out_layer2 = fc+"_lyr"
            arcpy.MakeFeatureLayer_management(in_feat2,out_layer2)
            print "Make Feature Layer for " + fc

            #make feat layer of fc_endswith_d1
            in_feat1 = buffs+"\\"+ fc[:-2]+"d1"
            out_layer1 = fc[:-2]+"d1"
            arcpy.MakeFeatureLayer_management(in_feat1, out_layer1)
            print "Make Feature Layer for " + in_feat1

            for ID2 in APR16_ID2:
                where_clause2=""" "APR16_ID" = """ + "'"+ID2+"'"
                arcpy.SelectLayerByAttribute_management(out_layer2,"NEW_SELECTION",where_clause2)
                arcpy.SelectLayerByAttribute_management(out_layer1,"NEW_SELECTION",where_clause2)
                erase_out= "C:\\BOEM\\Donuts\\Donuts_d2_output.gdb\\"+"don_d2_"+ID2
                arcpy.Erase_analysis(out_layer2, out_layer1,erase_out,"")
                print "Erase "+ out_layer1 +" from "+out_layer2+" to produce "+erase_out
                print "___"

        EndTime = time.clock()
        f=open("C:\\BOEM\\Donuts\\donut_log.txt",'a')
        f.write(str("Created donuts for "+fc+" in %s minutes." % ((EndTime - StartTime)/60))+"\n")
        f.close()
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.