Esempio n. 1
0
        //格式转换后,检验字段的有效性并修复字段 YH 15/4/09
        public static bool FieldsCheckAfterTransaction(IWorkspace pInputWorkspace, IWorkspace pOutPutWorkspace, IFields pInputFields, ref IFields pOoutPutFields)
        {
            bool            functionReturnValue = false;
            IFieldChecker   pFieldChecker       = default(IFieldChecker);
            IEnumFieldError pEnumFieldError     = null;

            pFieldChecker = new FieldChecker();
            pFieldChecker.InputWorkspace    = pInputWorkspace;
            pFieldChecker.ValidateWorkspace = pOutPutWorkspace;
            pFieldChecker.Validate(pInputFields, out pEnumFieldError, out pOoutPutFields);

            //处理字段中不符合语义规则的情况
            if ((pEnumFieldError != null))
            {
                pEnumFieldError.Reset();
                IFieldError pFieldError = default(IFieldError);
                pFieldError = pEnumFieldError.Next();
                //g_clsErrorHandle.DisplayInformation("导出字段中存在无效字段: " + pOoutPutFields.Field(pFieldError.FieldIndex).Name + "导出终止!!", false);
                MessageBoxEx.Show("导出字段中存在无效字段: " + pOoutPutFields.get_Field(pFieldError.FieldIndex).Name + "导出终止!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(false);

                return(functionReturnValue);
            }
            return(true);

            return(functionReturnValue);
        }
Esempio n. 2
0
        /// <summary>
        /// Validates a field name
        /// </summary>
        /// <param name="name">The field name to validate</param>
        /// <param name="message">If field name fails then this variable will contain a description</param>
        /// <returns>True if passed. False if failed.</returns>
        public bool ValidateFieldName(string name, out string message)
        {
            // Validate Input Arguments
            if (string.IsNullOrEmpty(name))
            {
                throw new NullReferenceException("<name> argument cannot be null");
            }

            // Create Field Checker
            IFieldChecker fieldChecker = new FieldCheckerClass();

            fieldChecker.ValidateWorkspace = this._workspace;

            // Create Field
            IFieldEdit fieldEdit = new FieldClass();

            fieldEdit.Name_2 = name;
            fieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;

            // Create Fields
            IFieldsEdit fieldsEdit = new FieldsClass();

            fieldsEdit.AddField((IField)fieldEdit);

            // Create Field Error Enumerator
            IEnumFieldError enumFieldError = null;
            IFields         fieldsFixed    = null;

            // Validate Field Name
            fieldChecker.ValidateField(0, (IFields)fieldsEdit, out enumFieldError, out fieldsFixed);

            // Get Validated Name (if any)
            string newName = null;

            if (fieldsFixed != null)
            {
                IField fieldFixed = fieldsFixed.get_Field(0);
                newName = fieldFixed.Name;
            }

            // Get Errors (if any)
            if (enumFieldError != null)
            {
                enumFieldError.Reset();
                IFieldError fieldError = enumFieldError.Next();
                if (fieldError != null)
                {
                    switch (fieldError.FieldError)
                    {
                    case esriFieldNameErrorType.esriDuplicatedFieldName:
                        message = "is duplicated";
                        return(false);

                    case esriFieldNameErrorType.esriInvalidCharacter:
                        message = "contains one or more invalid characters";
                        return(false);

                    case esriFieldNameErrorType.esriInvalidFieldNameLength:
                        message = "is too long";
                        return(false);

                    case esriFieldNameErrorType.esriNoFieldError:
                        message = "has no field error";
                        return(false);

                    case esriFieldNameErrorType.esriSQLReservedWord:
                        message = "contains one or more reserved word";
                        return(false);

                    default:
                        message = "contains an unknown error";
                        return(false);
                    }
                }
            }

            // The FieldChecker may have renamed the field without returning an error.
            if (newName != null)
            {
                if (newName != name)
                {
                    message = "has an invalid field name";
                    return(false);
                }
            }

            // No Errors
            message = null;
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// convert featureclass in shapefile
        /// </summary>
        /// <param name="sourceWorkspace">oggetto workspace</param>
        /// <param name="outputName">nome feature class e nome shapefile</param>
        /// <param name="targetWorkspacePath">cartella shapefile</param>
        /// <param name="errorField">lista degli eventuali errori nella creazione dei campi</param>
        /// <param name="invalidObject">lista degli eventuale errori di creazione record</param>
        private void ConvertFeatureClassToShapefile(IWorkspace sourceWorkspace, string outputName, string targetWorkspacePath, ref List <string> errorField, ref List <string> invalidObject)
        {
            IWorkspace targetWorkspace = null;

            try
            {
                Type factoryType = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
                IWorkspaceFactory targetWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
                targetWorkspace = targetWorkspaceFactory.OpenFromFile(targetWorkspacePath, 0);

                // Cast the workspaces to the IDataset interface and get name objects.
                IDataset       sourceWorkspaceDataset     = (IDataset)sourceWorkspace;
                IDataset       targetWorkspaceDataset     = (IDataset)targetWorkspace;
                IName          sourceWorkspaceDatasetName = sourceWorkspaceDataset.FullName;
                IName          targetWorkspaceDatasetName = targetWorkspaceDataset.FullName;
                IWorkspaceName sourceWorkspaceName        = (IWorkspaceName)sourceWorkspaceDatasetName;
                IWorkspaceName targetWorkspaceName        = (IWorkspaceName)targetWorkspaceDatasetName;

                // Create a name object for the shapefile and cast it to the IDatasetName interface.
                IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
                IDatasetName      sourceDatasetName      = (IDatasetName)sourceFeatureClassName;
                sourceDatasetName.Name          = outputName;
                sourceDatasetName.WorkspaceName = sourceWorkspaceName;

                // Create a name object for the FGDB feature class and cast it to the IDatasetName interface.
                IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
                IDatasetName      targetDatasetName      = (IDatasetName)targetFeatureClassName;
                targetDatasetName.Name          = outputName;
                targetDatasetName.WorkspaceName = targetWorkspaceName;

                // Open source feature class to get field definitions.
                IName         sourceName         = (IName)sourceFeatureClassName;
                IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();

                // Create the objects and references necessary for field validation.
                IFieldChecker   fieldChecker   = new FieldCheckerClass();
                IFields         sourceFields   = sourceFeatureClass.Fields;
                IFields         targetFields   = null;
                IEnumFieldError enumFieldError = null;

                // Set the required properties for the IFieldChecker interface.
                fieldChecker.InputWorkspace    = sourceWorkspace;
                fieldChecker.ValidateWorkspace = targetWorkspace;

                // Validate the fields and check for errors.
                fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields);
                if (enumFieldError != null)
                {
                    IFieldError fieldError = null;
                    enumFieldError.Reset();
                    while ((fieldError = enumFieldError.Next()) != null)
                    {
                        errorField.Add($"Errore: {Enum.GetName(typeof(esriFieldNameErrorType), fieldError.FieldError)} - Campo: {targetFields.get_Field(fieldError.FieldIndex).Name}");
                    }
                }

                // Find the shape field.
                string shapeFieldName  = sourceFeatureClass.ShapeFieldName;
                int    shapeFieldIndex = sourceFeatureClass.FindField(shapeFieldName);
                IField shapeField      = sourceFields.get_Field(shapeFieldIndex);

                // Get the geometry definition from the shape field and clone it.
                IGeometryDef geometryDef            = shapeField.GeometryDef;
                IClone       geometryDefClone       = (IClone)geometryDef;
                IClone       targetGeometryDefClone = geometryDefClone.Clone();
                IGeometryDef targetGeometryDef      = (IGeometryDef)targetGeometryDefClone;

                // Create the converter and run the conversion.
                IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
                IEnumInvalidObject    enumInvalidObject    =
                    featureDataConverter.ConvertFeatureClass(sourceFeatureClassName,
                                                             null, null, targetFeatureClassName, targetGeometryDef, targetFields,
                                                             string.Empty, 1000, 0);

                // Check for errors.
                IInvalidObjectInfo invalidObjectInfo = null;
                enumInvalidObject.Reset();
                while ((invalidObjectInfo = enumInvalidObject.Next()) != null)
                {
                    invalidObject.Add($"{invalidObjectInfo.InvalidObjectID}");
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                if (targetWorkspace != null)
                {
                    Marshal.FinalReleaseComObject(targetWorkspace);
                }
            }
        }
Esempio n. 4
0
        //不剪裁输出
        private void notcutExport(IFeatureClass pFromFeaCls, IFeatureClass pToFeatureClass, DateTime dt, string strUser, ref int intSucCount, ref string strError)
        {
            try
            {
                strError    = "";
                intSucCount = 0;
                int            intCount       = pFromFeaCls.FeatureCount(null);
                IFeatureCursor pCursor        = pFromFeaCls.Search(null, false);
                IFeature       pFeature       = pCursor.NextFeature();
                IFeatureCursor pFeatureCursor = pToFeatureClass.Insert(true);
                IFeatureBuffer pFeatureBuffer = pToFeatureClass.CreateFeatureBuffer();

                OnInitProBarChangHandler(intCount);
                OnLblChange("正在复制:" + pFromFeaCls.AliasName);

                int iCount = 0;
                while (pFeature != null)
                {
                    for (int i = 0; i < pFeature.Fields.FieldCount; i++)
                    {
                        string sFieldName = pFeature.Fields.get_Field(i).Name;

                        int iIndex = pFeatureBuffer.Fields.FindField(sFieldName);

                        if ((iIndex > -1) && (pFeatureBuffer.Fields.get_Field(iIndex).Editable == true))
                        {
                            pFeatureBuffer.set_Value(iIndex, pFeature.get_Value(i));
                        }
                    }
                    if (pEnumFieldError != null)//yjl20110804 add
                    {
                        pEnumFieldError.Reset();
                        IFieldError pFieldError = pEnumFieldError.Next();
                        while (pFieldError != null)
                        {
                            int srcIx = pFieldError.FieldIndex;                                             //错误字段的源索引
                            int desIx = pFeatureBuffer.Fields.FindField(pFixedField.get_Field(srcIx).Name); //错误字段的新索引
                            if (desIx == -1)
                            {
                                pFieldError = pEnumFieldError.Next();
                                continue;
                            }
                            IField pFld = pFeatureBuffer.Fields.get_Field(desIx);
                            if ((desIx > -1) && (pFld.Editable == true) && pFld.Type != esriFieldType.esriFieldTypeGeometry)
                            {
                                pFeatureBuffer.set_Value(desIx, pFeature.get_Value(srcIx));
                            }
                            pFieldError = pEnumFieldError.Next();
                        }
                    }
                    ////插入日期和入库人
                    //int intDateIndex = pFeatureBuffer.Fields.FindField("ImportTime");
                    //if (intDateIndex > -1) pFeatureBuffer.set_Value(intDateIndex, dt);
                    //int intUserIndex = pFeatureBuffer.Fields.FindField("ImportUser");
                    //if (intUserIndex > -1) pFeatureBuffer.set_Value(intUserIndex, strUser);

                    pFeatureBuffer.Shape = pFeature.ShapeCopy;
                    if (!InsertFea(ref pFeatureCursor, ref pFeatureBuffer))
                    {
                        strError = strError + Environment.NewLine + pFeature.OID + "  无法复制";

                        pFeatureCursor.Flush();
                        iCount = 0;
                        OnGoStepChangHandler();
                        pFeature = pCursor.NextFeature();
                        continue;
                    }

                    iCount++;
                    if (iCount == 2000)
                    {
                        pFeatureCursor.Flush();
                        iCount = 0;

                        Application.DoEvents();
                    }

                    //pbCopyRows.PerformStep();
                    OnGoStepChangHandler();
                    intSucCount++;

                    pFeature = pCursor.NextFeature();
                }
                if (iCount > 0)
                {
                    pFeatureCursor.Flush();
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
                pFeatureCursor = null;

                System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);
                pCursor        = null;
                pFeatureBuffer = null;
            }
            catch (Exception ex)
            {
                strError = strError + Environment.NewLine + ex.Message;
            }
        }
Esempio n. 5
0
        //复制要素
        public static void CopyFeatureAndTran(IFeatureCursor pCursor, IFeatureClass pToFeatureClass, ITransformation inTransformation)
        {
            IFeature       pFeature       = pCursor.NextFeature();
            IFeatureCursor pFeatureCursor = pToFeatureClass.Insert(true);
            int            iCount         = 0;

            while (pFeature != null)
            {
                IFeatureBuffer pFeatureBuffer = pToFeatureClass.CreateFeatureBuffer();

                for (int i = 0; i < pFeature.Fields.FieldCount; i++)
                {
                    string sFieldName = pFeature.Fields.get_Field(i).Name;

                    int iIndex = pFeatureBuffer.Fields.FindField(sFieldName);

                    if ((iIndex > -1) && (pFeatureBuffer.Fields.get_Field(iIndex).Editable == true))
                    {
                        pFeatureBuffer.set_Value(iIndex, pFeature.get_Value(i));
                    }
                }
                if (pEnumFieldError != null)//yjl20110804 add
                {
                    pEnumFieldError.Reset();
                    IFieldError pFieldError = pEnumFieldError.Next();
                    while (pFieldError != null)
                    {
                        int srcIx = pFieldError.FieldIndex;                                             //错误字段的源索引
                        int desIx = pFeatureBuffer.Fields.FindField(pFixedField.get_Field(srcIx).Name); //错误字段的新索引
                        if (desIx == -1)
                        {
                            pFieldError = pEnumFieldError.Next();
                            continue;
                        }
                        IField pFld = pFeatureBuffer.Fields.get_Field(desIx);
                        if ((desIx > -1) && (pFld.Editable == true) && pFld.Type != esriFieldType.esriFieldTypeGeometry)
                        {
                            pFeatureBuffer.set_Value(desIx, pFeature.get_Value(srcIx));
                        }
                        pFieldError = pEnumFieldError.Next();
                    }
                }
                IGeometry    shpTransformed = pFeature.ShapeCopy;
                ITransform2D pTransform2D   = shpTransformed as ITransform2D;
                pTransform2D.Transform(esriTransformDirection.esriTransformForward, inTransformation);
                pFeatureBuffer.Shape = shpTransformed;
                pFeatureCursor.InsertFeature(pFeatureBuffer);
                if (iCount == 500)
                {
                    pFeatureCursor.Flush();
                    iCount = 0;
                }
                iCount++;
                pFeature = pCursor.NextFeature();
            }
            if (iCount > 0)
            {
                pFeatureCursor.Flush();
            }

            System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
            pFeatureCursor = null;
        }