功能:远程web上传shp文件,然后添加到指定的SDE已经存在的FeatureClass里
思路:读取shp里的Feature,编程连接到SDE,打开指定的FeatureClass,然后插入。
缺点:现在只能适用没有注册为版本的FeatureClass,在测试过程中,如果注册为版本,则运行报“Objects in this class cannot be updated outside an edit session ” 这个错误,一直不知道怎么解决,还请高手指点。
效果图:
主要实现代码:
文件上传功能实现:
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功能实现:
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的功能实现:
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; }
使用注意事项:在使用时要设置SdeServer、SdeInstance、SdeUser、SdePassword、SdeVerson、FeatureClass等属性。