注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

快乐小屋

 
 
 

日志

 
 

SDE连接,创建数据集,数据导入以及数据导出  

2013-09-12 14:23:25|  分类: sde |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1:界面设置:

 

2:数据库的连接:

        IWorkspace workspace;
        IFeatureWorkspace featureWorkspace;
        IRasterWorkspaceEx rasterWorkspaceEx;
        IFeatureDataset featureDataset;
        IRasterDataset rasterDataset;
        IPropertySet propertySet;

        private void btnConnectDB_Click(object sender, EventArgs e)
        {
            propertySet = new PropertySetClass();
            propertySet.SetProperty("server", "Toby-PC");
            propertySet.SetProperty("instance", "5151/tcp");
            propertySet.SetProperty("database", "sde");
            propertySet.SetProperty("user", "sde");
            propertySet.SetProperty("password", "Toby521904");
            propertySet.SetProperty("version", "SDE.DEFAULT");
            IWorkspaceFactory workspaceFactory =new SdeWorkspaceFactory();
            workspace = workspaceFactory.Open(propertySet, 0);
            MessageBox.Show("连接成功");
          
        }

3:创建数据集:

 private void btnCreateDataSet_Click(object sender, EventArgs e)
        {
            if (this.txtRasterDS.Text != "" || this.txtFeatureDS.Text != "")
            {
                featureWorkspace = workspace as IFeatureWorkspace;
                rasterWorkspaceEx = workspace as IRasterWorkspaceEx;
                IEnumDatasetName enumDatasetName;
                bool isExit = false;
                IDatasetName datasetName;
                string dsName = "";
                if (this.txtFeatureDS.Text != "")
                {
                    //定义空间参考
                    ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
                    ISpatialReference spatialReference = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954);
                    spatialReference.SetDomain(-1000, -1000, 1000, 1000);
                    enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset);
                    datasetName = enumDatasetName.Next();
                    isExit = false;
                    dsName = "sde.SDE." + this.txtFeatureDS.Text;
                    while (datasetName != null)
                    {
                        if (datasetName.Name == dsName)
                        {
                            isExit = true;
                            MessageBox.Show("矢量数据集已经存在!");
                            this.txtFeatureDS.Focus();
                            return;
                        }
                        datasetName = enumDatasetName.Next();
                    }
                    if (isExit == false)
                    {
                        featureDataset = featureWorkspace.CreateFeatureDataset(this.txtFeatureDS.Text, spatialReference);
                    }
                    MessageBox.Show("矢量数据集创建成功");
                }
                if (this.txtRasterDS.Text != "")
                {
                    //创建影像数据集;
                    isExit = false;
                    enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTRasterDataset);
                    datasetName = enumDatasetName.Next();
                    dsName = "SDE." + this.txtRasterDS.Text;
                    while (datasetName != null)
                    {
                        if (datasetName.Name == dsName)
                        {
                            isExit = true;
                            MessageBox.Show("矢量数据集已经存在!");
                            break;
                        }
                        datasetName = enumDatasetName.Next();
                    }
                    if (isExit == false)
                    {
                        IRasterStorageDef rasterStorageDef = new RasterStorageDefClass();
                        rasterStorageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionUncompressed;
                        rasterStorageDef.PyramidLevel = 1;
                        rasterStorageDef.PyramidResampleType = rstResamplingTypes.RSP_BilinearInterpolation;
                        rasterStorageDef.TileHeight = 128;
                        rasterStorageDef.TileWidth = 128;
                        IRasterDef rasterDef = new RasterDefClass();
                        ISpatialReference rasterDSpatialReference = new UnknownCoordinateSystemClass();
                        rasterDef.SpatialReference = rasterDSpatialReference;
                        IGeometryDef geometryDef = new GeometryDefClass();
                        IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
                        geometryDefEdit.AvgNumPoints_2 = 5;
                        geometryDefEdit.GridCount_2 = 1;
                        geometryDefEdit.set_GridSize(0, 1000);
                        geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                        ISpatialReference spatialReference2 = new UnknownCoordinateSystemClass();
                        geometryDefEdit.SpatialReference_2 = spatialReference2;
                        rasterDataset = rasterWorkspaceEx.CreateRasterDataset(this.txtRasterDS.Text, 1, rstPixelType.PT_LONG, rasterStorageDef, "DEFAULTS", rasterDef, geometryDef);
                        MessageBox.Show("栅格数据集创建成功");
                    }
                }
            }
        }

 

4:矢量数据直接导入sde

private void importFToSDE_Click(object sender, EventArgs e)
        {
            try
            {
                IDataset pDataSet = workspace as IDataset;
                this.openFileDialog1.Filter = "shp file(*.shp)|*.shp";
                this.openFileDialog1.Title = "打开矢量数据";
                this.openFileDialog1.Multiselect = false;
                string fileName = "";
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    fileName = this.openFileDialog1.FileName;
                    FileInfo fileInfo = new FileInfo(fileName);
                    string filePath = fileInfo.DirectoryName;
                    string file = fileInfo.Name;
                    FeatureClassToFeatureClass(pDataSet, filePath, file, "dddd", true);
                }
                MessageBox.Show("导入成功");
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
           
        }

 

     
        public void FeatureClassToFeatureClass(IDataset pDataSet, string strFeatFileDir, string strFeatFileName, string strOutName, bool isWorkspace)
        {
            try
            {
                IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
                IFeatureWorkspace pFeatureWK = (IFeatureWorkspace)pWSF.OpenFromFile(strFeatFileDir, 0);
                IFeatureClass pInFeatureClass = pFeatureWK.OpenFeatureClass(strFeatFileName);
                if (pInFeatureClass == null || pDataSet == null)
                {
                    MessageBox.Show("创建失败");
                    return;
                }
                IFeatureClassName pInFeatureclassName;
                IDataset pIndataset = (IDataset)pInFeatureClass;
                pInFeatureclassName = (IFeatureClassName)pIndataset.FullName;
                //如果名称已存在
                IWorkspace2 pWS2 = null;
                if (isWorkspace)
                    pWS2 = pDataSet as IWorkspace2;
                else
                    pWS2 = pDataSet.Workspace as IWorkspace2;
                if (pWS2.get_NameExists(esriDatasetType.esriDTFeatureClass, strOutName))
                {

                    DialogResult result;
                    result = System.Windows.Forms.MessageBox.Show(null, "矢量文件名  " + strOutName + "  在数据库中已存在!" + "/r是否覆盖?", "相同文件名", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    //覆盖原矢量要素
                    if (result == DialogResult.Yes)
                    {
                        IFeatureWorkspace pFWS = (IFeatureWorkspace)pWS2;
                        IDataset pDataset = pFWS.OpenFeatureClass(strOutName) as IDataset;
                        pDataset.Delete();
                        pDataset = null;
                    }
                }
                IFields pInFields, pOutFields;
                IFieldChecker pFieldChecker = new FieldCheckerClass();
                IEnumFieldError pError;
                pInFields = pInFeatureClass.Fields;
                pFieldChecker.Validate(pInFields, out pError, out pOutFields);
                IField geoField = null;
                for (int i = 0; i < pOutFields.FieldCount; i++)
                {
                    IField pField = pOutFields.get_Field(i);
                    if (pField.Type == esriFieldType.esriFieldTypeOID)
                    {
                        IFieldEdit pFieldEdit = (IFieldEdit)pField;
                        pFieldEdit.Name_2 = pField.AliasName;
                    }
                    if (pField.Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        geoField = pField;
                        break;
                    }
                }
                IGeometryDef geometryDef = geoField.GeometryDef;
                IFeatureDataConverter one2another = new FeatureDataConverterClass();
                IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
                IDatasetName pOutDatasetName = (IDatasetName)pOutFeatureClassName;
                if (isWorkspace)
                    pOutDatasetName.WorkspaceName = (IWorkspaceName)pDataSet.FullName;
                else
                    pOutDatasetName.WorkspaceName = (IWorkspaceName)((IDataset)pDataSet.Workspace).FullName;
                pOutDatasetName.Name = strOutName;
                if (isWorkspace)
                {
                    one2another.ConvertFeatureClass(pInFeatureclassName, null, null, pOutFeatureClassName, geometryDef, pOutFields, "", 1000, 0);
                }
                else
                {
                    IFeatureDataset pFeatDS = (IFeatureDataset)pDataSet;
                    IFeatureDatasetName pOutFeatDSName = pFeatDS.FullName as IFeatureDatasetName;
                    one2another.ConvertFeatureClass(pInFeatureclassName, null, pOutFeatDSName, pOutFeatureClassName, geometryDef, pOutFields, "", 1000, 0);
                    pOutFeatDSName = null;
                    pFeatDS = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

        }

 

5:矢量数据导入sde数据集:

        private void importF_Click(object sender, EventArgs e)
        {
            featureWorkspace = workspace as IFeatureWorkspace;
            this.openFileDialog1.Filter = "shp file(*.shp)|*.shp";
            this.openFileDialog1.Title = "打开矢量数据";
            this.openFileDialog1.Multiselect = false;
            string fileName = "";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = this.openFileDialog1.FileName;
                FileInfo fileInfo = new FileInfo(fileName);
                string filePath = fileInfo.DirectoryName;
                string file = fileInfo.Name;
                IWorkspaceFactory shpwpf = new ShapefileWorkspaceFactory();
                IWorkspace shpwp = shpwpf.OpenFromFile(filePath, 0);
                IFeatureWorkspace shpfwp = shpwp as IFeatureWorkspace;
                IFeatureClass shpfc = shpfwp.OpenFeatureClass(file);

                //导入SDE数据库
                IFeatureClass sdeFeatureClass = null;
                IFeatureClassDescription featureClassDescription = new FeatureClassDescriptionClass();
                IObjectClassDescription objectClassDescription = featureClassDescription as IObjectClassDescription;
                IFields fields = shpfc.Fields;
                IFieldChecker fieldChecker = new FieldCheckerClass();
                IEnumFieldError enumFieldError = null;
                IFields vFields = null;
                fieldChecker.ValidateWorkspace = featureWorkspace as IWorkspace;
                fieldChecker.Validate(fields,out enumFieldError,out vFields);
                featureDataset = featureWorkspace.OpenFeatureDataset(this.txtFeatureDS.Text);
                try
                {
                    sdeFeatureClass = featureWorkspace.OpenFeatureClass(shpfc.AliasName);
                }
                catch
                {
                   
                }
                if (sdeFeatureClass == null)
                {
                    sdeFeatureClass = featureDataset.CreateFeatureClass(shpfc.AliasName, vFields,
                        objectClassDescription.InstanceCLSID, objectClassDescription.ClassExtensionCLSID,
                        shpfc.FeatureType, shpfc.ShapeFieldName, "");
                    IFeatureCursor featureCursor = shpfc.Search(null, true);
                    IFeature feature = featureCursor.NextFeature();
                    IFeatureCursor sdeFeatureCursor = sdeFeatureClass.Insert(true);
                    IFeatureBuffer sdeFeatureBuffer;
                    while (feature != null)
                    {
                        sdeFeatureBuffer = sdeFeatureClass.CreateFeatureBuffer();
                        IField shpField = new FieldClass();
                        IFields shpFields = feature.Fields;
                        for (int i = 0; i < shpFields.FieldCount; i++)
                        {
                            shpField = shpFields.get_Field(i);
                            int index = sdeFeatureBuffer.Fields.FindField(shpField.Name);
                            if (index != -1)
                            {
                                sdeFeatureBuffer.set_Value(index, feature.get_Value(i));
                            }
                        }
                        sdeFeatureCursor.InsertFeature(sdeFeatureBuffer);
                        sdeFeatureCursor.Flush();
                        feature = featureCursor.NextFeature();
                    }
                }
            }
            MessageBox.Show("导入成功");
        }

 

6:导入栅格数据:

 private void importR_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "Imag file (*.img)|*.img";
            this.openFileDialog1.Title = "打开影像数据";
            this.openFileDialog1.Multiselect = false;
            string fileName = "";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = this.openFileDialog1.FileName;
                string filepath;
                string file;
                string file1;
                int lastIndex;
                lastIndex = fileName.LastIndexOf(@"");
                filepath = fileName.Substring(0, lastIndex);
                file = fileName.Substring(lastIndex + 1);

                //导入SDE数据库
                rasterWorkspaceEx = workspace as IRasterWorkspaceEx;
                IWorkspaceFactory tifwpf = new RasterWorkspaceFactoryClass();
                IWorkspace tifwp = tifwpf.OpenFromFile(filepath, 0);
                IRasterWorkspace tifrwp = tifwp as IRasterWorkspace;
                IRasterDataset rasterDataset = tifrwp.OpenRasterDataset(file);
                lastIndex = file.LastIndexOf(@".");
                file1 = file.Substring(0, lastIndex);
                imortRasterData(filepath, file, file1);
                MessageBox.Show("导入成功");
            }

        }

 

 

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

private void imortRasterData(string strRasterFileDir, string strRasterFileName, string strOutName)
        {
            try
            {
                //判断是否有重名现象
                IWorkspace2 pWS2 = workspace as IWorkspace2;
                //如果名称已存在
                if (pWS2.get_NameExists(esriDatasetType.esriDTRasterDataset, strOutName))
                {
                    DialogResult result;
                    result = MessageBox.Show(this, "栅格文件名  " + strOutName + "  在数据库中已存在!" + "\r是否覆盖?", "相同文件名", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    //覆盖原矢量要素
                    if (result == DialogResult.Yes)
                    {
                        IRasterWorkspaceEx pRWs = workspace as IRasterWorkspaceEx;
                        IDataset pDataset = pRWs.OpenRasterDataset(strOutName) as IDataset;
                        pDataset.Delete();
                        pDataset = null;
                    }
                    else if (result == DialogResult.No)
                    {
                        return;
                    }
                }
                IWorkspaceFactory pRasterWsFac = new RasterWorkspaceFactoryClass();
                IWorkspace pWs = pRasterWsFac.OpenFromFile(strRasterFileDir, 0);
                IRasterDataset pRasterDs = null;
                IRasterWorkspace pRasterWs;
                if (!(pWs is IRasterWorkspace))
                {
                    return;
                }
                pRasterWs = pWs as IRasterWorkspace;
                pRasterDs = pRasterWs.OpenRasterDataset(strRasterFileName);
                ISaveAs2 saveAs2 = (ISaveAs2)pRasterDs;
                IRasterStorageDef rasterStorageDef = new RasterStorageDefClass();
                IRasterStorageDef2 rasterStorageDef2 = (IRasterStorageDef2)rasterStorageDef;
                rasterStorageDef2.CompressionType =esriRasterCompressionType.esriRasterCompressionUncompressed;
                rasterStorageDef2.CompressionQuality = 100;
                rasterStorageDef2.Tiled = true;
                rasterStorageDef2.TileHeight = 128;
                rasterStorageDef2.TileWidth = 128;
                saveAs2.SaveAsRasterDataset(strOutName, workspace, "gdb", rasterStorageDef2);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

 

 

7:从SDE中读取数据:

private void ReadFromSDE_Click(object sender, EventArgs e)
        {
            this.cbbDataSet.Items.Clear();
            this.FeatureLayer.Items.Clear();
            this.RasterLayer.Items.Clear();
            IEnumDatasetName enumDatasetName;
            IDatasetName datasetName;
            //获取矢量数据集
            enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset);
            datasetName = enumDatasetName.Next();
            while (datasetName != null)
            {
                this.cbbDataSet.Items.Add(datasetName.Name);
                datasetName = enumDatasetName.Next();
            }

            //获取图层名
            enumDatasetName=workspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass);
            datasetName = enumDatasetName.Next();
            while (datasetName != null)
            {
                this.FeatureLayer.Items.Add(datasetName.Name);
                datasetName = enumDatasetName.Next();
            }

            //获取栅格图层名
            enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTRasterDataset);
            datasetName = enumDatasetName.Next();
            while (datasetName != null)
            {
                this.RasterLayer.Items.Add(datasetName.Name);
                datasetName = enumDatasetName.Next();
            }
            MessageBox.Show("读取成功");
        }

 

8:遍历数据集中的文件:

private void cbbDataSet_TextChanged(object sender, EventArgs e)
        {
            this.FeatureLayer.Items.Clear();
            IDataset dataset;
            IEnumDataset enumDataset;
            enumDataset = workspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
            dataset = enumDataset.Next();
            IDataset pDataset = null;
            while (dataset != null)
            {
                if (dataset.Name == this.cbbDataSet.Text)
                {
                    pDataset = dataset as IDataset;
                    break;
                }
                dataset = enumDataset.Next();
            }
            IFeatureDataset featDs = pDataset as IFeatureDataset;

            IFeatureClassContainer fcContainer = featDs as IFeatureClassContainer;
            for (int i = 0; i < fcContainer.ClassCount; i++)
            {
                this.FeatureLayer.Items.Add(fcContainer.get_Class(i).AliasName);
            }
        }

 

9:数据的加载:

 private void btnOk_Click(object sender, EventArgs e)
        {
            //直接从sde中读取数据.
            if (this.FeatureLayer.Text != "" && this.cbbDataSet.Text == "")
            {
                IEnumDataset enumDataset;
                IDataset dataset;
                IFeatureLayer pFeatureLayer=new FeatureLayerClass();
                //获取图层名
                enumDataset = workspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
                dataset = enumDataset.Next();
                while (dataset != null)
                {
                    if (dataset.Name.ToLower() == this.FeatureLayer.Text.ToLower())
                    {
                        pFeatureLayer.FeatureClass = dataset as IFeatureClass;
                        break;
                    }
                    dataset = enumDataset.Next();
                }
                this.axMapControl1.AddLayer(pFeatureLayer as ILayer);
                this.axMapControl1.Extent = this.axMapControl1.FullExtent;
            }
            // 从sde数据集中读取数据
            if (this.FeatureLayer.Text != "" && this.cbbDataSet.Text != "")
            {
                IDataset dataset;
                IEnumDataset enumDataset;
                enumDataset = workspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
                dataset = enumDataset.Next();
                IDataset pDataset = null;
                while (dataset != null)
                {
                    if (dataset.Name == this.cbbDataSet.Text)
                    {
                        pDataset = dataset as IDataset;
                        break;
                    }
                    dataset = enumDataset.Next();
                }
                IFeatureDataset featDs = pDataset as IFeatureDataset;

                IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                IFeatureClassContainer fcContainer = featDs as IFeatureClassContainer;
                for (int i = 0; i < fcContainer.ClassCount; i++)
                {
                    if (fcContainer.get_Class(i).AliasName.ToLower() == this.FeatureLayer.Text.ToLower())
                    {
                        pFeatureLayer.FeatureClass = fcContainer.get_Class(i) as IFeatureClass;
                        break;
                    }
                    //this.FeatureLayer.Items.Add(fcContainer.get_Class(i).AliasName);
                }
                this.axMapControl1.AddLayer(pFeatureLayer as ILayer);
                this.axMapControl1.Extent = this.axMapControl1.FullExtent;
            }
            //加载栅格数据
            if (this.RasterLayer.Text != "")
            {
                IDataset dataset;
                IEnumDataset enumDataset;
                enumDataset = workspace.get_Datasets(esriDatasetType.esriDTRasterDataset);
                dataset = enumDataset.Next();
                IDataset pDataset = null;
                while (dataset != null)
                {
                    if (dataset.Name.ToLower() == this.RasterLayer.Text.ToLower())
                    {
                        pDataset = dataset as IDataset;
                        break;
                    }
                    dataset = enumDataset.Next();
                }
                IRasterDataset pRasterDataset = dataset as IRasterDataset;
                IRasterLayer pRasLyr=new RasterLayerClass();
                pRasLyr.CreateFromDataset(pRasterDataset);
                this.axMapControl1.AddLayer(pRasLyr as ILayer);
                this.axMapControl1.Extent = this.axMapControl1.FullExtent;
            }
            MessageBox.Show("加载成功");
        }



  评论这张
 
阅读(1371)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018