Field Calculator - Diameter conversion with conditions

378
8
Jump to solution
12-11-2017 05:30 AM
THomasDanks
New Contributor II

Hello,

I am very new with python so have been really struggling with the field calculator for what seems like a simple task. (it is in excel anyway)

I have a shapefile with a column named d_diameter, but this column is String and contains entires; 100mm, 2in,4in, 70in, 50mm, 73mm etc. I am looking to convert these to numbers, specifically to milimeters (mm) and to 2 decimal points.

So ones with "mm" extension, I just want the number. Entries with the "in" extension in I want this removed and the value timesed (*) by 25.4 to convert it to mm.

e.g. 100mm becomes 100 and 2in becomes 50.8 etc

The kicker is sometimes it has NULL values, or Text "Other" or "Unknown". Ideally I want in these cases for it to come through as NULL.

Any idea or input where to start would be greatly appreciated.

Cheers,

Thomas

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

try this but your field calculator expression would be

cal(!YourFieldWithTheOriginalDataHere! 

using the python parser

def cal(a):
    if 'in' in a:
        return str(float(a.split('in')[0])*2.54) + 'mm'
    else:
        return a

a = '9in'
b = '22.86mm'

cal(a)  #'22.86mm'
cal(b)  #'22.86mm'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

8 Replies
JoeBorgione
MVP Esteemed Contributor

Post a sample of how the data is formatted; sounds like the field is of text type and you need the strip off the mm?  Take a look at rstrip — Python Reference (The Right Way) 0.1 documentation ; rstrip is your friend.

Then take a look at the int() function: int — Python Reference (The Right Way) 0.1 documentation  Once you  get rid of the mm, you can treat a string like a number.

Finally, you can use round() to the decimal place of your choice: round — Python Reference (The Right Way) 0.1 documentation 

Untested and probably just one of a myriad of ways to approach it.

That should just about do it....
0 Kudos
THomasDanks
New Contributor II

Hi,

Thanks for the reply. I tried a similar thing with remove !d_material!.replace("in",("").replace("mm",""). I became stuck though because I only need to multiply the inches e.g. 2in, 4in, 15in by 25.4 and leave the milimeter (mm) ones alone in that caluclation. I didn't know how to do that though. Seems like I need a else if statement, but not sure how to contruct one.

The int function is very good to know thanks. A sample of my data with the column datatype is shown below:

Any ideas?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

try this but your field calculator expression would be

cal(!YourFieldWithTheOriginalDataHere! 

using the python parser

def cal(a):
    if 'in' in a:
        return str(float(a.split('in')[0])*2.54) + 'mm'
    else:
        return a

a = '9in'
b = '22.86mm'

cal(a)  #'22.86mm'
cal(b)  #'22.86mm'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
THomasDanks
New Contributor II

Thanks everyone this worked great. Fantastic!! I'm off to learn some Python!!

0 Kudos
TedKowal
Regular Contributor II

Pre-Logic Scipt Code:

def dmm (dia):
 if dia is not None:
 units=dia[-2:]
 if units=='in':
 cnv = int(int(filter(str.isdigit,dia))*25.4)
 return str(cnv)+'mm'

YourField=

dmm(str( !d_diameter!))
TedKowal
Regular Contributor II

Dan's is better --- the sample code I was using had unicode strings in it ....

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

move to Pro and Python 3. 

Unicode has been covered by python since 3 and I haven't suffered with Unicode issues in over 8 years

0 Kudos
TedKowal
Regular Contributor II

Love to move to Pro but it does not accept MS Access which is critical to our operations.  The choice of DB's is not mine to make!

0 Kudos