public static void createSpatialIndex(IFeatureClass fc, double gridOneSize = 0.0, double gridTwoSize = 0.0, double gridThreeSize = 0.0)
        {
            String shapeFieldName = fc.ShapeFieldName;

            // Clone the shape field from the feature class.
            int     shapeFieldIndex  = fc.FindField(shapeFieldName);
            IFields fields           = fc.Fields;
            IField  sourceField      = fields.get_Field(shapeFieldIndex);
            IClone  sourceFieldClone = (IClone)sourceField;
            IClone  targetFieldClone = sourceFieldClone.Clone();
            IField  targetField      = (IField)targetFieldClone;

            // Open the geometry definition from the cloned field and modify it.
            IGeometryDef     geometryDef     = targetField.GeometryDef;
            IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;

            if (gridTwoSize > 0.0)
            {
                geometryDefEdit.GridCount_2 = 1;
                geometryDefEdit.set_GridSize(0, gridOneSize);
            }
            if (gridTwoSize > 0.0)
            {
                geometryDefEdit.GridCount_2 = 2;
                geometryDefEdit.set_GridSize(1, gridTwoSize);
            }
            if (gridThreeSize > 0.0)
            {
                geometryDefEdit.GridCount_2 = 3;
                geometryDefEdit.set_GridSize(2, gridThreeSize);
            }

            // Create a spatial index and set the required attributes.
            IIndex     newIndex     = new IndexClass();
            IIndexEdit newIndexEdit = (IIndexEdit)newIndex;

            newIndexEdit.Name_2 = shapeFieldName + "_Indx";

            // Create a fields collection and assign it to the new index.
            IFields     newIndexFields     = new FieldsClass();
            IFieldsEdit newIndexFieldsEdit = (IFieldsEdit)newIndexFields;

            newIndexFieldsEdit.AddField(targetField);
            newIndexEdit.Fields_2 = newIndexFields;

            // Add the spatial index back into the feature class.
            fc.AddIndex(newIndex);
        }
Example #2
0
        public bool CopyRaster(IRasterWorkspace2 pRW, string pFileName, IRasterWorkspaceEx pWorkSpace, string pDestName)
        {
            try
            {
                IRasterDataset pRds = pRW.OpenRasterDataset(pFileName);


                IRasterProps      pRasterProps      = (IRasterProps)pRds.CreateDefaultRaster();
                IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass();

                IRasterDef pRasterDef = new RasterDefClass();
                pRasterDef.SpatialReference = pRasterProps.SpatialReference;
                IGeometryDef     pGeoDef     = new GeometryDefClass();
                IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit;
                pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                pGeoDefEdit.AvgNumPoints_2 = 4;
                pGeoDefEdit.GridCount_2    = 1;
                pGeoDefEdit.set_GridSize(0, 1000);
                pGeoDefEdit.SpatialReference_2 = pRasterProps.SpatialReference;
                IRasterDataset pRasterDataset = pWorkSpace.SaveAsRasterDataset(pDestName, pRds.CreateDefaultRaster(), pRasterStorageDef, "", pRasterDef, pGeoDef);

                return(true);
            }
            catch (System.Exception ex)
            {
                return(false);
            }
        }
Example #3
0
        /// <summary>
        /// 导出要素类到目标工作空间
        /// </summary>
        /// <param name="featureClass"></param>
        /// <param name="workspace"></param>
        /// <param name="queryGeometry"></param>
        /// <param name="spatialRelEnum"></param>
        /// <param name="whereClause"></param>
        /// <returns></returns>
        public static Boolean ExportToWorkspace(IFeatureClass featureClass, IWorkspace workspace, IGeometry queryGeometry = null, esriSpatialRelEnum spatialRelEnum = esriSpatialRelEnum.esriSpatialRelIntersects, string whereClause = "")
        {
            IDataset          inDataSet        = featureClass as IDataset;
            IFeatureClassName inFCName         = inDataSet.FullName as IFeatureClassName;
            IWorkspace        inWorkspace      = inDataSet.Workspace;
            IDataset          outDataSet       = workspace as IDataset;
            IWorkspaceName    outWorkspaceName = outDataSet.FullName as IWorkspaceName;
            IFeatureClassName outFCName        = new FeatureClassNameClass();
            IDatasetName      dataSetName      = outFCName as IDatasetName;

            dataSetName.WorkspaceName = outWorkspaceName;
            dataSetName.Name          = inDataSet.Name;

            IFieldChecker fieldChecker = new FieldCheckerClass
            {
                InputWorkspace    = inWorkspace,
                ValidateWorkspace = workspace
            };
            IFields fields = featureClass.Fields;

            fieldChecker.Validate(fields, out IEnumFieldError enumFieldError, out IFields outFields);
            IFeatureDataConverter featureDataConverter = null;

            IField geometryField;

            try
            {
                IGeometryDef   geometryDef = null;
                ISpatialFilter pSF         = new SpatialFilterClass();

                for (int i = 0; i < outFields.FieldCount; i++)
                {
                    if (outFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        geometryField = outFields.get_Field(i);
                        geometryDef   = geometryField.GeometryDef;
                        IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
                        targetFCGeoDefEdit.GridCount_2 = 1;
                        targetFCGeoDefEdit.set_GridSize(0, 0);
                        targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;

                        pSF.Geometry      = queryGeometry;
                        pSF.GeometryField = featureClass.ShapeFieldName;
                        pSF.SpatialRel    = spatialRelEnum;
                        pSF.WhereClause   = whereClause;
                        break;
                    }
                }

                featureDataConverter = new FeatureDataConverterClass();
                featureDataConverter.ConvertFeatureClass(inFCName, pSF, null, outFCName, geometryDef, outFields, " ", 1000, 0);

                return(true);
            }
            catch (Exception ex)
            {
                LOG.Error("图层数据导出出错!" + ex.Message);
                throw ex;
            }
        }
Example #4
0
        public bool CreateRaster(IRasterDataset pRDs, IRasterWorkspaceEx pWorkSpace)
        {
            IRasterProps      pRasterProps      = (IRasterProps)pRDs.CreateDefaultRaster();
            IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass();

            pRasterStorageDef.CompressionType     = esriRasterCompressionType.esriRasterCompressionJPEG2000;
            pRasterStorageDef.CompressionQuality  = 50;
            pRasterStorageDef.PyramidLevel        = 2;
            pRasterStorageDef.PyramidResampleType = rstResamplingTypes.RSP_BilinearInterpolation;
            pRasterStorageDef.TileHeight          = 128;
            pRasterStorageDef.TileWidth           = 128;
            IRasterDef pRasterDef = new RasterDefClass();

            pRasterDef.Description      = "RasterDataset";
            pRasterDef.SpatialReference = pRasterProps.SpatialReference;
            IGeometryDef     pGeoDef     = new GeometryDefClass();
            IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit;

            pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
            pGeoDefEdit.AvgNumPoints_2 = 4;
            pGeoDefEdit.GridCount_2    = 1;
            pGeoDefEdit.set_GridSize(0, 1000);
            pGeoDefEdit.SpatialReference_2 = pRasterProps.SpatialReference;
            IRasterDataset pRasterDataset = pWorkSpace.CreateRasterDataset("zzy", 3, rstPixelType.PT_UCHAR, pRasterStorageDef, "", pRasterDef, pGeoDef);

            pRasterDataset = pRDs;
            return(true);
        }
Example #5
0
        public IField2 GetGeometryField(string geometryType, string geometryFieldName)
        {
            esriGeometryType type = _geometryTypeProvider.GetGeometryType(geometryType);
            // Create the geometry field
            IGeometryDef     geometryDef     = new GeometryDefClass();
            IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;

            // Assign Geometry Definition
            geometryDefEdit.GeometryType_2 = type;
            geometryDefEdit.GridCount_2    = 1;
            geometryDefEdit.set_GridSize(0, 0.5);
            geometryDefEdit.AvgNumPoints_2 = 2;
            geometryDefEdit.HasM_2         = false;
            geometryDefEdit.HasZ_2         = false;

            // Create Geometry Field
            IField     fieldShape     = new FieldClass();
            IFieldEdit fieldEditShape = (IFieldEdit)fieldShape;

            fieldEditShape.Name_2        = geometryFieldName;
            fieldEditShape.AliasName_2   = geometryFieldName;
            fieldEditShape.Type_2        = esriFieldType.esriFieldTypeGeometry;
            fieldEditShape.GeometryDef_2 = geometryDef;

            return(null);
        }
Example #6
0
        private IFeatureClass CreateFeatureClass(IFeatureWorkspace FeatureWorkspace, string LayerName, esriFeatureType featureType, esriGeometryType GeometryType)
        {
            IFields     fields = new FieldsClass();
            IFieldsEdit edit   = fields as IFieldsEdit;

            //创建OBJECTID字段
            IField     field3 = new FieldClass();
            IFieldEdit edit2  = field3 as IFieldEdit;

            edit2.Name_2      = "OBJECTID";
            edit2.AliasName_2 = "OBJECTID";
            edit2.Type_2      = esriFieldType.esriFieldTypeOID;
            edit.AddField(field3);

            //创建Shape字段
            IGeometryDef     def   = new GeometryDefClass();
            IGeometryDefEdit edit4 = def as IGeometryDefEdit;

            edit4.GeometryType_2 = GeometryType;
            edit4.GridCount_2    = 1;
            edit4.set_GridSize(0, 1000);
            edit4.AvgNumPoints_2     = 2;
            edit4.HasM_2             = false;
            edit4.HasZ_2             = false;
            edit4.SpatialReference_2 = this.ExportSpatialReference;

            IField     field4 = new FieldClass();
            IFieldEdit edit3  = field4 as IFieldEdit;

            edit3.Name_2        = "SHAPE";
            edit3.AliasName_2   = "SHAPE";
            edit3.Type_2        = esriFieldType.esriFieldTypeGeometry;
            edit3.GeometryDef_2 = def;
            edit.AddField(field4);

            string ShapeFiledName = field4.Name;

            UID uid  = null;
            UID uid2 = null;

            switch (featureType)
            {
            case esriFeatureType.esriFTSimple:       //FeatureClass
                IObjectClassDescription description4 = new FeatureClassDescriptionClass();
                uid  = description4.InstanceCLSID;
                uid2 = description4.ClassExtensionCLSID;
                break;

            case esriFeatureType.esriFTAnnotation:     //AnnotationFeatureClass
                IObjectClassDescription description = new AnnotationFeatureClassDescriptionClass();
                uid          = description.InstanceCLSID;
                uid2         = description.ClassExtensionCLSID;
                GeometryType = esriGeometryType.esriGeometryPolygon;
                break;
            }
            //创建要素对象
            IFeatureClass fc = FeatureWorkspace.CreateFeatureClass(LayerName, fields, uid, uid2, featureType, ShapeFiledName, null);

            return(fc);
        }
Example #7
0
 /// <summary>
 /// SDE要素类导出mdb(要素集下要素类)
 /// </summary>
 /// <param name="IN_SourceWorkspace">源工作空间</param>
 /// <param name="IN_TargetWorkspace">目标要素数据集</param>
 /// <param name="IN_SourceFeatureClassName">源要素类名</param>
 /// <param name="IN_TargetFeatureClassName">目标要素类名</param>
 private void ConvertFeatureClass(IWorkspace IN_SourceWorkspace, IFeatureDataset IN_TargetWorkspace, string IN_SourceFeatureClassName, string IN_TargetFeatureClassName)
 {
     #region 环境配置
     //创建源工作空间名
     IDataset       Temp_SourceWorkspaceDataset = (IDataset)IN_SourceWorkspace;
     IWorkspaceName Temp_SourceWorkspaceName    = (IWorkspaceName)Temp_SourceWorkspaceDataset.FullName;
     //创建源要素数据集名
     IFeatureClassName Temp_SourceFeatureClassName = new FeatureClassNameClass();
     IDatasetName      Temp_SourceDatasetName      = (IDatasetName)Temp_SourceFeatureClassName;
     Temp_SourceDatasetName.WorkspaceName = Temp_SourceWorkspaceName;
     Temp_SourceDatasetName.Name          = IN_SourceFeatureClassName;
     //创建目标工作空间名
     IDataset       Temp_TargetWorkspaceDataset = (IDataset)IN_TargetWorkspace.Workspace;
     IWorkspaceName Temp_TargetWorkspaceName    = (IWorkspaceName)(Temp_TargetWorkspaceDataset.FullName);
     //创建目标要素类名
     IFeatureClassName Temp_TargetFeatureClassName = new FeatureClassNameClass();
     IDatasetName      Temp_TargetDatasetName      = (IDatasetName)Temp_TargetFeatureClassName;
     Temp_TargetDatasetName.WorkspaceName = Temp_TargetWorkspaceName;
     Temp_TargetDatasetName.Name          = IN_TargetFeatureClassName;
     //创建目标要素数据集名
     IFeatureDatasetName Temp_TargetFeatureDatasetName = new FeatureDatasetNameClass();
     IDatasetName        Temp_TargetDatasetName2       = (IDatasetName)Temp_TargetFeatureDatasetName;
     Temp_TargetDatasetName2.WorkspaceName = Temp_TargetWorkspaceName;
     Temp_TargetDatasetName2.Name          = IN_TargetWorkspace.Name;
     #endregion
     //打开源要素类获取字段定义
     ESRI.ArcGIS.esriSystem.IName Temp_SourceName = (ESRI.ArcGIS.esriSystem.IName)Temp_SourceFeatureClassName;
     IFeatureClass Temp_SourceFeatureClass        = (IFeatureClass)Temp_SourceName.Open();
     //验证字段
     IFieldChecker   Temp_FieldChecker = new FieldCheckerClass();
     IFields         Temp_TargetFeatureClassFields;
     IFields         Temp_SourceFeatureClassFields = Temp_SourceFeatureClass.Fields;
     IEnumFieldError enumFieldError;
     Temp_FieldChecker.InputWorkspace    = IN_SourceWorkspace;
     Temp_FieldChecker.ValidateWorkspace = IN_TargetWorkspace.Workspace;
     Temp_FieldChecker.Validate(Temp_SourceFeatureClassFields, out enumFieldError, out Temp_TargetFeatureClassFields);
     //批量导入
     IField Temp_GeometryField;
     for (int i = 0; i < Temp_TargetFeatureClassFields.FieldCount; i++)
     {
         if (Temp_TargetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
         {
             Temp_GeometryField = Temp_TargetFeatureClassFields.get_Field(i);
             //获取空间定义
             IGeometryDef     Temp_GeometryDef        = Temp_GeometryField.GeometryDef;
             IGeometryDefEdit Temp_TargetFCGeoDefEdit = (IGeometryDefEdit)Temp_GeometryDef;
             Temp_TargetFCGeoDefEdit.GridCount_2 = 1;
             Temp_TargetFCGeoDefEdit.set_GridSize(0, 0);
             Temp_TargetFCGeoDefEdit.SpatialReference_2 = Temp_GeometryField.GeometryDef.SpatialReference;
             //定义筛选条件
             IQueryFilter Temp_QueryFilter = new QueryFilterClass();
             Temp_QueryFilter.WhereClause = "";
             //导入要素类至要素数据集
             IFeatureDataConverter Temp_FeatureDataConverter = new FeatureDataConverterClass();
             IEnumInvalidObject    enumErrors = Temp_FeatureDataConverter.ConvertFeatureClass(Temp_SourceFeatureClassName, Temp_QueryFilter, Temp_TargetFeatureDatasetName, Temp_TargetFeatureClassName, Temp_GeometryDef, Temp_TargetFeatureClassFields, "", 1000, 0);
             break;
         }
     }
 }
Example #8
0
        /// <summary>
        /// 将一个要素类从一个工作空间转移到另外一个工作空间
        /// 注意目标工作空间不能有改要素类,必须先清除
        /// </summary>
        /// <param name="sourceWorkspace">源工作空间</param>
        /// <param name="targetWorkspace">目标工作空间</param>
        /// <param name="nameOfSourceFeatureClass">源要素类名</param>
        /// <param name="nameOfTargetFeatureClass">目标要素类名</param>
        public void IFeatureDataConverter_ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass)
        {
            //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);
                    break;
                }
            }
        }
        /// <summary>
        /// Coder:  梁  爽
        /// Date:   2008-10-16
        /// Content:该函数实现要素类的创建,首先用接口IGeometryDefEdit设置要素类的类型,即是Point,Polylin还是Polygon
        ///         然后用接口IFieldEdit设置要素类的字段,这里设置了三个字段,分别是ID,OID和Shape。最后通过接口IFeatureWorkspace
        ///         的CreateFeatureClass方法创建要素类,函数返回IFeatureClass接口类型
        /// </summary>
        /// <param name="pWorkspace">设置工作空间</param>
        /// <param name="pGeometryType">设置要素的类型,即是点,线,还是面</param>
        /// <param name="strFeatName">设置要素类的名称</param>
        /// <param name="ValCoordinatType">传递一个数值型参数,该参数为1-UnkownCoordinateSystem,2-GeographicCoordinateSystem
        ///            或3ProjectedCoordinateSystem</param>
        /// <returns></returns>
        private IFeatureClass CreateFeatureClass(esriGeometryType pesriGeometryType, string pstrLayerName,
                                                 IWorkspace pWorkspace, IMapControl4 pm_mapControl, List <string> pstrFieldNameLt = null, List <esriFieldType> pesriFieldTypeLt = null)
        {
            //创建要素空间
            IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
            IGeometryDef      geometryDef       = new GeometryDefClass();
            IGeometryDefEdit  pGeometryDefEdit  = (IGeometryDefEdit)geometryDef;

            //_esriGeometryType could be: esriGeometryPoint, esriGeometryMultipoint,
            //esriGeometryPolyline, esriGeometryPolygon, and esriGeometryMultiPatch.
            pGeometryDefEdit.GeometryType_2     = pesriGeometryType;
            pGeometryDefEdit.SpatialReference_2 = pm_mapControl.SpatialReference;
            // Set the grid count to 1 and the grid size to 0 to allow ArcGIS to
            // determine a valid grid size.
            pGeometryDefEdit.GridCount_2 = 1;
            pGeometryDefEdit.set_GridSize(0, 0);

            //新建字段
            IFieldsEdit pFieldsEdit = new FieldsClass();
            //添加“Shape”字段
            IFieldEdit pField;

            pField               = new FieldClass();
            pField.Type_2        = esriFieldType.esriFieldTypeGeometry;
            pField.GeometryDef_2 = geometryDef;
            pField.Name_2        = "Shape";
            pFieldsEdit.AddField((IField)pField);
            //添加其它字段
            if (pstrFieldNameLt != null)
            {
                //we don't need the first three FieldNames, i.e., "FID", "Shape", and "Id"
                for (int i = 0; i < pstrFieldNameLt.Count; i++)
                {
                    pFieldsEdit.AddField((IField)GenerateFieldEdit(pesriFieldTypeLt[i], pstrFieldNameLt[i]));
                }
            }


            string strFullName = pWorkspace.PathName + "\\" + pstrLayerName;

            File.Delete(strFullName + ".dbf");
            File.Delete(strFullName + ".lyr");
            File.Delete(strFullName + ".prj");
            File.Delete(strFullName + ".shp");
            //File.Delete(strFullName + ".shp.PALANQUE.2296.5388.sr.lock");
            File.Delete(strFullName + ".shx");

            try
            {
                return(pFeatureWorkspace.CreateFeatureClass(pstrLayerName,
                                                            (IFields)pFieldsEdit, null, null, esriFeatureType.esriFTSimple, "Shape&quot;", ""));
            }
            catch
            {
                return(pFeatureWorkspace.CreateFeatureClass(pstrLayerName,
                                                            (IFields)pFieldsEdit, null, null, esriFeatureType.esriFTSimple, "Shape", ""));
            }
        }
        /// <summary>
        /// Transfers data from an input FeatureClass to an output FeatureClass with data filtered with the specified query filter.
        /// </summary>
        /// <param name="inputWorkspace">The input workspace.</param>
        /// <param name="inputFeatureClassName">Name of the input feature class.</param>
        /// <param name="outputWorkspace">The output workspace.</param>
        /// <param name="outputFeatureClassName">Name of the output feature class.</param>
        /// <param name="outputFeatureDatasetName">The output FeatureDataset</param>
        /// <param name="queryFilter">The query filter used to filter data to be transfered.</param>
        public static IEnumInvalidObject TransferData(IFeatureWorkspace inputWorkspace, IFeatureClass inputFeatureClass, IFeatureWorkspace outputWorkspace, IFeatureClassName outputFeatureClassName, IFeatureDatasetName outputFeatureDatasetName, IQueryFilter queryFilter, ISpatialReference outputSpatialReference)
        {
            if (inputWorkspace == null)
            {
                throw new ArgumentNullException("A valid input feature workspace reference [inputWorkspace] is required.");
            }

            if (inputFeatureClass == null)
            {
                throw new ArgumentNullException("A valid input featureclass [inputFeatureClass] is required.");
            }

            if (outputWorkspace == null)
            {
                throw new ArgumentNullException("A valid output feature workspace reference [outputWorkspace] is required.");
            }

            if (outputFeatureClassName == null)
            {
                throw new ArgumentNullException("A valid output featureclass name reference [outputFeatureClassName] is required.");
            }

            IDataset          dataset = (IDataset)inputFeatureClass;
            IFeatureClassName inputFeatureClassName = (IFeatureClassName)dataset.FullName;

            IEnumFieldError   error        = null;
            IFields           outFields    = null;
            IFields           inFields     = inputFeatureClass.Fields;
            FieldCheckerClass fieldChecker = new FieldCheckerClass();

            fieldChecker.InputWorkspace    = (IWorkspace)inputWorkspace;
            fieldChecker.ValidateWorkspace = (IWorkspace)outputWorkspace;
            fieldChecker.Validate(inFields, out error, out outFields);

            IField shapeField = null;

            for (int i = 0; i < outFields.FieldCount; i++)
            {
                if (outFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    shapeField = outFields.get_Field(i);
                    break;
                }
            }


            IGeometryDef     shapeGeoDef     = shapeField.GeometryDef;
            IGeometryDefEdit shapeGeoDefEdit = shapeGeoDef as IGeometryDefEdit;

            shapeGeoDefEdit.GridCount_2 = 1;
            shapeGeoDefEdit.set_GridSize(0, DefaultIndexGrid(ref inputFeatureClass));
            shapeGeoDefEdit.SpatialReference_2 = outputSpatialReference;

            FeatureDataConverterClass fdc = new FeatureDataConverterClass();

            return(fdc.ConvertFeatureClass(inputFeatureClassName, queryFilter, outputFeatureDatasetName, outputFeatureClassName, shapeGeoDefEdit, outFields, "", 1000, 0));
        }
Example #11
0
        public void IFeatureDataConverter(IWorkspace pSourceWorkspace, IWorkspace pTargetWorkspace, string pSFeatureClass, string pDFeatureClass)
        {
            IDataset       sourceWorkspaceDataset = (IDataset)pSourceWorkspace;
            IWorkspaceName sourceWorkspaceName    = (IWorkspaceName)sourceWorkspaceDataset.FullName;

            IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
            IDatasetName      sourceDatasetName      = (IDatasetName)sourceFeatureClassName;

            sourceDatasetName.WorkspaceName = sourceWorkspaceName;
            sourceDatasetName.Name          = pSFeatureClass;

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

            IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
            IDatasetName      targetDatasetName      = (IDatasetName)targetFeatureClassName;

            targetDatasetName.WorkspaceName = targetWorkspaceName;
            targetDatasetName.Name          = pDFeatureClass;

            ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;
            IFeatureClass sourceFeatureClass        = (IFeatureClass)sourceName.Open();

            IFieldChecker   fieldChecker = new FieldCheckerClass();
            IFields         targetFeatureClassFields;
            IFields         sourceFeatureClassFields = sourceFeatureClass.Fields;
            IEnumFieldError enumFieldError;

            fieldChecker.InputWorkspace    = pSourceWorkspace;
            fieldChecker.ValidateWorkspace = pTargetWorkspace;
            fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);

            IField geometryField;

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

                    IGeometryDef geometryDef = geometryField.GeometryDef;

                    IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
                    targetFCGeoDefEdit.GridCount_2 = 1;
                    targetFCGeoDefEdit.set_GridSize(0, 0);

                    targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;

                    IQueryFilter queryFilter = new QueryFilterClass();
                    queryFilter.WhereClause = "";

                    IFeatureDataConverter fctofc     = new FeatureDataConverterClass();
                    IEnumInvalidObject    enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0);
                    break;
                }
            }
        }
Example #12
0
        private static void ConfigureSpatialIndexGrid(
            [NotNull] IGeometryDefEdit geometryDef,
            double gridSize1,
            double gridSize2,
            double gridSize3)
        {
            Assert.ArgumentNotNull(geometryDef, nameof(geometryDef));

            var gridCount = 0;

            if (gridSize1 > 0)
            {
                gridCount++;
            }

            if (gridSize2 > 0)
            {
                Assert.True(gridSize1 > 0, "Grid level 2 is defined, but grid level 1 is not");
                gridCount++;
            }

            if (gridSize3 > 0)
            {
                Assert.True(gridSize2 > 0, "Grid level 3 is defined, but grid level 2 is not");
                gridCount++;
            }

            geometryDef.GridCount_2 = gridCount;

            if (gridSize1 > 0)
            {
                geometryDef.set_GridSize(0, gridSize1);

                if (gridSize2 > 0)
                {
                    geometryDef.set_GridSize(1, gridSize2);

                    if (gridSize3 > 0)
                    {
                        geometryDef.set_GridSize(2, gridSize3);
                    }
                }
            }
        }
Example #13
0
        //字段集
        private IFields CreateFieldsCollectionForFeatueClass(ISpatialReference spatialReference,
                                                             esriGeometryType geometryType, string className)
        {
            //Use the feature class description to return the required fields in a fields collection.
            IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
            IObjectClassDescription  ocDesc = (IObjectClassDescription)fcDesc;
            //Create the fields using the required fields method.
            IFields fields = ocDesc.RequiredFields;
            //Locate the shape field with the name from the feature class description.
            int    shapeFieldIndex = fields.FindField(fcDesc.ShapeFieldName);
            IField shapeField      = fields.get_Field(shapeFieldIndex);
            //Modify the GeometryDef object before using the fields collection to create a
            //feature class.
            IGeometryDef     geometryDef     = shapeField.GeometryDef;
            IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;

            //Alter the feature class geometry type to the type we need.
            geometryDefEdit.GeometryType_2 = geometryType;
            geometryDefEdit.HasZ_2         = true;
            geometryDefEdit.HasM_2         = true;
            geometryDefEdit.GridCount_2    = 1;
            //Set the first grid size to zero and allow ArcGIS to determine a valid grid size.
            geometryDefEdit.set_GridSize(0, 0);
            geometryDefEdit.SpatialReference_2 = spatialReference;
            //添加地质属性概率字段
            if (geometryType == esriGeometryType.esriGeometryPoint)
            {
                //Create a user-defined double field for "probability"
                IFieldsEdit fieldsEdit      = (IFieldsEdit)fields;
                IField      incomeField     = new FieldClass();
                IFieldEdit  incomeFieldEdit = (IFieldEdit)incomeField;
                incomeFieldEdit.AliasName_2  = "probability";
                incomeFieldEdit.Editable_2   = true;
                incomeFieldEdit.IsNullable_2 = false;
                incomeFieldEdit.Name_2       = "probability";
                incomeFieldEdit.Precision_2  = 2;
                incomeFieldEdit.Scale_2      = 5;
                incomeFieldEdit.Type_2       = esriFieldType.esriFieldTypeDouble;
                fieldsEdit.AddField(incomeField);
                //Create a user-defined double field for "RegardP"
                IFieldsEdit fieldsEdit1      = (IFieldsEdit)fields;
                IField      incomeField1     = new FieldClass();
                IFieldEdit  incomeFieldEdit1 = (IFieldEdit)incomeField1;
                incomeFieldEdit1.AliasName_2  = "RegardP";
                incomeFieldEdit1.Editable_2   = true;
                incomeFieldEdit1.IsNullable_2 = false;
                incomeFieldEdit1.Name_2       = "RegardP";
                incomeFieldEdit1.Precision_2  = 2;
                incomeFieldEdit1.Scale_2      = 5;
                incomeFieldEdit1.Type_2       = esriFieldType.esriFieldTypeDouble;
                fieldsEdit1.AddField(incomeField1);
            }
            return(fields);
        }
Example #14
0
        public void SetGridSize()
        {
            //简单判断下
            try
            {
                if ((m_dblTempGridIndex1 != 0) && (m_dblTempGridIndex1 <= m_dblTempGridIndex2))
                {
                    return;
                }
                if ((m_dblTempGridIndex2 != 0) && (m_dblTempGridIndex2 < m_dblTempGridIndex3))
                {
                    return;
                }

                if (m_pFeaCls == null)
                {
                    return;
                }

                //获得igeometrydef
                string strShapeName = m_pFeaCls.ShapeFieldName;
                IField pShapeField  = m_pFeaCls.Fields.get_Field(m_pFeaCls.FindField(m_pFeaCls.ShapeFieldName));
                if (pShapeField == null)
                {
                    return;
                }
                IGeometryDefEdit pGeometryDefEdit = pShapeField.GeometryDef as IGeometryDefEdit;

                //根据设置值进行设置
                if (m_dblTempGridIndex1 == 0)
                {
                    pGeometryDefEdit.GridCount_2 = 0;
                    return;
                }
                if (m_dblTempGridIndex2 == 0)
                {
                    pGeometryDefEdit.GridCount_2 = 1;
                    pGeometryDefEdit.set_GridSize(0, m_dblTempGridIndex1);
                    return;
                }

                if (m_dblTempGridIndex3 == 0)
                {
                    pGeometryDefEdit.GridCount_2 = 2;
                    pGeometryDefEdit.set_GridSize(0, m_dblTempGridIndex1);
                    pGeometryDefEdit.set_GridSize(1, m_dblTempGridIndex2);

                    return;
                }

                pGeometryDefEdit.GridCount_2 = 3;
                pGeometryDefEdit.set_GridSize(0, m_dblTempGridIndex1);
                pGeometryDefEdit.set_GridSize(1, m_dblTempGridIndex2);
                pGeometryDefEdit.set_GridSize(2, m_dblTempGridIndex3);
            }
            catch (Exception ex)
            {
                MessageBox.Show("空间索引设置出现错误" + Environment.NewLine + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Example #15
0
        /// <summary>
        /// 设置空间索引
        /// </summary>
        /// <param name="pTargetFields"></param>
        /// <returns>SHAPE字段名</returns>
        public string AddSpatialIndex(IFields pTargetFields, Double gridOneSize = 0, Double gridTwoSize = 0, Double gridThreeSize = 0)
        {
            string       sShapeFieldName    = "";
            IGeometryDef pTargetGeometryDef = null;

            for (int i = 0; i < pTargetFields.FieldCount; i++)
            {
                IField pField = pTargetFields.get_Field(i);
                if (pField.Type == esriFieldType.esriFieldTypeGeometry)
                {
                    sShapeFieldName    = pField.Name;
                    pTargetGeometryDef = pField.GeometryDef;
                    //Allow ArcGIS to determine a valid grid size for the data loaded
                    IGeometryDefEdit pGeometryDefEdit = pTargetGeometryDef as IGeometryDefEdit;
                    pGeometryDefEdit.GridCount_2 = 3;
                    pGeometryDefEdit.set_GridSize(0, gridOneSize);
                    pGeometryDefEdit.set_GridSize(1, gridTwoSize);
                    pGeometryDefEdit.set_GridSize(2, gridThreeSize);
                    pGeometryDefEdit.SpatialReference_2 = pField.GeometryDef.SpatialReference;
                    break;
                }
            }
            return(sShapeFieldName);
        }
Example #16
0
        private IFields CloneFeatureClassFields(IFeatureClass pFeatureClass, IEnvelope pDomainEnv)
        {
            IFields     pFields     = new FieldsClass();
            IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;
            //根据传入的要素类,将除了shape字段之外的字段复制
            long nOldFieldsCount = pFeatureClass.Fields.FieldCount;
            long nOldGeoIndex    = pFeatureClass.Fields.FindField(pFeatureClass.ShapeFieldName);

            for (int i = 0; i < nOldFieldsCount; i++)
            {
                if (i != nOldGeoIndex)
                {
                    pFieldsEdit.AddField(pFeatureClass.Fields.get_Field(i));
                }
                else
                {
                    IGeometryDef      pGeomDef     = new GeometryDefClass();
                    IGeometryDefEdit  pGeomDefEdit = (IGeometryDefEdit)pGeomDef;
                    ISpatialReference pSR          = null;
                    if (pDomainEnv != null)
                    {
                        pSR = new UnknownCoordinateSystemClass();
                        pSR.SetDomain(pDomainEnv.XMin, pDomainEnv.XMax, pDomainEnv.YMin, pDomainEnv.YMax);
                    }
                    else
                    {
                        IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;
                        pSR = CloneSpatialReference(pGeoDataset.SpatialReference);
                    }
                    //设置新要素类Geometry的参数
                    pGeomDefEdit.GeometryType_2 = pFeatureClass.ShapeType;
                    pGeomDefEdit.GridCount_2    = 1;
                    pGeomDefEdit.set_GridSize(0, 10);
                    pGeomDefEdit.AvgNumPoints_2     = 2;
                    pGeomDefEdit.SpatialReference_2 = pSR;
                    //产生新的shape字段
                    IField     pField     = new FieldClass();
                    IFieldEdit pFieldEdit = (IFieldEdit)pField;
                    pFieldEdit.Name_2        = "shape";
                    pFieldEdit.AliasName_2   = "shape";
                    pFieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
                    pFieldEdit.GeometryDef_2 = pGeomDef;
                    pFieldsEdit.AddField(pField);
                }
            }
            return(pFields);
        }
Example #17
0
        /// <summary>
        /// 创建几何对象字段定义字段
        /// </summary>
        /// <param name="geometryType"></param>
        /// <param name="spatialRef"></param>
        /// <returns></returns>
        private static IGeometryDef CreateGeometryDef(esriGeometryType geometryType, ISpatialReference spatialRef)
        {
            //创建几何对象字段定义字段
            IGeometryDef     geometryDef     = new GeometryDefClass();
            IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;

            //指定几何对象字段属性值
            geometryDefEdit.GeometryType_2 = geometryType;
            geometryDefEdit.GridCount_2    = 1;
            geometryDefEdit.set_GridSize(0, 1000);
            if (spatialRef != null)
            {
                geometryDefEdit.SpatialReference_2 = spatialRef;
            }

            return(geometryDef);
        }
Example #18
0
        /// <summary>
        /// 设置几何空间参考定义
        /// </summary>
        /// <param name="pSpatialRes"></param>
        /// <returns></returns>
        private IGeometryDef CreateGeoDef(ISpatialReference pSpatialRes)
        {
            IGeometryDef     pGeoDef     = new GeometryDefClass();
            IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit;

            pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
            pGeoDefEdit.AvgNumPoints_2 = 4;
            pGeoDefEdit.GridCount_2    = 1;
            pGeoDefEdit.set_GridSize(0, 1000);

            if (pSpatialRes == null)
            {
                pSpatialRes = new UnknownCoordinateSystemClass();
            }
            pGeoDefEdit.SpatialReference_2 = pSpatialRes;
            pGeoDef = pGeoDefEdit as IGeometryDef;
            return(pGeoDef);
        }
Example #19
0
        private IFeatureClass method_2(IDataset idataset_1, string string_2)
        {
            IFields     fields = new FieldsClass();
            IFieldsEdit edit   = (IFieldsEdit)fields;

            for (int i = 0; i < this.arrayList_0.Count; i++)
            {
                edit.AddField(this.arrayList_0[i] as IField);
            }
            IGeometryDef     def   = new GeometryDefClass();
            IGeometryDefEdit edit2 = (IGeometryDefEdit)def;

            edit2.GeometryType_2 = esriGeometryType.esriGeometryPoint;
            edit2.GridCount_2    = 1;
            edit2.set_GridSize(0, 200.0);
            edit2.AvgNumPoints_2 = 1;
            edit2.HasM_2         = false;
            edit2.HasZ_2         = false;
            ISpatialReference           reference  = new UnknownCoordinateSystemClass();
            ISpatialReferenceResolution resolution = reference as ISpatialReferenceResolution;

            resolution.ConstructFromHorizon();
            resolution.SetDefaultXYResolution();
            (reference as ISpatialReferenceTolerance).SetDefaultXYTolerance();
            edit2.SpatialReference_2 = reference;
            IField     field = new FieldClass();
            IFieldEdit edit3 = (IFieldEdit)field;

            edit3.Name_2      = "OBJECTID";
            edit3.AliasName_2 = "OBJECTID";
            edit3.Type_2      = esriFieldType.esriFieldTypeOID;
            edit.AddField(field);
            IField     field2 = new FieldClass();
            IFieldEdit edit4  = (IFieldEdit)field2;

            edit4.Name_2        = "SHAPE";
            edit4.AliasName_2   = "SHAPE";
            edit4.Type_2        = esriFieldType.esriFieldTypeGeometry;
            edit4.GeometryDef_2 = def;
            edit.AddField(field2);
            return(this.CreateFeatureClass(idataset_1, string_2, new UnknownCoordinateSystemClass(),
                                           esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPoint, fields, null, null, null));
        }
Example #20
0
        public static IField CreateGeometryField(esriGeometryType pGT, ISpatialReference pSR, bool pHasZ)
        {
            FieldClass class2 = new FieldClass();
            IFieldEdit edit   = class2;

            edit.Name_2      = "SHAPE";
            edit.AliasName_2 = "图形对象";
            edit.Type_2      = esriFieldType.esriFieldTypeGeometry;
            IGeometryDef     pGeoDef = new GeometryDefClass();
            IGeometryDefEdit class3  = (IGeometryDefEdit)pGeoDef;

            class3.GeometryType_2 = pGT;
            class3.HasM_2         = false;
            class3.HasZ_2         = pHasZ;
            class3.GridCount_2    = 1;
            class3.set_GridSize(0, 1000.0);
            class3.SpatialReference_2 = pSR;
            edit.GeometryDef_2        = class3;
            return(class2);
        }
Example #21
0
        private IFields CreateFieldsCollection(ISpatialReference spatialReference, esriGeometryType geometryType)
        {
            IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
            IObjectClassDescription  ocDesc = fcDesc as IObjectClassDescription;

            IFields     fields     = ocDesc.RequiredFields;
            IFieldsEdit fieldsEdit = fields as IFieldsEdit;

            int    shapeFieldIndex = fields.FindField(fcDesc.ShapeFieldName);
            IField shapeField      = fields.get_Field(shapeFieldIndex);

            IGeometryDef     geometryDef     = shapeField.GeometryDef;
            IGeometryDefEdit geometryDefEdit = geometryDef as IGeometryDefEdit;

            geometryDefEdit.GeometryType_2 = geometryType;
            geometryDefEdit.GridCount_2    = 1;
            geometryDefEdit.set_GridSize(0, 0);
            geometryDefEdit.SpatialReference_2 = spatialReference;

            return(fields);
        }
Example #22
0
        //得到要素类的字段集合
        private IFields GetFieldsFromFeatureClass(IFeatureClass fc, ISpatialReference spatialReference)
        {
            int          index = fc.FindField(fc.ShapeFieldName);
            IGeometryDef pGD   = fc.Fields.get_Field(index).GeometryDef;

            IFields     pFields     = new FieldsClass();
            IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

            for (int i = 0; i < fc.Fields.FieldCount; i++)
            {
                IField pSourceField = fc.Fields.get_Field(i);
                if (fc.Fields.get_Field(i).Name == "shape" || fc.Fields.get_Field(i).Name == "SHAPE")
                {
                    IGeometryDef     geometryDef     = new GeometryDefClass();
                    IGeometryDefEdit geometryDefEdit = geometryDef as IGeometryDefEdit;
                    geometryDefEdit.GeometryType_2 = fc.ShapeType;
                    geometryDefEdit.GridCount_2    = 1;
                    geometryDefEdit.set_GridSize(0, 1000);
                    geometryDefEdit.AvgNumPoints_2     = 2;
                    geometryDefEdit.SpatialReference_2 = spatialReference;
                    geometryDefEdit.HasM_2             = pGD.HasM;
                    geometryDefEdit.HasZ_2             = pGD.HasZ;

                    IFieldEdit pField_SHAPE = new FieldClass();
                    pField_SHAPE.Name_2        = "SHAPE";
                    pField_SHAPE.AliasName_2   = "SHAPE";
                    pField_SHAPE.Type_2        = esriFieldType.esriFieldTypeGeometry;
                    pField_SHAPE.IsNullable_2  = true;
                    pField_SHAPE.GeometryDef_2 = geometryDef;
                    pFieldsEdit.AddField(pField_SHAPE as IField);
                }
                else
                {
                    pFieldsEdit.AddField(fc.Fields.get_Field(i));
                }
            }
            return(pFields);
        }
Example #23
0
        public IFeatureClass CreateFeatureClassWithoutAttribute(string feaClassName, esriGeometryType enumGometryType)
        {
            string featureClassName = feaClassName + "Time" + DateTime.Now.ToString("yyyyMMddHHmmss");
            //Basic field settings of FeatureClass
            IFields     fields     = new FieldsClass();
            IFieldsEdit fieldsEdit = (IFieldsEdit)fields;

            ISpatialReferenceFactory spatialrefFactory = new SpatialReferenceEnvironmentClass();//SpatialReference settings
            ISpatialReference        spatialReference  = spatialrefFactory.CreateProjectedCoordinateSystem(
                (int)(esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_CM_111E));
            IGeometryDef geometryDef = new GeometryDefClass();

            IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;//Geometry field settings

            geometryDefEdit.GeometryType_2 = enumGometryType;
            geometryDefEdit.GridCount_2    = 1;
            geometryDefEdit.set_GridSize(0, 0);
            geometryDefEdit.SpatialReference_2 = spatialReference;

            IField     fieldUserDefined = new FieldClass();//Field creation
            IFieldEdit fieldEdit        = (IFieldEdit)fieldUserDefined;

            fieldEdit.Name_2        = "Shape";
            fieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
            fieldEdit.GeometryDef_2 = geometryDef;
            fieldEdit.IsNullable_2  = true;
            fieldEdit.Required_2    = true;

            fieldsEdit.AddField(fieldUserDefined);

            //Create FeatureClass
            UID CLSID = new UIDClass();

            CLSID.Value = "esriGeodatabase.Feature";
            IFeatureClass createdFeatureClass = pFeaWorkspace.CreateFeatureClass(featureClassName, fields, CLSID, null, esriFeatureType.esriFTSimple, "Shape", " ");

            return(createdFeatureClass);
        }
Example #24
0
        public IFeatureClass CreateFeatureClass(object object_0, string string_2, ISpatialReference ispatialReference_0,
                                                esriFeatureType esriFeatureType_0, esriGeometryType esriGeometryType_0, IFields ifields_0, UID uid_0,
                                                UID uid_1, string string_3)
        {
            if (object_0 == null)
            {
                throw new Exception("[objectWorkspace] cannot be null");
            }
            if (!((object_0 is IWorkspace) || (object_0 is IFeatureDataset)))
            {
                throw new Exception("[objectWorkspace] must be IWorkspace or IFeatureDataset");
            }
            if (string_2 == "")
            {
                throw new Exception("[name] cannot be empty");
            }
            if ((object_0 is IWorkspace) && (ispatialReference_0 == null))
            {
                throw new Exception("[spatialReference] cannot be null for StandAlong FeatureClasses");
            }
            if (uid_0 == null)
            {
                uid_0 = new UIDClass();
                switch (esriFeatureType_0)
                {
                case esriFeatureType.esriFTSimple:
                    uid_0.Value = "{52353152-891A-11D0-BEC6-00805F7C4268}";
                    break;

                case esriFeatureType.esriFTSimpleJunction:
                    esriGeometryType_0 = esriGeometryType.esriGeometryPoint;
                    uid_0.Value        = "{CEE8D6B8-55FE-11D1-AE55-0000F80372B4}";
                    break;

                case esriFeatureType.esriFTSimpleEdge:
                    esriGeometryType_0 = esriGeometryType.esriGeometryPolyline;
                    uid_0.Value        = "{E7031C90-55FE-11D1-AE55-0000F80372B4}";
                    break;

                case esriFeatureType.esriFTComplexJunction:
                    uid_0.Value = "{DF9D71F4-DA32-11D1-AEBA-0000F80372B4}";
                    break;

                case esriFeatureType.esriFTComplexEdge:
                    esriGeometryType_0 = esriGeometryType.esriGeometryPolyline;
                    uid_0.Value        = "{A30E8A2A-C50B-11D1-AEA9-0000F80372B4}";
                    break;

                case esriFeatureType.esriFTAnnotation:
                    esriGeometryType_0 = esriGeometryType.esriGeometryPolygon;
                    uid_0.Value        = "{E3676993-C682-11D2-8A2A-006097AFF44E}";
                    break;

                case esriFeatureType.esriFTDimension:
                    esriGeometryType_0 = esriGeometryType.esriGeometryPolygon;
                    uid_0.Value        = "{496764FC-E0C9-11D3-80CE-00C04F601565}";
                    break;
                }
            }
            if (uid_1 == null)
            {
                switch (esriFeatureType_0)
                {
                case esriFeatureType.esriFTAnnotation:
                    uid_1       = new UIDClass();
                    uid_1.Value = "{24429589-D711-11D2-9F41-00C04F6BC6A5}";
                    break;

                case esriFeatureType.esriFTDimension:
                    uid_1       = new UIDClass();
                    uid_1.Value = "{48F935E2-DA66-11D3-80CE-00C04F601565}";
                    break;
                }
            }
            if (ifields_0 == null)
            {
                ifields_0 = new FieldsClass();
                IFieldsEdit      edit  = (IFieldsEdit)ifields_0;
                IGeometryDef     def   = new GeometryDefClass();
                IGeometryDefEdit edit2 = (IGeometryDefEdit)def;
                edit2.GeometryType_2 = esriGeometryType_0;
                edit2.GridCount_2    = 1;
                edit2.set_GridSize(0, 0.5);
                edit2.AvgNumPoints_2 = 2;
                edit2.HasM_2         = false;
                edit2.HasZ_2         = true;
                if (object_0 is IWorkspace)
                {
                    edit2.SpatialReference_2 = ispatialReference_0;
                }
                IField     field = new FieldClass();
                IFieldEdit edit3 = (IFieldEdit)field;
                edit3.Name_2      = "OBJECTID";
                edit3.AliasName_2 = "OBJECTID";
                edit3.Type_2      = esriFieldType.esriFieldTypeOID;
                edit.AddField(field);
                IField     field2 = new FieldClass();
                IFieldEdit edit4  = (IFieldEdit)field2;
                edit4.Name_2        = "SHAPE";
                edit4.AliasName_2   = "SHAPE";
                edit4.Type_2        = esriFieldType.esriFieldTypeGeometry;
                edit4.GeometryDef_2 = def;
                edit.AddField(field2);
            }
            string shapeFieldName = "";

            for (int i = 0; i <= (ifields_0.FieldCount - 1); i++)
            {
                if (ifields_0.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    shapeFieldName = ifields_0.get_Field(i).Name;
                    break;
                }
            }
            if (shapeFieldName == "")
            {
                throw new Exception("Cannot locate geometry field in FIELDS");
            }
            IFeatureClass class2 = null;

            if (object_0 is IWorkspace)
            {
                IWorkspace        workspace  = (IWorkspace)object_0;
                IFeatureWorkspace workspace2 = (IFeatureWorkspace)workspace;
                return(workspace2.CreateFeatureClass(string_2, ifields_0, uid_0, uid_1, esriFeatureType_0,
                                                     shapeFieldName, string_3));
            }
            if (object_0 is IFeatureDataset)
            {
                class2 = ((IFeatureDataset)object_0).CreateFeatureClass(string_2, ifields_0, uid_0, uid_1,
                                                                        esriFeatureType_0, shapeFieldName, string_3);
            }
            return(class2);
        }
Example #25
0
        public IFeatureClass CreateFeatureClass(string featureClassName, esriGeometryType geometryType, ISpatialReference spatialReference)
        {
            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetTempPath(), 0);
            IFields           fields           = new FieldsClass();
            IFieldsEdit       fieldsEdit       = (IFieldsEdit)fields;

            fieldsEdit.FieldCount_2 = 2;
            //Create the Object ID field.
            IField     fieldUserDefined = new Field();
            IFieldEdit fieldEdit        = (IFieldEdit)fieldUserDefined;

            fieldEdit.Name_2      = "FID";
            fieldEdit.AliasName_2 = "OBJECT ID";
            fieldEdit.Type_2      = esriFieldType.esriFieldTypeOID;
            fieldsEdit.set_Field(0, fieldUserDefined);
            // Create the Shape field.
            fieldUserDefined = new Field();
            fieldEdit        = (IFieldEdit)fieldUserDefined;
            // Set up the geometry definition for the Shape field.
            IGeometryDef     geometryDef     = new GeometryDefClass();
            IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;

            switch (geometryType)
            {
            case esriGeometryType.esriGeometryPoint:
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
                break;

            case esriGeometryType.esriGeometryPolyline:
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
                break;

            case esriGeometryType.esriGeometryPolygon:
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                break;

            default:
                break;
            }
            geometryDefEdit.GridCount_2 = 1;
            geometryDefEdit.set_GridSize(0, 0);
            geometryDefEdit.HasM_2 = false;
            geometryDefEdit.HasZ_2 = false;
            if (spatialReference != null)
            {
                geometryDefEdit.SpatialReference_2 = spatialReference;
            }
            // Set standard field properties.
            fieldEdit.Name_2        = "SHAPE";
            fieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
            fieldEdit.GeometryDef_2 = geometryDef;
            fieldEdit.IsNullable_2  = true;
            fieldEdit.Required_2    = true;
            fieldsEdit.set_Field(1, fieldUserDefined);

            UID CLSID = new UIDClass();

            CLSID.Value = "esriGeoDatabase.Feature";

            return(featureWorkspace.CreateFeatureClass(featureClassName, fields, CLSID, null, esriFeatureType.esriFTSimple, fields.get_Field(1).Name, ""));
        }
Example #26
0
        //将mdb中要素类转换复制到GDB数据库中
        private void ImportMDBToGDB(string file, string outfilename)
        {
            m_success = false;//初始化
            try
            {
                string filepath = file.Substring(0, file.LastIndexOf("---"));
                string filename = file.Substring(file.LastIndexOf("-") + 1);

                //打开mdb文件所在的工作空间
                ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wf = new AccessWorkspaceFactory();
                IFeatureWorkspace pFeatureWorkspaceMDB       = wf.OpenFromFile(@filepath, 0) as IFeatureWorkspace;
                IWorkspace        pWorkspaceMDB = pFeatureWorkspaceMDB as IWorkspace;
                // 创建源工作空间名称
                IDataset       sourceWorkspaceDataset = (IDataset)pWorkspaceMDB;
                IWorkspaceName sourceWorkspaceName    = (IWorkspaceName)sourceWorkspaceDataset.FullName;

                //创建源数据集名称
                //IFeatureClassName sourceFeatureClassName = serverContext.CreateObject("esriGeoDatabase.FeatureClassName") as IFeatureClassName;
                IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
                IDatasetName      sourceDatasetName      = (IDatasetName)sourceFeatureClassName;
                sourceDatasetName.WorkspaceName = sourceWorkspaceName;
                sourceDatasetName.Name          = filename;

                //打开存在的工作空间,作为导入的空间
                IWorkspaceFactory Pwf = new FileGDBWorkspaceFactoryClass();
                //IWorkspace pWorkspaceGDB = Pwf.OpenFromFile(GetSourcePath(comboBoxSource.Text), 0);
                //IWorkspace2 pWorkspace2 = (IWorkspace2)(Pwf.OpenFromFile(GetSourcePath(comboBoxSource.Text), 0));
                IWorkspace pWorkspaceGDB = GetWorkspace(comboBoxSource.Text);
                if (pWorkspaceGDB == null)
                {
                    m_strErr  = "数据源未找到";
                    m_success = false;
                    return;
                }
                string username = GetSourceUser(comboBoxSource.Text);
                if (username.Trim() != "")
                {
                    outfilename = username + "." + outfilename;
                }
                IWorkspace2       pWorkspace2       = pWorkspaceGDB as IWorkspace2;
                IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceGDB;

                //创建目标工作空间名称
                IDataset       targetWorkspaceDataset = (IDataset)pWorkspaceGDB;
                IWorkspaceName targetWorkspaceName    = (IWorkspaceName)targetWorkspaceDataset.FullName;

                //创建目标数据集名称
                // IFeatureClassName targetFeatureClassName = serverContext.CreateObject("esriGeoDatabase.FeatureClassName") as IFeatureClassName;
                //判断要素是否存在,若存在将删除源文件
                if (pWorkspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, outfilename))
                {
                    if (m_newfile == true)
                    {
                        IFeatureClass tmpfeatureclass;
                        tmpfeatureclass = pFeatureWorkspace.OpenFeatureClass(outfilename);
                        IDataset set = tmpfeatureclass as IDataset;
                        fdr.DeleteSql(filename);
                        set.CanDelete();
                        set.Delete();
                    }
                    else
                    {
                        m_strErr  = "存在相同文件名";
                        m_success = false;
                        return;
                    }
                }
                IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
                IDatasetName      targetDatasetName      = (IDatasetName)targetFeatureClassName;
                targetDatasetName.WorkspaceName = targetWorkspaceName;
                targetDatasetName.Name          = outfilename;

                //打开输入的要素类以得到字段定义
                ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;
                IFeatureClass sourceFeatureClass        = (IFeatureClass)sourceName.Open();//打开源要素类

                //验证字段名称,因为你正在不同类型的工作空间之间进行数据转换
                //IFieldChecker fieldChecker = serverContext.CreateObject("esriGeoDatabase.FieldChecker") as IFieldChecker;
                IFieldChecker   fieldChecker             = new FieldCheckerClass();
                IFields         sourceFeatureClassFields = sourceFeatureClass.Fields;
                IFields         targetFeatureClassFields;
                IEnumFieldError enumFieldError;

                //最重要的设置输入和验证工作空间
                fieldChecker.InputWorkspace    = pWorkspaceMDB;
                fieldChecker.ValidateWorkspace = pWorkspaceGDB;
                fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);

                //遍历所有输出字段找到几何字段
                IField geometryField;
                for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
                {
                    if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        geometryField = targetFeatureClassFields.get_Field(i);
                        //得到几何字段的几何定义
                        IGeometryDef geometryDef = geometryField.GeometryDef;
                        //赋予几何定义一个空间索引格网数目和格网大小值
                        IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
                        targetFCGeoDefEdit.GridCount_2 = 1;
                        targetFCGeoDefEdit.set_GridSize(0, 0);
                        //允许ArcGIS为数据加载确定一个有效的格网大小
                        targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
                        //转换要素类中所有的要素
                        //IQueryFilter queryFilter = serverContext.CreateObject("esriGeoDatabase.QueryFilter") as IQueryFilter; ;
                        QueryFilter queryFilter = new QueryFilterClass();
                        queryFilter.WhereClause = "";
                        //加载要素类
                        //IFeatureDataConverter fctofc = serverContext.CreateObject("esriGeoDatabase.FeatureDataConverter") as IFeatureDataConverter;
                        if (InsertIntoDatabase(outfilename))//更新数据库
                        {
                            IFeatureDataConverter fctofc     = new FeatureDataConverterClass();
                            IEnumInvalidObject    enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0);
                        }
                    }
                }
                m_success = true;
            }
            catch { m_success = false; m_strErr = ""; }
        }
        //** 功能描述: 通过一个字段集,创建另外一个字段集,直接添加传入的字段集中的所有字段的话
        //             会产生高版本和低版本不兼容的问题,

        public static IFields GetFieldsByFields(IFields pFields, ref ISpatialReference pDesSpatialReference, Dictionary <string, string> pDicField)
        {
            //pDesSpatialReference = null;
            //pDicField = null;

            int               i                    = 0;
            IField            pField               = default(IField);
            IFieldEdit        pFieldEdit           = default(IFieldEdit);
            IFieldsEdit       pFieldsEdit          = default(IFieldsEdit);
            IField            pCreateField         = default(IField);
            ISpatialReference pOriSpatialReference = default(ISpatialReference);

            IGeometryDef     pGeometryDef     = default(IGeometryDef);
            IGeometryDefEdit pGeometryDefEdit = default(IGeometryDefEdit);
            double           ymin             = 0;
            double           xmin             = 0;
            double           xmax             = 0;
            double           ymax             = 0;
            double           mMin             = 0;
            double           zmin             = 0;
            double           zmax             = 0;
            double           mMax             = 0;
            IEnvelope        pEnvelop         = default(IEnvelope);
            IGeometry        pGeometry        = default(IGeometry);
            IClone           pClone           = default(IClone);
            //标识该字段是否被添加
            bool bIsAddField = false;

            //应该把OID字段添加进去,否则会产生错误
            pFieldsEdit = new FieldsClass();

            for (i = 0; i <= pFields.FieldCount - 1; i++)
            {
                pField = pFields.get_Field(i);

                if (pField.Editable | pField.Type == esriFieldType.esriFieldTypeOID | pField.Type == esriFieldType.esriFieldTypeGlobalID | pField.Type == esriFieldType.esriFieldTypeGUID)
                {
                    pClone       = (IClone)pField;
                    pCreateField = (IField)pClone.Clone();

                    //如果更改字段名称:如果是OID字段的话,即便不在dic中也要进行添加
                    if ((pDicField != null))
                    {
                        if (pDicField.ContainsKey(Strings.Trim(pField.Name)))
                        {
                            bIsAddField = true;
                            //修改字段的名称
                            pFieldEdit        = (IFieldEdit)pCreateField;
                            pFieldEdit.Name_2 = pDicField[Strings.Trim(pField.Name)];

                            //需不需要把别名赋过去,根据需要再看吧,现在看的话应该不要赋过去,因此一般字段名和别名是一样的
                            //如果赋过去的话,显示的是原字段的别名,即不是用户想看到的字段名
                            //pFieldEdit.AliasName = pField.AliasName
                        }
                        else if (pField.Type == esriFieldType.esriFieldTypeOID | pField.Type == esriFieldType.esriFieldTypeGlobalID | pField.Type == esriFieldType.esriFieldTypeGUID)
                        {
                            bIsAddField = true;
                        }
                        else
                        {
                            bIsAddField = false;
                        }
                    }
                    else
                    {
                        bIsAddField = true;
                    }
                    if (pField.Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        pGeometryDef     = pCreateField.GeometryDef;
                        pGeometryDefEdit = (IGeometryDefEdit)pGeometryDef;



                        if ((pDesSpatialReference != null))
                        {
                            pOriSpatialReference = pGeometryDef.SpatialReference;

                            //从原来的空间参考中得到domain
                            if (pOriSpatialReference.HasXYPrecision())
                            {
                                pOriSpatialReference.GetDomain(out xmin, out xmax, out ymin, out ymax);
                            }

                            pEnvelop = new EnvelopeClass();
                            pEnvelop.PutCoords(xmin, ymin, xmax, ymax);
                            pGeometry = pEnvelop;
                            pGeometry.SpatialReference = pOriSpatialReference;
                            pGeometry.Project(pDesSpatialReference);
                            xmax = pEnvelop.XMax;
                            xmin = pEnvelop.XMin;
                            ymax = pEnvelop.YMax;
                            ymin = pEnvelop.YMin;

                            pDesSpatialReference.SetDomain(xmin, xmax, ymin, ymax);

                            if (pOriSpatialReference.HasZPrecision())
                            {
                                pOriSpatialReference.GetZDomain(out zmin, out zmax);
                                pDesSpatialReference.SetZDomain(zmin, zmax);
                            }

                            if (pOriSpatialReference.HasMPrecision())
                            {
                                pOriSpatialReference.GetMDomain(out mMin, out mMax);
                                pDesSpatialReference.SetMDomain(mMin, mMax);
                            }

                            pGeometryDefEdit.SpatialReference_2 = pDesSpatialReference;
                        }
                        //修改grid的大小
                        if (pGeometryDef.GridCount != 0)
                        {
                            if (pGeometryDef.get_GridSize(0) < 100)
                            {
                                pGeometryDefEdit.set_GridSize(0, 100);
                            }
                        }
                        bIsAddField = true;
                    }

                    if (bIsAddField == true)
                    {
                        pFieldsEdit.AddField(pCreateField);
                        bIsAddField = false;
                    }
                }
            }
            return(pFieldsEdit);
        }
Example #28
0
        //创建要素类
        private bool CreateFeatureLayer(string FName)
        {
            try
            {
                IFields     fields     = new FieldsClass();
                IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
                fieldsEdit.FieldCount_2 = 2;
                IField     fieldUserDefined = new Field();
                IFieldEdit fieldEdit        = (IFieldEdit)fieldUserDefined;
                fieldEdit.Name_2      = "FID";
                fieldEdit.AliasName_2 = "OBJECT ID";
                fieldEdit.Type_2      = esriFieldType.esriFieldTypeOID;
                fieldsEdit.set_Field(0, fieldUserDefined);

                fieldUserDefined = new Field();
                fieldEdit        = (IFieldEdit)fieldUserDefined;

                IGeometryDef     geometryDef     = new GeometryDefClass();
                IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                geometryDefEdit.GridCount_2    = 1;
                geometryDefEdit.set_GridSize(0, 0);
                geometryDefEdit.HasM_2 = false;
                geometryDefEdit.HasZ_2 = false;

                if (spatialReference != null)
                {
                    geometryDefEdit.SpatialReference_2 = spatialReference;
                }

                fieldEdit.Name_2        = "SHAPE";
                fieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
                fieldEdit.GeometryDef_2 = geometryDef;
                fieldEdit.IsNullable_2  = true;
                fieldEdit.Required_2    = true;
                fieldsEdit.set_Field(1, fieldUserDefined);

                UID CLSID = new UIDClass
                {
                    Value = "esriGeoDatabase.Feature"
                };
                Thread.Sleep(1000);
                string        tmpLyrName   = DateTime.Now.ToString("yyyyMMddHHmmss");
                IFeatureClass featureClass = featureWorkspace.CreateFeatureClass(tmpLyrName, fields, CLSID, null, esriFeatureType.esriFTSimple, fields.get_Field(1).Name, "");
                ROIFileNames.Add(tmpLyrName);
                IFeatureLayer featureLayer = new FeatureLayerClass();
                featureLayer.FeatureClass = featureClass;
                featureLayer.Name         = FName;
                featureLayer.Visible      = true;

                ILayerEffects layerEffects = featureLayer as ILayerEffects;
                layerEffects.Transparency = 55;

                m_mapControl.ActiveView.FocusMap.AddLayer(featureLayer);
                m_mapControl.ActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography, null, null);

                return(true);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
                return(false);
            }
        }
Example #29
0
        /// <summary>
        /// 将GDB的数据入到SDE中 xisheng 20110919
        /// </summary>
        private void ImportGDBToSDE(string file, string outfilename, out int featurecount)
        {
            m_success = false;//初始化
            try
            {
                string filepath = file.Substring(0, file.LastIndexOf("---"));
                string filename = file.Substring(file.LastIndexOf("-") + 1);

                //打开mdb文件所在的工作空间
                ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wf = new FileGDBWorkspaceFactory();
                IFeatureWorkspace pFeatureWorkspaceGDB       = wf.OpenFromFile(@filepath, 0) as IFeatureWorkspace;
                IWorkspace        pWorkspaceGDB = pFeatureWorkspaceGDB as IWorkspace;
                // 创建源工作空间名称
                IDataset       sourceWorkspaceDataset = (IDataset)pFeatureWorkspaceGDB;
                IWorkspaceName sourceWorkspaceName    = (IWorkspaceName)sourceWorkspaceDataset.FullName;

                //创建源数据集名称
                //IFeatureClassName sourceFeatureClassName = serverContext.CreateObject("esriGeoDatabase.FeatureClassName") as IFeatureClassName;
                IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
                IDatasetName      sourceDatasetName      = (IDatasetName)sourceFeatureClassName;
                sourceDatasetName.WorkspaceName = sourceWorkspaceName;
                sourceDatasetName.Name          = filename;

                //打开存在的工作空间,作为导入的空间;
                IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pTargetworkspace;

                //创建目标工作空间名称
                IDataset        targetWorkspaceDataset = (IDataset)pTargetworkspace;
                IWorkspaceName  targetWorkspaceName    = (IWorkspaceName)targetWorkspaceDataset.FullName;
                IWorkspace2     pWorkspace2            = pTargetworkspace as IWorkspace2;
                IFeatureDataset tmpfeaturedataset;
                //创建目标数据集名称
                // IFeatureClassName targetFeatureClassName = serverContext.CreateObject("esriGeoDatabase.FeatureClassName") as IFeatureClassName;
                //判断要素是否存在,若存在将删除源文件
                if (pWorkspace2.get_NameExists(esriDatasetType.esriDTFeatureDataset, textBox.Text))
                {
                    tmpfeaturedataset = pFeatureWorkspace.OpenFeatureDataset(textBox.Text);
                    if (text_prj.Text != "")
                    {
                        IGeoDatasetSchemaEdit pgeodataset = tmpfeaturedataset as IGeoDatasetSchemaEdit;
                        if (pgeodataset.CanAlterSpatialReference)
                        {
                            pgeodataset.AlterSpatialReference(GetSpatialReferenceformFile(text_prj.Text));
                        }
                    }
                }
                else
                {
                    tmpfeaturedataset = CreateFeatureDataset(pTargetworkspace as IFeatureWorkspace, textBox.Text, text_prj.Text);
                }
                if (pWorkspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, outfilename))
                {
                    IFeatureClass tmpfeatureclass;
                    tmpfeatureclass = pFeatureWorkspace.OpenFeatureClass(outfilename);
                    IDataset tempset = tmpfeatureclass as IDataset;
                    tempset.Delete();
                }

                IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
                IDatasetName      targetDatasetName      = (IDatasetName)targetFeatureClassName;
                targetDatasetName.WorkspaceName = targetWorkspaceName;
                targetDatasetName.Name          = outfilename;
                //目标数据集
                IFeatureDatasetName outfeaturedatasetname = tmpfeaturedataset.FullName as IFeatureDatasetName;


                //打开输入的要素类以得到字段定义
                ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;
                IFeatureClass sourceFeatureClass        = (IFeatureClass)sourceName.Open();//打开源要素类

                //验证字段名称,因为你正在不同类型的工作空间之间进行数据转换
                //IFieldChecker fieldChecker = serverContext.CreateObject("esriGeoDatabase.FieldChecker") as IFieldChecker;
                IFieldChecker   fieldChecker             = new FieldCheckerClass();
                IFields         sourceFeatureClassFields = sourceFeatureClass.Fields;
                IFields         targetFeatureClassFields;
                IEnumFieldError enumFieldError;

                //最重要的设置输入和验证工作空间
                fieldChecker.InputWorkspace    = pWorkspaceGDB;
                fieldChecker.ValidateWorkspace = pTargetworkspace;
                fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);

                //遍历所有输出字段找到几何字段
                IField geometryField;
                for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
                {
                    if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        geometryField = targetFeatureClassFields.get_Field(i);
                        //得到几何字段的几何定义
                        IGeometryDef geometryDef = geometryField.GeometryDef;
                        //赋予几何定义一个空间索引格网数目和格网大小值
                        IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
                        targetFCGeoDefEdit.GridCount_2 = 1;
                        targetFCGeoDefEdit.set_GridSize(0, 0);
                        //允许ArcGIS为数据加载确定一个有效的格网大小
                        targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
                        //转换要素类中所有的要素
                        //IQueryFilter queryFilter = serverContext.CreateObject("esriGeoDatabase.QueryFilter") as IQueryFilter; ;
                        QueryFilter queryFilter = new QueryFilterClass();
                        queryFilter.WhereClause = "";
                        //加载要素类
                        //IFeatureDataConverter fctofc = serverContext.CreateObject("esriGeoDatabase.FeatureDataConverter") as IFeatureDataConverter;

                        IFeatureDataConverter fctofc     = new FeatureDataConverterClass();
                        IEnumInvalidObject    enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, outfeaturedatasetname, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0);
                    }
                }
                featurecount = sourceFeatureClass.FeatureCount(null);
                m_success    = true;
            }
            catch (Exception ee) { m_success = false; m_strErr = ee.Message; featurecount = 0; }
        }
Example #30
0
        /// <summary>
        /// SDE要素类导出mdb
        /// </summary>
        /// <param name="sourceWorkspace">源工作空间</param>
        /// <param name="targetWorkspace">目标工作空间</param>
        /// <param name="nameOfSourceFeatureClass">源要素类名</param>
        /// <param name="nameOfTargetFeatureClass">目标要素类名</param>
        /// <param name="queryFilter"></param>
        /// <returns></returns>
        private bool ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass)
        {
            //创建一个源数据的工作空间的name
            IDataset       sourceWorkspaceDataset = (IDataset)sourceWorkspace;
            IWorkspaceName sourceWorkspaceName    = (IWorkspaceName)sourceWorkspaceDataset.FullName;

            //创建源数据Name作为转换参数
            IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
            IDatasetName      sourceDatasetName      = (IDatasetName)sourceFeatureClassName;

            sourceDatasetName.WorkspaceName = sourceWorkspaceName;
            sourceDatasetName.Name          = nameOfSourceFeatureClass;

            //创建目标(导出)数据空间的name
            IDataset       targetWorkspaceDataset = (IDataset)targetWorkspace;
            IWorkspaceName targetWorkspaceName    = (IWorkspaceName)targetWorkspaceDataset.FullName;

            //创建目标数据Name作为转换参数
            IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
            IDatasetName      targetDatasetName      = (IDatasetName)targetFeatureClassName;

            targetDatasetName.WorkspaceName = targetWorkspaceName;

            //这个命名也很重要,如果是mdb,那么这个命名在数据库中必须是本来不存在的(当然你可以先清空数据库)
            targetDatasetName.Name = nameOfTargetFeatureClass;

            //根据FetureClassName打开数据
            ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;
            IFeatureClass sourceFeatureClass        = (IFeatureClass)sourceName.Open();

            //在两个不同的工作空间转换数据要检查字段的有效性
            //一些检查有效性的参数
            IFieldChecker   fieldChecker = new FieldCheckerClass();
            IFields         targetFeatureClassFields;
            IFields         sourceFeatureClassFields = sourceFeatureClass.Fields;
            IEnumFieldError enumFieldError;

            //设置检查有效性的源数据工作空间
            fieldChecker.InputWorkspace    = sourceWorkspace;
            fieldChecker.ValidateWorkspace = targetWorkspace;
            fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);

            //通过输出字段循环找出几何字段
            IField geometryField;

            //遍历字段
            for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
            {
                //找到定义几何要素类型的字段
                if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    //获取当前字段
                    geometryField = targetFeatureClassFields.get_Field(i);
                    //编辑几何要素类型字段
                    IGeometryDef     geometryDef        = geometryField.GeometryDef;
                    IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
                    targetFCGeoDefEdit.GridCount_2 = 1;
                    targetFCGeoDefEdit.set_GridSize(0, 0);
                    //保证空间参考      SpatialReference_2属性字段才是可写的,SpatialReference属性只可读
                    targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
                    //开始转换
                    IFeatureDataConverter myConvert = new FeatureDataConverterClass();
                    myConvert.ConvertFeatureClass(sourceFeatureClassName, null, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0);
                    return(true);
                }
            }
            return(false);
        }