/// <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);
        }
예제 #2
0
        public static void SpatialJoin(object targetFC, object joinFC, string outputFcPath, JoinType joinType, string matchOption, IGPFieldMapping fieldMapping = null)
        {
            var geoprocessor = new Geoprocessor()
            {
                OverwriteOutput = true,
                AddOutputsToMap = false
            };

            try
            {
                Delete(outputFcPath);

                SpatialJoin spatialJoin = new SpatialJoin
                {
                    join_features     = joinFC,
                    target_features   = targetFC,
                    join_type         = joinType.ToString(),
                    match_option      = matchOption,
                    out_feature_class = outputFcPath,
                    field_mapping     = fieldMapping
                };

                var result = geoprocessor.Execute(spatialJoin, null);
            }
            catch (Exception)
            {
                if (geoprocessor.MessageCount > 0)
                {
                    string excMessage = null;
                    for (int i = 0; i < geoprocessor.MessageCount; i++)
                    {
                        excMessage += geoprocessor.GetMessage(i) + Environment.NewLine;
                    }

                    throw new Exception(excMessage);
                }
            }
        }