打开shapfile,raster文件

打开shapfile文件,代码如下
 1    // 打开工作空间需要对应的工作空间工厂
 2             IWorkspaceFactory PWorkSpaceFactory  =   new  ShapefileWorkspaceFactory();
 3
 4             IFeatureWorkspace PFeatureWorkSpace;
 5             IFeatureLayer PFeatureLayer  =   new  FeatureLayerClass();
 6             OpenFileDialog Open  =   new  OpenFileDialog();
 7
 8             Open.Title  =   " Select File " ;
 9             Open.Filter  =   " shapefiel(.shp)|*.shp " ;
10
11             Open.ShowDialog();
12
13              string  strFullPath  =  Open.FileName;
14              if  (strFullPath  ==   "" return ;
15
16              // 提取文件名和打开路径
17              int  index  =  strFullPath.LastIndexOf( " \\ " );
18              string  filepath  =  strFullPath.Substring( 0 , index);
19              string  filename  =  strFullPath.Substring(index  +   1 );
20
21              // 设置shapefile工作空间,即打开路径
22             PFeatureWorkSpace  =  (IFeatureWorkspace)PWorkSpaceFactory.OpenFromFile(filepath,  0 );
23
24              // 打开一个类要素
25             PFeatureLayer.FeatureClass  =  PFeatureWorkSpace.OpenFeatureClass(filename);
26              // PFeatureLayer.Name = PFeatureLayer.FeatureClass.AliasName;
27
28              // 首先清空已有图层
29             axMapControl1.Map.ClearLayers();
30
31              // 添加图层
32             axMapControl1.Map.AddLayer(PFeatureLayer);
33             axMapControl1.Refresh();


打开Raster文件
 1 IWorkspaceFactory PWorkSpaceFactory  =   new  RasterWorkspaceFactory();
 2             IRasterWorkspace PRasterWorkSpace;
 3             IRasterDataset PRasterDataset  =   new  RasterDatasetClass();
 4             IRasterLayer PRasterlayer  =   new  RasterLayerClass();
 5             OpenFileDialog Open  =   new  OpenFileDialog();
 6
 7             Open.Title  =   " Select File " ;
 8             Open.Filter  =   " Layer File(*.lyr)|*.jpg;*.bmp;*.tiff " ;
 9
10             Open.ShowDialog();
11              string  strFullPath  =  Open.FileName;
12              if  (strFullPath  ==   "" return ;
13
14              // 提取栅格数据集名称和打开路径
15              int  index  =  strFullPath.LastIndexOf( " \\ " );
16              string  filepath  =  strFullPath.Substring( 0 , index);
17              string  filename  =  strFullPath.Substring(index  +   1 );
18
19             PRasterWorkSpace  =  (IRasterWorkspace)PWorkSpaceFactory.OpenFromFile(filepath, 0 );
20             PRasterDataset  =  (IRasterDataset)PRasterWorkSpace.OpenRasterDataset(filename);
21
22              // 初始化一个新的Rasterlayer有三种方式CreateFromDataset,CreateFromFilePath,CreateFromRaster
23
24              // CreateFromDataset方式
25             PRasterlayer.CreateFromDataset(PRasterDataset);
26
27              // CreateFromFilePath方式
28              // PRasterlayer.CreateFromFilePath(strFullPath);
29
30             axMapControl1.Map.ClearLayers();
31             axMapControl1.AddLayer(PRasterlayer);
32             axMapControl1.Refresh();

其他的类型文件,如Coverage,Geodatabase,CAD,等等,原理类似,改天贴上来^_^

你可能感兴趣的:(pfile)