Friday 14 April 2017

labeling - Using Python Dictionary to label subscript formatting ArcMap 10.6



*Updated Question at bottom Update #3


I need to label chemical formulas out of the attribute table but the attribute table doesn't recognize anything but plain text.


Therefore I have built a dictionary containing each formula so Arc will know how to format each formula,



Issue #1 BUT only the first chemical formula formats correctly and the rest prints as plain text. Does anyone know how to fix this? This has been fixed, please continue to Update #3


Below is an example of my code.


NOTE: my Feature is Wells and its labeling field is GWMW_NAME. The field is printed as such [Wells.GWMW_NAME] as I have joined my shape with another table to join the chemical formula data I need to label.


My chemical formula data is in the following fields: [Exceedance Figure Data - FOXLF.csv.SPRING_EXC] & [Exceedance Figure Data - FOXLF.csv.FALL_EXC]


These fields contain my formulas which are in brackets and are separated by a comma as such: (EC, HCO3-, CaCO3, Ca, OH-, Mg, K, SO42-, TDS, Mn, Sr)


Here is a snip from my attribute table:


enter image description here


def FindLabel ( [Wells.GWMW_NAME] , [Exceedance Figure Data - FOXLF.csv.SPRING_EXC] , [Exceedance Figure Data - FOXLF.csv.FALL_EXC]   ):
formats = {
'SiO2': 'SiO2',

'CO32-': 'CO32-',
'HCO3-': 'HCO3-',
'CaCO3': 'CaCO3',
'NH3': 'NH3',
'OH-': 'OH-',
'NO2-': 'NO2-',
'SO42-': 'SO42-'
}
formula1 = [Exceedance Figure Data - FOXLF.csv.SPRING_EXC].strip('()').split(',')
formula2 = [Exceedance Figure Data - FOXLF.csv.FALL_EXC].strip('()').split(',')

label1 = ','.join([formats.get(f, f) for f in formula1])
label2 = ','.join([formats.get(f, f) for f in formula2])

return "{}\r\n ({})\r\n({})".format( [Wells.GWMW_NAME] , label1,label2 )

Below is a snip from what my code produces, note the properly formatted formulas circled in green, and the subsequent formulas all print as plain text (I circled some in red to show which did not label correctly).


Can anyone help me fix this so all of my chemical formulas will format and not just the first? This has been fixed, please go to Update #3 for newest issue


Example


Update #1 I've modified my code to try and incorperate Bera's code into mine as seen below**, however I am getting an error of 'Syntax error: 'return' outside function (,string>,line 19).


def FindLabel ( [Wells.GWMW_NAME],  [Exceedance Figure Data - FOXLF.csv.SPRING_EXC] , [Exceedance Figure Data - FOXLF.csv.FALL_EXC]  ):

a = [Exceedance Figure Data - FOXLF.csv.SPRING_EXC]
b = [Exceedance Figure Data - FOXLF.csv.FALL_EXC]
formats = {
'SiO2': 'SiO2',
'CO32-': 'CO32-',
'HCO3-': 'HCO3-',
'CaCO3': 'CaCO3',
'NH3': 'NH3',
'OH-': 'OH-',
'NO2-': 'NO2-',

'SO42-': 'SO42-'
}
c = [i.strip(',') for i in a[1:-1].split()]
d = [i.strip(',') for i in b[1:-1].split()]
e = [formats.get(i,i) for i in c]
f = [formats.get(i,i) for i in d]
return "{}\r\n ({})\r\n({})".format( [Wells.GWMW_NAME] ,e ,f )

This has been fixed through comments, please go to update #3 for most recent issue


Update #2 I have fixed the indent as per Matt's recommendation, however now I have correct subscript formatting but every formatted chemical formula is being placed inside of ' '. The total line of formulas which is supposed to be in regular brackets are also now inside square brackets. I'm not sure why these extra characters andf ' ' are being added in, especially as I'm not asking for square brackets anywhere. Snip below



enter image description here And here is the code which produced this snip. Update #2 issue fixed via comments from Matt


def FindLabel ( [Wells.GWMW_NAME],  [Separate Exceedance Figure Data - FOXLF.csv.SPRING_EXC] , [Separate Exceedance Figure Data - FOXLF.csv.FALL_EXC] ):
a = [Separate Exceedance Figure Data - FOXLF.csv.SPRING_EXC]
b = [Separate Exceedance Figure Data - FOXLF.csv.FALL_EXC]
formats = {
'SiO2': 'SiO2',
'CO32-': 'CO32-',
'HCO3-': 'HCO3-',
'CaCO3': 'CaCO3',
'NH3': 'NH3',

'OH-': 'OH-',
'NO2-': 'NO2-',
'SO42-': 'SO42-'
}
c = [str(i.strip(',')) for i in a[1:-1].split()]
d = [str(i.strip(',')) for i in b[1:-1].split()
e = [formats.get(i,i) for i in c]
f = [formats.get(i,i) for i in d]
return "{}\r\n ({})\r\n({})".format( [Wells.GWMW_NAME] ,e ,f )


Update #3 I got my code to work perfectly, UNTIL I needed to make a small edit on my map - now arc is throwing me all kind of unexpected errors and I can't get my code to work anymore. Most recently, it will label one symbol/label class properly, then fail for the other class from the same feature.. so bizarre and its delaying my finished jobs!


Whats even more bizarre is that I tried exporting a shape after joining my original shape with the exceedance table data (just in case the table join was causing the python errors). Once I did this and corrected the fields, wells with no FALL or SPRING_EXC values should label just the [GWMW_NAME] (as per my elif at the end of my code) but instead it also ended up labeling empty brackets below the [GWMW_NAME] (which I do not ask for if [SPRING_EXC] and [FALL_EXC] both == None). So the same code is producing multiple outputs and sometimes just returning errors.


A snip of the error I am getting is below with my most up to date code: Current Error This is the error I am getting.


Here is the code producing said error:


def FindLabel ( [WellShape.GWMW_NAME]  ,  [2018ExceedanceFigureData-SGPLF_mhuber2.csv.SPRING_EXC], [2018ExceedanceFigureData-SGPLF_mhuber2.csv.FALL_EXC] ):
a = [2018ExceedanceFigureData-SGPLF_mhuber2.csv.SPRING_EXC]
b = [2018ExceedanceFigureData-SGPLF_mhuber2.csv.FALL_EXC]
formats = {
'SiO2': 'SiO2',
'CO32-': 'CO32-',

'HCO3-': 'HCO3-',
'CaCO3': 'CaCO3',
'NH3': 'NH3',
'NO3-': 'NO3-',
'NO2-': 'NO2-',
'NO3-+NO2-': 'NO3-+NO2-',
'OH-': 'OH-',
'NO2-': 'NO2-',
'SO42-': 'SO42-'
}

c = [str(i.strip(',')) for i in a[1:-1].split()]
d = [str(i.strip(',')) for i in b[1:-1].split()]
e = ','.join([formats.get(i,i) for i in c])
f = ','.join([formats.get(i,i) for i in d])
if a == None and b != None :
return "{}\r\n ({})".format( [WellShape.GWMW_NAME] ,f )
elif a != None and b == None:
return "{}\r\n ({})".format( [WellShape.GWMW_NAME] ,e )
elif a != None and b != None:
return "{}\r\n ({})\r\n({})".format( [WellShape.GWMW_NAME] ,e ,f )

elif a == None and b == None:
return [WellShape.GWMW_NAME]

Answer



All the code that should be inside the function must be indented in Python.


def FindLabel ( [Wells.GWMW_NAME],  [Exceedance Figure Data - FOXLF.csv.SPRING_EXC] , [Exceedance Figure Data - FOXLF.csv.FALL_EXC]  ):
a = [Exceedance Figure Data - FOXLF.csv.SPRING_EXC]
b = [Exceedance Figure Data - FOXLF.csv.FALL_EXC]
formats = {
'SiO2': 'SiO2',
'CO32-': 'CO32-',

'HCO3-': 'HCO3-',
'CaCO3': 'CaCO3',
'NH3': 'NH3',
'OH-': 'OH-',
'NO2-': 'NO2-',
'SO42-': 'SO42-'
}
c = [i.strip(',') for i in a[1:-1].split()]
d = [i.strip(',') for i in b[1:-1].split()]
e = [formats.get(i,i) for i in c]

f = [formats.get(i,i) for i in d]
return "{}\r\n ({})\r\n({})".format( [Wells.GWMW_NAME] ,e ,f )

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...