What about if you're referencing the World Magnetic Model...?
Nothing would calculate true magnetic bearing since it varies by location and (often erratically) over time
Is there any way of telling if the "LINE_BEARING" calculates magnetic or grid?
Hello,
I know it's been awhile since the original post, but I just came across this comment and figured I'd add to the conversation since it seems like this can be done with the built-in toolboxes quite simply.
The "Add Geometry Attributes" tool has an option to calculate the "LINE_BEARING", described as "The start-to-end bearing of the line. Values range from 0 to 360, with 0 meaning north, 90 east, 180 south, 270 west, and so on."
Here's the docs for more info:
Add Geometry Attributes—Help | ArcGIS Desktop
Hopefully this helps someone out there,
Cheers,Ben
I'll just point out that this type of script can be greatly simplified by using modern arcpy geometry objects:
>>> fc = 'lines' # line feature layer ... bearing_field = 'BEARING' # bearing field ... sr = arcpy.Describe(fc).spatialReference # spatial reference of lines ... with arcpy.da.UpdateCursor(fc,['SHAPE@',bearing_field],spatial_reference=sr) as cursor: # create cursor ... for row in cursor: # loop through lines ... pt1 = arcpy.PointGeometry(row[0].firstPoint,sr) # first point geometry ... pt2 = arcpy.PointGeometry(row[0].lastPoint,sr) # last point geometry ... row[1] = pt1.angleAndDistanceTo(pt2)[0] # (angle, distance)[0] = angle ... if row[1] < 0: # if you want all positive angles ... row[1] += 360 ... cursor.updateRow(row) # write value
Thanks for the tool. It worked like a charm!
This thread just solved a problem for me, so I thought I would comment on what I did to get the desired outcome, the same as what the original question is.
I tried the python script Chase Carter added earlier in this thread, worked perfectly for me, it provided the below columns in a feature class:
I did two things differently, the first being I ran it in the python command line, rather than making a toolbox tool. The reason being this will be a once off use, I dont see myself needing this tool very often.
The second thing I did was added a line to define the feature class I wanted the fields to be added to. the reason I did this was because I was not using a toolbox tool, but simply running command line.
So from Chase' code I deleted these two lines:
#Get evidence feature layer name out_feat_class = gp.GetParameterAsText(0)
Then I added a single line in the same spot I deleted the previous two lines from (approx line 26-27)
out_feat_class = r'Database Connections\GISADMIN@SDE_Spatial@Smithy.sde\SDE_SPATIAL.GISADMIN.Uploading_New_Data\SDE_SPATIAL.GISADMIN.Airport_ORC_Bearing_Lines'
So in summary, this is the entire code I ran to produce the two columns highlighted yellow above:
""" Add fields of azimuths and bearings to a featurelayer of lines. Azimuths can be based on map or geodgraphic coordinates. For segmented lines, azimuths are length-weighted averages of segments. """ # Import system modules import sys, string, os, math, random, traceback, tempfile # Create the Geoprocessor object #gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") import arcgisscripting gp = arcgisscripting.create() ### Check out any necessary licenses ##gp.CheckOutExtension("spatial") ## gp.OverwriteOutput = 1 # Load required toolboxes... #gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx") # Script arguments... try: #Get bearing type geodesic = 'true' #gp.GetParameterAsText(1) == 'true' #Get evidence feature layer name out_feat_class = r'Database Connections\GISADMIN@SDE_Spatial@Smithy.sde\SDE_SPATIAL.GISADMIN.Uploading_New_Data\SDE_SPATIAL.GISADMIN.Airport_ORC_Bearing_Lines' #gp.AddMessage(gp.describe(out_feat_class).shapetype) if gp.describe(out_feat_class).shapetype != "Polyline": raise Exception,'not a polyline-type feature class' #Check for Azimuth field fieldnames = [] fields = gp.ListFields(out_feat_class) field = fields.next() while field: fieldnames.append(field.name) field = fields.next() if 'SDMBearing' not in fieldnames: gp.addfield_management(out_feat_class,'SDMBearing','long',5) if 'SDMAzimuth' not in fieldnames: gp.addfield_management(out_feat_class,'SDMAzimuth','long',5) #Open attribute table for update featureclass outrows = gp.updatecursor(out_feat_class) #Define the bearing, azimuth algorithm def geodesic_azimuth(inshape): """ Map azimuth calculation from a polyline shape """ sumazmlen = 0 sumbrglen = 0 sumlen = 0 line = inshape.getpart(0) pnt = line.next() if pnt: pnt0 = pnt pnt = line.next() while pnt: delx = pnt.X-pnt0.X dely = pnt.Y-pnt0.Y sdmazimuth = int(89.5-math.atan2(dely,delx)*180/math.pi) if sdmazimuth < -90: sdmbearing = sdmazimuth + 180 elif sdmazimuth > 90: sdmbearing = sdmazimuth - 180 else: sdmbearing = sdmazimuth #gp.AddMessage(str((sdmbearing,sdmazimuth))) # sumbrglen += sdmbearing*len # sumazmlen += sdmazimuth*len # sumlen += len pnt0 = pnt pnt = line.next() # return [sumbrglen/sumlen,sumazmlen/sumlen] return [sdmbearing,sdmazimuth] #Process the lines outrow = outrows.next() while outrow: if geodesic: outshape = outrow.shape [sdmbearing,sdmazimuth] = geodesic_azimuth(outshape) #gp.AddMessage('!: %s,%s'%(sdmbearing,sdmazimuth)) outrow.sdmbearing = sdmbearing outrow.sdmazimuth = sdmazimuth else: pass #map_azimuth(inrow.shape) outrows.updaterow(outrow) outrow = outrows.next() #georow = georows.next() except (Exception): # get the traceback object tb = sys.exc_info()[2] # tbinfo contains the line number that the code failed on and the code from that line tbinfo = traceback.format_tb(tb)[0] # concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + \ str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n" # generate a message string for any geoprocessing tool errors msgs = "GP ERRORS:\n" + gp.GetMessages(2) + "\n" gp.AddError(msgs) # return gp messages for use with a script tool gp.AddError(pymsg) # print messages for use in Python/PythonWin print pymsg print msgs raise
Just wondering about the update to 10.1, I've been looking everywhere for an answer to this. I'm still looking so maybe there is another thread with an answer that I haven't clicked on yet. And I've downloaded the easycalculate add-ins but I don't know how to fix them, or the python script to run in 10.1
Thanks to anyone who can help me out.
Hi Chase,I am wondering if you can include some directions with the python script you attached? I tried to use it but apparently do not know the correct way to set it up in Arc 10. Thank you very much.ORABeavis, if you could share the python script you found I would also appreciate that (if you are able to share freely). Thank you.Ashley
You have a couple options here.Option #1If you are familiar with VB coding, then contact your ESRI dealer to get the VB license to add the Macro menu back into ArcGIS 10. That being said it might not be available at all in 10.1.Option #2I've attached a python script that is from the ArcSDM toolbox which does exactly what you want.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.