Hi,
I am trying to write a for loop that iterates line generation for 4 compass directions, using the "Bearing Distance to Line" tool on ArcGIS Notebooks.
Bascically, I want to input 4 fields into the line "Bearing field". I have stored the 4 fields in a list named "Dirfields". I'm trying to use this list as an input for "Bearing Field".
Any idea on how can I achieve this workflow? I have attached a photo of the Bearing Distance to line dialog box and an excerpt from my script below. Please let me know if there is anything else I can provide.
P.S. Reposting since I didn't get a response to my previous post.
Thank you
# Creating four fields (one for each compass direction) and specifying their values in the feature class "SusBui_Point". Values are in decimal degrees.
arcpy.management.CalculateField("SusBui_Point", "DirNorth", "0", "PYTHON3", '', "DOUBLE", "NO_ENFORCE_DOMAINS")
arcpy.management.CalculateField("SusBui_Point", "DirEast", "90", "PYTHON3", '', "DOUBLE", "NO_ENFORCE_DOMAINS")
arcpy.management.CalculateField("SusBui_Point", "DirSouth", "180", "PYTHON3", '', "DOUBLE", "NO_ENFORCE_DOMAINS")
arcpy.management.CalculateField("SusBui_Point", "DirWest", "270", "PYTHON3", '', "DOUBLE", "NO_ENFORCE_DOMAINS")
#Creating a list of the four fields
Dirfields = arcpy.ListFields ("SusBui_Point", "Dir*", "Double")
# Now I want to use a for loop to iterate line creation in each of the four compass direction using the "Bearing Distance to Line" tool and the list created in the previous step.
# Not sure how to get started
Solved! Go to Solution.
Ah, my mistake. You should be using field.name for the bearing_field parameter. The tool needs the field by name to work. I just tried it out on some dummy data and it appears to work now.
Below is at least one way to do it. The output feature class name in this case would be the field name. If you need to make the names more unique than that, you would have to append some kind of string to the name.
Note too you will have to specify the various parameters required by the Bearing Distance To Line tool Bearing Distance To Line (Data Management)—ArcGIS Pro | Documentation. This just shows where to put the variables:
for f in Dirfields:
arcpy.management.BearingDistanceToLine("SusBui_Point", f.name, x_field, y_field,
distance_field, {distance_units}, f,
{bearing_units}, {line_type},
{id_field}, {spatial_reference},
{attributes})
At a minimum, you still need to specify x_field, y_field, and distance_field, and then as many of the optional input parameters in {} as needed. Using the list of field objects, we put the f.name, or field's name as the output feature class. And we use f, or the field itself, as the bearing field.
for field in Dirfields:
arcpy.management.BearingDistanceToLine("SusBui_Point", "Dirfields.name", "POINT_X", "POINT_Y",
"LengthOfSampleLine", "METERS", "Dirfields", "DEGREES", "GEODESIC", None,
'{spatial_reference}', "NO_ATTRIBUTES")
Hi Daniel, thank you so much for your response, but I am still getting the following errors when I use this method -
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000354: The name contains invalid characters
ERROR 000728: Field Dirfields does not exist within table
Failed to execute (BearingDistanceToLine).
I have attached the code I used above. Note that I used the full spatial reference in my original code but I have shortened it here for readability.
I am very new to ArcPy so I might be missing something obvious...any idea what it could be?
Please let me know if there is anything else you need to assist me.
Thanks
You need to change two parameters in your Bearing Distance to Line:
Instead of "Dirfields.name" use field.name (with no quotations). Instead of "Dirfields" use field with no quotations. When using variables for input parameters, we don't use quotes. Otherwise, the tool thinks you are just specifying a string, or basically, a field named "Dirfields"
When we use the following code:
for field in Dirfields:
we are using the variable field as a "stand-in" for each item on your list. So, when the loop runs on each item, we refer to it by field. That's why we use field.name instead of Dirfields.name. and field instead of Dirfields.
You can use nearly anything as your loop variable. Field makes sense since that's what it is, but it could just as easily be f, or apple, or whatever. You just need to use that variable inside your loop when referring to items on the list.
I hope this makes sense, it can be a hard concept to grasp initially.
Hi Daniel,
I am getting a different error now.
Here is the code I tried -
for field in Dirfields:
arcpy.management.BearingDistanceToLine("SusBui_Point", field.name, "POINT_X", "POINT_Y",
"LengthOfSampleLine", "METERS", field, "DEGREES", "GEODESIC", None,
'{spatial_reference}', "NO_ATTRIBUTES")
And here is the error message -
ExecuteError: ERROR 000622: Failed to execute (Bearing Distance To Line). Parameters are not valid.
ERROR 000623: Invalid value type for parameter bearing_field
What am I missing?
Once again thank you for your help and let me know if you need anything else to assist me.
Thanks
Ah, my mistake. You should be using field.name for the bearing_field parameter. The tool needs the field by name to work. I just tried it out on some dummy data and it appears to work now.
Works perfectly!
I used the field.name for the output file name as well as the bearing_field parameter.
Thank you so much!