Hi everyone,
I’ve been trying to solve this one for a couple of days, so any help would be greatly appreciated!
I have created a Script Tool to perform the following tasks:
- Buffer the selected features, using arcpy.Buffer_analysis
- Select the features that intersect the buffer, using arcpy.SelectLayerByLocation_management, and add them to the existing selection
- Return two output parameters (the Buffer and the new Selected Features) as Feature Layers
The Script Tool code is below:
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="comment token"># Get input parameters</SPAN>
inputFeatureSet <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
bufferDistanceMetres <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Create buffer</SPAN>
outputBufferLayer <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Buffer_analysis<SPAN class="punctuation token">(</SPAN>
in_features<SPAN class="operator token">=</SPAN>inputFeatureSet<SPAN class="punctuation token">,</SPAN>
out_feature_class<SPAN class="operator token">=</SPAN><SPAN class="string token">'in_memory/Buffer'</SPAN><SPAN class="punctuation token">,</SPAN>
buffer_distance_or_field<SPAN class="operator token">=</SPAN><SPAN class="string token">'{0} meters'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>bufferDistanceMetres<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
line_side<SPAN class="operator token">=</SPAN><SPAN class="string token">'FULL'</SPAN><SPAN class="punctuation token">,</SPAN>
line_end_type<SPAN class="operator token">=</SPAN><SPAN class="string token">'ROUND'</SPAN><SPAN class="punctuation token">,</SPAN>
dissolve_option<SPAN class="operator token">=</SPAN><SPAN class="string token">'ALL'</SPAN><SPAN class="punctuation token">,</SPAN>
dissolve_field<SPAN class="operator token">=</SPAN>None
<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Select intersecting features</SPAN>
outputFeatureLayer <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation_management<SPAN class="punctuation token">(</SPAN>
in_layer<SPAN class="operator token">=</SPAN>inputFeatureSet<SPAN class="punctuation token">,</SPAN>
overlap_type<SPAN class="operator token">=</SPAN><SPAN class="string token">'INTERSECT'</SPAN><SPAN class="punctuation token">,</SPAN>
select_features<SPAN class="operator token">=</SPAN>outputBufferLayer<SPAN class="punctuation token">,</SPAN>
selection_type<SPAN class="operator token">=</SPAN><SPAN class="string token">'ADD_TO_SELECTION'</SPAN>
<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> Exception<SPAN class="punctuation token">,</SPAN> e<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Set output parameters</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SetParameter<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> outputBufferLayer<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SetParameter<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> outputFeatureLayer<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>The parameters are configured as follows:
| Display Name | Data Type | Direction |
|---|
| InputFeatures | Feature Set | Input |
| BufferDistanceMetres | Long | Input |
| OutputBufferLayer | Feature Layer | Output |
| OutputFeatureLayer | Feature Layer | Output |
Ultimately, I need to call the GP Service from the Geoprocessing Widget in Web AppBuilder. The Script Tool works fine in ArcMap, but when I publish it as Geoprocessing Service, it fails in SelectLayerByLocation_management with the following error:
Failed to execute. Parameters are not valid.
ERROR 000368: Invalid input data.
Failed to execute (SelectLayerByLocation).
The error is because SelectLayerByLocation_management expects a Feature Layer instead of a Feature Set. However, the Script Tool input parameter must be a Feature Set in order for the user to be able to select a layer in the Geoprocessing Widget. If I try to create a Feature Layer from the Feature Set, only the originally selected features are included (instead of all features), so the selection cannot be added to when intersecting with the Buffer. Including the InputFeatures as an additional input parameter with type Feature Layer could work, but I don't believe that's possible because it would also have to be selected by the user.
Besides creating a custom widget, does anyone know of a way to do this?
Thanks,
Wayne