Select to view content in your preferred language

Python script/expression for field calculator for converting bearings to directions?

8752
28
Jump to solution
09-22-2016 10:03 AM
bobcostanza
Emerging Contributor

I have a double field with bearings/azimuths (numbers) and I need them in letter directions: N, NE, NW,etc. I would like to calculate them in  a new field.  I have searched high and low for the code to use in the field calculator but I have failed to figure it out.

Thanks,

Bob

0 Kudos
28 Replies
DanPatterson_Retired
MVP Emeritus

FYI  I haven't accounted for changes in the polar axis since this thread was begun 

bobcostanza
Emerging Contributor

Better get on it

Sent from my iPhone

0 Kudos
bobcostanza
Emerging Contributor

Dan,

the field I was using your script in was a text field but it still failed to execute.  One of the error messages in the Geoprocessing results is "The field is not nullable. [dir] ".  Is that the problem?

dir is the field I was working on, Bearing is the field with the angles

Thanks a million,

Bob

0 Kudos
DanPatterson_Retired
MVP Emeritus

you didn't switch to the python parser did you

0 Kudos
bobcostanza
Emerging Contributor

Dan,

The field is text, the parser is python. Attached is a screenshot of what

it looks like. I am using the script from Sep 23, 2016 10:38 AM

I am just copying and pasting the script, other then specifying the

bearing field do I need to do anything else?

Thanks again,

Bob

0 Kudos
DanPatterson_Retired
MVP Emeritus

number is selected as the field type instead of 

A small but important fact... your 'bearings' appear to go from -180 to 180 instead of 0-360 as expected by the code.  I think the angles are relative to the X axis and not compass bearings.  This will have to be fixed on your end.

I could fix the code to do this but it would go from a simple expression expecting a specific input to one that accommodates all possible variants of angle types.

0 Kudos
bobcostanza
Emerging Contributor

Dan,

The script on *https://community.esri.com/blogs/dan_patterson/2016/09/01/distance-calculations-using-the-field-calculator

<https://community.esri.com/blogs/dan_patterson/2016/09/01/distance-calculations-using-the-field-calculator>*

returns angles between successive points, is there a script anywhere that

returns bearings or azimuths? Do any of the other suggested scripts I

received a couple weeks ago return azimuths or bearings between successive

points? Once I have bearings I can then use the *Convert Azimuth to

Compass Bearing *script.

Thanks,

Bob

0 Kudos
DanPatterson_Retired
MVP Emeritus

the first link contains the angle code for successive points and ... a variant of the code that I have posted in several incarnations in this thread... I just wanted to consolidate the field calculator geometry 'stuff' in one place as I think of it.

jarbaslima55dias
Emerging Contributor

I am using the following script in ArcGIS 10.3 to calculate Quadrant (in the format angle/NE/SE/SW/NW) from azimuth. See example in: 

: https://support.esri.com/pt-br/knowledge-base/instruo-calcular-a-direo-da-linha-em-lote-usando-a-cal...

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

def quadrant(NorthAzimuth): 

 if ((NorthAzimuth>=0) & (NorthAzimuth<90)):     

quad = 'N '+str(NorthAzimuth)+' E'   

elif ((NorthAzimuth>=90) & (NorthAzimuth<180)):       

quad = 'S '+str(180-NorthAzimuth)+' E'    

elif ((NorthAzimuth>=180) & (NorthAzimuth<270)):      

quad = 'S '+str(NorthAzimuth-180)+' W'    

else: 

quad = 'N '+str(360-NorthAzimuth)+' W'   

return quad

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

The box Show Codeblock is cheked. This script is copied in the box Pre-logic Script Code.  In the box below - "Quadrant = " is typed quadrant (!NorthAzimuth!).

My polyline shape file contains the followings fields: Start_X (double), Start_Y (double), End_X (double), End_Y (Double), Compriment (double) - polyline lenght, and NorthAzimu (double) - azimuth in degrees.

I already replace the term NorthAzimuth of script with NorthAzimu, but without sucess.

The script always returns an error message.

Please, where is the error?

0 Kudos