Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm wanting to create a field with a maximum of 1 decimal place and then calculate a value into it: (I know that all my values are < 10)

arcpy.AddField_management('C:/shapefile.shp', 'myField', "FLOAT", 2, 1, "", "", "", "NON_REQUIRED", "")
arcpy.CalculateField_management('C:/shapefile.shp', 'myField', "[MEAN]", "VB", "")

It seems that no matter what I try I can't get the number of decimal places I want - I get the full value from the field I'm calculating from : [0.888889] and when I view the field properties I just see 0 for precision and 0 for scale. The data context is ESRI shapefile. And altering the numeric settings in the field properties is not an option.

enter image description here

Anyone have any ideas?

share|improve this question
    
What is your data source? Precision and scale only apply to RDBMSes that support them, such as Oracle and SQL Server. File and personal geodatabases do not. –  blah238 Jul 2 '12 at 18:15
    
As I said it's from shape to shape. By shape I mean shapefile. –  valveLondon Jul 2 '12 at 18:17
    
Oh I missed that, sorry. This looks like another case where the ESRI documentation falls short as there is no mention of shapefile support for precision/scale, although it does appear to be supported if you set them when adding a field through the UI. –  blah238 Jul 2 '12 at 18:30
    
Yeah - When adding a new field via both the Attribute Table and ArcToolbox - the scale and precision values are respected. –  valveLondon Jul 2 '12 at 18:41
add comment

2 Answers

What is your data source? Precision and scale only apply to DBMSes that support them, such as Oracle and SQL Server. According to the Add Field (Data Management) documentation file and personal geodatabases do not:

  • The precision and scale of a field describe the maximum size and precision of data that can be stored in the field. The precision describes the number of digits that can be stored in the field and the scale describes the number of decimal places for float and double fields. For example, if the field value is 54.234, then scale = 3 and precision = 5. Use the following guidelines for choosing the correct field type for a given precision and scale:
    • When you create a float, double, or integer field and specify 0 for precision and scale, the tool will attempt to create a binary type field if the underlying database supports it. Personal and file geodatabases support only binary type fields, and precision and scale are ignored.
    • When you create float and double fields and specify a precision and scale, if your precision is greater than 6, use a double; otherwise, use a float. If you create a double field and specify a precision of 6 or less, a float field is created. If you create a float field and specify a precision greater than 6, a double field is created.
    • If you specify a scale of 0 and a precision of 10 or less, you should be creating integer fields. When creating integer fields, your precision should be 10 or less, or your field may be created as double.

Shapefiles, which use dBASE tables as the underlying storage format for the attribute table, DO appear to support precision and scale. I was able to add a field of type Float with Precision 6 and scale 4 and it shows up correctly in the UI. I did it both through the UI and through arcpy (AddField):

arcpy.AddField_management(fc, "test3", "FLOAT", 6, 4)

Shapefile field properties with precision 6 and scale 4 visible

share|improve this answer
    
So is it safe to assume that precision and scale also DO NOT apply to shapefiles? –  Chad Cooper Jul 2 '12 at 18:22
2  
Looks like shapefiles (which use dBASE tables as the underlying attribute table) DO support precision and scale although this is not spelled out in the documentation. –  blah238 Jul 2 '12 at 18:32
1  
Wow, there's a shocker, shapefiles support it, but file geodatabase doesn't. –  Chad Cooper Jul 2 '12 at 19:14
1  
@Chad That's not a shock at all: precision and scale are needed precisely because shapefiles do not store floats or doubles: they store decimal-encoded ASCII strings. As such, internally they contain information about the total number of characters the field can hold. If ArcGIS is equating this with precision, then a (2,1) field cannot exist: two characters suffice for the "." and the following digit, but do not leave room for the initial digit. Specifying (3,1) ought to work, much as Blah238 has found. –  whuber Jul 2 '12 at 19:43
    
I think I didn't phrase my question well. I know that shapefiles support prec. and scale. and I know how to specify them when adding a field in ArcMap. What I don't know is why the values that I pass in via scripting (AddField) are not respected. –  valveLondon Jul 3 '12 at 16:12
add comment

I think that arcpy is looking for number as opposed to a string. If you look at the documentation:

field_precision (Optional) Describes the number of digits that can be stored in the field. All digits are counted no matter what side of the decimal they are on.

If the input table is a personal or file geodatabase the field precision value will be ignored.

(Data Type) Long

To check you could always open up model bulider, set up a similar gp workflow and export as a python script.

share|improve this answer
    
Good catch, but that doesn't fix it. I've updated the question. thanks –  valveLondon Jul 2 '12 at 18:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.