/// <summary>
/// shape数据加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuFile_4_1_Click(object sender, System.EventArgs e)
{
//this.openFileDialog1.FilterIndex =1;
this.openFileDialog1.Filter="ShapeFile(*.shp)|*.shp";
//定义文件名称
string filePath=null;
if(this.openFileDialog1.ShowDialog()==DialogResult.OK )
{
filePath=this.openFileDialog1.FileName;
}
else
{
return;
}
//如果shape文件名称不为空则加载文档数据
if(filePath!=null && filePath.Length >0)
{
string pathToWorkspace = System.IO.Path.GetDirectoryName(filePath) ;
string shapefileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
//============================================================================
//获取FeatureClass
IFeatureClass pFClass=null;
IFeatureWorkspace pFWS;
try
{
IWorkspaceFactory pWSFact=new ShapefileWorkspaceFactoryClass();
//打开shape文件所在的工作空间
IWorkspace pWS=pWSFact.OpenFromFile(pathToWorkspace,0);
//转换为IFeatureWorkspace对象
pFWS = pWS as IFeatureWorkspace;
//根据shape文件名称打开shape文件
pFClass=pFWS.OpenFeatureClass(shapefileName);
//获取IFeatureLayer对象
IFeatureLayer pLayer=new FeatureLayerClass();
pLayer.FeatureClass=pFClass;
//
IDataset pDataset=pFClass as IDataset;
pLayer.Name=pDataset.Name;
//将图层加入Map对象中
this.axMapControl1.AddLayer(pLayer,0);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
浙公网安备 33010602011771号