Select to view content in your preferred language

code python for azimuth and quadrant

3434
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

format the code would be useful

Code formatting ... the Community Version - Esri Community

as would the error message and a screengrab of the calculate field dialog


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

You should also see this thread and solution

Solved: Python script/expression for field calculator for ... - Esri Community


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

Sorry, I still didn't get the expected results

0 Kudos
DanPatterson
MVP Esteemed Contributor

your code still isn't formatted properly in window1.jpg.  Why not copy and format the code you are using here, using Wednesday's link.

A screen grab of it won't solve code formatting issues


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

Hello DanPatterson

 

Fields: NorthAzimu (Double), Quadrant (Text), Start_X (Double), Start_Y (Double), End_X (Double), End_Y (Double)

Box Pre -logic script code:

def quadrant(NorthAzimu):
if ((NorthAzimu>=0) & (NorthAzimu<90)):
quad = 'N '+str(NorthAzimu)+' E'
elif ((NorthAzimu>=90) & (NorthAzimu<180)):
quad = 'S '+str(180-NorthAzimu)+' E'
elif ((NorthAzimu>=180) & (NorthAzimu<270)):
quad = 'S '+str(NorthAzimu-180)+' W'
else: quad = 'N '+str(360-NorthAzimu)+' W'
return quad

Box Quadrant =

quadrant (!NorthAzimuth!)

 

Thank You

0 Kudos
DanPatterson
MVP Esteemed Contributor

 

def quadrant(NorthAzimu):
    if ((NorthAzimu >= 0) & (NorthAzimu < 90)):
        quad = 'N ' + str(NorthAzimu) + ' E'
    elif ((NorthAzimu >= 90) & (NorthAzimu < 180)):
        quad = 'S ' + str(180 - NorthAzimu) + ' E'
    elif ((NorthAzimu >= 180) & (NorthAzimu < 270)):
        quad = 'S ' + str(NorthAzimu - 180) + ' W'
    else:
        quad = 'N ' +s tr(360 - NorthAzimu) + ' W'
    return quad

 

is how it should be formatted


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

Hello Dan

 

Sorry my insistence but I still get error with tthe scipt. The code was copied exactly was sent in the window "quadrant = " .  The box Show Codeblock was not checked. If the box is checked  I also get error.

Please see box1.jpg

 

Thank You

 

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

show code block

paste my copy of the code and check to see if it is ok

Quadrant (Text)  is the field name, you currently are using a lower case

quadrant =

 


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

Hello Dan

thank you for the code, it worked correctly. But I made a small change to include Azimuth =0 or Azimuth=90 or Azimuth=180 or Azimuth=270 or Amimuth = 360. In this case, should I include 'str' in the line Quad= 'N'?

Thank You for help

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

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

 

0 Kudos