0

I need to make a function to call in another script to count the buildings within an area using a field value UseCode

Here is what I have with my test script. I know the make feature layer is not working because it gives me the result of 7 no matter what use code I use, that feature class the only one that should give 7 is use code 3

#Count Buildings
def CountBuildings(bldgfc,bldgcode,boxfc):
 arcpy.MakeFeatureLayer_management(bldgfc, "bldgs", '[UseCode] = ' + bldgcode, wrkspc,)
 sfbl = arcpy.management.SelectLayerByLocation("bldgs", 'HAVE_THEIR_CENTER_IN', boxfc)
 bldgcount = arcpy.management.GetCount(sfbl)
 return bldgcount
#test script
print(CountBuildings("BldgFootprints",'0',"FireBoxMap_0"))

wrkspc is my workspace I defined it earlier in the script. the issue I think lies in this '[UseCode] = ' + bldgcode bldgcode is a user input, but I am lost on how to include that in a SQL expression.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Dec 11, 2022 at 4:02
2
  • 1
    String math isn't best practice (slow and subject to error); use str.format() instead. It's unclear if your data type is integer or string or something else. It also makes a significant difference as to the data provider asto how columns are referenced. Please [Rdit] the Question Commented Dec 11, 2022 at 4:24
  • the data type is long int in arc Commented Dec 11, 2022 at 4:27

1 Answer 1

0

Thanks to @Vince I was able to get it. Was a formatting issue.

 #Count Buildings
def CountBuildings(bldgfc,bldgcode,boxfc):
 arcpy.MakeFeatureLayer_management(bldgfc,"bldgs",'UseCode ='+format(bldgcode))
 sfbl = arcpy.management.SelectLayerByLocation("bldgs",'HAVE_THEIR_CENTER_IN', boxfc)
 bldgcount = arcpy.management.GetCount(sfbl)
 return bldgcount
#test script
print(CountBuildings("BldgFootprints",'3',"FireBoxMap_0"))
Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
answered Dec 11, 2022 at 4:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.