Working in ArcGIS Pro with ArcPy, I'm trying to create a script that creates a density based clustering, performs a select layer by attribute on it and then takes the extent of that selection and use it for my layout.
When run as a script from my toolbox, the script runs without fail except that it doesn't select any features. If I paste the code into the Python window it works 100% correctly and actually selects the features.
My code looks something like this:
#Import necessary modules import arcpy import os #Get project variables arcpy.env.overwriteOutput = True aprx = arcpy.mp.ArcGISProject('CURRENT') maps = aprx.listMaps("Map") GDB = aprx.defaultGeodatabase lyt = aprx.listLayouts("Boorpunten1") lyr = maps.listLayers("Boringen") mf = lyt.listElements("","Main") mf.map = maps #Group boringen for custom mapseries arcpy.stats.DensityBasedClustering("Boringen", "Boringen_DBC", "HDBSCAN", 4, None, 50) #Add the created layer to the map so it can be accessed by layer/layout functions dbc2 = arcpy.management.MakeFeatureLayer(GDB+"\\Boringen_DBC", "Boringen_DBC").getOutput(0) maps.addLayer(dbc2,'Bottom') dbc = maps.listLayers("Boringen_DBC") #Select features and zoom the camera to those features arcpy.management.SelectLayerByAttribute(dbc, "NEW_SELECTION", "CLUSTER_ID = 1", None) ex = mf.getLayerExtent(dbc, True, False) mf.camera.setExtent(ex)
What am I missing here? My guess is it has something to do with adding the newly created layer to the map document but I feel like I covered every aspect.