读取gdb方法
private void btn_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.Description = "打开GDB文件夹";
if (DialogResult.OK == dlg.ShowDialog())
{
if (Directory.Exists(dlg.SelectedPath))
{
if(dlg.SelectedPath.ToUpper().Contains(".GDB"))
{
GDBTextBox.Text = dlg.SelectedPath;
}
}
}
}
#region 直接获取FeatureClass
string filePath = GDBTextBox.Text;
FileGDBWorkspaceFactoryClass fac=new FileGDBWorkspaceFactoryClass();
IFeatureWorkspace space = (IFeatureWorkspace)fac.OpenFromFile(filePath, 0);
IFeatureClass pPointFClass = space.OpenFeatureClass("zhongxindian"); //获取FeatureClass
#endregion
#region 添加到comboBox里面、获取FeatureClass
IWorkspaceFactory m_pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
IWorkspace pWorkspace = m_pWorkspaceFactory.OpenFromFile(GDBTextBox.Text, 0);
IFeatureWorkspace m_pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IEnumDatasetName FeatureEnumDatasetName = pWorkspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass);
if (FeatureEnumDatasetName == null) return;
FeatureEnumDatasetName.Reset();
IDatasetName pDatasetName = FeatureEnumDatasetName.Next();
while (pDatasetName != null)
{
tcCombox.Items.Add(pDatasetName.Name);//添加到comboBox里面
pDatasetName = FeatureEnumDatasetName.Next();
}
IFeatureClass m_pFeatureClass2=null;
private void tcCombox_SelectedIndexChanged(object sender, EventArgs e)
{
m_pFeatureClass2 = m_pFeatureWorkspace.OpenFeatureClass(tcCombox.Text);//comboBox里选择图层,获取FeatureClass
}
#endregion
#region 遍历GDB
IWorkspace pCarWorkspace = m_pWorkspaceFactory.OpenFromFile(GDBTextBox.Text, 0);
IEnumDataset FeatureEnumDataset = pCarWorkspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
if (FeatureEnumDataset == null) return;
FeatureEnumDataset.Reset();
IDataset pDataset = FeatureEnumDataset.Next();
while (pDataset != null)
{
IFeatureClass fc = pDataset as IFeatureClass;//获取FeatureClass
//对FeatureClass的操作
//比如给FeatureClass添加字段、代码如下
if (pPolygonFClass.Fields.FindField("字段") > -1) return;//如果存在字段
IClass pTable = pPolygonFClass as IClass;
IFieldEdit pFieldEdit = new FieldClass();
pFieldEdit.Name_2 = "字段";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
pTable.AddField((IField)pFieldEdit);
int countIndex = pPolygonFClass.Fields.FindField("字段");//获取字段索引
pDataset = FeatureEnumDataset.Next();
}
#endregion
#region 遍历FeatureClass
IFeatureCursor Cur = fc.Search(null, false);//null可替换为QueryFilter
IFeature pFeature = Cur.NextFeature();
while (pFeature != null)
{
//对Feature进行操作
pFeature = Cur.NextFeature();
}
读取shapefile
private void btn_Click(object sender, EventArgs e)
{
OpenFileDialog fileDlg = new OpenFileDialog();
fileDlg.Filter = "Shape File | *.shp";
fileDlg.Multiselect = false;
if (fileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
IWorkspaceFactory pWorkSpaceFactory = new ShapefileWorkspaceFactory();
IWorkspace pWorkSpace = pWorkSpaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(fileDlg.FileName), 0);
IFeatureWorkspace pFeatureWorkspace = pWorkSpace as IFeatureWorkspace;
IFeatureClass layPolygon = pFeatureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(fileDlg.FileName));//获取FeatureClass
//_rowCount = (layPolygon as ITable).RowCount(null);//获取FeatureClass数量
shapefileTextBox.Text = fileDlg.FileName;//在TextBox中显示
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
}