[GIS] Having trouble with ‘and’ ‘or’ statements and negative numbers

arcgis-10.0arcpypython

I have set up a program that calculates the relationship between a point and the first point of a polyline. The aim of the program is to run this calculation and then copy the result to a certain field in the attribute table. However, if the cell/field is already occupied it copies to the next field in line and so on.

This runs pretty smoothly in most cases. However, if the result/calculation happens to return a negative number all hell breaks loose. When the negative number is returned the loop registers the result in the correct up/down link column but seems to ignore the command to check if the cell is already occupied. This leads to other valid data being overwritten.

I'm not sure whether this is a result of the way I have set up my 'and'/'or' operators or whether I modify the way I use negative numbers in python.

Below is an example of the trouble I have been having. If anyone has any ideas please let me know.

q_fid = 1
calculation = -30
linkup = "EDRN21"
linkup2 = "EDRN03"
linkup3 = " "
linkdown = "EDRN10"
linkdown2 = " "
linkdown3 = " "
link_id = "EDRN44"

arcpy.MakeFeatureLayer_management("uk_cities", "node_feature") 
arcpy.SelectLayerByAttribute_management ("node_feature", "NEW_SELECTION", '\"FID\" =  {0}'.format(q_fid))

rows = arcpy.UpdateCursor("node_feature")
for row in rows:
    if calculation < -0.001 or calculation > 0.001 and linkup == " ":
        row.setValue("Up_LinkA", link_id)
    elif calculation < -0.001 or calculation > 0.001 and linkup2 == " ":
        row.setValue("Up_LinkB", link_id)
    elif calculation < -0.001 or calculation > 0.001 and linkup3 == " ":
        row.setValue("Up_LinkC", link_id)
    elif calculation > -0.001 and calculation < 0.001 and linkdown == " ":
        row.setValue("Down_LinkA", link_id)
    elif calculation > -0.001 and calculation < 0.001 and linkdown2 == " ":
        row.setValue("Down_LinkB", link_id)
    elif calculation > -0.001 and calculation < 0.001 and linkdown3 == " ":
        row.setValue("Down_LinkC", link_id)
    rows.updateRow(row)

Best Answer

I would group with parens as follows

if (calculation < -0.001 or calculation > 0.001) and linkup == " ":