arcgis engine中添加几种数据的加载方法

  • Post author:
  • Post category:其他


基于arcgis engine的二次开发,首先要根据需求添加相应的数据,然后在进行功能操作,现在列出五种常用的数据的加载方法。

一、mxd文件的添加

IMapDocument mapDocument;
private void FileOpen_Click(object sender, EventArgs e)
{
    mapDocument = new ESRI.ArcGIS.Carto.MapDocumentClass();
    try
    {
        System.Windows.Forms.OpenFileDialog openFileDialog;
        openFileDialog = new OpenFileDialog();
        openFileDialog.Title = "打开地图文档";
        openFileDialog.Filter = "map documents(*.mxd)|*.mxd";
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
           string filePath = openFileDialog.FileName;
           mapDocument.Open(filePath, "");
           for (int i = 0; i < mapDocument.MapCount; i++)
           {
                axMapControl1.Map = mapDocument.get_Map(i);
           }
                axMapControl1.Refresh();
        }
        else
        {
               mapDocument = null;
        }
   }
    catch (Exception ex)
    {
             MessageBox.Show(ex.ToString());
    }

}

二、shp文件的添加

OpenFileDialog pOpenFileDialog = new OpenFileDialog();
pOpenFileDialog.CheckFileExists = true;
pOpenFileDialog.Title = "打开Shape文件";
pOpenFileDialog.Filter = "Shape文件(*.shp)|*.shp";
pOpenFileDialog.ShowDialog();
IWorkspaceFactory pWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
string pFullPath = pOpenFileDialog.FileName;
if (pFullPath == "") return;
int pIndex = pFullPath.LastIndexOf("\\");
string pFilePath = pFullPath.Substring(0, pIndex); //文件路径
string pFileName = pFullPath.Substring(pIndex + 1); //文件名
//实例化ShapefileWorkspaceFactory工作空间,打开Shape文件
pWorkspaceFactory = new ShapefileWorkspaceFactory();
pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(pFilePath, 0);
//创建并实例化要素集
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(pFileName);
pFeatureLayer = new FeatureLayer();
pFeatureLayer.FeatureClass = pFeatureClass;
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
//ClearAllData();    //新增删除数据
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh(); 

三、栅格数据的添加

OpenFileDialog pOpenFileDialog = new OpenFileDialog();
pOpenFileDialog.CheckFileExists = true;
pOpenFileDialog.Title = "打开Raster文件";
pOpenFileDialog.Filter = "栅格文件 (*.*)|*.bmp;*.tif;*.jpg;*.img|(*.bmp)|*.bmp|(*.tif)|*.tif|(*.jpg)|*.jpg|(*.img)|*.img";
pOpenFileDialog.ShowDialog();
string pRasterFileName = pOpenFileDialog.FileName;
if (pRasterFileName == "")
{
     return;
}
string pPath = System.IO.Path.GetDirectoryName(pRasterFileName);
string pFileName = System.IO.Path.GetFileName(pRasterFileName);

IWorkspaceFactory pWorkspaceFactory = new RasterWorkspaceFactory();
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pPath, 0);
IRasterWorkspace pRasterWorkspace = pWorkspace as IRasterWorkspace;
IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(pFileName);
 //影像金字塔判断与创建
IRasterPyramid3 pRasPyrmid;
pRasPyrmid = pRasterDataset as IRasterPyramid3;
if (pRasPyrmid != null)
{
    if (!(pRasPyrmid.Present))
    {
        pRasPyrmid.Create(); //创建金字塔
    }
}
IRaster pRaster;
pRaster = pRasterDataset.CreateDefaultRaster();
IRasterLayer pRasterLayer;
pRasterLayer = new RasterLayerClass();
pRasterLayer.CreateFromRaster(pRaster);
ILayer pLayer = pRasterLayer as ILayer;
mainMapControl.AddLayer(pLayer, 0);

四、CAD文件的添加

IWorkspaceFactory pWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
IFeatureDataset pFeatureDataset;

OpenFileDialog pOpenFileDialog = new OpenFileDialog();
pOpenFileDialog.Filter = "CAD(*.dwg)|*.dwg";
pOpenFileDialog.Title = "打开CAD数据文件";
pOpenFileDialog.ShowDialog();

string pFullPath = pOpenFileDialog.FileName;
if (pFullPath == "")
{
     return;
}
//获取文件名和文件路径
int pIndex = pFullPath.LastIndexOf("\\");
string pFilePath = pFullPath.Substring(0, pIndex);
string pFileName = pFullPath.Substring(pIndex + 1);
//打开CAD数据集
pWorkspaceFactory = new CadWorkspaceFactoryClass(); //using ESRI.ArcGIS.DataSourcesFile;
pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(pFilePath, 0);
//打开一个要素集
pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(pFileName);
//IFeatureClassContainer可以管理IFeatureDataset中的每个要素类
IFeatureClassContainer pFeatClassContainer = (IFeatureClassContainer)pFeatureDataset;
//对CAD文件中的要素进行遍历处理
for (int i = 0; i < pFeatClassContainer.ClassCount; i++)
{
   IFeatureClass pFeatClass = pFeatClassContainer.get_Class(i);
   //如果是注记,则添加注记层
   if (pFeatClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
   {
        pFeatureLayer = new CadAnnotationLayerClass();
        pFeatureLayer.Name = pFeatClass.AliasName;
        pFeatureLayer.FeatureClass = pFeatClass;
        axMapControl1.Map.AddLayer(pFeatureLayer);
   }
   else //如果是点、线、面则添加要素层
   {
        pFeatureLayer = new FeatureLayerClass();
        pFeatureLayer.Name = pFeatClass.AliasName;
        pFeatureLayer.FeatureClass = pFeatClass;
        axMapControl1.Map.AddLayer(pFeatureLayer);
   }
   axMapControl1.ActiveView.Refresh();
}

五、个人数据库(GeoDataBase)的添加

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();
      }
  }



版权声明:本文为u012972326原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。