Hello,
Can somebody advise me on how to code this properly? The below script was attempted using google AI snippets to create an in memory buffer layer of 500 feet based on the feature currently selected in the map. It throws an error when trying to run it. Also, if there is any advice on creating an editable layer from all features that intersect the buffer. Eventually, I plan to publish everything as a GP service.
import arcpy
import arcpy.mp
class Toolbox:
def __init__(self):
self.label = "Custom Buffer Tools"
self.alias = "CustomBuffer"
self.tools = [BufferSelectedFeatures] # Add your tool class here
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):
param0 = arcpy.Parameter(
displayName="Input Feature Layer",
name="in_features",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param1 = arcpy.Parameter(
displayName="Output Buffer Layer Name",
name="out_buffer_name",
datatype="GPString",
parameterType="Required",
direction="Input")
param1.value = "Selected_Feature_Buffer" # Default name
return [param0, param1]
def execute(self, parameters, messages):
in_features = parameters[0].valueAsText
out_buffer_name = parameters[1].valueAsText
# Set the scratch workspace for temporary data
arcpy.env.scratchWorkspace = "in_memory"
# Create the full path for the output buffer feature class
out_buffer_fc = arcpy.ValidateTableName(out_buffer_name, arcpy.env.scratchWorkspace)
# Buffer the selected features
arcpy.Buffer_analysis(in_features, out_buffer_fc, "500 Feet")
messages.addMessage(f"Buffer created: {out_buffer_fc}")
# Add the buffer layer to the map (for ArcGIS Pro)
aprx = arcpy.mp.ArcGISProject("CURRENT")
active_map = aprx.activeMap
# Make a feature layer from the buffered output
arcpy.management.MakeFeatureLayer(out_buffer_fc, out_buffer_name)
new_layer = arcpy.mp.Layer(out_buffer_name) # Create a Layer object from the temporary layer
# Add the layer to the active map
active_map.addLayer(new_layer)
messages.addMessage(f"Buffer layer '{out_buffer_name}' added to the map.")
return-Brandon