一、shp文件的加载:
IWorkspaceFactory pWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
//获取当前路径和文件名
OpenFileDialog dlg=new OpenFileDialog();
dlg.ShowDialog();
string strFullPath=dlg.FileName;
if(strFullPath=="") return;
int Index=strFullPath.LastIndexOf("//");
string filePath=strFullPath.Substring(0,Index);
string fileName=strFullPath.Substring(Index+1);
//打开工作空间并添加shp文件
pWorkspaceFactory=new ShapefileWorkspaceFactoryClass();
//注意此处的路径是不能带文件名的
pFeatureWorkspace=(IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(filePath,0);
pFeatureLayer=new FeatureLayerClass();
//注意这里的文件名是不能带路径的
pFeatureLayer.FeatureClass=pFeatureWorkspace.OpenFeatureClass(fileName);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh();
二、个人数据库(personGeodatabase)的加载:
IWorkspaceFactory pWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
IFeatureDataset pFeatureDataset;
//获取当前路径和文件名
OpenFileDialog dlg=new OpenFileDialog();
dlg.ShowDialog();
string strFullPath=dlg.FileName;
if(strFullPath=="") return;
//打开personGeodatabase,并添加图层
IWorkspaceFactory pAccessWorkspaceFactory=new AccessWorkspaceFactoryClass();
//打开工作空间并遍历数据集
IWorkspace pWorkspace=pAccessWorkspaceFactory.OpenFromFile(strFullPath,0);
IEnumDataset pEnumDataset=pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
pEnumDataset.Reset();
IDataset pDataset=pEnumDataset.Next();
//如果数据集是IFeatureDataset,则遍历它下面的子类
if(pDataset is IFeatureDataset)
{
pFeatureWorkspace=(IFeatureWorkspace)pAccessWorkspaceFactory.OpenFromFile(strFullPath,0);
pFeatureDataset=pFeatureWorkspace.OpenFeatureDataset(pDataset.Name);
IEnumDataset pEnumDataset1=pFeatureDataset.Subsets;
pEnumDataset1.Reset();
IDataset pDataset1=pEnumDataset1.Next();
//如果子类是FeatureClass,则添加到axMapControl1中
if(pDataset1 is IFeatureClass)
{
pFeatureLayer=new FeatureLayerClass();
pFeatureLayer.FeatureClass=pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh();
}
}
三、CAD文件的加载:
IWorkspaceFactory pWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
IFeatureDataset pFeatureDataset;
//获取当前路径和文件名
OpenFileDialog dlg=new OpenFileDialog();
dlg.ShowDialog();
string strFullPath=dlg.FileName;
if(strFullPath=="") return;
int Index=strFullPath.LastIndexOf("//");
string filePath=strFullPath.Substring(0,Index);
string fileName=strFullPath.Substring(Index+1);
//打开CAD数据集
pWorkspaceFactory=new CadWorkspaceFactoryClass();
pFeatureWorkspace=(IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(filePath,0);
//打开一个要素集
pFeatureDataset=pFeatureWorkspace.OpenFeatureDataset(fileName);
//IFeaturClassContainer可以管理IFeatureDataset中的每个要素类
IFeatureClassContainer pFeatClassContainer=(IFeatureClassContainer)pFeatureDataset;
//对CAD文件中的要素进行遍历处理
for(int i=0;i
{
IFeatureClass pFeatClass=pFeatClassContainer.get_Class(i);
if(pFeatClass.FeatureType==esriFeatureType.esriFTCoverageAnnotation)
//如果是注记,则添加注记层
pFeatureLayer=new CadAnnotationLayerClass();
else
//如果是点、线、面,则添加要素层
pFeatureLayer=new FeatureLayerClass();
pFeatureLayer.Name=pFeatClass.AliasName;
pFeatureLayer.FeatureClass=pFeatClass;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh();
}
四、栅格数据的加载:
IWorkspaceFactory pWorkspaceFactory;
IRasterWorkspace pRasterWorkspace;
OpenFileDialog openFileDialog2=new OpenFileDialog();
openFileDialog2.Filter="Layer File(*.lyr)|*.jpg;*.bmp;*.tiff";
openFileDialog2.Title="Open Map File";
openFileDialog2.ShowDialog();
//获取当前路径和文件名
string strFullPath=openFileDialog2.FileName;
if(strFullPath=="") return;
int Index=strFullPath.LastIndexOf("//");
string fileName=strFullPath.Substring(Index+1);
string filePath=strFullPath.Substring(0,Index);
pWorkspaceFactory=new RasterWorkspaceFactoryClass();
pRasterWorkspace=(IRasterWorkspace)pWorkspaceFactory.OpenFromFile(filePath,0);
IRasterDataset pRasterDataset=(IRasterDataset)pRasterWorkspace.OpenRasterDataset(fileName);
IRasterLayer pRasterLayer= new RasterLayerClass();
pRasterLayer.CreateFromDataset(pRasterDataset);
axMapControl1.Map.AddLayer(pRasterLayer);