Hello,
I am working on a python toolbox. I posted a couple weeks earlier about how to create a buffer layer based on the selected feature which I received help on.
The next step was for the toolbox to create an in memory layer from selected parcels that intersect the buffer. It seems to work, although the layer doesn't turn on/off when checked/unchecked. Also, the buffer layer shows up underneath it and nothing happens even when moved on top of it in the table of contents. Is there anything that can be done to fix these issues?
import arcpy
class Toolbox:
def __init__(self):
self.label = "Custom Buffer Tools"
self.alias = "CustomBuffer"
self.tools = [BufferSelectedFeatures]
class BufferSelectedFeatures(object):
def __init__(self):
self.label = "Buffer Selected Features"
self.description = "Creates a 500-foot buffer around selected features."
self.canRunInBackground = False
def getParameterInfo(self):
in_features = arcpy.Parameter(
name="in_features",
displayName="Input Feature Layer",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
out_buffer_name = arcpy.Parameter(
name="out_buffer_name",
displayName="Output Buffer Layer Name",
datatype="GPString",
parameterType="Required",
direction="Input")
out_buffer_name.value = "Selected_Feature_Buffer" # Default name
out_intersect_name = arcpy.Parameter(
name="out_intersect_name",
displayName="Output Intersect Layer Name",
datatype="GPString",
parameterType="Required",
direction="Input")
out_intersect_name.value = "Intersected_Features" # Default name
return [in_features, out_buffer_name, out_intersect_name]
def execute(self, parameters: list[arcpy.Parameter], messages):
params = {p.name: p for p in parameters}
in_features = params['in_features'].value
out_intersect_name = params['out_intersect_name'].value
out_buffer_name = params['out_buffer_name'].value
buffer_layer_file = r"C:\Documents\ArcGIS\Projects\Existing Land Use Generator\Existing_LU_Buffers (500ft).lyrx"
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0] # Assuming "Map" is the name of your map
parcel_layer = m.listLayers("ASSR_PARCELS")[0] # Replace "MyInputLayerName" with your layer's name
with arcpy.EnvManager(workspace='memory'):
# Create the full path for the output buffer feature class
out_intersect_fc = arcpy.ValidateTableName(out_intersect_name)
out_buffer_fc = arcpy.ValidateTableName(out_buffer_name)
# Buffer the selected features
arcpy.analysis.Buffer(in_features, out_buffer_fc, "500 Feet")
# Make a feature layer from the buffered output and style it using layer file
new_layer = arcpy.management.MakeFeatureLayer(out_buffer_fc, out_buffer_name)[0]
final_layer = arcpy.management.ApplySymbologyFromLayer(new_layer, buffer_layer_file)[0]
arcpy.AddMessage(f"Buffer created: {out_buffer_fc}")
arcpy.SelectLayerByLocation_management(parcel_layer, "INTERSECT", new_layer, "", "NEW_SELECTION", "")
arcpy.CopyFeatures_management(parcel_layer, out_intersect_fc)
parcel_intersect = arcpy.management.MakeFeatureLayer(out_intersect_fc, out_intersect_name)[0]
arcpy.SelectLayerByAttribute_management(parcel_layer, "CLEAR_SELECTION")
# Add the layer to the active map
aprx.activeMap.addLayer(final_layer)
aprx.activeMap.addLayer(parcel_intersect)
arcpy.AddMessage(f"Buffer layer '{out_buffer_name}' added to the map.")
Solved! Go to Solution.
Please disregard the above post. I couldn't delete it. Everything seems ok now, maybe there was just a lag.
Please disregard the above post. I couldn't delete it. Everything seems ok now, maybe there was just a lag.