enter image description here

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.

  1. it creates a new field (count in)
  2. it makes a feature class
  3. selection by location and then by attribute
  4. creates a new feature class for results
  5. count

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 = 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"

##fieldList = arcpy.ListFields(FC_BLDG)
##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)
    for field in fieldList:
    if "count_in" not in fieldListName:

    if c == 1:
        exp = '"count_in" IS NULL'
        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)

        print arcpy.GetMessages(2)

    print "count features after selection",arcpy.GetCount_management(lyr1)
    return arcpy.GetCount_management(lyr1)





print "-------OK-------"
  • 1
    Your question is very difficult ro read With Every Word Capitalized. Please Edit it to follow proper English usage. – Vince May 11 '17 at 11:14
  • I agree with @Vince your question is very difficult to read. Please edit your question so that not every word is capitalized which will make it easier to read and understand what you are asking. – Midavalo May 11 '17 at 16:19
  • The feature class also contain buildings that have no building inside them and you dont want to export these? If not then it is easy to create a new fc from each feature – BERA May 16 '17 at 11:57
  • Yes. there are buildings without building, which mean- single. those that contain single building will have the number 1 in "count_in" field. rest of the building will have 2,3,4 …. depending on what it contains – avi May 16 '17 at 12:28

This is quick and dirty. This script works for me on getting the counts of parcels "COMPLETELY_WITHIN". It adds the field if it doesn't exist and goes though each row in the polygon fc and updates the "count_in" field no matter if it is NULL or NOT. You could esily add an if statement after the select by attribute and test if row[1] is null. Im also not sure if the select by location method "COMPLETELY_WITHIN" is what you wanted. Hope this helps

import arcpy

path = r"Connection\To\FC"

#layer names
parcels = "parcels"
parcelsSelected = "parcelsSelected"

#feature layers
arcpy.MakeFeatureLayer_management(path, parcels)
arcpy.MakeFeatureLayer_management(path, parcelsSelected)

fieldNames = [field.name for field in arcpy.ListFields(parcels)]
if "count_in" not in fieldNames:
    print "adding 'count_in' field"
    print "count_in field already exists"

with arcpy.da.UpdateCursor(parcels,['OBJECTID','count_in']) as cur:
    for row in cur:
        where_clause = "OBJECTID = {0}".format(row[0])
        arcpy.SelectLayerByAttribute_management(parcels, "NEW_SELECTION", where_clause)
        arcpy.SelectLayerByLocation_management(parcelsSelected, "COMPLETELY_WITHIN", parcels, "", "NEW_SELECTION", "NOT_INVERT")
        c_child = arcpy.GetCount_management(parcelsSelected)
        print "OBJECTID: {0}, # of Parcels Within: {1}".format(row[0],c_child)
        row[1] = int(c_child.getOutput(0)) + 1 #+1 to count the selected feature itself
  • Thank you for your answer. I try to set it up with my data (change names- that all), and it's just give the number by order 1,2,3… in "count_in" field. There is no really buildings extracting – avi May 17 '17 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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