1

I'm attempting to create a script which assesses the intersect between a layer "BldgFootprints" and several polygons "FireBoxMap_1"-"FireBoxMap_44". And from this intersects performs a getCount on a particular attribute of the BldgFootprint layer and uses it to populate a newly created field in the FireBoxMap polygon attribute table. Essentially I'm trying to create a count of single family homes within particular fire districts using a buildings database and then adding this count to the fire district attribute table to tell at a glance how many homes are in each district. I am using PyCharm IDE and ArcGIS Pro.

So far I have:

import arcpy
try:
    arcpy.env.workspace = (r'L:\ProgrammingPro\FireDepartment.gdb')
    fcs = arcpy.ListFeatureClasses("FireBoxMap*")
    print (fcs)
    lyr = arcpy.ListFeatureClasses("B*")
    print (lyr)
    for fc in fcs:
        inFeatures = "fc"
        fieldName1 = "Single"
        fieldName2 = "Multi"
        arcpy.AddField_management(inFeatures, fieldName1, "SHORT", "","","","","","","")
        arcpy.AddField_management(inFeatures,fieldName2, "SHORT", "","","","","","","")
    bldg_lyr = arcpy.MakeFeatureLayer_management(b*,r'in_memory\bldg_lyr')
    firebox_lyr = arcpy.MakeFeatureLayer_management(FireBoxMap*,r'in_memory\firebox_lyr')
    firebox_bldg = arcpy.SelectLayerByLocation_management(bldg_lyr,'HAVE_THEIR_CENTER_IN',firebox_lyr*)       

I have a failed to execute error in the MakeFeatureLayer section and don't know where to go from there.

  • 1
    Welcome to GIS StackExchange! Please edit your question to include the specific error that you receive with the above code snippet. (I assume there's more code below this since it's missing an except clause.) Note: if the * appearing in the last line (firebox_lyr*) is in your code and isn't a typo, remove it. Also the MakeFeatureLayer_management needs to pass a filename and/or path, so replace b* with lyr and FireboxMap* with inFeatures or fc. – smiller Oct 4 '18 at 18:20
1

Please try this updated code sample based on the comments above. Some general notes: You need to pass a specific feature class or shapefile into MakeFeatureLayer_management, so need to remove the wildcards. Variables, full file paths, or file names within the workspace all work. Double-check the output of lyr as I think it's a list; in the make feature classes I assumed you'd want the first list element.

import arcpy
try:
    arcpy.env.workspace = (r'L:\ProgrammingPro\FireDepartment.gdb')
    fcs = arcpy.ListFeatureClasses("FireBoxMap*")  
    print (fcs)
    lyr = arcpy.ListFeatureClasses("B*")
    bldg_lyr = arcpy.MakeFeatureLayer_management(lyr[0],r'in_memory\bldg_lyr') # changed this line
    print (lyr)
    for fc in fcs:
        inFeatures = "fc"
        fieldName1 = "Single"
        fieldName2 = "Multi"
        arcpy.AddField_management(inFeatures, fieldName1, "SHORT", "","","","","","","")
        arcpy.AddField_management(inFeatures,fieldName2, "SHORT", "","","","","","","")

        firebox_lyr = arcpy.MakeFeatureLayer_management(fc,r'in_memory\firebox_lyr') # changed this line
        firebox_bldg = arcpy.SelectLayerByLocation_management(bldg_lyr,'HAVE_THEIR_CENTER_IN',firebox_lyr)     # changed this line
  • This worked! No errors, but I have some follow-up questions, I have 44 separate FireBox polygons. Is this syntax running a separate SelectLayerbyLocation for the single Building layer and each Firebox? Or just the Building Layer and the first listed Firebox? – Michael Phelps Oct 4 '18 at 19:23
  • As listed, this will do just the first firebox. Are you looking to match each individual firebox (44) to the same number of buildings (44) on a 1:1 basis, or to loop through each combination of firebox and buildings? – smiller Oct 4 '18 at 20:20
  • Based on this question and a re-read of your post I moved the three lines into the loop. Each individual firebox (for fc in fcs) is evaluated against one single bldg_lyr. You will need to modify if you want to evaluate against multiple building layers. – smiller Oct 4 '18 at 20:25

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.