//添加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 openFile = 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 ());
}
}
}
引文来源
C# +AE加载shp、栅格、地图文档数据 -ArcGIS家园
<script type="text/javascript" id="wumiiRelatedItems"> </script>