GIS

(ArcObject-C#) SDE 공간정보 ShapeFile 컨버전, DBF(DBASE) 파일로 컨버전

조심이 2009. 10. 1. 11:53
반응형

/// <summary>
        /// SDE 데이터 ShapeFile로 컨버젼
        /// sourceWorkspace: sde
        /// targetWorkspace: 세입
        /// filename : 피쳐클래스이름
        /// </summary>
        /// <returns></returns>
        //shape 파일로 내리기

        public bool ConvertSDEtoShapefile(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string TargetName,string SourceName)
        {
            try
            {
               
                IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
                IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;


                IName sourceWorkspaceDatasetName = sourceWorkspaceDataset.FullName;
                IName targetWorkspaceDatasetName = targetWorkspaceDataset.FullName;

                IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDatasetName;
                IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDatasetName;


                IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
                IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
                sourceDatasetName.Name = SourceName;
                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 = TargetName;
                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;
                    while ((fieldError = enumFieldError.Next()) != null)
                    {
                        // Get the field the error occurred on.     
                        IField errorField = sourceFields.get_Field(fieldError.FieldIndex);
                        IField errorField2 = targetFields.get_Field(fieldError.FieldIndex);
                        MessageBox.Show(name + "=" + errorField.Name + " :   " + fieldError.FieldError + "원본:  " + errorField2.Name);

                    }
                    // Handle the errors in a way appropriate to your application.
                    //MessageBox.Show("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 = ""; //쿼리필터

                // 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.
                    MessageBox.Show("Errors occurred for the following feature:" + invalidObjectInfo.InvalidObjectID);
                }

              
                return true;

            }
            catch (Exception ex)
            {
                
                       return false;
            }
        }




   /// <summary>
        /// 속성 데이터(테이블) dbf File로 컨버젼
        /// sourceWorkspace: Sde
        /// targetworkspce: 쉐입
        /// filename: 테이블 이름
        /// </summary>
        /// <returns></returns>
        //dbf 파일로 내리기

        public bool ConvertSDEtoDbase(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string filename)
        {
            try
            {
             

                IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
                IName sourceWorkspaceDatasetName = sourceWorkspaceDataset.FullName;
                IWorkspaceName pInWorkspaceName = (IWorkspaceName)sourceWorkspaceDatasetName;
                pInWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.SDEWorkspaceFactory.1";

                IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
                IName targetWorkspaceDatasetName = targetWorkspaceDataset.FullName;
                IWorkspaceName pOutWorkspaceName = (IWorkspaceName)targetWorkspaceDatasetName;

 

                //Setup input dataset name
                IDatasetName pInDatasetName;
                pInDatasetName = (IDatasetName)new TableName();

                pInDatasetName.Name = filename;
                pInDatasetName.WorkspaceName = pInWorkspaceName;
                // Set output feature dataset name

 

                // Set output dataset name
                IDatasetName pOutDatasetName = null;
                pOutDatasetName = (IDatasetName)new TableName();

                pOutDatasetName.WorkspaceName = pOutWorkspaceName;
                pOutDatasetName.Name = filename;

                IName pName = (IName)pInDatasetName; ;
                ITable pInTable = (ITable)pName.Open();

                IFields pInFields = pInTable.Fields;

                IFieldChecker pFieldCheck = new FieldChecker();

                IFields pOutFields = null;
                IEnumFieldError pEnumFieldError = null;

                pFieldCheck.Validate(pInFields, out pEnumFieldError, out pOutFields);
                String strFldErr = "Some columns will be given new names as follows:";
                IFieldError pFieldError = null;

                if (pEnumFieldError != null)
                    pFieldError = pEnumFieldError.Next();

                IField pInField, pOutField;
                while (pFieldError != null)
                {
                    pInField = pInFields.get_Field(pFieldError.FieldIndex);
                    pOutField = pOutFields.get_Field(pFieldError.FieldIndex);
                    strFldErr = strFldErr + pOutField.Name + "reason: " + pInField.Name;
                    pFieldError = pEnumFieldError.Next();
                }


                IFeatureDataConverter pConverter = new FeatureDataConverter();
                IEnumInvalidObject pEnumErrors = null;

                pEnumErrors = pConverter.ConvertTable((IDatasetName)pInDatasetName, null,
                 (IDatasetName)pOutDatasetName, pOutFields, "", 1000, 0);


              
                return true;
           
            }catch (Exception ex)
            {
              
                             return false;
            }
         
        }

반응형