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.
1 Answer 1
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"))
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