File geodatabase Feature class vs Shapefile Feature class

345
1
08-13-2021 09:51 AM
BensonColeC
New Contributor II

I am trying to create fields of type double in my code. To do this I used Geoprocessing.ExecuteToolAsync("AddField_management", Geoprocessing.MakeValueArray(featureclass, fieldname, type)). I do this for type double. Then I use calculate field to populate those new fields. For some reason, it works fine on file geodatabase classes, however, when I try this on shapefile feature classes it says the new fields are type text which messes up the whole program. Why is it working for some but not others? Would it be a problem with how I added the fields or something else?

0 Kudos
1 Reply
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I will check with the dev team next week, however, i found that the order of the precision and scale parameters are switched.  Depending on those values the field type was changed.  The following snippet worked for me when using a shapfile (it's a simple button in an add-in module and all paths and values are hard coded):

internal class AddDoubleToShape : Button
{
  protected override async void OnClick()
  {
	var shpPath = $@"C:\Data\FeatureTest\shapefile\base_state.shp";
	var fldName = "DTest";
    var alias = "";
	var fldType = "DOUBLE";
	var precision = 11;
    var scale = 3;
    var result = await ExecuteAddFieldTool(shpPath, fldName, alias, fldType, scale, precision, null, true);
  }

  /// <summary>
  /// Adds a field to a table using GP
  /// </summary>
  /// <param name="shpPath"></param>
  /// <param name="fieldName"></param>
  /// <param name="fieldAlias"></param>
  /// <param name="fieldType"></param>
  /// <param name="precision"></param>
  /// <param name="scale"></param>
  /// <param name="fieldLength"></param>
  /// <param name="isNullable"></param>
  /// <returns>true: if field was added</returns>
  private async Task<bool> ExecuteAddFieldTool(string shpPath, string fieldName, 
                          string fieldAlias, 
                          string fieldType, int precision, int scale,
                          int? fieldLength = null, bool isNullable = true)
  {
    try
    {
      return await QueuedTask.Run(() =>
      {
        // from arcpy command (from geoprocessing dockpane / share):
        // arcpy.management.AddField(r"C:\Data\FeatureTest\shapefile\base_state.shp",
        // "TestMyD", // name
        // "DOUBLE",  // type
        // 2,         // scale
        // 10,        // precision (reversed order ?  with scale ?)
        // None,      // length
        // '',        // name
        // "NULLABLE",
        // "NON_REQUIRED",
        // '')
        var parameters = Geoprocessing.MakeValueArray(shpPath, fieldName, 
          fieldType.ToUpper(), scale, precision,
          fieldLength, fieldAlias, isNullable ? "NULLABLE" : "NON_NULLABLE");
        var cts = new CancellationTokenSource();
        var result = Geoprocessing.ExecuteToolAsync("management.AddField", parameters, null, cts.Token,
            (eventName, o) =>
            {
              System.Diagnostics.Debug.WriteLine($@"GP event: {eventName}");
            });
        return !result.Result.IsFailed && !result.Result.IsCanceled;
      });
    }
    catch (Exception ex)
    {
      MessageBox.Show(ex.ToString());
      return false;
    }
  }
}

 

0 Kudos