3

I've been trying to use the select layer by location tool in a loop to select features that are within a specific distance to one feature class for each feature in a different feature class (each retaining wall has a search distance around it of twice its height). Unfortunately I've realized from Esri Support that it isn't possible to use a FIELD as the argument for the "Search Distance" Parameter.

Is there any other way to do what I'm trying to do here without using the Select Layer by Location tool? I'll post the relevant pieces of my code below.

Retain=arcpy.GetParameterAsText(0)
TSafetyRW=arcpy.GetParameterAsText(1)

if Retain=="":
    Retain=dataPath+"/LayerHome.gdb/Retaining_Walls"
if TSafetyRW =="":
    TSafetyRW=dataPath+"/LayerHome.gdb/Transportation_Safety_RW"

inRetain=os.path.splitext(os.path.basename(Retain))[0]
inTSafetyRW=os.path.splitext(os.path.basename(TSafetyRW))[0]

TranspoSafetyScore = "Socio_Transpo_Safety"
Wall_Height = "RW_Height"
Road_to_Wall_Dist = "Distance"
Road_Class = "Class_Type"
Twice_Height = "Threshold_Distance"

##Adding and assigning a default value to some fields
arcpy.AddField_management(inRetain, TranspoSafetyScore, "FLOAT")
arcpy.CalculateField_management(inRetain, TranspoSafetyScore, 1)

##Tuples used to assign a Road Class to a Retaining wall
Road_to_Wall = [("No Road", 1),
                ("MUP", 1),
                ("Sidewalk", 1),
                ("Collector", 2),
                ("Major Collector", 2),
                ("Railway", 3),
                ("Arterial", 4),
                ("Transitway", 4),
                ("City Freeway", 4)]

##Tuple calculation which creats a temporary layer of the TSafetyRW FC for each road class
for pairs in Road_to_Wall:
    (roadclass, score) = pairs
    arcpy.MakeFeatureLayer_management(TSafetyRW, "justselected", "\""+Road_Class+"\"='"+str(roadclass+"'"))
    arcpy.SelectLayerByLocation_management(inRetain,"WITHIN_A_DISTANCE","justselected", Twice_Height,"NEW_SELECTION")
    arcpy.CalculateField_management(inRetain, TranspoSafetyScore, score)
    arcpy.SelectLayerByAttribute_management(inRetain, "CLEAR_SELECTION")
    arcpy.Delete_management("justselected")

As I mentioned above, I know that you can't have a field as the search distance parameter for the Select by Location tool so I know that this code won't run properly.

I specified the type of variable that I wanted to use for the search distance parameter in the first paragraph


In other words, I want to use a field from the Target Feature Class as a variable, so it's constantly changing with each retaining wall as the loop is executed. In this case I want to use the Twice_Height variable as the search distance, which is the field "Threshold_Distance" which is a part of the target Feature Class "inRetain".

closed as unclear what you're asking by BERA, whyzar, Andre Silva, xunilk, Jochen Schwarze Dec 20 '18 at 8:50

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • In the retaining wall layer you could calculate a field that is 2x the height. Then buffer the layer based on this field. Then perform the select by location and use intersects with or the appropriate topology rule you need. – landocalrissian Dec 18 '18 at 14:26
  • I don't see anything that says you can't have a variable for search_distance -- desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/…. You just need to make sure you specify the linear unit as part of this e.g. twice_height = str(height * 2) + " Meters" – smiller Dec 18 '18 at 14:38
  • Buffer the retaining walls, and then run SelectLayerByLocation using Intersect. Or, if you need to specifically identify which road is within the distance of which retaining wall, then your code is on the right path. You'll just need to use a cursor to iterate over the features and read the retaining wall name(?) and its height. – Tom Dec 18 '18 at 15:28
  • Still unclear what you are asking. Are you asking how to use arcpy.GetParameterAsText() and select values from a field? – BERA Dec 19 '18 at 6:01
4

Your question is a bit unclear. But you can construct a search distance in whatever way and from whatever source you want as long as it is formatted correctly. For example 100 wont work, but "100 Meters" will.

Example of using a variable:

distance = "100 Meters"
arcpy.SelectLayerByLocation_management("AnglePoints_1","WITHIN_A_DISTANCE","AnglePoints",distance)

enter image description here

Or use values in a field:

fc_with_searchdistances = r'C:\data.gdb\featureclass'
integer_field_with_distances = 'distancefield'

search_distances = ["{0} Meters".format(i[0]) for i in arcpy.da.SearchCursor(fc_with_searchdistances,integer_field_with_distances)]

for distance in search_distances:
    arcpy.SelectLayerByLocation_management(..., search_distance=distance,...
  • 2
    To compliment this answer, it's worth emphasising that distance variable is a string which is a number, space then units, e.g. "300 Miles". The syntax section of the help file calls this parameter type a Linear Unit. – Hornbydd Dec 18 '18 at 14:51
  • 1
    Ah I should have worded the question differently, I wanted to use a field from the Target Feature Class as a variable, so it's constantly changing with each retaining wall as the loop is executed. In this case I wanted to use the Twice_Height variable as the search distance, which is the field "Threshold_Distance" which is a part of the target Feature Class "inRetain". – Adam Reed Dec 18 '18 at 15:06
  • I've edited the question to clear this up. – landocalrissian Dec 18 '18 at 20:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.