不同数据的导入
MXD文档导入
1 private void btnLoadMxFile_Click(object sender, EventArgs e) 2 { 3 OpenFileDialog pOpenFile = new OpenFileDialog(); 4 pOpenFile.CheckFileExists = true; 5 pOpenFile.Filter = "ArcMap文档(*.mxd)|*.mxd;|ArcMap模板(*.mxt)|*.mxt;|发布文件地图(*.pmf)|*.pmf"; 6 pOpenFile.Title = "打开地图文档"; 7 pOpenFile.Multiselect = false; 8 pOpenFile.RestoreDirectory = true; //保存打开路径 9 if (pOpenFile.ShowDialog() == DialogResult.OK) 10 { 11 string fileName = pOpenFile.FileName; 12 if (fileName == "") return; 13 if (mainMapControl.CheckMxFile(fileName)) 14 { 15 mainMapControl.ClearLayers(); 16 mainMapControl.LoadMxFile(fileName); //采用AxMapControl控件的LoadMxFile()方法添加MXD文档 17 } 18 else 19 { 20 MessageBox.Show(fileName + "是无效的地图文档!", "信息提示"); 21 return; 22 } 23 } 24 }
1 private void btnImapDocument_Click(object sender, EventArgs e) 2 { 3 OpenFileDialog pOpenFile = new OpenFileDialog(); 4 pOpenFile.CheckFileExists = true; 5 pOpenFile.Filter = "ArcMap文档(*.mxd)|*.mxd;|ArcMap模板(*.mxt)|*.mxt;|发布文件地图(*.pmf)|*.pmf"; 6 pOpenFile.Title = "打开地图文档"; 7 pOpenFile.Multiselect = false; 8 pOpenFile.RestoreDirectory = true; 9 if (pOpenFile.ShowDialog() == DialogResult.OK) 10 { 11 string fileName = pOpenFile.FileName; 12 if (fileName == "") return; 13 if (mainMapControl.CheckMxFile(fileName)) 14 { 15 IMapDocument pMapDoc = new MapDocument(); //采用IMapDocument接口实例的Open()方法打开MXD文档 16 pMapDoc.Open(fileName); 17 mainMapControl.Map = pMapDoc.ActiveView.FocusMap; 18 mainMapControl.ActiveView.Refresh(); 19 } 20 else 21 { 22 MessageBox.Show(fileName + "是无效的地图文档!", "信息提示"); 23 return; 24 } 25 } 26 }
Shp文档格式导入
1 private void btnAddShapeFile_Click(object sender, EventArgs e)
2 {
3 OpenFileDialog pOpenFile = new OpenFileDialog();
4 pOpenFile.CheckFileExists = true;
5 pOpenFile.Filter = "Shape文件(*.shp)|*.shp";
6 pOpenFile.Title = "打开shape文件";
7 pOpenFile.Multiselect = false;
8 pOpenFile.RestoreDirectory = true;
9 if (pOpenFile.ShowDialog() == DialogResult.OK)
10 {
11 string FullPath = pOpenFile.FileName;
12 int pIndex = FullPath.LastIndexOf("\\");
13 string pFilePath = FullPath.Substring(0, pIndex);//文件路径
14 string pFileName = FullPath.Substring(pIndex + 1);//文件名
15 mainMapControl.AddShapeFile(pFilePath, pFileName); //AddShapeFile(文件路径,文件名)
16 mainMapControl.ActiveView.Refresh();
17 }
18 }
栅格数据导入
1 private void btnAddRasterFile_Click(object sender, EventArgs e) 2 { 3 OpenFileDialog pFileDialog = new OpenFileDialog(); 4 pFileDialog.Title = "打开栅格数据"; 5 pFileDialog.Filter = "栅格数据(*.*)|*.img;*.bmp;*tif;*img|(*.img)|*.img|(*.tif)|*.tif;|(*.bmp)|*.bmp|(*.img)|*.img"; 6 pFileDialog.Multiselect = true;//对选 7 pFileDialog.CheckFileExists = true;//判断是否存在文件,返回bool 8 if (pFileDialog.ShowDialog() == DialogResult.OK) 9 { 10 //获取文件基本信息 11 string fullPath = pFileDialog.FileName; 12 int pIndex = fullPath.LastIndexOf("\\"); 13 string pFilePath = fullPath.Substring(0, pIndex); 14 string pFileName = fullPath.Substring(pIndex + 1); 15 //获取一个工作空间并转化为Raster栅格空间 16 IWorkspaceFactory pWorkspaceFac = new RasterWorkspaceFactoryClass(); 17 IWorkspace pWorkspace = pWorkspaceFac.OpenFromFile(pFilePath, 0);//打开的是路径 18 IRasterWorkspace pRasterWorkspace = pWorkspace as IRasterWorkspace; 19 IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(pFileName); 20 //金字塔判断与创建 21 IRasterPyramid3 pRasterPyramid; 22 pRasterPyramid = pRasterDataset as IRasterPyramid3; 23 if (pRasterPyramid != null)//判断文件是否存在 24 { 25 if (!pRasterPyramid.Present) 26 { pRasterPyramid.Create(); } 27 } 28 IRaster pRaster; 29 pRaster = pRasterDataset.CreateDefaultRaster(); 30 IRasterLayer pRasterLayer; 31 pRasterLayer = new RasterLayerClass(); 32 //pRasterLayer.CreateFromRaster(pRaster); 33 pRasterLayer.CreateFromDataset(pRasterDataset); 34 ILayer pLayer = pRasterLayer as ILayer; 35 mainMapControl.AddLayer(pLayer); 36 EagleEyeMapControl.AddLayer(pLayer); 37 mainMapControl.ActiveView.Refresh(); 38 EagleEyeMapControl.ActiveView.Refresh(); 39 } 40 }
CAD格式导入之单个要素图层加载
1 private void btnAddCadByLayer_Click(object sender, EventArgs e) 2 { 3 OpenFileDialog pOpenFileDia = new OpenFileDialog(); 4 pOpenFileDia.Title = "打开CAD数据文件"; 5 pOpenFileDia.Multiselect = true; 6 pOpenFileDia.Filter = "CAD(*.dwg)|*.dwg"; 7 pOpenFileDia.CheckFileExists = true; 8 if (pOpenFileDia.ShowDialog() == DialogResult.OK) 9 { 10 mainMapControl.ClearLayers(); 11 //string FullName=pOpenFileDia.FileName; 12 //int pIndex=FullName.LastIndexOf("\\"); 13 //string filePath=FullName.Substring(0,pIndex); 14 //string fileName=FullName.Substring(pIndex+1); 15 16 string filePath = System.IO.Path.GetDirectoryName(pOpenFileDia.FileName); //使用System.IO.Path直接获取路径名称和文件名 17 string fileName = System.IO.Path.GetFileName(pOpenFileDia.FileName); 18 19 IWorkspaceFactory pWorkspaceFac = new CadWorkspaceFactoryClass(); 20 IWorkspace pWorkspace = pWorkspaceFac.OpenFromFile(filePath, 0); 21 IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace; 22 23 IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName + ":Annotation"); //fileName是文件名,后面加上:Annotation/point/polyline/polygon是打开对应的注记、点、线、面文件 24 IFeatureLayer pFeatureLayer = new FeatureLayerClass(); //featureLayer可以实例化,它是要素类的表现形式 25 pFeatureLayer.FeatureClass = pFeatureClass; //featureClass是存储数据,通过直接获取无法实例化,每一个featureClass都有一个Geometry模型 26 pFeatureLayer.Name = fileName; 27 mainMapControl.AddLayer(pFeatureLayer); 28 mainMapControl.ActiveView.Refresh(); 29 EagleEyeMapControl.AddLayer(pFeatureLayer); 30 EagleEyeMapControl.ActiveView.Refresh(); 31 } 32 }
CAD格式导入之全体要素图层加载
1 private void btnAddWholeCad_Click(object sender, EventArgs e) 2 { 3 OpenFileDialog pOpenFileDia = new OpenFileDialog(); 4 pOpenFileDia.Title = "打开CAD数据文件"; 5 pOpenFileDia.Multiselect = true; 6 pOpenFileDia.Filter = "CAD(*.dwg)|*.dwg"; 7 pOpenFileDia.CheckFileExists = true; 8 if (pOpenFileDia.ShowDialog() == DialogResult.OK) 9 { 10 //打开CAD数据集 11 mainMapControl.ClearLayers(); 12 string pFilePath = System.IO.Path.GetDirectoryName(pOpenFileDia.FileName); 13 string pFileName = System.IO.Path.GetFileName(pOpenFileDia.FileName); 14 IWorkspaceFactory pWorkspaceFac = new CadWorkspaceFactoryClass(); 15 IFeatureWorkspace pFeatWorkspace = (IFeatureWorkspace)pWorkspaceFac.OpenFromFile(pFilePath, 0); 16 //打开一个要素集 17 IFeatureDataset pFeatDataset = pFeatWorkspace.OpenFeatureDataset(pFileName); 18 IFeatureClassContainer pFetClassContainer = (IFeatureClassContainer)pFeatDataset; 19 //利用FeatureClassContainer遍历所有要素 20 for (int i = 0; i < pFetClassContainer.ClassCount; i++) 21 { 22 IFeatureClass pFeatureClass = pFetClassContainer.get_Class(i); 23 if (pFeatureClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation) 24 { 25 IFeatureLayer pFeaturelayer = new FeatureLayer(); 26 pFeaturelayer.Name = pFeatureClass.AliasName; 27 pFeaturelayer.FeatureClass = pFeatureClass; 28 mainMapControl.AddLayer(pFeaturelayer); 29 } 30 else 31 { 32 IFeatureLayer pFeaturelayer = new FeatureLayer(); 33 pFeaturelayer.Name = pFeatureClass.AliasName; 34 pFeaturelayer.FeatureClass = pFeatureClass; 35 mainMapControl.AddLayer(pFeaturelayer); 36 } 37 mainMapControl.ActiveView.Refresh(); 38 } 39 } 40 }
加载个人地理数据库中的数据集
1 private void btnAddPersonalGeodatabase_Click(object sender, EventArgs e) 2 { 3 OpenFileDialog pOpenFileDia = new OpenFileDialog(); 4 pOpenFileDia.Title = "打开mdb数据文件"; 5 pOpenFileDia.Multiselect = true; 6 pOpenFileDia.Filter = "Personal Geodatabase(*.mdb)|*.mdb"; 7 pOpenFileDia.CheckFileExists = true; 8 if (pOpenFileDia.ShowDialog() == DialogResult.OK) 9 { 10 //打开mdb数据集,引用DataBaseGDB 11 mainMapControl.ClearLayers(); 12 string filePath = System.IO.Path.GetDirectoryName(pOpenFileDia.FileName); 13 string fileName = System.IO.Path.GetFileName(pOpenFileDia.FileName); 14 IWorkspaceFactory pWorkspaceFac = new AccessWorkspaceFactoryClass(); 15 IWorkspace pWorkspace = pWorkspaceFac.OpenFromFile(pOpenFileDia.FileName, 0); 16 AddAllData.AddAllDataset(pWorkspace, mainMapControl); 17 } 18 }
加载文件型地理数据库的数据集
1 private void btnAddFiledatabase_Click(object sender, EventArgs e) 2 { 3 FolderBrowserDialog dia = new FolderBrowserDialog(); 4 if (dia.ShowDialog() != DialogResult.OK) return; 5 string fullPath = dia.SelectedPath; 6 if (fullPath == "") return; 7 IWorkspaceFactory pWorkSpaceFac = new FileGDBWorkspaceFactory(); 8 IWorkspace pWorkspace = (IWorkspace)pWorkSpaceFac.OpenFromFile(fullPath, 0); 9 AddAllData.AddAllDataset(pWorkspace, mainMapControl); 10 AddAllData.AddAllDataset(pWorkspace, EagleEyeMapControl); 11 }
AddAllDataset()方法可供多种数据调用
class AddAllData { public static void AddAllDataset(IWorkspace pWorkspace, AxMapControl mapControl) { IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny); pEnumDataset.Reset(); IDataset pDataset = pEnumDataset.Next(); while (pDataset != null) { if (pDataset is IFeatureDataset) //要素数据集 { IFeatureWorkspace pFeatWorkspace = (IFeatureWorkspace)pWorkspace; IFeatureDataset pFeatureDataset = pFeatWorkspace.OpenFeatureDataset(pDataset.Name); IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets; pEnumDataset1.Reset(); IGroupLayer pGroupLayer = new GroupLayerClass(); pGroupLayer.Name = pFeatureDataset.Name; IDataset pDataset1 = pEnumDataset1.Next(); while (pDataset1 != null) { if (pDataset1 is IFeatureClass) { // IFeatureClass pFeatureClass = (IFeatureClass)pDataset1; IFeatureLayer pFeatLayer = new FeatureLayerClass(); pFeatLayer.FeatureClass = pFeatWorkspace.OpenFeatureClass(pDataset1.Name); if (pFeatLayer.FeatureClass != null) { pFeatLayer.Name = pFeatLayer.FeatureClass.AliasName; pGroupLayer.Add(pFeatLayer); mapControl.Map.AddLayer(pFeatLayer); } } pDataset1 = pEnumDataset1.Next(); } } else if (pDataset is IFeatureClass) //要素类 { IFeatureWorkspace pFeatWorkspace = (IFeatureWorkspace)pWorkspace; IFeatureLayer pFeatLayer = new FeatureLayerClass(); pFeatLayer.FeatureClass = pFeatWorkspace.OpenFeatureClass(pDataset.Name); pFeatLayer.Name = pFeatLayer.FeatureClass.AliasName;//pFeatWorkspace.OpenFeatureClass(pDataset.Name).AliasName; mapControl.Map.AddLayer(pFeatLayer); } else if (pDataset is IRasterDataset) //栅格数据集 { IRasterWorkspaceEx pRasterWorkspace = (IRasterWorkspaceEx)pWorkspace; IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(pDataset.Name); IRasterPyramid3 pRasPyramid; pRasPyramid = pRasterDataset as IRasterPyramid3; if (pRasPyramid != null) { if (!(pRasPyramid.Present)) { pRasPyramid.Create(); } } IRasterLayer pRasLayer = new RasterLayerClass(); pRasLayer.CreateFromDataset(pRasterDataset); ILayer pLayer = pRasLayer as ILayer; mapControl.Map.AddLayer(pLayer); } pDataset = pEnumDataset.Next(); } mapControl.ActiveView.Refresh(); } }
总结:
加载空间数据主要分为5个步骤:
1、创建数据对应的工作空间工厂(WorkSpaceFactory)
2、使用WorkSpaceFactory创建要要加载数据的工作空间(WorkSpace)
3、使用WorkSpace打开并得到图层的数据集DataSet
4、将DataSet赋值给新建图层的数据源
5、添加图层到MapControl中显示
ps:工作空间(WorkSpace)对象,在物理级别上相当于地理数据库本身。