AE中shp文件的加載
步驟:
1) 創建工作空間工廠
2) 打開shapefile工作空間
3) 打開要素類
4) 創建要素圖層
5) 關聯圖層和要素類
6) 添加到地圖空間
具體代碼(後面數字爲對應的步驟):
IWorkspaceFactory pWorkspaceFactory=new ShapefileWorkspaceFactory (); // 1
openFileDialog1.Filter="shaperfile(*.shp)|*.shp";
openFileDialog1.InitialDirectory=@"E:\test\文檔和數據\Data";
openFileDialog1.Multiselect=false;
DialogResult pDialogResult=openFileDialog1.ShowDialog ();
if(pDialogResult !=DialogResult.OK)
return;
string pPath=openFileDialog1 .FileName;
string pFolder=Path.GetDirectoryName (pPath);
string pFileName=Path.GetFileName(pPath);
IWorkspace pWorkspace=pWorkspaceFactory .OpenFromFile(pFolder ,0); // 2
IFeatureWorkspace pFeatureWorkspace =pWorkspace as IFeatureWorkspace ;
IFeatureClass pFC=pFeatureWorkspace .OpenFeatureClass (pFileName ); //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 ();
-----------------------------------------------------------------------------------------------------------
//添加ArcGIS命名空間
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesRaster;
//存儲打開文件的全路徑
string fullFilePath;
//設置OpenFileDialog的屬性,使其能打開多種類型文件
OpenFileDialog penFile = new OpenFileDialog();
openFile.Filter = "shape文件(*.shp)|*.shp|";
openFile.Filter +="柵格數據(*.jpg,*.bmp,*.tiff)|*.jpg;*.bmp;*.tiff|";
openFile.Filter +="地圖文檔(*.mxd,*.mxt,*.jmf)|*.mxd;*.mxt;*.jmf";
openFile.Title = "打開文件";
try
{
if (openFile.ShowDialog() == DialogResult.OK)
{
fullFilePath = openFile.FileName;
//獲得文件路徑
int index = fullFilePath.LastIndexOf("\\");
string filePath = fullFilePath.Substring(0, index);
//獲得文件名稱
string fileNam = fullFilePath.Substring(index + 1);
//加載shape文件
if (openFile.FilterIndex == 1)
{
//打開工作空間工廠
IWorkspaceFactory workspcFac = new ShapefileWorkspaceFactory();
IFeatureWorkspace featureWorkspc;
IFeatureLayer featureLay = new FeatureLayerClass();
//打開路徑
featureWorkspc = workspcFac.OpenFromFile(filePath, 0) as IFeatureWorkspace;
//打開類要素
featureLay.FeatureClass = featureWorkspc.OpenFeatureClass(fileNam);
//清空圖層
axMapControl1.ClearLayers();
//添加圖層
axMapControl1.AddLayer(featureLay);
axMapControl1.Refresh();
}
//加載柵格圖像
else if (openFile.FilterIndex == 2)
{
IWorkspaceFactory workspcFac = new RasterWorkspaceFactory();
IRasterWorkspace rasterWorkspc;
IRasterDataset rasterDatst = new RasterDatasetClass();
IRasterLayer rasterLay = new RasterLayerClass();
rasterWorkspc = workspcFac.OpenFromFile(filePath, 0) as IRasterWorkspace;
rasterDatst = rasterWorkspc.OpenRasterDataset(fileNam );
rasterLay.CreateFromDataset(rasterDatst);
axMapControl1.ClearLayers();
axMapControl1.AddLayer(rasterLay);
axMapControl1.Refresh();
}
//加載地圖文檔
else if (openFile.FilterIndex == 3)
{
IMapDocument mapDoc = new MapDocumentClass();
mapDoc.Open(filePath ,"");
axMapControl1.ClearLayers();
for (int i = 0; i < mapDoc.MapCount - 1; i++)
{
axMapControl1.Map =mapDoc.get_Map (i);
}
IActiveView activeViw = axMapControl1.Map as IActiveView;
activeViw.Extent = axMapControl1.FullExtent;
axMapControl1.Refresh();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString ());
}
}
}
原文鏈接:
http://hi.baidu.com/galileo0405/blog/item/6689be922a431381a977a4bd.html