I'm using arcpy.AddXY_management to add coordinates, and I want to reduce each X/Y field by x number. I've tried to reduce it by string slicing and that obviously didn't work. I also tried setting the precision but that doesn't work on FC's. Anyone have a better idea?
2 Answers
After you run ADD XY, run Calculate Field on each X/Y field. You just need the following in the expression (set the the expression type to PYTHON)
round(!POINT_Y!, 2)
and
round(!POINT_X!, 2)
Where 2 = the number of decimal places.
-
I got an error when I tried this. Does it matter which field type point_x and point_y are defined as? I tried the round function on another field, so its not my syntax. Commented Oct 30, 2013 at 16:58
-
1I changed the field type to "float" and it now works fine. I had it set to "double" and it didn't work. Commented Oct 30, 2013 at 17:06
I don't know why string slicing wouldn't work, so long as your fields are strings. Try this with table tableName that has two fields, POINT_X and POINT_Y that you want to reduce to int x numbers:
import arcpy
from arcpy import env
env.workspace = "yourFCPath"
arcpy.AddField_management("tableName", "newX", "TEXT", "", "", "20")
arcpy.AddField_management("tableName", "newY", "TEXT", "", "", "20")
arcpy.CalculateField_management("tableName", "newX",
'!POINT_X![:x]', "PYTHON")
arcpy.CalculateField_management("tableName", "newY",
'!POINT_Y![:x]', "PYTHON")
I don't see anywhere in documentation the output data type of arcpy.AddXY_management but if your fields are float/double instead of strings simply replace the last two lines with:
arcpy.CalculateField_management("tableName", "newX",
'str(!POINT_X!)[:x]', "PYTHON")
arcpy.CalculateField_management("tableName", "newY",
'str(!POINT_Y!)[:x]', "PYTHON")