feature class上传sde数据库

功能:远程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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值