Select to view content in your preferred language

Round to nearest 100th

10540
15
Jump to solution
06-06-2013 07:57 AM
ChrisBrannin
Regular Contributor
I am a little confused about how to round a field to the nearest 100th. Would I use math.ceil() or the round()?

math.ceil([field]/100)*100


Does this only round down though?

Thanks
Tags (2)
0 Kudos
15 Replies
RhettZufelt
MVP Notable Contributor
Deleted my last post after re-reading.

However, it looks like you can use one for loop to replace all three that you are running.

might try something like:


import arcpy import os, math from arcpy import env   in_workspace = r"F:/Inventory_export_dev/testRnd" fcList = []    for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"):     for filename in filenames:         arcpy.AddField_management(os.path.join(dirpath, filename), fieldName, "SHORT", "", "", "", "", "NULLABLE")         arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON") 


No need to put the filenames in a list if all you are going to do is iterate through them again.  Just as well do the work on the first iteration.

Of course, this will errror out if any of the FC's in your in_workspace do not have the numeric ELEVATION field.

R_
0 Kudos
ChrisBrannin
Regular Contributor
Deleted my last post after re-reading.

However, it looks like you can use one for loop to replace all three that you are running.

might try something like:


import arcpy
import os, math
from arcpy import env


in_workspace = r"F:/Inventory_export_dev/testRnd"
fcList = []



for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"):
    for filename in filenames:
        arcpy.AddField_management(os.path.join(dirpath, filename), fieldName, "SHORT", "", "", "", "", "NULLABLE")
        arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON")



No need to put the filenames in a list if all you are going to do is iterate through them again.  Just as well do the work on the first iteration.

Of course, this will errror out if any of the FC's in your in_workspace do not have the numeric ELEVATION field.

R_


I see this works perfectly then. Is there an easy way to skip the files that do not have the elevation field?
0 Kudos
RhettZufelt
MVP Notable Contributor
Replace the work part with this should do it:


for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"):
    for filename in filenames:
       if "ELEVATION" in [f.name for f in arcpy.ListFields(os.path.join(dirpath, filename))]:
         arcpy.AddField_management(os.path.join(dirpath, filename), "SL_ELEV", "SHORT", "", "", "", "", "NULLABLE")
         arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON")
       else:
           print "no Elevation field for dataset ", filename



Of course, you can skip the else: part, only needed if you want it to do something when ELEVATION field not present.

R_
0 Kudos
ChrisBrannin
Regular Contributor
Replace the work part with this should do it:


for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"):
    for filename in filenames:
       if "ELEVATION" in [f.name for f in arcpy.ListFields(os.path.join(dirpath, filename))]:
         arcpy.AddField_management(os.path.join(dirpath, filename), "SL_ELEV", "SHORT", "", "", "", "", "NULLABLE")
         arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON")
       else:
           print "no Elevation field for dataset ", filename



Of course, you can skip the else: part, only needed if you want it to do something when ELEVATION field not present.

R_


This is seriously great. I tried something like this with no luck. But now I understand why it did not work.
if "ELEVATION" == arcpy.ListFields(os.path.join(dirpath, filename)):


Thank you for your time with this.
0 Kudos
RhettZufelt
MVP Notable Contributor
So you see that it was trying to compare a single string "ELEVATION" to a list of objects.

basically, could do something like:


fieldList = arcpy.ListFields(os.path.join(dirpath, filename))

for f in fieldList:
   if f.name == "ELEVATION":
         arcpy.AddField_management(os.path.join(dirpath, filename), "SL_ELEV", "SHORT", "", "", "", "", "NULLABLE")
         arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON")



Which is the same thing, I just used list comprehension to shorten the list, iterator, and comparison into one line instead of three. (love list comprehension, just wish I were better at it...)

R_
0 Kudos
wilfredwaters
Regular Contributor
Is there a way to do this in raster calculator?

Would Int( Floor( [in_raster] + 0.5 ) ) suffice?

Or even Int( [in_raster] + 0.5 ) ?
0 Kudos