Select to view content in your preferred language

Hatch expression help

3544
15
Jump to solution
08-12-2013 01:32 PM
BrettMorse
Emerging Contributor
This is driving me crazy and I'm sure its something really simple but I can't find examples to show me what to do so I'm hoping someone here can help me out.  I have a route with measures that I'm trying to format into 123+45

I ran this in PythonWin just to make sure I had this part right:

import arcpy
station = "37000"
rs = station[-2:]
ls = station[:-2]
print ls + "+" + rs

it gave me this:

370+00

just what I'm trying to do.  So then I go to the Hatch Text Expression dialogue and hit advanced.  First thing I probably need to know is what FindLabel is doing?  I looked around on the internet  trying to find it and still I'm not very sure.  Could be I just suck at the internet though.  I'm guessing the statement def FindLabel ( esri_measure) just lists the arguments you're going to use below it.  Does it make a string or is it a still a number?

So far in the advanced box I can get this much to work:

def FindLabel ( esri__measure 😞
  return str(int(esri__measure))

and it seems to return a value with no decimals just fine.

But when I start trying to add to that to get my 123+45 format it keeps giving me the error "No features found.  Could not verify expression."

This is going to have a lot to do with my inexperience using Python this way and my inexperience with Python in general I'm sure, but I haven't been able to find any good examples on the internet to help me puzzle this out or explain it to me.

If someone could show me how to enter this into the advanced expression box or point me to some good examples so I can figure it out myself that would be great.

Thanks in advance!
Tags (2)
0 Kudos
15 Replies
StacyRendall1
Frequent Contributor
To get to the hatching you have to be using a route.  The esri__measure comes from that automatically and is not a field that requires the [].

To get to the hatch just go to your route properties and it will be a tab at the top.  Under the Hatch Class in the box on the left there will be a Hatch Def(1) or however many you add.  If you click on the Hatch Def you will have options, one of which will be to label the hatches you have set up.  You can control the symbol here like normal.  You can then click the label settings in the label section and click the build a text expression bubble and then click the expression button.

Once you click the expression button the dialogue box looks the exactly the same except it says Hatch Text Expression at the top instead of  Label Expression.  This is why I assume they are the same, just in two different places.

I appreciate the help with this,

Thanks


See answer above. Finally found Hatch Expression on my own so updated the previous post. Didn't see your post until I had finished editing...
0 Kudos
StacyRendall1
Frequent Contributor

def FindLabel ( esri__measure ):   if len(str(int( esri__measure ))) == 0:     return "0+00"   elif len(str(int( esri__measure ))) == 2:     return "0+{0}{1}".format( *esri__measure )   elif len(str(int( esri__measure ))) == 3:     return "{0}+{1}{2}".format( *esri__measure )   elif len(str(int( esri__measure ))) == 4:     return "{0}{1}+{2}{3}".format( *esri__measure )   elif len(str(int( esri__measure))) == 5:     return "{0}{1}{2}+{3}{4}".format( *esri__measure )   elif len(str(int( esri__measure ))) == 6:     return "{0}{1}{2}{3}+{4}{5}".format( *esri__measure )

It verifies, but it returns nothing.  Just a blank.  I also tried it without the str(int part and got the same thing.  A blank when verified.


This fails because the asterisk (*) only works on certain iterables: strings, lists, tuples. A float (which esri__measure is) is not iterable. However, I am surprised it didn't throw a more useful error, like TypeError.

The other issues you discussed in the above-mentioned post appear to be related to having square brackets within the expression. I cannot get square brackets within the expression to work...

The code below is an adaptation of your code above, but includes the case where len == 1. My earlier answer didn't account for the fact that esri_measure is a floating point number, so will not work correctly. You should note that using int() on floats doesn't round as you might expect, so I use int(round(esri__measure)).

def FindLabel (esri__measure):   EM = str(int(round(esri__measure)))   if len(EM) == 0:     return "0+00"   elif len(EM) == 1:     return "0+0{0}".format(*EM)     elif len(EM) == 2:     return "0+{0}{1}".format(*EM)   elif len(EM) == 3:     return "{0}+{1}{2}".format(*EM)   elif len(EM) == 4:     return "{0}{1}+{2}{3}".format(*EM)   elif len(EM) == 5:     return "{0}{1}{2}+{3}{4}".format(*EM)   elif len(EM) == 6:     return "{0}{1}{2}{3}+{4}{5}".format(*EM)
0 Kudos
Luke_Pinner
MVP Regular Contributor
The code block, which is simply a python function that takes an input and returns an output, should use a Python variable -- not field names. (The insertion of the field value for [fieldname] only happens in the expression block.)

Expression: FindLabel(![FIELDNAME]!)

Code:
def FindLabel(fn):
  rs = fn[:3] # first three chars
  ls = fn[3:]  # characters four-> end
  return "{0}+{1}".format(rs, ls)  
 


In 10.1 there's no separate expression and code blocks, just an expression block in which you write the label function (in the Advanced view). The function def and the function code accepts field names:
[ATTACH=CONFIG]26686[/ATTACH]
0 Kudos
BrettMorse
Emerging Contributor
Thank you  StacyRendall!!!!

I was almost there, not sure what happened to the == 1 in that one block, I swear it was there before.

So the trick is to make the variable and do the rounding and integer business then get the length of that instead of trying to do it on every if statement, saving all that typing in the process haha.

I'm still pretty new to python, only on chapter 7 in my book and that was the first time using the .format and * so thanks for teaching me something new.  I'll get it all figured out eventually.

Thanks again!

Now I just have to figure out how to get them perpendicular to my line.

Oh, that worked if someone missed it.
0 Kudos
StacyRendall1
Frequent Contributor
Thank you  StacyRendall!!!!

I was almost there, not sure what happened to the == 1 in that one block, I swear it was there before.

So the trick is to make the variable and do the rounding and integer business then get the length of that instead of trying to do it on every if statement, saving all that typing in the process haha.

I'm still pretty new to python, only on chapter 7 in my book and that was the first time using the .format and * so thanks for teaching me something new.  I'll get it all figured out eventually.

Thanks again!

Now I just have to figure out how to get them perpendicular to my line.

Oh, that worked if someone missed it.


Great! I had never used .format() until now either!

Cheers,
Stacy
0 Kudos
IvySchultz1
Emerging Contributor
How do I do this if I'm trying to add an integer to each hatching value (to match up mileposts which don't start at zero) and round to the nearest tenth? If I use a hatching label expression, it won't let me also select the precision value so I need to include what I want the label to be rounded to in the hatching label expression and I don't know enough python to do this. Also, I'm using a measured polyline with one record so I don't have any fields to base it off of...except esri_measure, I think. I'm new to using measured polylines..
0 Kudos