Create lines at specific bearing

5064
14
08-25-2021 03:50 PM
TinaKwitkoski
Occasional Contributor

 I am wondering if there is a script that will create multiple lines based on the bearing and coordinates of a point feature held in a XLS. 

What I am trying to do is create slope arrows for a cruise plan map.  I have a slope table (xls), with ID, Length, Bearing, Slope%, Easting, Northing. Currently I create each line using the create line => Direction, length using the xls which is fine if I have only a few. But when I have 50+ to create it is time consuming. It would be nice to run a script/tool to create my lines automatically.. is there such a thing out there?  I searched online did not see anything helpful. 

Any help much appreciated. 

Tina 

0 Kudos
14 Replies
TinaKwitkoski
Occasional Contributor

Dan

2.8.0 for Pro. 

0 Kudos
DanLee
by Esri Regular Contributor
Esri Regular Contributor

Hi Tina,

Thanks for sharing the test data. We have tested in 2.6, 2.8, and our current development versions, but could not reproduce the problem. 

DanLee_0-1630186501394.png

I noticed the OBJECTID values are not sequential. That should not matter. But you can try using Copy Rows tool to turn it to a gdb table, then run Bearing Distance To Line.  Hope that works.

0 Kudos
JohannesLindner
MVP Frequent Contributor

Hmm, the tool worked for me...

Try constructing the lines yourself: edit the input and output parameters of this script and run it in the python window.

import logging

# define input data
in_table = "H:/testCruise_data_DDS.csv"
field_x = "Long"
field_y = "Lat"
field_distance = "distance"
field_bearing = "bearing"
wkid = 4326  # wkid of your data's coordinate system

# define output data
out_path = "memory"
out_name = "test"
out_fields = [  # list of [field_name, field_type]
    ["PLOT_ID", "LONG"],
    ["TYPE", "TEXT"],
    ["slope", "FLOAT"],
    ["bearing", "FLOAT"],
    ["Long", "FLOAT"],
    ["Lat", "FLOAT"],
    ]

# read data
fields = [f.name for f in arcpy.ListFields(in_table)]
data = [dict(zip(fields, row)) for row in arcpy.da.SearchCursor(in_table, fields)]

# construct lines
spatial_reference = arcpy.SpatialReference(wkid)
for d in data:
    try:
        point = arcpy.Point(d[field_x], d[field_y])
        point_geometry = arcpy.PointGeometry(point, spatial_reference)
        target_geometry = point_geometry.pointFromAngleAndDistance(d[field_bearing], d[field_distance])
        point_array = arcpy.Array([point_geometry.firstPoint, target_geometry.firstPoint])
        line_geometry = arcpy.Polyline(point_array, spatial_reference)
        d["SHAPE@"] = line_geometry
    except: # e.g. bearing or distance are empty
        logging.exception("Could not create line for this row: {}".format(d))
        d["SHAPE@"] = None

# create output fc
# skip this if you already have a line fc that you want to append to
out_fc = arcpy.management.CreateFeatureclass(out_path, out_name, "POLYLINE", spatial_reference=spatial_reference)
for f in out_fields:
    arcpy.management.AddField(out_fc, f[0], f[1])

# write the lines into out_fc
fields = ["SHAPE@"] + [f[0] for f in out_fields]
with arcpy.da.InsertCursor(out_fc, fields) as cursor:
    for d in data:
        values = [d[f] for f in fields]
        cursor.insertRow(values)

Have a great day!
Johannes
0 Kudos
DanPatterson
MVP Esteemed Contributor

As suggested earlier, excel to table or table-to-table tool with the output as a file geodatabase table, then run the tool which I suggested and Dan Lee confirmed works, unless the data fields are in the wrong format or the data content needs adjusting.


... sort of retired...
0 Kudos