HOW TO
In ArcGIS Pro, randomly angled lines can be generated at a specific location on the map. This is useful in environmental studies where randomly angled lines can simulate transects for wildlife surveys. This ensures unbiased sampling across varied terrains, providing accurate data for habitat analysis and conservation planning.
This article describes the workflow to generate randomly angled lines using ArcPy in ArcGIS Pro.
Note: This workflow requires the full script to run in the ArcGIS Pro Python window.
import arcpy
import random
import math
output_fc = r"C:\path\to\the\geodatabase.gdb\RandomLines" # Change to the output feature class path.
spatial_reference = arcpy.SpatialReference(4326) # WGS 84, or change to match the map’s coordinate system.
extent = arcpy.Extent(-117.2, 34.05, -117.15, 34.1) # Adjust the coordinates as needed.
num_lines = 10
min_length = 0.01
max_length = 0.02
min_angle = 0
max_angle = 360
try:
if not arcpy.Exists(output_fc):
arcpy.CreateFeatureclass_management(out_path=arcpy.Describe(output_fc).path,
out_name=arcpy.Describe(output_fc).name,
geometry_type="POLYLINE",
spatial_reference=spatial_reference)
print("Feature class created at: {}".format(output_fc))
else:
print("Feature class already exists.")
arcpy.AddField_management(output_fc, "LineID", "LONG")
print("Field 'LineID' added.")
except arcpy.ExecuteError:
print("Error during feature class creation or field addition.")
print(arcpy.GetMessages())
try:
with arcpy.da.InsertCursor(output_fc, ["SHAPE@", "LineID"]) as cursor:
for line_id in range(1, num_lines + 1):
x_start = random.uniform(extent.XMin, extent.XMax)
y_start = random.uniform(extent.YMin, extent.YMax)
angle = random.uniform(min_angle, max_angle)
length = random.uniform(min_length, max_length)
x_end = x_start + length * math.cos(math.radians(angle))
y_end = y_start + length * math.sin(math.radians(angle))
array = arcpy.Array([arcpy.Point(x_start, y_start), arcpy.Point(x_end, y_end)])
polyline = arcpy.Polyline(array, spatial_reference)
cursor.insertRow([polyline, line_id])
print("Line {} created: Start({}, {}) -> End({}, {})".format(line_id, x_start, y_start, x_end, y_end))
print("All random lines have been successfully added to the feature class.")
except arcpy.ExecuteError:
print("Error during line creation or insertion.")
print(arcpy.GetMessages())
try:
aprx = arcpy.mp.ArcGISProject("CURRENT")
map_obj = aprx.listMaps()[0]
layer = map_obj.addDataFromPath(output_fc)
map_obj.defaultView.zoomToLayer(layer)
print("Feature class added to the map and zoomed in.")
except Exception as e:
print("Error adding layer to map or zooming in.")
print(str(e))
print("Script finished. Randomly angled lines created and displayed.")
The code block below demonstrates the full working script.
import arcpy
import random
import math
output_fc = r"C:\Users\Documents\ArcGIS\Projects\MyProject42\MyProject42.gdb\RandomLines" # Change to the output feature class path.
spatial_reference = arcpy.SpatialReference(4326) # WGS 84, or change to match the map’s coordinate system.
extent = arcpy.Extent(-117.2, 34.05, -117.15, 34.1) # Adjust coordinates as needed (Redlands, CA)
num_lines = 10
min_length = 0.01
max_length = 0.02
min_angle = 0
max_angle = 360
try:
if not arcpy.Exists(output_fc):
arcpy.CreateFeatureclass_management(out_path=arcpy.Describe(output_fc).path,
out_name=arcpy.Describe(output_fc).name,
geometry_type="POLYLINE",
spatial_reference=spatial_reference)
print("Feature class created at: {}".format(output_fc))
else:
print("Feature class already exists.")
arcpy.AddField_management(output_fc, "LineID", "LONG")
print("Field 'LineID' added.")
except arcpy.ExecuteError:
print("Error during feature class creation or field addition.")
print(arcpy.GetMessages())
try:
with arcpy.da.InsertCursor(output_fc, ["SHAPE@", "LineID"]) as cursor:
for line_id in range(1, num_lines + 1):
x_start = random.uniform(extent.XMin, extent.XMax)
y_start = random.uniform(extent.YMin, extent.YMax)
angle = random.uniform(min_angle, max_angle)
length = random.uniform(min_length, max_length)
x_end = x_start + length * math.cos(math.radians(angle))
y_end = y_start + length * math.sin(math.radians(angle))
array = arcpy.Array([arcpy.Point(x_start, y_start), arcpy.Point(x_end, y_end)])
polyline = arcpy.Polyline(array, spatial_reference)
cursor.insertRow([polyline, line_id])
print("Line {} created: Start({}, {}) -> End({}, {})".format(line_id, x_start, y_start, x_end, y_end))
print("All random lines have been successfully added to the feature class.")
except arcpy.ExecuteError:
print("Error during line creation or insertion.")
print(arcpy.GetMessages())
try:
aprx = arcpy.mp.ArcGISProject("CURRENT")
map_obj = aprx.listMaps()[0]
layer = map_obj.addDataFromPath(output_fc)
map_obj.defaultView.zoomToLayer(layer)
print("Feature class added to the map and zoomed in.")
except Exception as e:
print("Error adding layer to map or zooming in.")
print(str(e))
print("Script finished. Randomly angled lines created and displayed.")
The map below shows the random lines generated at random angles.
Get help from ArcGIS experts
Download the Esri Support App