1

I'm working with Arcgis 10.1, i'm trying to loop if condition on Python code, but they give me as error :

Parsing error IndentationError: unexpected indent (line 18)

I don't know what is going wrong.

This is the code.

import arcpy
import math
arcpy.AddField_management("Parcelles_class_FeatureToPoi3","new_field","DOUBLE")
cur = arcpy.UpdateCursor("Parcelles_class_FeatureToPoi3") 
 for row in cur:
... PointX = row.getValue("POINT_X")
... print PointX
... PointY = row.getValue("POINT_Y")
... print PointY
... EndX = row.getValue("end_x")
... print EndX
... EndY = row.getValue("end_y")
... print EndY
... dx = EndX - PointX 
... dy = EndY - PointY 
... azimuth = math.atan2(dy, dx)
... print azimuth
... azimuth_gr = azimuth*200/3.14519
... print azimuth_gr
 if (dy>=0 and dx>= 0 ):
... azimuth_fi = azimuth_gr
... else : 
... if (dy>=0 and dx<= 0 ):
... azimuth_fi = 200- azimuth_gr
... else :
... if (dy<=0 and dx<= 0 ):
... azimuth_fi = 200 + azimuth_gr
... else :
... if (dy<=0 and dx>= 0 ):
... azimuth_fi = 400 - azimuth_gr
... print azimuth_fi
 row.setValue("new_field", azimuth_fi)
 cur.updateRow(row)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Dec 8, 2014 at 11:49
6
  • 6
    I think the problem is that your else: and the lines in that code block need to be dedented one level to line up with the preceding if:. However, it is hard to be certain with those three dots at the beginning of many lines which I think are an artefact of a copy/ paste from ArcMap's Python window. Commented Dec 8, 2014 at 12:00
  • Is there an other way to compile this code on arcgis without using Python window ?! Commented Dec 8, 2014 at 12:11
  • I use IDLE which is installed with ArcGIS for Desktop for most of my Python work but I use the Python window for quick tests. Commented Dec 8, 2014 at 12:17
  • PolyGeo is correct, all the else conditions needs to line up with their respective if conditions. And I'd also recommend IDLE for coding, with a quick copy-paste into the Python window to execute. Commented Dec 8, 2014 at 12:21
  • You'll find IDLE on the Windows Start Menu under ArcGIS and then Python2.7 Commented Dec 8, 2014 at 12:28

1 Answer 1

2

It would be much clearer with elif then with else: if: . Not that you could also group your condiftion with hierachical if (first test dy, then test dx =>you make 2 tests instead of up to 4 tests)

... print azimuth_gr
 if (dy>=0 and dx>= 0 ): #if should be aligned with print
 azimuth_fi = azimuth_gr
 elif (dy>=0 and dx<= 0 ):
 azimuth_fi = 200- azimuth_gr
 elif (dy<=0 and dx<= 0 ):
 azimuth_fi = 200 + azimuth_gr
 else:
 azimuth_fi = 400 - azimuth_gr
 print azimuth_fi
 row.setValue("new_field", azimuth_fi) #these line should also be aligned, otherwise you only update the last row
 cur.updateRow(row)
 del(row, cur)
answered Dec 8, 2014 at 13:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.