I have a buildings feature class which contains a building in a building (4 times), and I want to extract each building to a new feature class which created. I have written a Python code that does half what I want.
- it creates a new field (count in)
- it makes a feature class
- selection by location and then by attribute
- creates a new feature class for results
I want that script extract the single building and give them the number 1 in "count_ in" field, and then copy the row to the new fc(fc_1). then extract the building in building and give the number 2 to the feature class which selected, and copy to the new fc(fc_2). exactly to the other feature (3 bldg and 4 bldg).
this is the code:
#this script selected(by location)bldg in bldg, and print the result by 1 or 2 #or 3 (or more)in a new field("count_in")if was select bldg in bldg. print "Start" import time import arcpy from time import strftime arcpy.env.overwriteOutput = 1 FC_BLDG = r"D:\AVI_DAVID\DB_CORRECTION_TEST\test.gdb\poly_test" gdb_name = "bldg_in_bldg_check"+'_'+strftime("%Y-%m-%d_%H%M")# GDB_Destination = r"D:\AVI_DAVID\DB_CORRECTION_TEST"# GDB = arcpy.CreateFileGDB_management(GDB_Destination,gdb_name+'_'+strftime("%Y-%m-%d_%H%M")) #create gdb+location count = arcpy.GetCount_management(FC_BLDG) for con in count: print con ," TOTAL bldg COUNT" print"" print"" ##fieldList = arcpy.ListFields(FC_BLDG) ##fieldListName= ##for field in fieldList: ## fieldListName.append(field.name) ## if "count_in" not in fieldListName: ## arcpy.AddField_management(FC_BLDG,"count_in") ## arcpy.CalculateField_management(FC_BLDG,"count_in","1") def count_inside(FC_BLDG,c): fieldList = arcpy.ListFields(FC_BLDG) fieldListName= for field in fieldList: fieldListName.append(field.name) if "count_in" not in fieldListName: arcpy.AddField_management(FC_BLDG,"count_in") if c == 1: exp = '"count_in" IS NULL' else: exp = '"count_in" IS NOT NULL' lyr1= arcpy.MakeFeatureLayer_management(FC_BLDG,"lyr1",exp) lyr2= arcpy.MakeFeatureLayer_management(FC_BLDG,"lyr2",exp) print "count features before selection",arcpy.GetCount_management(lyr1) arcpy.SelectLayerByLocation_management(lyr1,"COMPLETELY_WITHIN",lyr2, "","NEW_SELECTION") NEW = arcpy.CreateFeatureclass_management(GDB,"BLDG"+str(c),"POLYGON",FC_BLDG) arcpy.CopyFeatures_management(lyr1,NEW) try: arcpy.CalculateField_management(lyr1,"count_in",c) except: print arcpy.GetMessages(2) print "count features after selection",arcpy.GetCount_management(lyr1) return arcpy.GetCount_management(lyr1) c=1 count_inside(FC_BLDG,c) c=2 count_inside(FC_BLDG,c) c=3 count_inside(FC_BLDG,c) c=4 count_inside(FC_BLDG,c) print "-------OK-------"