Calculating Bearing of a Polyline ArcGIS 10

17896
16
08-15-2011 04:55 PM
AlexanderBeavis
New Contributor II
Hey there,

Firstly sorry if I have put this thread in the wrong location, this is my first post and wasn't quite sure where to put it.

I'm looking to calculate the bearing of a polyline with just two points, with reference to north. I found some other threads on this but didn't have any success.

I think it maybe because I was using VBA coding which is apparently not compatible with ArcGIS 10 which requires python or dot net.

Any help would be greatly appreciated. You might need to spell it out for me though as my knowledge of GIS is pretty limited to what I've been able to teach myself over the last couple of months ion my honours.
16 Replies
AlexanderBeavis
New Contributor II
Is there a way that I can do it using this?

EasyCalculate 10

http://www.ian-ko.com/free/EC10/EC10_main.htm
AlexanderBeavis
New Contributor II
Have I put this thread in the wrong location?

How long do you usually wait for a response??
0 Kudos
ChaseCarter
New Contributor III
You have a couple options here.

Option #1
If 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 #2
I've attached a python script that is from the ArcSDM toolbox which does exactly what you want.
0 Kudos
AlexanderBeavis
New Contributor II
Great thanks for the reply :)

I did end up finding a python script I could use.

Cheers!
0 Kudos
AshleyCummings
New Contributor
You have a couple options here.

Option #1
If 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 #2
I've attached a python script that is from the ArcSDM toolbox which does exactly what you want.


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.

OR

ABeavis, if you could share the python script you found I would also appreciate that (if you are able to share freely). Thank you.

Ashley
0 Kudos
yaskshelat
New Contributor
I am not aware of the python concept.

Can anyone suggest me how to use this script in ArcMap?

We have ArcView Licence

Thanks
Yask
0 Kudos
nimitz
by
New Contributor III
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.

OR

ABeavis, if you could share the python script you found I would also appreciate that (if you are able to share freely). Thank you.

Ashley


If anyone could post some info on the python script above, that would be helpful. I can't seem to get it to work. I will add the script (in a new toolbox), but the input fields aren't what I'd expect. The dialog boxes are asking you to create the fields (ie type). The comments in the script aren't very helpful.

Thanks in advance,
0 Kudos
BrettMorse
New Contributor

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.

0 Kudos
BenVan_Kesteren1
Regular Contributor

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:

2016-02-18 095738 Greenshot.jpg

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
    
0 Kudos