ArcGIS Engine中加载数据

1.加载Shapefile数据
1 IWorkspaceFactorypWorkspaceFactory;
2 IFeatureWorkspacepFeatureWorkspace;
3 IFeatureLayerpFeatureLayer;
4
5 // 获取当前路径和文件名
6 OpenFileDialogdlg = new OpenFileDialog();
7 dlg.Filter = " Shape(*.shp)|*.shp|AllFiles(*.*)|*.* " ;
8 dlg.Title = " OpenShapefiledata " ;
9 dlg.ShowDialog();
10 string strFullPath = dlg.FileName;
11 if (strFullPath == "" ) return ;
12 int Index = strFullPath.LastIndexOf( " \\ " );
13 string filePath = strFullPath.Substring( 0 ,Index);
14 string fileName = strFullPath.Substring(Index + 1 );
15
16 // 打开工作空间并添加shp文件
17 pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
18 pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(filePath, 0 );
19 pFeatureLayer = new FeatureLayerClass();
20
21 pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);
22 pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
23 axMapControl1.Map.AddLayer(pFeatureLayer);
24 axMapControl1.ActiveView.Refresh();

2.加载栅格数据
1 IWorkspaceFactorypWorkspaceFactory;
2 IRasterWorkspacepRasterWorkspace;
3
4 OpenFileDialogdlg = new OpenFileDialog();
5 dlg.Filter = " LayerFile(*.lyr)|*.jpg;*.bmp;*.tiff " ;
6 dlg.Title = " OpenRasterDataFile " ;
7 dlg.ShowDialog();
8 // 获取当前路径和文件名
9 string strFullPath = dlg.FileName;
10 if (strFullPath == "" ) return ;
11 int Index = strFullPath.LastIndexOf( " \\ " );
12 string fileName = strFullPath.Substring(Index + 1 );
13 string filePath = strFullPath.Substring( 0 ,Index);
14
15 pWorkspaceFactory = new RasterWorkspaceFactoryClass();
16 pRasterWorkspace = (IRasterWorkspace)pWorkspaceFactory.OpenFromFile(filePath, 0 );
17 IRasterDatasetpRasterDataset = (IRasterDataset)pRasterWorkspace.OpenRasterDataset(fileName);
18 IRasterLayerpRasterLayer = new RasterLayerClass();
19 pRasterLayer.CreateFromDataset(pRasterDataset);
20 axMapControl1.Map.AddLayer(pRasterLayer);
21 axMapControl1.ActiveView.Refresh();

3.加载CAD数据
1 IWorkspaceFactorypWorkspaceFactory;
2 IFeatureWorkspacepFeatureWorkspace;
3 IFeatureLayerpFeatureLayer;
4 IFeatureDatasetpFeatureDataset;
5 // 获取当前路径和文件名
6 OpenFileDialogdlg = new OpenFileDialog();
7 dlg.Filter = " CAD(*.dwg)|*.dwg|AllFiles(*.*)|*.* " ;
8 dlg.Title = " OpenCADDatafile " ;
9 dlg.ShowDialog();
10 string strFullPath = dlg.FileName;
11 if (strFullPath == "" ) return ;
12 int Index = strFullPath.LastIndexOf( " \\ " );
13 string filePath = strFullPath.Substring( 0 ,Index);
14 string fileName = strFullPath.Substring(Index + 1 );
15 // 打开CAD数据集
16 pWorkspaceFactory = new CadWorkspaceFactoryClass();
17 pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(filePath, 0 );
18 // 打开一个要素集
19 pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName);
20 // IFeaturClassContainer可以管理IFeatureDataset中的每个要素类
21 IFeatureClassContainerpFeatClassContainer = (IFeatureClassContainer)pFeatureDataset;
22 // 对CAD文件中的要素进行遍历处理
23 for ( int i = 0 ;i < pFeatClassContainer.ClassCount - 1 ;i ++ )
24 {
25IFeatureClasspFeatClass=pFeatClassContainer.get_Class(i);
26if(pFeatClass.FeatureType==esriFeatureType.esriFTCoverageAnnotation)
27//如果是注记,则添加注记层
28pFeatureLayer=newCadAnnotationLayerClass();
29else//如果是点、线、面,则添加要素层
30{
31pFeatureLayer=newFeatureLayerClass();
32pFeatureLayer.Name=pFeatClass.AliasName;
33pFeatureLayer.FeatureClass=pFeatClass;
34axMapControl1.Map.AddLayer(pFeatureLayer);
35axMapControl1.ActiveView.Refresh();
36}

37}
4.加载PersonGeodatabase数据
1 IWorkspaceFactorypAccessWorkspaceFactory;
2 IFeatureWorkspacepFeatureWorkspace;
3 IFeatureLayerpFeatureLayer;
4 IFeatureDatasetpFeatureDataset;
5
6 // 获取当前路径和文件名
7 OpenFileDialogdlg = new OpenFileDialog();
8 dlg.Filter = " PersonalGeodatabase(*.mdb)|*.mdb|AllFiles(*.*)|*.* " ;
9 dlg.Title = " OpenPersonGeodatabasefile " ;
10 dlg.ShowDialog();
11 string strFullPath = dlg.FileName;
12 if (strFullPath == "" ) return ;
13
14 // 打开personGeodatabase,并添加图层
15 pAccessWorkspaceFactory = new AccessWorkspaceFactoryClass();
16 // 打开工作空间并遍历数据集
17 IWorkspacepWorkspace = pAccessWorkspaceFactory.OpenFromFile(strFullPath, 0 );
18 IEnumDatasetpEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
19 pEnumDataset.Reset();
20 IDatasetpDataset = pEnumDataset.Next();
21 // 如果数据集是IFeatureDataset,则遍历它下面的子类
22 if (pDataset is IFeatureDataset)
23 {
24pFeatureWorkspace=(IFeatureWorkspace)pAccessWorkspaceFactory.OpenFromFile(strFullPath,0);
25pFeatureDataset=pFeatureWorkspace.OpenFeatureDataset(pDataset.Name);
26IEnumDatasetpEnumDataset1=pFeatureDataset.Subsets;
27pEnumDataset1.Reset();
28IDatasetpDataset1=pEnumDataset1.Next();
29//如果子类是FeatureClass,则添加到axMapControl1中
30if(pDataset1isIFeatureClass)
31{
32pFeatureLayer=newFeatureLayerClass();
33pFeatureLayer.FeatureClass=pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
34pFeatureLayer.Name=pFeatureLayer.FeatureClass.AliasName;
35axMapControl1.Map.AddLayer(pFeatureLayer);
36axMapControl1.ActiveView.Refresh();
37}

38else
39{
40MessageBox.Show("NoFeatureLayer!");
41}

42}

43 else
44 {
45pFeatureWorkspace=(IFeatureWorkspace)pWorkspace;
46pFeatureLayer=newFeatureLayerClass();
47pFeatureLayer.FeatureClass=pFeatureWorkspace.OpenFeatureClass("图层名");
48pFeatureLayer.Name=pFeatureLayer.FeatureClass.AliasName;
49axMapControl1.Map.AddLayer(pFeatureLayer);
50axMapControl1.ActiveView.Refresh();
51}
5.加载SDE数据库数据
1 public ESRI.ArcGIS.Geodatabase.IWorkspacearcSDEWorkspaceOpen(Stringserver,Stringinstance,Stringuser,Stringpassword,Stringdatabase,Stringversion)
2 {
3try
4{
5//Createandpopulatethepropertyset
6ESRI.ArcGIS.esriSystem.IPropertySetpropertySet=newESRI.ArcGIS.esriSystem.PropertySetClass();
7propertySet.SetProperty("SERVER",server);
8propertySet.SetProperty("INSTANCE",instance);
9propertySet.SetProperty("DATABASE",database);
10propertySet.SetProperty("USER",user);
11propertySet.SetProperty("PASSWORD",password);
12propertySet.SetProperty("VERSION",version);
13
14ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2workspaceFactory;
15workspaceFactory=(ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)newSdeWorkspaceFactoryClass();
16returnworkspaceFactory.Open(propertySet,0);
17}

18catch(Exceptione)
19{
20thrownewException(String.Format("arcSDEWorkspaceOpen:{0}",e.Message),e);
21}

22}

23
24 IWorkspacepWorkspace = arcSDEWorkspaceOpen( " 计算机名 " , " 5151 " , "数据库 用户 " , " 密码 " , "" , " sde.DEFAULT " );
25
26 IFeatureWorkspacepFeattureWS = (IFeatureWorkspace)pWorkspace;
27 IFeatureClasspFeatureClass = pFeattureWS.OpenFeatureClass( "图层名 " );
28 IFeatureLayerpFeatureLayer = new FeatureLayerClass();
29 pFeatureLayer.FeatureClass = pFeatureClass;
30
31 axMapControl1.AddLayer(pFeatureLayer);
32 axMapControl1.Refresh();

你可能感兴趣的:(Engine,arcgis)