添加SHP文件到SDE中指定FeatureClass中(方法一)

本方法通过打开SDE中对应的FeatureClass,在其中添加Featurer的方式将SHP文件中的Feature添加到SDE中的FeatureClass中.

 

 #region 添加要SHP要素到SDE数据库方法一
    /// <summary>
    /// 添加要SHP要素到SDE数据库方法一
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void add_Click(object sender, EventArgs e)
    {
        ///获取shp文件的要素集
        IFeatureClass sFeatureClass = null;
        //打开shp文件数据的工作空间   
        string filepath = Server.MapPath("./") + "temp";
        ///IWorkspaceFactory pWorkspaceFactory = (IWorkspaceFactory)soc.CreateObject("esriDataSourcesFile.ShapefileWorkspaceFactory");
        IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
        IFeatureWorkspace pFeatWS = pWorkspaceFactory.OpenFromFile(@filepath, 0) as IFeatureWorkspace;
        //从工作空间中获取要素集
        sFeatureClass = pFeatWS.OpenFeatureClass("road03.shp");//源数据要素集


        ///==============
        ///
        IWorkspaceFactory dWSF = new CadWorkspaceFactoryClass();
        ICadDrawingWorkspace dCadWS = dWSF.OpenFromFile(@filepath, 0) as ICadDrawingWorkspace;
        ICadDrawingDataset dCadDST = dCadWS.OpenCadDrawingDataset("kyx.dwg");
        ////==============

 

        ///获取sde中的要素集
        string tFeatureClassName = "roadtest";//sde中要素名

        //打开SDE空间空间
        IPropertySet pProSet = new PropertySetClass();
        pProSet.SetProperty("Server", "GZGISSER");
        pProSet.SetProperty("Instance", "5151");
        pProSet.SetProperty("Database", "gzgddb");
        pProSet.SetProperty("user", "sde");
        pProSet.SetProperty("password", "gzgissersde");
        pProSet.SetProperty("version", "SDE.DEFAULT");
        IWorkspaceFactory factory = new SdeWorkspaceFactoryClass();
        IWorkspace workspace = factory.Open(pProSet, 0);

        IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
        //打开获取SDE要素集
        IFeatureClass tFeatureClass = featureWorkspace.OpenFeatureClass(tFeatureClassName);//目标数据要素集
        //定义编辑工作空间
        IWorkspaceEdit workspaceEdite = (IWorkspaceEdit)workspace;
        //查询条件,获取要素集中所有要素
        IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilter();
        //要素集中要素个数
        int num = sFeatureClass.FeatureCount(queryFilter);


        //编辑工作空间开启编辑
        workspaceEdite.StartEditing(true);
        workspaceEdite.StartEditOperation();

        //根据获取的shp文件中的要素,逐一插入到SDE要素集中
        for (int i = 0; i < num; i++)
        {
            //创建临时SDE要素
            IFeature feature = tFeatureClass.CreateFeature();
            //创建临时SHP要素集中要素
            IFeature sFeature = sFeatureClass.GetFeature(i);

            //获取SHP要素集中对应要素的几何形态
            IGeometry pGeo = sFeature.ShapeCopy;
            IPolyline pl2 = pGeo as IPolyline;

            //将SHP要素集中对应要素几何形态赋给SDE中对应要素
            feature.Shape = pl2;

            ISubtypes subtypes = (ISubtypes)tFeatureClass;
            IRowSubtypes rowSubtypes = (IRowSubtypes)feature;
            if (subtypes.HasSubtype)
            {
                rowSubtypes.SubtypeCode = 0;
            }
            rowSubtypes.InitDefaultValues();
            feature.Store();
            //设置要素的属性值-(Num字体对应值为12)
            feature.set_Value(feature.Fields.FindField("Num"), "12");
            feature.Store();
        }
        //保存对SDE要素集做的编辑、更改
        bool hasEdits = false;
        workspaceEdite.HasEdits(ref hasEdits);
        if (hasEdits)
        {
            workspaceEdite.StopEditing(true);
        }

   }
    #endregion

posted @ 2012-05-02 16:21  LinHugh  阅读(1157)  评论(0编辑  收藏  举报