1

I am new to using Python and seek help building a code for an ArcGIS project. I took one introductory C programming course in school, but other than that I am green.

My objective is to have the conditions below fill a new comment field in the attribute table with the following return statements. Multiple return statements can be true for one attribute.

def comments(?): *comments is the field I wish to populate with return statements. 

    if (((PLOT_MASTER.HEIGHT - "BL_FinalLayer_SpatialJoin.OHT" ) >0.1) OR ((PLOT_MASTER.HEIGHT - "BL_FinalLayer_SpatialJoin.OHT" ) <-0.1)): 
        return “Height does not match OHT”

    elif (((PLOT_MASTER.STOCKING_D - "BL_FinalLayer_SpatialJoin.STKG" ) >0.01) OR ((PLOT_MASTER.STOCKING_D - "BL_FinalLayer_SpatialJoin.STKG") <-0.01)):
        return "Stocking_D does not match STKG" 

    elif (PLOT_MASTER.LEADING_SPECIES_CODE<> "BL_FinalLayer_SpatialJoin.OLEADSPC"):
        return "Leading_Species_Code does not match OLEADSPC" 

    elif (PLOT_MASTER.AGE<> "BL_FinalLayer_SpatialJoin.OAGE"):
        return "Age does not match OAGE"

    else ((( PLOT_MASTER.ECO_CODE - "BL_FinalLayer_SpatialJoin.PRI_ECO_2" ) >0.01) OR ((PLOT_MASTER.ECO_CODE - "BL_FinalLayer_SpatialJoin.PRI_ECO_2") <-0.01))
        return "ECO_CODE does not match PRI_ECO"

comments(?)

  • 1
    This isn't really a directed question suitable for the site. When you run your code what happens? Where are you stuck? – nmtoken Jul 24 '17 at 18:43
  • As a field calculator expression, which I assume this is, the fields are out of scope.. you need to feed them into your calculation as variables Have a read of desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/… . I would suggest starting with an empty string and then concatenate your fail statements then return the fail string; as soon as the interpreter hits a return statement no other code is evaluated so if you're expecting even the remote possibility of failing on more than one condition do not return. As an aside I think you should abs(value) – Michael Stimson Jul 24 '17 at 20:44
2

Just looking at the code I can see a few glaring errors:

  • Not equals is != not <>. The <> operator is VB, in C and python not equals is !=
  • You've copied some code that has been in Microsoft Word or Outlook at some stage... These text editors are very helpful, too helpful in fact, it's replaced your "" string with “” which isn't python string identifiers.

I covered in my comments the multiple return statements which will drop out your code as soon as a condition returns true.. to check for multiple conditions you can build a string or a list of strings then join together:

def comments(PLOT_MASTER_HEIGHT,
                         BL_FinalLayer_SpatialJoin_OHT,
                         PLOT_MASTER_STOCKING_D,
                         BL_FinalLayer_SpatialJoin_STKG,
                         PLOT_MASTER_LEADING_SPECIES_CODE,
                         BL_FinalLayer_SpatialJoin_OLEADSPC,
                         PLOT_MASTER_AGE,
                         BL_FinalLayer_SpatialJoin_OAGE,
                         PLOT_MASTER_ECO_CODE,
                         BL_FinalLayer_SpatialJoin_PRI_ECO_2)

    failStrings = [] # a new empty list
    if abs(PLOT_MASTER_HEIGHT - BL_FinalLayer_SpatialJoin_OHT) > 0.1:
        failStrings.append('Height does not match OHT')

    if abs(PLOT_MASTER_STOCKING_D - BL_FinalLayer_SpatialJoin_STKG ) >0.01:
        failStrings.append('Stocking_D does not match STKG')

    if PLOT_MASTER_LEADING_SPECIES_CODE != BL_FinalLayer_SpatialJoin_OLEADSPC:
        failStrings.append('Leading_Species_Code does not match OLEADSPC')

    if PLOT_MASTER_AGE != BL_FinalLayer_SpatialJoin_OAGE:
        failStrings.append('Age does not match OAGE')

    if abs( PLOT_MASTER_ECO_CODE - BL_FinalLayer_SpatialJoin_PRI_ECO_2 ) >0.01:
        failStrings.append('ECO_CODE does not match PRI_ECO')

    return ','.join(failStrings) # joins the list strings together with a comma

Instead of checking if > 0.1 or < -0.1 I have used the abs() operator so you only need to check greater than because the value is not negative.

In field calculator you would call like this:

comments(!PLOT_MASTER.HEIGHT!, !BL_FinalLayer_SpatialJoin.OHT!,!PLOT_MASTER.STOCKING_D!, !BL_FinalLayer_SpatialJoin.STKG!,!PLOT_MASTER.LEADING_SPECIES_CODE!, !BL_FinalLayer_SpatialJoin.OLEADSPC!,!PLOT_MASTER.AGE!, !BL_FinalLayer_SpatialJoin.OAGE!,!PLOT_MASTER.ECO_CODE!, !BL_FinalLayer_SpatialJoin.PRI_ECO_2!)

enter image description here

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.