I am currently to make a Python toolbox in ArcGIS Pro to search and count for tourist attraction/activities that I can decide at some distance to at least two infrastructure facilities, and also using buffering around the facilities then selectbylocation() then be stored in a CSV file.
However, the issue appears relation between buffering distance and the filtering to the specific facilities and attractions from two point shapefiles. The output of CSV is the same regardless of with and without filters. See code:
def getParameterInfo(self):
"""Define parameter definitions"""
params = [
arcpy.Parameter(
displayName="Tourist Attractions",
name="tourist_attractions",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input"
),
arcpy.Parameter(
displayName="Infrastructure Facilities",
name="infrastructure_facilities",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input"
),
arcpy.Parameter(
displayName="Attraction Type Field",
name="attraction_type_field",
datatype="Field",
parameterType="Required",
direction="Input"
),
arcpy.Parameter(
displayName="Infrastructure Type Field",
name="infrastructure_type_field",
datatype="Field",
parameterType="Required",
direction="Input"
),
arcpy.Parameter(
displayName="Buffer Distance",
name="buffer_distance",
datatype="GPLong",
parameterType="Required",
direction="Input"
),
arcpy.Parameter(
displayName="Output CSV File",
name="output_csv",
datatype="DEFile",
parameterType="Required",
direction="Output"
)
]
params[2].parameterDependencies = [params[0].name] # Set dependencies for attraction type field
params[3].parameterDependencies = [params[1].name] # Set dependencies for infrastructure type field
return params
def execute(self, parameters, messages):
"""The source code of the tool."""
tourist_attraction_layer = parameters[0].valueAsText
infrastructure_facilities_layer = parameters[1].valueAsText
attraction_type_field = parameters[2].valueAsText
infrastructure_type_field = parameters[3].valueAsText
buffer_distance = int(parameters[4].valueAsText)
output_csv = parameters[5].valueAsText
# Create a feature layer for infrastructure facilities
arcpy.MakeFeatureLayer_management(in_features=infrastructure_facilities_layer, out_layer="infrastructure_layer")
# Create a feature layer for tourist attractions
arcpy.MakeFeatureLayer_management(in_features=tourist_attraction_layer, out_layer="tourist_layer")
# Set the buffer unit to meters
buffer_unit = 'Meters'
# Use Spatial Join to find tourist attractions within the buffer distance of infrastructure facilities
arcpy.analysis.SpatialJoin(
target_features="tourist_layer",
join_features="infrastructure_layer",
out_feature_class="spatial_join_output",
join_type="KEEP_COMMON",
search_radius=f"{buffer_distance} {buffer_unit}" # Include unit buffer_unit
)
# Further filter based on the type of infrastructure facility
sql_expression = f"{infrastructure_type_field} IN ('airport', 'harbour') AND {attraction_type_field} = 'Beach'"
arcpy.SelectLayerByAttribute_management("spatial_join_output", "NEW_SELECTION", sql_expression)
# Count the number of selected tourist attractions
result_count = int(arcpy.GetCount_management("spatial_join_output").getOutput(0))
arcpy.AddMessage(f"Number of selected attractions: {result_count}")
# Export the count to a CSV file
csv_output = f"{output_csv}.csv"
with open(csv_output, 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(["Attribute", "Count"])
csv_writer.writerow(["Number of selected attractions", result_count])
return
sql_expressionis structured correctly? I think it may need some quote marks" "or square brackets[ ]around the field names if I'm reading your code correctly