Calculating polyline azimuth - code error

8368
16
Jump to solution
08-05-2014 01:43 AM
CamillaRootes
New Contributor III

Hi. I'm trying to run a Python script in the field calculator to output the azimuth values of a series of polylines. However, everytime I try to run the code it throws the following error:

"File "<string>", line 2, in GetAzimuthPolyline

AttributeError: 'str' object has no attribute 'x'

Failed to execute (CalculateField)."

The code block I am using is:

  1. import math 
  2. def GetAzimuthPolyline(shape): 
  3. radian = math.atan((shape.lastpoint.x - shape.firstpoint.x)/(shape.lastpoint.y - shape.firstpoint.y)) 
  4. degrees = radian * 180 / math.pi 
  5. return degrees 

I sourced the code from this thread Calculate angle of each line in a polyline shapefile‌. It seems to be working for the people in the thread but not for me. I'm running Arc 10.1 with the basic license, by the way.

Can anyone see where this is going wrong?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Last kick.  Add a double field, precision 16, scale 4

emulate the field calculator expression before heading to the tool in arctoolboxtest1.jpg

View solution in original post

0 Kudos
16 Replies
DanPatterson_Retired
MVP Emeritus

A screen grab of what you are using would help since the code block seems incomplete and improperly formatted.  Also, you are running this with the shapefield as input?

0 Kudos
CamillaRootes
New Contributor III

Here are some screenshots of what I've been doing:

Calculate field_python.jpg

And this is the error that I am getting:

Python_error.jpg

0 Kudos
DanPatterson_Retired
MVP Emeritus

See the attached image, you were missing the bottom part of the dialog I think.  I am using the field calculator which is much quicker

Also ensure that your field is a Double field with the appropriate width and precision.codeblock.jpg

0 Kudos
RiyasDeen
Occasional Contributor III

hi Camilla,

What i see from your screen shot is you are trying to calculate value for field Shape and you are passing your shape input as Shape field again.

What is the data type of Shape field? is it a number or geometry?

0 Kudos
CamillaRootes
New Contributor III

By the bottom part, do you mean the "test = test(!shape!)" bit? Should I put this below my code block or in the 'Expression' box?

I had been using a Float field, so have made a Double field called 'Angle' to see if this works better. But Arc keeps changing 'Angle' back to a Float, any idea why it won't let me use a Double field?

0 Kudos
CamillaRootes
New Contributor III

Hi, yeah I realised that I was doing that. Hence creating the new 'Angle' field because the 'Shape' field was geometry. But when I try running it to fill the 'Angle' field I now get a generic syntax error.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Last kick.  Add a double field, precision 16, scale 4

emulate the field calculator expression before heading to the tool in arctoolboxtest1.jpg

0 Kudos
CamillaRootes
New Contributor III

Ah ha! It's just worked. This is the set up that worked:

Calculate field_python_success!.jpg

The only problem is that it is giving me a negative output (-61 degrees in the case of the tester polyline that I used). How can I make the code output the azimuth in the familar True North 0-360 degree output?

0 Kudos
RiyasDeen
Occasional Contributor III

def GetAzimuthPolyline(shape):

radian = math.atan((shape.lastPoint.X - shape.firstPoint.X)/(shape.lastPoint.Y - shape.firstPoint.Y))

degrees = math.degrees(radian)

if degrees < 0:

  return degrees + 360

else:

  return degrees