I need a script for Display Expression of Mile Markers

1005
5
04-23-2014 10:24 AM
AlmaGormley
New Contributor
Hi-
I am trying to show only the "whole Number" mile markers from my data file of State post Mile Markers.
The data right now has tenth displayed as well. I am not savvy in scripting but think this should be a fairly easy one?
Could you be so kind to shoot me over a script? Python or VB with any explaination on how to enter it properly.
My field is PM (PostMiles)

Kindest Regards,
Alma Gormley
almag@wallacegroup.us
Tags (2)
0 Kudos
5 Replies
Zeke
by
Regular Contributor III
Are you familiar with modulus division? It gives you the remainder. You want to check if PM % 1 = 0. That will be true for whole numbers, such as 11 % 1 == 0. 11.5 % 1 will equal 0.5. (This is python notation).

It's a little more complicated if PM is a float or double datatype, because there you'll have to check if the value is very close to 0, rather than equaling 0, but the principle is the same. If PM is text, there's an even simpler way, but I'm guessing it's numeric.

So in label expressions, you want something like this (in vbscript):

Function FindLabel ( [PM] )
  if [PM] mod 1 = 0 then
      FindLabel = [PM]
  end if
End Function
0 Kudos
AlmaGormley
New Contributor
Are you familiar with modulus division? It gives you the remainder. You want to check if PM % 1 = 0. That will be true for whole numbers, such as 11 % 1 == 0. 11.5 % 1 will equal 0.5. (This is python notation).

It's a little more complicated if PM is a float or double datatype, because there you'll have to check if the value is very close to 0, rather than equaling 0, but the principle is the same. If PM is text, there's an even simpler way, but I'm guessing it's numeric.

So in label expressions, you want something like this (in vbscript):

Function FindLabel ( [PM] )
  if [PM] mod 1 = 0 then
      FindLabel = [PM]
  end if
End Function


Thank you for your reply, for some reason, that code still shows every tenth value. I did check if my field is a number field, it's a DOUBLE.
0 Kudos
MattEiben
Occasional Contributor
Are you looking for ArcMap to only display labels if the float number is equivalent an even integer?
Ex: 12.0 is labeled, but 12.3 is NOT

If you're looking to not display them at all, you can use the Definition Query tab, which actually used SQL queries instead of Python.

Assuming your PM field is a float or double, you can use something like this.

MOD(ROUND("PM" * 10, 0), 10) = 0


The Modulus Operator in SQL doesn't handle floats, but if multiply your values by 10 and round them, you should be able to Modulus them by 10 to test for the same thing in a roundabout fashion.


For Labeling only these features heres my version of the labeling function in Python:

def FindLabel ( [PM] ):
    if float([PM]) % 1 == 0:
        return [PM]
0 Kudos
AlmaGormley
New Contributor
Are you looking for ArcMap to only display labels if the float number is equivalent an even integer?
Ex: 12.0 is labeled, but 12.3 is NOT

If you're looking to not display them at all, you can use the Definition Query tab, which actually used SQL queries instead of Python.

Assuming your PM field is a float or double, you can use something like this.

MOD(ROUND("PM" * 10, 0), 10) = 0


The Modulus Operator in SQL doesn't handle floats, but if multiply your values by 10 and round them, you should be able to Modulus them by 10 to test for the same thing in a roundabout fashion.


For Labeling only these features heres my version of the labeling function in Python:

def FindLabel ( [PM] ):
    if float([PM]) % 1 == 0:
        return [PM]


Thanks! That's exactly what I needed! It worked beautifully 🙂

Alma
0 Kudos
MattEiben
Occasional Contributor
You're welcome, glad I can help!

Matt
0 Kudos