How To: Determine the cardinal compass direction of line features
The following instructions describe how to determine the cardinal compass direction of line features. In this article, the below feature class of a downtown city is used.
Use the Linear Directional Mean tool output to obtain the compass angle of the line features.
In the Catalog window, navigate to System Toolboxes > Spatial Statistics Tools > Measuring Geographic Distributions > Linear Directional Mean. Open the tool.
Select the line feature as the input feature class.
Select a field from the attribute table as the case field.
The Case Field is used to group features for separate linear directional mean computations. When a Case Field is specified, the input line features are first grouped according to case field values, then an output line feature is created for each group. The case field can be of integer, date, or string type. Records with NULL values for the Case Field are excluded from analysis.
Convert the compass angle value to a direction.
Add a new Text field to the attribute table.
Use the Field Calculator to create a Python function to convert the angle values to text direction.
Start an edit session on the feature layer.
Right-click the field header > Field Calculator.
In the Field Calculator, select Python as the parser.
Check the Show Codeblock checkbox.
Type the following script in the Pre-Logic Script Code text box.
if var < 22.5:
if var >= 22.5 and var < 67.5:
if var >= 67.5 and var < 112.5:
if var >= 112.5 and var < 157.5:
if var >= 157.5 and var < 202.5:
if var >= 202.5 and var < 247.5:
if var >= 247.5 and var < 292.5:
if var >= 292.5 and var < 337.5:
if var >= 337.5:
Type the following expression in the Direction = text box.
Click OK. The direction of the line features are displayed in the attribute table. Note the difference in cardinal direction for the northwest-most line feature in the downtown city image, labeled as "Northeast" and shown in blue.