How do I account for Az Bearings over 360 degrees?

1842
8
Jump to solution
04-27-2017 01:54 PM
DuncanMacIntosh
New Contributor II

Hello, I am working on a script to add AzBearings to a polyline shapefile and have a successful  script that does this. However, I need to add 14 to those values to account for magnetic north based on my location. If I add 14 to the field, I will then potentially have values over 360 so I need to account for that. My question is, in python how would I take the field containing the AzBearings and add 14 to that value and then subtract 360, but only from the values greater than 360? Thank you!

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

I would do a manual test using calculate field and copy the expression to  a python snippet.... 

the expression should be something like  .... "(!BEARING! + 14) % 360"  but I can't test to see whether the quotes are correct and you should check

maybe....

arcpy.CalculateField_management("inFeatures", fieldName1, '(!Bearing! + 14) % 360)', "PYTHON_9.3")

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

modulus 

(360 + 14) % 360
Out[1]: 14

(200 + 14) % 360
Out[2]: 214
DuncanMacIntosh
New Contributor II

Thank you Dan, that makes sense to me. I am trying to add this to a new field (Compass Bearing) and populate the new field with the values from my AzBearing field. The outFeatureClass is from earlier in my script referring to the .shp file that contains the field with AzBearing. ArcGIS names that field BEARING, but I do not know how to call that field in the expression for the new field. I hope that I am making sense and that you can help.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I would do a manual test using calculate field and copy the expression to  a python snippet.... 

the expression should be something like  .... "(!BEARING! + 14) % 360"  but I can't test to see whether the quotes are correct and you should check

maybe....

arcpy.CalculateField_management("inFeatures", fieldName1, '(!Bearing! + 14) % 360)', "PYTHON_9.3")

DuncanMacIntosh
New Contributor II

Dan,

Python disliked inputting '(!Bearing! +14) % 360)' directly into the arcpy.CalculateField so I defined the expression variable to be that equation and it worked brilliantly! Thank you very much for your help.

0 Kudos
DanPatterson_Retired
MVP Emeritus

glad it worked

VinceAngelo
Esri Esteemed Contributor

The Python modulus operator (%) can be used to force a range on even floating-point values:

>>> print (358.1 + 4) % 360.0
2.1
>>> print (3.0 - 12) % 360.0
351.0
DanPatterson_Retired
MVP Emeritus

In python 3 at least, modulus works with both in and float.  Are you referring to controlling decimals?  Here is an option

(375.1 + 4.01) % 360
Out[11]: 19.110000000000014

round((375.1 + 4.01) % 360, 2)
Out[10]: 19.11

VinceAngelo
Esri Esteemed Contributor

Python is one of very few languages where the modulus operator works on floating-point datatypes.  Usually a math function (e.g., fmod) is required, with the operator only supporting integer types.  I tested in Python 2.7 (which is why I got away with the paren-less print).