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)
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.
    – PolyGeo
    Dec 8 '14 at 12:00
  • Is there an other way to compile this code on arcgis without using Python window ?!
    – geosevda
    Dec 8 '14 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.
    – PolyGeo
    Dec 8 '14 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.
    – Erica
    Dec 8 '14 at 12:21
  • You'll find IDLE on the Windows Start Menu under ArcGIS and then Python2.7
    – PolyGeo
    Dec 8 '14 at 12:28
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.