示例#1
0
        /// <summary>
        ///     Creates an <see cref="IEnumerable{T}" /> from an <see cref="IEnumDomain" />
        /// </summary>
        /// <param name="source">An <see cref="IEnumInvalidObject" /> to create an <see cref="IEnumerable{T}" /> from.</param>
        /// <returns>An <see cref="IEnumerable{T}" /> that contains the fields from the input source.</returns>
        public static IEnumerable <IInvalidObjectInfo> AsEnumerable(this IEnumInvalidObject source)
        {
            if (source != null)
            {
                source.Reset();
                var value = source.Next();
                while (value != null)
                {
                    yield return(value);

                    value = source.Next();
                }
            }
        }
示例#2
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);
                }
            }
        }
示例#3
0
        /// <summary>
        /// 拷贝源图层到空间数据集下的目标图层
        /// </summary>
        /// <param name="sourceWorkspace"></param>
        /// <param name="targetWorkspace"></param>
        /// <param name="nameOfSourceFeatureClass"></param>
        /// <param name="nameOfTargetFeatureClass"></param>
        public static void ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace,
                                               string nameOfSourceFeatureClass, string nameOfTargetFeatureClass)
        {
            try
            {
                //create source workspace name
                IDataset       sourceWorkspaceDataset = (IDataset)sourceWorkspace;
                IWorkspaceName sourceWorkspaceName    = (IWorkspaceName)sourceWorkspaceDataset.FullName;

                //create source dataset name
                IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();

                IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
                sourceDatasetName.WorkspaceName = sourceWorkspaceName;
                sourceDatasetName.Name          = nameOfSourceFeatureClass;
                //create target workspace name
                IDataset       targetWorkspaceDataset = (IDataset)targetWorkspace;
                IWorkspaceName targetWorkspaceName    = (IWorkspaceName)targetWorkspaceDataset.FullName;
                //create target dataset name
                IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
                IDatasetName      targetDatasetName      = (IDatasetName)targetFeatureClassName;
                targetDatasetName.WorkspaceName = targetWorkspaceName;
                targetDatasetName.Name          = nameOfTargetFeatureClass;
                //Open input Featureclass to get field definitions.
                IName         sourceName         = (IName)sourceFeatureClassName;
                IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();
                //Validate the field names because you are converting between different workspace types.
                IFieldChecker   fieldChecker = new FieldCheckerClass();
                IFields         targetFeatureClassFields;
                IFields         sourceFeatureClassFields = sourceFeatureClass.Fields;
                IEnumFieldError enumFieldError;
                // Most importantly set the input and validate workspaces!
                fieldChecker.InputWorkspace    = sourceWorkspace;
                fieldChecker.ValidateWorkspace = targetWorkspace;
                fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);
                // Loop through the output fields to find the geomerty field
                IField geometryField;
                for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
                {
                    geometryField = targetFeatureClassFields.get_Field(i);
                    if (geometryField.Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        // Get the geometry field's geometry defenition
                        IGeometryDef geometryDef = geometryField.GeometryDef;

                        //Give the geometry definition a spatial index grid count and grid size
                        IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;

                        //targetFCGeoDefEdit.GridCount_2 = 1;
                        //targetFCGeoDefEdit.set_GridSize(0, 1000);
                        //Allow ArcGIS to determine a valid grid size for the data loaded
                        targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
                        // we want to convert all of the features
                        IQueryFilter queryFilter = new QueryFilterClass();
                        queryFilter.WhereClause = "";

                        IEnumDataset    pEnumDataset  = targetWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
                        IFeatureDataset ipDataset     = (IFeatureDataset)pEnumDataset.Next();
                        IDatasetName    pTargetDsName = (IDatasetName)ipDataset.FullName;
                        // Load the feature class
                        IFeatureDataConverter fctofc = new FeatureDataConverterClass();

                        IEnumInvalidObject enumErrors =
                            fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter,
                                                       pTargetDsName as IFeatureDatasetName, targetFeatureClassName,
                                                       geometryDef, targetFeatureClassFields, "", 1000, 0);
                        IInvalidObjectInfo obj = enumErrors.Next();
                        break;
                    }
                }
            }
            catch (Exception exp)
            {
                Hy.Common.Utility.Log.OperationalLogManager.AppendMessage(exp.ToString());

                return;
            }
        }
示例#4
0
        /// <summary>
        /// 拷贝源图层到空间数据集下的目标图层
        /// </summary>
        /// <param name="sourceWorkspace">源Workspace</param>
        /// <param name="targetWorkspace">目标Workspace</param>
        /// <param name="sourceClassName">源图层</param>
        /// <param name="destClassName">待创建的图层名</param>
        public static bool ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace,
                                               string sourceClassName, string destClassName)
        {
            try
            {
                IDataset       sourceWorkspaceDataset = (IDataset)sourceWorkspace;
                IWorkspaceName sourceWorkspaceName    = (IWorkspaceName)sourceWorkspaceDataset.FullName;

                IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();

                IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
                sourceDatasetName.WorkspaceName = sourceWorkspaceName;
                sourceDatasetName.Name          = sourceClassName;

                IDataset       targetWorkspaceDataset = (IDataset)targetWorkspace;
                IWorkspaceName targetWorkspaceName    = (IWorkspaceName)targetWorkspaceDataset.FullName;

                IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
                IDatasetName      targetDatasetName      = (IDatasetName)targetFeatureClassName;
                targetDatasetName.WorkspaceName = targetWorkspaceName;
                targetDatasetName.Name          = destClassName;

                IName         sourceName         = (IName)sourceFeatureClassName;
                IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();

                IFields         targetFeatureClassFields;
                IFields         sourceFeatureClassFields = sourceFeatureClass.Fields;
                IEnumFieldError enumFieldError;

                IFieldChecker fieldChecker = new FieldCheckerClass();
                fieldChecker.InputWorkspace    = sourceWorkspace;
                fieldChecker.ValidateWorkspace = targetWorkspace;
                fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);

                IField geometryField = targetFeatureClassFields.get_Field(targetFeatureClassFields.FindField(sourceFeatureClass.ShapeFieldName));

                //for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
                //{
                //    geometryField = targetFeatureClassFields.get_Field(i);
                //    if (geometryField.Type == esriFieldType.esriFieldTypeGeometry)
                //    {
                IGeometryDef geometryDef = geometryField.GeometryDef;

                IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;

                targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
                IQueryFilter queryFilter = new QueryFilterClass();
                queryFilter.WhereClause = "";

                IEnumDataset pEnumDataset = targetWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);

                IFeatureDataConverter fConverter = new FeatureDataConverterClass();

                IEnumInvalidObject enumErrors =
                    fConverter.ConvertFeatureClass(sourceFeatureClassName, queryFilter,
                                                   null, targetFeatureClassName,
                                                   geometryDef, targetFeatureClassFields, "", 1000, 0);
                IInvalidObjectInfo obj = enumErrors.Next();
                //        break;
                //    }
                //}

                return(true);
            }
            catch (Exception exp)
            {
                return(false);
            }
        }
示例#5
0
        /// <summary>
        /// 将一个要素类从一个工作空间转移到另外一个工作空间
        /// 注意目标工作空间不能有改要素类,必须先清除
        /// </summary>
        /// <params name="sourceWorkspace">源工作空间</params>
        /// <params name="targetWorkspace">目标工作空间</params>
        /// <params name="nameOfSourceFeatureClass">源要素类名</params>
        /// <params name="nameOfTargetFeatureClass">目标要素类名</params>
        public bool IFeatureDataConverter_ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass)
        {
            bool change = false;
            //create source workspace name
            IDataset       sourceWorkspaceDataset = (IDataset)sourceWorkspace;
            IWorkspaceName sourceWorkspaceName    = (IWorkspaceName)sourceWorkspaceDataset.FullName;
            //create source dataset name
            IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
            IDatasetName      sourceDatasetName      = (IDatasetName)sourceFeatureClassName;

            sourceDatasetName.WorkspaceName = sourceWorkspaceName;
            sourceDatasetName.Name          = nameOfSourceFeatureClass;
            //create target workspace name
            IDataset       targetWorkspaceDataset = (IDataset)targetWorkspace;
            IWorkspaceName targetWorkspaceName    = (IWorkspaceName)targetWorkspaceDataset.FullName;
            //create target dataset name
            IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
            IDatasetName      targetDatasetName      = (IDatasetName)targetFeatureClassName;

            targetDatasetName.WorkspaceName = targetWorkspaceName;
            targetDatasetName.Name          = nameOfTargetFeatureClass;
            //Open input Featureclass to get field definitions.
            ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;
            IFeatureClass sourceFeatureClass        = (IFeatureClass)sourceName.Open();

            //Validate the field names because you are converting between different workspace types.
            IFieldChecker   fieldChecker = new FieldCheckerClass();
            IFields         targetFeatureClassFields;
            IFields         sourceFeatureClassFields = sourceFeatureClass.Fields;
            IEnumFieldError enumFieldError;

            // Most importantly set the input and validate workspaces!
            fieldChecker.InputWorkspace    = sourceWorkspace;
            fieldChecker.ValidateWorkspace = targetWorkspace;
            fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);
            // Loop through the output fields to find the geomerty field
            IField geometryField;

            for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
            {
                if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    geometryField = targetFeatureClassFields.get_Field(i);
                    // Get the geometry field's geometry defenition
                    IGeometryDef geometryDef = geometryField.GeometryDef;
                    //Give the geometry definition a spatial index grid count and grid size
                    IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
                    targetFCGeoDefEdit.GridCount_2 = 1;
                    targetFCGeoDefEdit.set_GridSize(0, 0);
                    //Allow ArcGIS to determine a valid grid size for the data loaded
                    targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
                    // we want to convert all of the features
                    IQueryFilter queryFilter = new QueryFilterClass();
                    queryFilter.WhereClause = "";
                    // Load the feature class
                    IFeatureDataConverter fctofc     = new FeatureDataConverterClass();
                    IEnumInvalidObject    enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0);
                    if (enumErrors.Next() == null)
                    {
                        change = true;
                    }
                    break;
                }
            }
            return(change);
        }
示例#6
0
        public void ConvertFileGDBToSDE(string fileGDBPath, string sourceFCName, string targetFCName)
        {
            // Create a name object for the source (fileGDB) workspace and open it.
            IWorkspaceName sourceWorkspaceName = getWorkspaceName("esriDataSourcesGDB.FileGDBWorkspaceFactory", fileGDBPath);

            IName      sourceWorkspaceIName = (IName)sourceWorkspaceName;
            IWorkspace sourceWorkspace      = (IWorkspace)sourceWorkspaceIName.Open();

            // Create a name object for the target (SDE) workspace and open it.
            IWorkspaceName targetWorkspaceName  = getWorkspaceName("esriDataSourcesGDB.SdeWorkspaceFactory", sdePath);
            IName          targetWorkspaceIName = (IName)targetWorkspaceName;
            IWorkspace     targetWorkspace      = (IWorkspace)targetWorkspaceIName.Open();

            // Create a name object for the source dataset.
            IFeatureClassName sourceFeatureClassName = getFeatureClassName(sourceWorkspaceName, sourceFCName);

            // Create a name object for the target dataset.
            IFeatureClassName targetFeatureClassName = getFeatureClassName(targetWorkspaceName, targetFCName);

            // 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)
            {
                // Handle the errors in a way appropriate to your application.
                Console.WriteLine("Errors were encountered during field validation.");
            }

            // 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;

            // Cast the IGeometryDef to the IGeometryDefEdit interface.
            IGeometryDefEdit targetGeometryDefEdit = (IGeometryDefEdit)targetGeometryDef;

            // Set the IGeometryDefEdit properties.
            //targetGeometryDefEdit.GridCount_2 = 1;
            //targetGeometryDefEdit.set_GridSize(0, 0.75);

            // Create a query filter to only select features you want to convert
            IQueryFilter queryFilter = new QueryFilterClass();

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

            // Check for errors.
            IInvalidObjectInfo invalidObjectInfo = null;

            enumInvalidObject.Reset();
            while ((invalidObjectInfo = enumInvalidObject.Next()) != null)
            {
                // Handle the errors in a way appropriate to the application.
                Console.WriteLine("Errors occurred for the following feature: {0}",
                                  invalidObjectInfo.InvalidObjectID);
            }
        }
示例#7
0
        /// <summary>
        /// 将指定要素类复制到目标数据库中(在目标数据库中创建要素类)
        /// </summary>
        /// <param name="pSrcFC">源要素类</param>
        /// <param name="pQueryFilter">查询条件</param>
        /// <param name="pTargetWks">目标数据库</param>
        /// <param name="sMessage">转换消息</param>
        /// <param name="sTargetName">目标要素类名</param>
        /// <param name="sAlias">目标要素类别名</param>
        /// <param name="sFDatasetName">目标要素数据集</param>
        /// <returns></returns>
        public bool TranslateFeatureClass(IFeatureClass pSrcFC, IQueryFilter pQueryFilter, IWorkspace pTargetWks, string sTargetName = null, string sAlias = null, string sFDatasetName = null)
        {
            GeoDataUtils geoDataUtils = new GeoDataUtils();

            IDataset            pSrcDataset         = pSrcFC as IDataset;
            IFeatureClassName   pSrcName            = pSrcDataset.FullName as IFeatureClassName;
            IFeatureDatasetName pFeatureDatasetName = null;

            if (pTargetWks.Type != esriWorkspaceType.esriFileSystemWorkspace)
            {
                string sFeatureDatasetName = sFDatasetName;
                if (string.IsNullOrEmpty(sFeatureDatasetName) && pSrcFC.FeatureDataset != null)
                {
                    sFeatureDatasetName = geoDataUtils.ExtractDatasetName(pSrcFC.FeatureDataset.Name);
                }
                if (!string.IsNullOrEmpty(sFeatureDatasetName))
                {
                    if (geoDataUtils.IsNameExist(pTargetWks, sFeatureDatasetName, esriDatasetType.esriDTFeatureDataset))
                    {
                        IFeatureDataset pTargetFeatureDataset = (pTargetWks as IFeatureWorkspace).OpenFeatureDataset(sFeatureDatasetName);
                        pFeatureDatasetName = pTargetFeatureDataset.FullName as IFeatureDatasetName;
                        Marshal.ReleaseComObject(pTargetFeatureDataset);
                    }
                    else
                    {
                        pFeatureDatasetName = new FeatureDatasetName() as IFeatureDatasetName;
                        IDatasetName pDatasetName = pFeatureDatasetName as IDatasetName;
                        pDatasetName.Name          = sFeatureDatasetName;
                        pDatasetName.WorkspaceName = (pTargetWks as IDataset).FullName as IWorkspaceName;
                    }
                }
            }

            //目标要素类名
            FeatureClassName pTargetName        = new FeatureClassName();
            IDatasetName     pTargetDatasetName = pTargetName as IDatasetName;

            if (string.IsNullOrEmpty(sTargetName))
            {
                sTargetName = geoDataUtils.ExtractDatasetName(pSrcDataset.Name);
            }
            pTargetDatasetName.Name          = sTargetName;
            pTargetDatasetName.WorkspaceName = (pTargetWks as IDataset).FullName as IWorkspaceName;
            geoDataUtils.DeleteDataset(pTargetWks, pTargetDatasetName);

            // 验证字段
            IFields pTargetFields;
            string  sShapeFieldName;

            geoDataUtils.CheckFields(pSrcFC, pTargetWks, out pTargetFields, out sShapeFieldName);
            IEnumInvalidObject    pEnumInvalidObject = null;
            IFeatureDataConverter pConverter         = null;

            try
            {
                int          iIndex             = pTargetFields.FindField(sShapeFieldName);
                IGeometryDef pTargetGeometryDef = pTargetFields.get_Field(iIndex).GeometryDef;

                // 执行批量转换
                pConverter         = new FeatureDataConverter();
                pEnumInvalidObject = pConverter.ConvertFeatureClass(pSrcName, pQueryFilter, pFeatureDatasetName as IFeatureDatasetName,
                                                                    pTargetName as IFeatureClassName, pTargetGeometryDef, pTargetFields, "", 1000, 0);

                // 检查转换过程中是否产生错误
                if (pEnumInvalidObject != null)
                {
                    IInvalidObjectInfo pInvalidInfo = null;
                    pEnumInvalidObject.Reset();
                    while ((pInvalidInfo = pEnumInvalidObject.Next()) != null)
                    {
                        ErrorMessage += pSrcFC.AliasName + "图层要素:" + pInvalidInfo.InvalidObjectID + "执行失败!";
                    }
                }
            }
            finally //释放资源
            {
                if (pEnumInvalidObject != null)
                {
                    Marshal.ReleaseComObject(pEnumInvalidObject);
                }
                if (pConverter != null)
                {
                    Marshal.ReleaseComObject(pConverter);
                }
            }
            //修改要素类别名
            if (string.IsNullOrEmpty(sAlias))
            {
                sAlias = pSrcFC.AliasName;
            }
            IFeatureClass pTargetFC = (pTargetWks as IFeatureWorkspace).OpenFeatureClass(sTargetName);

            geoDataUtils.AlterDatasetAlias(pTargetFC, sAlias);
            Marshal.ReleaseComObject(pTargetFC);
            return(true);
        }
        public void DataImport(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, IFeatureClass sourceFeatureClass, IDataset targetFeatureDataset, string storeName)
        {
            int errorRowCount = 0;
            int totalRowCount = 0;
            Dictionary <int, string> dicError         = new Dictionary <int, string>();
            IDataset       sourceWorkspaceDataset     = (IDataset)sourceWorkspace;
            IDataset       targetWorkspaceDataset     = (IDataset)targetWorkspace;
            string         sourceLayerName            = ((IDataset)sourceFeatureClass).Name;
            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          = sourceLayerName;
            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          = storeName;// importMappingScheme.StoreDatasetName;"GEODATA.DLTB7"
            targetDatasetName.WorkspaceName = targetWorkspaceName;

            // 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);

            // 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;

            // Cast the IGeometryDef to the IGeometryDefEdit interface.
            IGeometryDefEdit targetGeometryDefEdit = (IGeometryDefEdit)targetGeometryDef;

            // Set the IGeometryDefEdit properties.
            targetGeometryDefEdit.GridCount_2 = 1;
            targetGeometryDefEdit.set_GridSize(0, 0.75);

            //修改目标字段
            string fieldNameString = ChangeTargetFields(targetFields);

            if (fieldNameString != string.Empty)
            {
                fieldNameString += string.Format(",{0}", shapeFieldName);
            }
            else
            {
                fieldNameString = shapeFieldName;
            }
            IQueryFilter queryFilter = new QueryFilterClass();

            queryFilter.SubFields = fieldNameString;

            //设置导出的要素集
            IFeatureDatasetName featureDatasetName = null;

            if (targetFeatureDataset != null)
            {
                featureDatasetName = (IFeatureDatasetName)targetFeatureDataset.FullName;
            }

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

            // Check for errors.
            IInvalidObjectInfo invalidObjectInfo = null;

            enumInvalidObject.Reset();
            while ((invalidObjectInfo = enumInvalidObject.Next()) != null)
            {
                if (dicError.ContainsKey(invalidObjectInfo.InvalidObjectID) == false)
                {
                    // Handle the errors in a way appropriate to the application.
                    dicError.Add(invalidObjectInfo.InvalidObjectID, invalidObjectInfo.ErrorDescription);
                    errorRowCount++;
                }
                Console.WriteLine(invalidObjectInfo.InvalidObjectID + "" + invalidObjectInfo.ErrorDescription);
            }
        }
示例#9
0
        public static void ConvertFeatureClassToShapefile(string sourceWorkspacePath, string targetWorkspacePath, string sourceDataName, string targetDataName)
        {
            // Open the source and target workspaces.
            //String sourceWorkspacePath = @"C:\arcgis\DeveloperKit\SamplesNET\data\Atlanta.gdb";
            //String targetWorkspacePath = @"C:\Temp";
            IWorkspaceFactory sourceWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
            IWorkspaceFactory targetWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspace        sourceWorkspace        = sourceWorkspaceFactory.OpenFromFile
                                                           (sourceWorkspacePath, 0);
            IWorkspace 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          = sourceDataName;//"streets";
            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          = targetDataName;// "AtlantaStreets";
            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)
            {
                // Handle the errors in a way appropriate to your application.
                Console.WriteLine("Errors were encountered during field validation.");
            }

            // 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 a query filter to remove ramps, interstates and highways.
            IQueryFilter queryFilter = new QueryFilterClass();

            //queryFilter.WhereClause = "NAME <> 'Ramp' AND PRE_TYPE NOT IN ('I', 'Hwy')";
            queryFilter.WhereClause = "1=1";
            // Create the converter and run the conversion.
            IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
            IEnumInvalidObject    enumInvalidObject    =
                featureDataConverter.ConvertFeatureClass(sourceFeatureClassName,
                                                         queryFilter, null, targetFeatureClassName, targetGeometryDef, targetFields,
                                                         "", 1000, 0);

            // Check for errors.
            IInvalidObjectInfo invalidObjectInfo = null;

            enumInvalidObject.Reset();
            while ((invalidObjectInfo = enumInvalidObject.Next()) != null)
            {
                // Handle the errors in a way appropriate to the application.
                Console.WriteLine("Errors occurred for the following feature: {0}",
                                  invalidObjectInfo.InvalidObjectID);
            }
        }
示例#10
0
        //public static ICursor UncheckGridCode(ITable pTable,string pVersionName)
        //{
        //    //根据表格数据获取grid code:
        //    IQueryFilter filter = new QueryFilterClass();
        //    filter.WhereClause = "TaskName = '" + pVersionName + "'";
        //    ICursor cursor = pTable.Search(filter, true);
        //    return cursor;
        //}

        //public static void Check(IFeature pFeature, IWorkspace pWorkspace,string pVersionName)
        //{
        //    //更新检查状态:
        //    int ii = 1;
        //    int fieldindex = pFeature.Fields.FindField("passed");
        //    if (pFeature.get_Value(pFeature.Fields.FindField("VersionName")).ToString() == pVersionName)
        //    {
        //        pFeature.set_Value(fieldindex, ii);
        //        pFeature.Store();
        //    }

        //}

        //public static void Check(IFeature pFeature, IWorkspace pWorkspace)
        //{
        //    //更新检查状态:
        //    int ii = 1;
        //    int fieldindex = pFeature.Fields.FindFieldByAliasName("Pass");

        //        pFeature.set_Value(fieldindex, ii);
        //        pFeature.Store();

        //}

        public static bool ExportSHP(string pDestPath, IWorkspace pWorkspace)
        {
            IWorkspace    sourceWorkspace    = pWorkspace;
            IFeatureClass sourceFeatureClass = DBOperator.getFeatureClass(pWorkspace, "CheckArea");
            string        shpFolderPath      = pDestPath.Substring(0, pDestPath.LastIndexOf("\\"));
            string        shpFileName        = pDestPath.Substring(pDestPath.LastIndexOf("\\") + 1);

            shpFileName = shpFileName.Substring(0, shpFileName.Length - 4);

            IWorkspace targetWorkspace = DBOperator.openShapeFileWorkspace(shpFolderPath);

            if (File.Exists(pDestPath) == true)
            {
                DBOperator.DeleteFeatureClass(targetWorkspace, shpFileName);
            }

            IDataset       sourcedataset       = sourceWorkspace as IDataset;
            IWorkspaceName sourceWorkspaceName = sourcedataset.FullName as IWorkspaceName;
            IDataset       targetdataset       = targetWorkspace as IDataset;
            IWorkspaceName targetWorkspaceName = targetdataset.FullName as IWorkspaceName;

            sourcedataset = sourceFeatureClass as IDataset;
            IFeatureClassName sourceFeatureClassName = sourcedataset.FullName as IFeatureClassName;

            // Create a name object for the target dataset.
            IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
            IDatasetName      targetDatasetName      = (IDatasetName)targetFeatureClassName;

            targetDatasetName.Name          = shpFileName;
            targetDatasetName.WorkspaceName = targetWorkspaceName;

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

            // Validate the fields and check for errors.
            fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields);
            if (enumFieldError != null)
            {
                IFieldError error = enumFieldError.Next();
                while (error != null)
                {
                    // Handle the errors in a way appropriate to your application.
                    Console.WriteLine("Errors were encountered during field validation.");
                    error = enumFieldError.Next();
                }
            }

            // 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;

            // Cast the IGeometryDef to the IGeometryDefEdit interface.
            IGeometryDefEdit targetGeometryDefEdit = (IGeometryDefEdit)targetGeometryDef;

            // Set the IGeometryDefEdit properties.
            targetGeometryDefEdit.GridCount_2 = 1;
            targetGeometryDefEdit.set_GridSize(0, 0.75);

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

            // Create the converter and run the conversion.
            IFeatureDataConverter2 featureDataConverter = new FeatureDataConverterClass();

            IDatasetName       sourceFClassName  = sourceFeatureClassName as IDatasetName;
            IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass
                                                       (sourceFClassName, null, null, null, targetFeatureClassName,
                                                       targetGeometryDef, targetFields, "", 1000, 0);
            // Check for errors.
            IInvalidObjectInfo invalidObjectInfo = null;

            enumInvalidObject.Reset();
            while ((invalidObjectInfo = enumInvalidObject.Next()) != null)
            {
                // Handle the errors in a way appropriate to the application.
                Console.WriteLine("Errors occurred for the following feature: {0}",
                                  invalidObjectInfo.InvalidObjectID);
                return(false);
            }
            return(true);
        }