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:
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?
Solved! Go to Solution.
Last kick. Add a double field, precision 16, scale 4
emulate the field calculator expression before heading to the tool in arctoolbox
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?
Here are some screenshots of what I've been doing:
And this is the error that I am getting:
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.
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?
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?
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.
Last kick. Add a double field, precision 16, scale 4
emulate the field calculator expression before heading to the tool in arctoolbox
Ah ha! It's just worked. This is the set up that worked:
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?
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