首页 > 代码库 > AE中Shapefile文件添加到SDE数据集
AE中Shapefile文件添加到SDE数据集
linder_lee 原文 AE中Shapefile文件添加到SDE数据集(c#)
主要完成用C#,通过AE将本地Shapefile文件导入到SDE的指定数据集下面。
首先说下思路:
(1) 通过OpenFileDialog打开本地的Shp文件;
(2)通过获取的Shp文件创建FeatureClass,获取shp的字段 IFields,并判断图层类别;
(3)连接SDE中指定数据集,并创建新的FeatureClass;
(4)将shp文件对应的 FeatureClass里的Feature复制到SDE中新建的FeatureClass中。
接着说明下,开发过程中遇到的一些诡异问题:
(1) 在调用IFeatureDataset的CreateFeatureClass方法时,报错提示:The application is not licensed to create or modify schema for this type of data!
其解决方案 是:修改在form窗体中添加的axLicenseControl的属性,设置为ArcGIS Engine Enterprise...选项;并重新Update下ESRI的License文件。
同时一定要记住,要将除了ArcGIS Engine Enterprise Geo....外的其它选项的勾去掉。
最后为了记录下一天的成果,贴下代码:
(1)调用 OpenFileDialog
openFileDialog1.Filter = ("ShapeFile 文件(*.shp)|*.shp"); openFileDialog1.InitialDirectory = "D:\\project\\ThematicDataManager";//对话框目录 openFileDialog1.ShowDialog();
(2)打开shp文件
//打开ShapeFile文件 ShapefileWorkspaceFactory fileWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace featureWorkspce = fileWorkspaceFactory.OpenFromFile(strWorkspacePath, 0) as IFeatureWorkspace; IFeatureLayer featureLayer = new FeatureLayerClass(); IFeatureClass featureClass = featureWorkspce.OpenFeatureClass(strNewLayer); featureLayer.FeatureClass = featureClass;
(3)获取shp文件所有字段信息
IFields fields;// = featureClass.Fields; IEnumFieldError pFieldError; IFieldChecker pFieldChecker = new FieldCheckerClass(); pFieldChecker.ValidateWorkspace = featureWorkspce as IWorkspace; pFieldChecker.Validate(featureClass.Fields, out pFieldError, out fields);
(4)打开数据集(省略,前面已写过)
(5)创建新要素类
//创建新featureclass IFeatureClass pFCls = ztDataset.CreateFeatureClass(strtemp, fields, null, null, featureClass.FeatureType, featureClass.ShapeFieldName, "");
(6) 在新要素类中添加要素Feature(调用函数)
//追加要素,shp文件追加featureclass中(三个参数:shp文件,shp字段集,sde中FeatureClass文件) private void shp2fc(IFeatureClass featureClass, IFields sourceFeatureClassFields, IFeatureClass pFCls) { IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; IFeatureCursor pFeaCursor = pFCls.Insert(true); IFeatureBuffer pFeaBuffer = pFCls.CreateFeatureBuffer(); IField pField = new FieldClass(); IFeature pFeature = null; int iIndex = 0; long n = 0; for (int i = 0; i < featureClass.FeatureCount(queryFilter); i++) { pFeature = featureClass.GetFeature(i); try { pFeaBuffer = pFCls.CreateFeatureBuffer(); //添加字段值 for (int j = 0; j < sourceFeatureClassFields.FieldCount; j++) { pField = sourceFeatureClassFields.get_Field(j); iIndex = pFeaBuffer.Fields.FindField(pField.Name); if (iIndex != -1) { pFeaBuffer.set_Value(iIndex, pFeature.get_Value(j)); } } pFeaCursor.InsertFeature(pFeaBuffer); } catch (System.Exception ex) { MessageBox.Show("单要素写入异常!" + ex.Message, "提示"); return; } finally { n++; if (n % 100 == 0) { pFeaCursor.Flush(); } } pFeaCursor.Flush(); } }
到此,shp文件数据添加到Sde中就完成了。
AE中Shapefile文件添加到SDE数据集