shape文件的读取和展示
程序员文章站
2023-12-26 17:25:39
...
//选择shape文件
public string[] OpenShapeFile()
{
string[] ShpFile = new string[2];
OpenFileDialog OpenShpFile = new OpenFileDialog();
OpenShpFile.Title = "打开Shape文件";
OpenShpFile.InitialDirectory = "E:";
OpenShpFile.Filter = "Shape文件(*.shp)|*.shp";
if (OpenShpFile.ShowDialog() == DialogResult.OK)
{
string ShapPath = OpenShpFile.FileName;
//利用"\\"将文件路径分成两部分
int Position = ShapPath.LastIndexOf("\\");
string FilePath = ShapPath.Substring(0, Position);
string ShpName = ShapPath.Substring(Position + 1);
ShpFile[0] = FilePath;
ShpFile[1] = ShpName;
}
return ShpFile;
}
//读取shape文件,新建图层,并显示在地图
private void button2_Click(object sender, EventArgs e)
{
// IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); // 1
// IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); // 1
string[] ShpFile = OpenShapeFile();
string FilePath = ShpFile[0];
string ShpName = ShpFile[1];
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(FilePath, 0); // 2
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(ShpName); //3
IFeatureLayer pFLayer = new FeatureLayerClass(); // 4
pFLayer.FeatureClass = pFC;
pFLayer.Name = pFC.AliasName; // 5
ILayer pLayer = pFLayer as ILayer;
IMap pMap = axMapControl1.Map;
pMap.AddLayer(pLayer); // 6
axMapControl1.ActiveView.Refresh();
}