六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 91|回复: 0

feature class上传sde数据库

[复制链接]

升级  27.33%

23

主题

23

主题

23

主题

秀才

Rank: 2

积分
91
 楼主| 发表于 2013-1-27 04:40:25 | 显示全部楼层 |阅读模式
功能:远程web上传shp文件,然后添加到指定的SDE已经存在的FeatureClass里

思路:读取shp里的Feature,编程连接到SDE,打开指定的FeatureClass,然后插入。

缺点:现在只能适用没有注册为版本的FeatureClass,在测试过程中,如果注册为版本,则运行报“Objects in this class cannot be updated outside an edit session ” 这个错误,一直不知道怎么解决,还请高手指点。

效果图:



主要实现代码:

文件上传功能实现:

C#代码
private void UpLoadFiles()   
    {   
        string filepath = Server.MapPath("./") + "UpLoadFiles";   
  
        HttpFileCollection uploadedFiles = Request.Files;   
        for (int i = 0; i < uploadedFiles.Count; i++)   
        {   
            HttpPostedFile userPostedFile = uploadedFiles[i];   
            try  
            {   
                if (userPostedFile.ContentLength > 0)   
                {   
                    userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName));                       
                }   
                message.Text = this.ShpFile + "上传成功!";   
            }   
            catch  
            {   
                message.Text = this.ShpFile + "上传失败!";   
            }   
        }   
    }  

private void UpLoadFiles()
    {
        string filepath = Server.MapPath("./") + "UpLoadFiles";

        HttpFileCollection uploadedFiles = Request.Files;
        for (int i = 0; i < uploadedFiles.Count; i++)
        {
            HttpPostedFile userPostedFile = uploadedFiles[i];
            try
            {
                if (userPostedFile.ContentLength > 0)
                {
                    userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName));                    
                }
                message.Text = this.ShpFile + "上传成功!";
            }
            catch
            {
                message.Text = this.ShpFile + "上传失败!";
            }
        }
    }

读取shp里的Feature功能实现:

C#代码
public IFeatureClass GetShpFeatureClass()   
    {   
        IFeatureClass returnFeatureClass = null;   
        //取得服务中的基本信息        
        IServerContext soc = GetSoc();   
  
        string filepath = Server.MapPath("./") + "UpLoadFiles";   
  
        IWorkspaceFactory pWorkspaceFactory = (IWorkspaceFactory)soc.CreateObject("esriDataSourcesFile.ShapefileWorkspaceFactory");   
        IFeatureWorkspace pFeatWS = pWorkspaceFactory.OpenFromFile(@filepath, 0) as IFeatureWorkspace;   
        //IFeatureLayer pLayer = (IFeatureLayer)pSOC.CreateObject("esriCarto.FeatureLayer");   
        returnFeatureClass = pFeatWS.OpenFeatureClass("china-hlj.shp");   
        //pLayer.Name = "rivers";   
        soc.ReleaseContext();   
        return returnFeatureClass;   
    }  

public IFeatureClass GetShpFeatureClass()
    {
        IFeatureClass returnFeatureClass = null;
        //取得服务中的基本信息     
        IServerContext soc = GetSoc();

        string filepath = Server.MapPath("./") + "UpLoadFiles";

        IWorkspaceFactory pWorkspaceFactory = (IWorkspaceFactory)soc.CreateObject("esriDataSourcesFile.ShapefileWorkspaceFactory");
        IFeatureWorkspace pFeatWS = pWorkspaceFactory.OpenFromFile(@filepath, 0) as IFeatureWorkspace;
        //IFeatureLayer pLayer = (IFeatureLayer)pSOC.CreateObject("esriCarto.FeatureLayer");
        returnFeatureClass = pFeatWS.OpenFeatureClass("china-hlj.shp");
        //pLayer.Name = "rivers";
        soc.ReleaseContext();
        return returnFeatureClass;
    }添加Feature到SDE的功能实现:

C#代码
public void Add_Fea(string FeaName, IFeature insertFeature)   
    {   
        IServerContext soc = GetSoc();   
  
        IPropertySet propSet = new PropertySetClass();   
        propSet.SetProperty("SERVER", this.SdeServer);   
        propSet.SetProperty("INSTANCE", this.SdeInstance);   
        propSet.SetProperty("USER", this.SdeUser);   
        propSet.SetProperty("PASSWORD", this.SdePassword);   
        propSet.SetProperty("VERSION", this.SdeVerson);   
  
  
        IWorkspaceFactory pWorkSpFac = (IWorkspaceFactory)soc.CreateObject("esriDataSourcesGDB.SDEWorkspaceFactory");   
        IFeatureWorkspace pFeaWorkSp = null;   
        pFeaWorkSp = (IFeatureWorkspace)(pWorkSpFac.Open(propSet, 0));//打开要素空间   
        IFeatureClass FeaCls = pFeaWorkSp.OpenFeatureClass(FeaName);//取得要素集   
        IFeatureCursor FeaCursor = FeaCls.Insert(true);   
        IFeatureBuffer FeaBuffer = FeaCls.CreateFeatureBuffer(); ;   
  
  
        IField Fld = new FieldClass();   
        IFields Flds = insertFeature.Fields;   
        for (int i = 0; i < Flds.FieldCount; i++)   
        {   
            Fld = Flds.get_Field(i);   
            int index = FeaBuffer.Fields.FindField(Fld.Name);   
            if (index != -1)   
            {   
                FeaBuffer.set_Value(index, insertFeature.get_Value(i));   
            }   
        }   
  
        FeaCursor.InsertFeature(FeaBuffer);   
        soc.ReleaseContext();   
        pFeaWorkSp = null;   
  
    }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表