Saturday, 25 November 2017

arcgis desktop - Python Labeling using dictionary not printing all values


I am labeling manhole inverts using python and ArcMap 10.3. When I use a dictionary to control the label order some manholes don't print all of the inverts while other manholes do. I can post field information/attribute table info if needed. I suspect its an error on my part using an empty/none field in the dictionary and underlying hash table but I really don't know enough to say that with confidence.



How can I get all the inverts to display? - do a check on every argument for none or "" before inserting into the dictionary?


Working Label Expression:


I think here dict is set to a list with grouped values (key,value),(key,value),etc and everything is working correctly:


import json
def FindLabel ( [Elevation] , [Out_Elev] , [Out_Loc] , [In1_Elev] , [In1_Loc] , [In2_Elev] , [In2_Loc] , [In3_Elev] , [In3_Loc] ):
dict = ([Out_Elev],[Out_Loc]),([In1_Elev],[In1_Loc]),([In2_Elev],[In2_Loc]),([In3_Elev],[In3_Loc])
x = json.dumps(dict, sort_keys=True)
return x

Inverts Showing



Not-Working Label Expressions:


The next two pictures show where I try to use a dictionary. This first one is changing dict from a list to a dictionary using dict = OrderedDict(dict) and the second one is setting straight to a dictionary using dict = {key:value,key:value,etc}. I don't understand why all 3 inverts display for the manhole at the top of the picture but not for the one on bottom left.


Using dict = OrderedDict(dict)


Inverts not showing


from collections import OrderedDict
import json
def FindLabel ( [Elevation] , [Out_Elev] , [Out_Loc] , [In1_Elev] , [In1_Loc] , [In2_Elev] , [In2_Loc] , [In3_Elev] , [In3_Loc] ):
dict = ([Out_Elev],[Out_Loc]),([In1_Elev],[In1_Loc]),([In2_Elev],[In2_Loc]),([In3_Elev],[In3_Loc])
dict = OrderedDict(dict)
x = json.dumps(dict, sort_keys=True)

return x

Another code variant where inverts are not showing using dict = {key:value,key:value,etc}


Inverts not showing 2


import json
def FindLabel ( [Elevation] , [Out_Elev] , [Out_Loc] , [In1_Elev] , [In1_Loc] , [In2_Elev] , [In2_Loc] , [In3_Elev] , [In3_Loc] ):
dict = {[Out_Elev]:[Out_Loc],[In1_Elev]:[In1_Loc],[In2_Elev]:[In2_Loc],[In3_Elev]:[In3_Loc],}
x = json.dumps(dict, sort_keys=True)
return x

Answer




It looks like you are accidentally key clobbering. In your first example that works in the bottom left, you have a list with paired values:


332.207435 -> South
332.307435 -> North
332.307435 -> West

North and West have the same key value. If you want to put this into a dictionary, your key must be unique, otherwise it will just overwrite the value without creating a new key.


Try swapping your [Elev] and [Loc] values in your dictionary:


dict = {[Out_Loc]:[Out_Elev],[In1_Loc]:[In1_Elev],[In2_Loc]:[In2_Elev],[In3_Loc]:[In3_Elev],}

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...