Select to view content in your preferred language

Using a list of feature classes as an input for the "generate points along lines" tool

1069
5
01-12-2023 08:32 PM
KedaravindanBhaskar
Regular Contributor

Hello,

I am writing an ArcPy script in which I have created a list of line features. I want to use this list as the input for the "generate points along lines" tool.

Here is a sample of my code -

 

 

 

#Creating lines in each of the compass directions
for field in Dirfields:
    arcpy.management.BearingDistanceToLine("SusBui_Point", field.name + "Lines", "POINT_X", "POINT_Y",
                                           "LengthOfSampleLine", "METERS", field.name, "DEGREES", "GEODESIC", None,
                                           '{spatial_reference}', "NO_ATTRIBUTES")
#Creating list of lines
SampleLines=["DirWestLines", "DirSouthLines", "DirNorthLines", "DirWestLines"]
#Generating points along endpoints of line
###Here is where I get the error
for fc in SampleLines :
    arcpy.management.GeneratePointsAlongLines(SampleLines, r"C:\Users\Kedaravindan Bhaskar\Desktop\FBM_research\Testing_ArcpyScript\TestingArcpyScript.gdb\SamplePoints"
    "DISTANCE", "400 Meters", None, "END_POINTS")

 

 

 

This is the error message I get - 

 

 

RuntimeError: Object: Error in executing tool

 

 

The error message doesn't elaborate on the error but from my understanding either the input list (Input_Features) or the output_feature_class is invalid. Refer to the syntax of the generate points along lines tool - 

arcpy.management.GeneratePointsAlongLines(Input_Features, Output_Feature_Class, Point_Placement, {Distance}, {Percentage}, {Include_End_Points})

Any idea what I'm doing wrong?

Please let me know if there is anything else you need to assist me.

Warm regards

Kedar

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

Bearing Distance To Line (Data Management)—ArcGIS Pro | Documentation

in lines 3-5

arcpy.management.BearingDistanceToLine("SusBui_Point", field.name + "Lines", "POINT_X", "POINT_Y",
                                           "LengthOfSampleLine", "METERS", field.name, "DEGREES", "GEODESIC", None,
                                           '{spatial_reference}', "NO_ATTRIBUTES")

you can remove the last 2 parameters since you don't specify a spatial reference and '{spatial reference}' isn't valid in any event, it is either None or a spatial reference

 


... sort of retired...
0 Kudos
KedaravindanBhaskar
Regular Contributor

In my original code I used a spatial reference but it was very long and made the code difficult to read. In the post I changed it to '{spatial reference}'. I meant to leave a note mentioning I made this change but I forgot. My bad.

Regards, Kedar

0 Kudos
JohannesLindner
MVP Frequent Contributor

You're trying to use a list of layers as input, but the tool expects a layer.

JohannesLindner_0-1673603408162.png

 

So you will have to create your output fc and iterate through your SampleLines and append the result to the oputput fc.

Or you can do it with an InsertCursor. And at that point, you can take the next logical step and skip the tools altogether and just use the geometry methods.

input_point_fcs = ["TestPoints"]
bearings = [0, 90, 180, 270, 45]
distances = [50, 100, 150, 200]
output_folder = "memory"
output_name = "result"

output_fc = arcpy.management.CreateFeatureclass(output_folder, output_name, "POINT")
arcpy.management.AddField(output_fc, "Source", "TEXT")
arcpy.management.AddField(output_fc, "Bearing", "DOUBLE")
arcpy.management.AddField(output_fc, "Distance", "DOUBLE")

with arcpy.da.InsertCursor(output_fc, ["SHAPE@", "Source", "Bearing", "Distance"]) as i_cursor:
    for point_fc in input_point_fcs:
        with arcpy.da.SearchCursor(point_fc, ["SHAPE@"]) as s_cursor:
            for point, in s_cursor:
                for bearing in bearings:
                    for distance in distances:
                        new_point = point.pointFromAngleAndDistance(bearing, distance)
                        i_cursor.insertRow([new_point, point_fc, bearing, distance])

 

JohannesLindner_1-1673608268602.png

 


Have a great day!
Johannes
0 Kudos
KedaravindanBhaskar
Regular Contributor

I think I would prefer to iterate through the sample lines and append the output to the output fc. If so, would I be able to use the list as a input FC, rather than a layer?

Regards, Kedar

0 Kudos
KedaravindanBhaskar
Regular Contributor

I found a solution that worked for me. In this example, to create endpoint on the line I used this code - 

for lines in SampleLines:
arcpy.management.GeneratePointsAlongLines(lines, 'points_{}'.format(lines), "DISTANCE", "400 Meters", None, "END_POINTS")
SamplePoints = ["points_DirWestLines", "points_DirSouthLines", "points_DirNorthLines", "points_DirEastLines"]

 

0 Kudos