1

I have a set of wells I am trying to label with contaminant data using python. I'd like the well name and constituents stacked but also formatted a certain way. This is what I have so far:

def FindLabel ( [Location], [Benzene],[Ethyl_Benzene],[Toluene],[Total_Xylenes_result],[TPH_as_Gasoline],[Diesel_range_organics]):
  if [Benzene] is None:
    return [Location] + '\n' + "NS"
  else:
    return [Location] + '\n' + "B: " + [Benzene] + '\n' + "E: " + [Ethyl_Benzene] + '\n' + "T: " + [Toluene] + '\n' + "X: " + [Total_Xylenes_result] + '\n' + "GRO: " + [TPH_as_Gasoline] + '\n' + "DRO: " + [Diesel_range_organics]

This works fine but I'd like to add some conditional formatting to it so the Benzene value is red for instance if it's above some criteria. Something like this:

def FindLabel ( [Location], [Benzene] ):
  if [Benzene] is None:
    return [Location] + '\n' + "NS"
  elif int(float([Benzene].replace("<",""))) > 5:
    return [Location] + '\n' + "B: " + "<CLR red='255'>" + [Benzene] + "</CLR>" 
  else:
    return [Benzene]

Is there a way to combine this all? Basically the value would be "NS" if there's no data, just the value if it doesn't exceed the criteria, and the value displayed red if it does exceed the criteria, and include all the values listed and stacked. The complicating factor is that I have some non-numerical flags ("<") in my data which is where the replace comes in.

Example Data Location,Benzene,Ethyl Benzene,Toluene,Total_Xylenes_result,TPH as Gasoline,Diesel range organics MW-1,98.2,504,6.7,578.8,10800,4350 MW-4,<1.0,<1.0,<1.0,<3,<100,<400 MW-5,<1.0,<1.0,<1.0,<3,<100,<400

Here is what it might look like, but the value of 98.2 in MW-1 would be red. enter image description here

4
  • Can you provie some example data?
    – Xeppit
    Commented Feb 23 at 20:12
  • @xeppit edited to include some example data. Commented Feb 23 at 20:27
  • So this attributes are all strings (text)?
    – Xeppit
    Commented Feb 23 at 20:29
  • They are all stored as text, though some might be numbers. Commented Feb 23 at 20:32

1 Answer 1

1
def FindLabel ([Location], [Benzene], [Ethyl_Benzene], [Toluene]):
    labelStatic = '\n' + "E: " + [Ethyl_Benzene] + '\n' + "T: " + [Toluene]
    try:
        controlValue = float([Benzene].replace("<",""))
    except:
        pass    
    if not [Benzene]:
        return [Location] + '\n' + "NS" + labelStatic
    elif  controlValue > 5:
        return [Location] + '\n' + "B: " + "<CLR red='255'>" + "&#60;" + str(controlValue) + "</CLR>" + labelStatic + "Hallo"
    else:
        return [Location] + '\n' + "B: " + [Benzene] + labelStatic

enter image description here

"&#60;" is ASCII-Code for <. You should escape signs that could be interpreted as tag (opening or cloesing).

UPDATE on Comment

def FindLabel ([Location], [Benzene], [Ethyl_Benzene], [Toluene]):
    lblLocation = [Location]
    lblBenzene = '\n' + "NS" if not [Benzene] else '\n' + "B: " + [Benzene]
    lblEthyl_Benzene = '\n' + "NS" if not [Ethyl_Benzene] else '\n' + "E: " + [Ethyl_Benzene]
    lblToluene = '\n' + "NS" if not [Toluene] else '\n' + "B: " + [Toluene]

    try:
        ctrlBenzene = float([Benzene].replace("<",""))
        if ctrlBenzene > 5:
            lblBenzene = '\n' + "B: " + "<CLR red='255'>" + "&#60;" + str(ctrlBenzene) + "</CLR>"
    except:
        pass
    try:
        ctrlEthyl_Benzene = float([Ethyl_Benzene].replace("<",""))
        if ctrlEthyl_Benzene > 5:
            lblEthyl_Benzene = '\n' + "B: " + "<CLR red='255'>" + "&#60;" + str(ctrlEthyl_Benzene) + "</CLR>"
    except:
        pass
    try:
        ctrlToluene = float([Toluene].replace("<",""))
        if ctrlToluene > 5:
            lblToluene = '\n' + "B: " + "<CLR red='255'>" + "&#60;" + str(ctrlToluene) + "</CLR>"
    except:
        pass
        
    return lblLocation + lblBenzene + lblEthyl_Benzene + lblToluene

enter image description here

4
  • If I am reading that right, it looks like benzene is the only thing that is conditional. What if all the constituents I want to have some sort of conditional test? Sorry if I wasn't clear about that. Thanks for your help! Commented Feb 23 at 21:58
  • As far as I know ArcGis Pro 3.2 supportes python version Python 3.9.17, python Switch Statements are supported from python 3.10 upwards, so you have to use elif, elif .... freecodecamp.org/news/… you could implement varaible ctrlBenzene, ctrlToluene ... you could optimize it with an own function for control but just six values that not necessary. In addition you could wrtite lblBenzene = '\n' + "B: " + [Benzene] and so on to make the code a little bit more read able and for maintenace
    – Xeppit
    Commented Feb 23 at 22:11
  • Updated the code
    – Xeppit
    Commented Feb 23 at 22:47
  • This worked great! I changed things around a bit, but this does exactly what I want now. Thanks! Commented Feb 26 at 22:46

Your Answer

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

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