ArcgisEngine大神之路之一:shp文件的加载与字段获取
上班了以后,以前学校计算机基础知识不扎实的毛病就显露出来了,现在学习AE的二次开发,折腾了大半天,不断看资料,没办法,要成为一个大神,就必须懂得如何学习别人的代码,现在总算是搞明白了shp文件如何打开以及获取字段值。现将代码附下:
OpenFileDialog pOpenFileDialog = new OpenFileDialog();
pOpenFileDialog.CheckFileExists = true;//当文件不存在时发出警告
pOpenFileDialog.Title = "打开Shape文件";
pOpenFileDialog.Filter = "Shape文件(*.shp)|*.shp";
pOpenFileDialog.ShowDialog();
txtLayerFile.Text = pOpenFileDialog.FileName;
//如果未选择任何文件,则直接返回程序,不再进行下一步操作
if (pOpenFileDialog.FileName == "")
return;
string pFullPath = txtLayerFile.Text;//全路径
int index = pFullPath.LastIndexOf("\");
string filePath=pFullPath.Substring(0,index);//路径
string fileName = pFullPath.Substring( index);//文件名
String txtFieldText = "";//字段列表中要显示的内容
//创建ShapfielWrokspaceFactory实例
ShapefileWorkspaceFactory pShapefile = new ShapefileWorkspaceFactory();
//打开shp文件并转换为IFeatureWrokspace
IFeatureWorkspace featureWorkspace = pShapefile.OpenFromFile(filePath,0) as IFeatureWorkspace;
//创建要素图层实例
IFeatureLayer featureLayer = new FeatureLayerClass();
//创建要素类实例并将要素类赋值给要素图层的要素类属性
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(fileName);
featureLayer.FeatureClass = featureClass;
int num = featureClass.Fields.FieldCount;
for (int i = 0; i < num; i++)
{
txtFieldText = txtFieldText + featureClass.Fields.get_Field(i).Name + "\r\n";
}
txtField.Text = txtFieldText;