Select to view content in your preferred language

code python for azimuth and quadrant

3647
13
06-07-2023 12:38 PM
Labels (3)
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.

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

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
13 Replies
DanPatterson
MVP Esteemed Contributor

N is already a string

You only use 'str' to convert numbers to text


... sort of retired...
0 Kudos
DanPatterson
MVP Esteemed Contributor

Answered?


... sort of retired...
0 Kudos
jarbaslima55dias
Emerging Contributor

Hello Dan

 

I am using the last code you sent:

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

def Quadrant(NorthAzimu):
if ((NorthAzimu >= 0) & (NorthAzimu < 90)):
Quad = str(NorthAzimu) + 'NE'
elif ((NorthAzimu >= 90) & (NorthAzimu < 180)):
Quad = str(180 - NorthAzimu) + 'SE'
elif ((NorthAzimu >= 180) & (NorthAzimu < 270)):
Quad = str(NorthAzimu - 180) + ' SW'
else:
Quad = str(360 - NorthAzimu) + ' NW'
return Quad

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

In this other code (with slight modification of your code) there is a small error:

 

def Quadrant(NorthAzimu):
if (NorthAzimu = 0):
Quad = 'N'
elif (NorthAzimu = 90):
Quad = 'E'
elif (NorthAzimu = 180):
Quad = 'S'
elif (NorthAzimu = 270):
Quad = 'W'
elif (NorthAzimu = 360):
Quad = 'N'
elif ((NorthAzimu > 0) & (NorthAzimu < 90)):
Quad = str(NorthAzimu) + 'NE'
elif ((NorthAzimu > 90) & (NorthAzimu < 180)):
Quad = str(180 - NorthAzimu) + 'SE'
elif ((NorthAzimu > 180) & (NorthAzimu < 270)):
Quad = str(NorthAzimu - 180) + ' SW'
else:
Quad = str(360 - NorthAzimu) + ' NW'
return Quad

Thank You

0 Kudos
DanPatterson
MVP Esteemed Contributor

Your code lacks formatting...

again... Code formatting ... the Community Version - Esri Community

and in python, condition checking for equality, like

NorthAzimu = 0    # -- is wrong

NorthAzimu == 0   # --this is right


... sort of retired...
0 Kudos