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)
asked Dec 8, 2014 at 11:49
-
6I 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– PolyGeo ♦2014年12月08日 12:00:53 +00:00Commented Dec 8, 2014 at 12:00
-
Is there an other way to compile this code on arcgis without using Python window ?!geosevda– geosevda2014年12月08日 12:11:51 +00:00Commented 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.PolyGeo– PolyGeo ♦2014年12月08日 12:17:38 +00:00Commented 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.Erica– Erica2014年12月08日 12:21:46 +00:00Commented Dec 8, 2014 at 12:21
-
You'll find IDLE on the Windows Start Menu under ArcGIS and then Python2.7PolyGeo– PolyGeo ♦2014年12月08日 12:28:35 +00:00Commented Dec 8, 2014 at 12:28
1 Answer 1
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
lang-py