/// <summary>
        /// Creates fieldmapping object with the OBJECTID field to be extracted to be used with GP tools.
        /// </summary>
        /// <param name="classObj">FeatureClass or table.</param>
        /// <param name="newOIDFldName">Name of the object id field in the mapping.</param>
        /// <returns></returns>
        public static IGPFieldMapping CreateFieldMap(this IClass classObj, string newOIDFldName, bool newOidFldNullable = false, int newOidDefaultVal = -1)
        {
            IGPFieldMapping fieldmapping = null;

            if (classObj is ITable || classObj is IFeatureClass)
            {
                IGPUtilities gputilities = new GPUtilities();

                IDETable inputTableA = (IDETable)gputilities.MakeDataElementFromNameObject((classObj as IDataset).FullName);

                IArray inputTables = new ESRI.ArcGIS.esriSystem.Array();
                inputTables.Add(inputTableA);

                fieldmapping = new GPFieldMapping() as IGPFieldMapping;
                fieldmapping.Initialize(inputTables, null);

                IFieldEdit2 oidField = new Field() as IFieldEdit2;
                oidField.Name_2         = newOIDFldName;
                oidField.Type_2         = esriFieldType.esriFieldTypeInteger;
                oidField.IsNullable_2   = newOidFldNullable;
                oidField.DefaultValue_2 = newOidDefaultVal;

                IGPFieldMap orgOIDFldMap = new GPFieldMap {
                    OutputField = oidField
                };
                IField OIDFld = classObj.Fields.Field[classObj.FindField(classObj.OIDFieldName)];
                orgOIDFldMap.AddInputField(inputTableA, OIDFld, 0, OIDFld.Length);

                fieldmapping.AddFieldMap(orgOIDFldMap);
            }

            return(fieldmapping);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Pre validates the given set of values.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <param name="environmentManager">Provides access to all the current environments and settings of the current client.</param>
        /// <param name="utilities">
        ///     The utilities object that provides access to the properties and methods of a geoprocessing
        ///     objects.
        /// </param>
        protected override void UpdateParameters(Dictionary<string, IGPParameter> parameters, IGPEnvironmentManager environmentManager, IGPUtilities2 utilities)
        {
            // Retrieve the input parameter value.
            IGPValue value = utilities.UnpackGPValue(parameters["in_table"]);
            if (!value.IsEmpty())
            {
                // Create the domain based on the fields on the table.
                IDETable table = value as IDETable;
                if (table != null)
                {
                    IFields fields = table.Fields;
                    if (fields != null)
                    {
                        IGPCodedValueDomain codedValueDomain = new GPCodedValueDomainClass();
                        foreach (var field in fields.AsEnumerable())
                            codedValueDomain.AddStringCode(field.Name, field.Name);

                        IGPParameterEdit3 derivedFields = (IGPParameterEdit3) parameters["in_fields"];
                        derivedFields.Domain = (IGPDomain) codedValueDomain;
                    }
                }
            }
        }
        private bool CheckForTableFields(IDETable inputTable, string[] fieldNames, esriFieldType[] fieldTypes,
                                         IGPMessage gpMessage)
        {
            IFields fields = inputTable.Fields;
            int fieldIndex;

            for (int i = 0; i < fieldNames.Length; i++)
            {
                fieldIndex = fields.FindField(fieldNames[i]);
                if (fieldIndex == -1)
                {
                    gpMessage.Type = esriGPMessageType.esriGPMessageTypeError;
                    gpMessage.Description = "Field named " + fieldNames[i] + " not found.";
                    return false;
                }

                if (fields.get_Field(fieldIndex).Type != fieldTypes[i])
                {
                    gpMessage.Type = esriGPMessageType.esriGPMessageTypeError;
                    gpMessage.Description = "Field named " + fieldNames[i] + " is not the expected type.";
                    return false;
                }
            }
            return true;
        }
Exemplo n.º 4
0
        private static void RunGPFieldMapping()
        {
            // Initialize the Geoprocessor
            ESRI.ArcGIS.Geoprocessor.Geoprocessor GP = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
            GP.OverwriteOutput = true;

            // Create the GPUtilites object
            IGPUtilities gputilities = new GPUtilitiesClass();

            // Create a DETable data element object
            IDETable inputTableA = (IDETable)gputilities.MakeDataElement(@"C:\data\citiblocks.gdb\census", null, null);

            // Create an array of input tables
            IArray inputtables = new ArrayClass();

            inputtables.Add(inputTableA);

            // Initialize the GPFieldMapping
            IGPFieldMapping fieldmapping = new GPFieldMappingClass();

            fieldmapping.Initialize(inputtables, null);

            // Create a new output field
            IFieldEdit trackidfield = new FieldClass();

            trackidfield.Name_2   = "TRACTID";
            trackidfield.Type_2   = esriFieldType.esriFieldTypeString;
            trackidfield.Length_2 = 50;

            // Create a new FieldMap
            IGPFieldMap trackid = new GPFieldMapClass();

            trackid.OutputField = trackidfield;

            // Find field map "STFID" containing the input field "STFID". Add input field to the new field map.
            int         fieldmap_index = fieldmapping.FindFieldMap("STFID");
            IGPFieldMap stfid_fieldmap = fieldmapping.GetFieldMap(fieldmap_index);
            int         field_index    = stfid_fieldmap.FindInputField(inputTableA, "STFID");
            IField      inputField     = stfid_fieldmap.GetField(field_index);

            trackid.AddInputField(inputTableA, inputField, 5, 10);

            // Add the new field map to the field mapping
            fieldmapping.AddFieldMap(trackid);

            // Execute Table to Table tool using the FieldMapping
            TableToTable tblTotbl = new TableToTable();

            tblTotbl.in_rows       = inputTableA;
            tblTotbl.out_path      = @"C:\data\citiblocks.gdb";
            tblTotbl.out_name      = "census_out";
            tblTotbl.field_mapping = fieldmapping;

            object sev = null;

            try
            {
                GP.Execute(tblTotbl, null);
                System.Windows.Forms.MessageBox.Show(GP.GetMessages(ref sev));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                System.Windows.Forms.MessageBox.Show(GP.GetMessages(ref sev));
            }
        }
        public static IFeatureClass CreateFeatureClass(IGPValue gpFeatureClass, IGPEnvironmentManager environment, IFields fields = null)
        {
            if (gpFeatureClass == null)
            {
                throw new ArgumentException("Argument can not be null", "gpFeatureClass");
            }
            if (environment == null)
            {
                throw new ArgumentException("Argument can not be null", "environment");
            }


            IGeoProcessorSettings gpSettings = (IGeoProcessorSettings)environment;

            if (gpUtilities.Exists(gpFeatureClass))
            {
                if (gpSettings.OverwriteOutput == true)
                {
                    gpUtilities.Delete(gpFeatureClass);
                }
                else
                {
                    throw new Exception("Output feature class already exists: " + gpFeatureClass.GetAsText());
                }
            }


            IDEFeatureClass deFeatureClass = (IDEFeatureClass)gpUtilities.DecodeDETable(gpFeatureClass);

            if (deFeatureClass == null)
            {
                throw new Exception("Data Element decode return null");
            }

            IObjectClassDescription objectClassDescription = (IObjectClassDescription) new FeatureClassDescriptionClass();
            UID clsid    = objectClassDescription.InstanceCLSID;
            UID extclsid = objectClassDescription.ClassExtensionCLSID;

            IDataElement dataElement = (IDataElement)deFeatureClass;

            if (dataElement.CatalogPath == null)
            {
                throw new ArgumentException("Catalog path is null", "CatalogPath");
            }
            IFeatureClassName featureClassName = (IFeatureClassName)gpUtilities.CreateFeatureClassName(dataElement.CatalogPath);

            string path = dataElement.GetPath();
            string name = dataElement.Name;

            IDEGeoDataset     geoDataElement   = (IDEGeoDataset)deFeatureClass;
            ISpatialReference spatialReference = geoDataElement.SpatialReference;

            IDETable deTable        = (IDETable)deFeatureClass;
            string   shapeFieldName = deFeatureClass.ShapeFieldName;

            Dictionary <string, IField> fieldBuilder = new Dictionary <string, IField>();

            foreach (var input_fields in new IFields[] { deTable.Fields, fields })
            {
                if (input_fields == null)
                {
                    continue;
                }
                for (int i = 0; i < input_fields.FieldCount; i++)
                {
                    IField field = deTable.Fields.get_Field(i);

                    if (fieldBuilder.ContainsKey(field.Name.ToLower()))
                    {
                        fieldBuilder[field.Name.ToLower()] = (IField)((IClone)field).Clone();
                    }
                    else
                    {
                        fieldBuilder.Add(field.Name.ToLower(), (IField)((IClone)field).Clone());
                    }

                    if (field.Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        shapeFieldName = field.Name;
                        break;
                    }
                }
            }

            IFields     output_fields = new FieldsClass();
            IFieldsEdit fields_edit   = (IFieldsEdit)output_fields;

            foreach (IField field in fieldBuilder.Values)
            {
                fields_edit.AddField(field);
                if (field.Type == esriFieldType.esriFieldTypeGeometry)
                {
                    IGeometryDefEdit defEdit = (IGeometryDefEdit)field.GeometryDef;
                    defEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                }
            }

            string configKeyword = ((IGPString)environment.FindEnvironment(CONFIG_KEYWORD_PROP_NAME).Value).Value;
            //if (String.IsNullOrWhiteSpace(configKeyword)) configKeyword = "DEFAULTS";

            IFeatureClass ret = null;

            if (featureClassName.FeatureDatasetName != null)
            {
                IFeatureDataset featureDataset = (IFeatureDataset)((IName)featureClassName.FeatureDatasetName).Open();
                try
                {
                    ret = featureDataset.CreateFeatureClass(name, output_fields, clsid, extclsid, esriFeatureType.esriFTSimple, shapeFieldName, configKeyword);
                }
                finally
                {
                    Marshal.ReleaseComObject(featureDataset);
                }
            }
            else
            {
                IWorkspace workspace = (IWorkspace)((IName)((IDatasetName)featureClassName).WorkspaceName).Open();
                try
                {
                    IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
                    ret = featureWorkspace.CreateFeatureClass(name, output_fields, clsid, extclsid, esriFeatureType.esriFTSimple, shapeFieldName, configKeyword);
                }
                finally
                {
                    Marshal.ReleaseComObject(workspace);
                }
            }
            return(ret);
        }
Exemplo n.º 6
0
 public static AoTable From(IDETable t)
 {
     return(new AoTable(t.Fields));
 }