向SDE中写入栅格和矢量数据类

说明:本类使用了上一篇文章中的打开sdeworkspace的类库。

 

本类是实现了向SDE中写入栅格和矢量数据,在SDE for Sql2005+ae9.3上测试通过。

 

使用时注意里面有一些向数据库中写入图层信息的内容

 

源代码如下:

/********************************************************************
 filename:  WriteToSDE.cs
 created: 2009/12/20
 author:  张烨
 purpose: 向空间数据库写入矢量或者栅格数据
*********************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geometry;

namespace RadioAnalysisGIS.DataToSDE
{
    /// <summary>
    /// 写入SDE
    /// </summary>
    /// <history>
    /// [张烨] 2009/12/20 创建
    /// [张烨] 2009/12/25 编辑,向属性数据库中添加空间数据库更新说明
    ///</history>
    class WriteToSDE
    {

        #region 全局变量,构造函数
        protected SDEConnectParas paras;
        private FileWorkSpace pFileWorkSpace;
        public WriteToSDE(SDEConnectParas para)
        {
            paras = para;
            pFileWorkSpace = new FileWorkSpace();
        }

        #endregion

        #region 初始化SDE连接参数
        private SDEConnectPara InitPara()
        {
            paras.ReadFromFile();
            string Server = "jiarui-dfe6951b";
            string Instance = "5152";
            string User = "gis";
            string Pass = "gis";
            string Database = "gisstations";
            string Version = "SDE_DEFAULT";
            paras.GetSDEParameters(out Server, out Instance, out User, out Pass, out Database, out Version);
            //从paras中获取SDE连接参数
            SDEConnectPara para = new SDEConnectPara();
            para.Server = Server;
            para.Instance = Instance;
            para.User = User;
            para.Password = Pass;
            para.Database = Database;
            para.Version = Version;
            return para;
        }
        #endregion

        #region 连接到工作空间
        public ConnectRasterWorkSpaceDef ConnToSdeRasterDef()
        {
            SDEConnectPara para = InitPara();
            ConnectRasterWorkSpaceDef ConnRasterDef = new ConnectRasterWorkSpaceDef(para);
            return ConnRasterDef;
        }

        public ConnectFeatureWorkSpaceDef ConnToSdeFeatureDef()
        {
            SDEConnectPara para = InitPara();
            ConnectFeatureWorkSpaceDef ConnFeatureDef = new ConnectFeatureWorkSpaceDef(para);
            return ConnFeatureDef;
        }
        #endregion

        #region 读取空间矢量和栅格数据
        public IRasterDataset GetRasterDataset(ConnectRasterWorkSpaceDef ConnDef, string RasterName)
        {
            ConnectRasterWorkSpaceGet pWorkSpaceGet = new ConnectRasterWorkSpaceGet(ConnDef);
            IRasterWorkspaceEx pWorkSpace = pWorkSpaceGet.GetSDEWorkspace();
            if (pWorkSpace == null) return null;
            try
            {
                return pWorkSpace.OpenRasterDataset(RasterName);
            }
            catch
            {
                return null;
            }
        }
        public IFeatureClass GetFeatureClass(ConnectFeatureWorkSpaceDef ConnDef, string FeatureName)
        {
            ConnectFeatureWorkSpaceGet pWorkSpaceGet = new ConnectFeatureWorkSpaceGet(ConnDef);
            IFeatureWorkspace pWorkSpace = pWorkSpaceGet.GetSDEWorkspace();
            if (pWorkSpace == null) return null;
            try
            {
                return pWorkSpace.OpenFeatureClass(FeatureName);
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region sde处理栅格
        public bool WriteRasterToSDE(Guid MapGuid,ConnectRasterWorkSpaceDef ConnDef, string filePath)
        {
            ConnectRasterWorkSpaceGet pWorkSpaceGet = new ConnectRasterWorkSpaceGet(ConnDef);
            IRasterWorkspaceEx pWorkSpace = pWorkSpaceGet.GetSDEWorkspace();
            //pWorkSpace.SaveAsRasterDataset("fasdf",pDataSet.CreateDefaultRaster(),null,"",null,null);
            string LayerStoreName=CopyRasterToSSDE(filePath, pWorkSpace);

            #region 写入数据库图层信息
            WriteToDB wtd = new WriteToDB();
            FilePath file = new FilePath(filePath);
            wtd.WriteLayerInfo(file.GetFileName(), MapGuid, 1, LayerStoreName);
            #endregion

            return true;
        }
        public string CopyRasterToSSDE(string filePath, IRasterWorkspaceEx pWorkSpace)
        {
            FilePath file = new FilePath(filePath);
            IRasterWorkspace2 pRW = pFileWorkSpace.OpenRasterWorkspace(file.GetDir());
            IRasterDataset pRds = pRW.OpenRasterDataset(file.GetFileName());


            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;
            DateTime dt = DateTime.Now;
            string timestr = "sde" + "_" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + "_" + dt.Hour + dt.Minute + dt.Second;
            IRasterDataset pRasterDataset = pWorkSpace.SaveAsRasterDataset(timestr, pRds.CreateDefaultRaster(), pRasterStorageDef, "", pRasterDef, pGeoDef);
            return timestr;
        }
        public bool CreateRasterToSSDE(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;

            //使用CreateRasterDataset创建一个新层可以
            IRasterDataset pRasterDataset = pWorkSpace.CreateRasterDataset("zzy", 3, rstPixelType.PT_UCHAR, pRasterStorageDef, "", pRasterDef, pGeoDef);
            pRasterDataset = pRDs;
            return true;

        }
        #endregion

        #region sde处理feature
        public bool AddFeatureToSDE(Guid MapGuid,ConnectFeatureWorkSpaceDef ConnDef, IFeatureClass pFC, string LayerName)
        {
            ConnectFeatureWorkSpaceGet pWorkSpaceGet = new ConnectFeatureWorkSpaceGet(ConnDef);
            IFeatureWorkspace pWorkSpace = pWorkSpaceGet.GetSDEWorkspace();
            //pWorkSpace.SaveAsRasterDataset("fasdf",pDataSet.CreateDefaultRaster(),null,"",null,null);
            string LayerStoreName=CopyFeatureToSSDE(pFC, pWorkSpace,LayerName);

            #region 写入数据库图层信息
            WriteToDB wtd = new WriteToDB();
            wtd.WriteLayerInfo(LayerName, MapGuid, 0, LayerStoreName);
            #endregion

            return true;
        }
        public bool AddFeatureToSDE(ConnectFeatureWorkSpaceDef ConnDef, string filePath)
        {
            try
            {
                FilePath file = new FilePath(filePath);
                IFeatureWorkspace pFW = pFileWorkSpace.OpenFeatureWorkspace(file.GetDir());
                IFeatureClass pFC = pFW.OpenFeatureClass(file.GetFileName());
                ConnectFeatureWorkSpaceGet pWorkSpaceGet = new ConnectFeatureWorkSpaceGet(ConnDef);
                IFeatureWorkspace pWorkSpace = pWorkSpaceGet.GetSDEWorkspace();
                CopyFeatureToSSDE(pFC, pWorkSpace,file.GetFileName());
            }
            catch
            {
                return false;
            }
            return true;
        }
        public string CopyFeatureToSSDE(IFeatureClass pFC, IFeatureWorkspace pWorkSpace, string LayerName)
        {
            IDataset pDataset = pFC as IDataset;
            //IFeatureDataset pFD = pWorkSpace.OpenFeatureDataset("test.DBO.ContourDataSet");
            DateTime dt = DateTime.Now;
            string timestr = "contour" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + "_" + dt.Hour + dt.Minute + dt.Second;
            IFeatureDataConverter_ConvertFeatureClass(pDataset.Workspace as IWorkspace, pWorkSpace as IWorkspace, LayerName, timestr);
            return timestr;
        }

        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;
                }
            }
        }
        #endregion

    }
}

 

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值