6

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
1

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],}
  • I'm not sure what you mean by key clobbering, sometimes inverts are the same elevation which is what I have there. But it was the unique key that did the trick. Thank you. Problem now is that I can have multiple inverts in the same direction - not common but possible. Perhaps I can just print the list with pairs instead of worrying about dictionaries. – Barrett Feb 19 '16 at 20:52
  • Just out of curiosity, if lists were working, what was the reason for trying to use dictionaries? – Mintx Feb 19 '16 at 21:15
  • Probably the main reason is that I hack around on my code snippets till they work. Second, what I was labeling made sense with key:value pairs (invert and direction) and I figured a dictionary was made that for so I used it. In-fact, when i realized the code wasn't printing all inverts on some, I had to work backwards until I figured out that the list would work. – Barrett Feb 19 '16 at 21:24

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.