码迷,mamicode.com
首页 > 其他好文 > 详细

ArcGIS Engine中加载数据

时间:2014-07-29 11:18:56      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:blog   http   os   文件   数据   io   for   art   

        ArcGIS Engine中加载数据

 
分类: AE开发积累

1.加载Shapefile数据

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


2.加载栅格数据

1bubuko.com,布布扣 IWorkspaceFactory pWorkspaceFactory;
2bubuko.com,布布扣 IRasterWorkspace pRasterWorkspace;
3bubuko.com,布布扣
4bubuko.com,布布扣 OpenFileDialog dlg = new OpenFileDialog();
5bubuko.com,布布扣 dlg.Filter = "Layer File(*.lyr)|*.jpg;*.bmp;*.tiff";
6bubuko.com,布布扣 dlg.Title = "Open Raster Data File";
7bubuko.com,布布扣 dlg.ShowDialog();
8bubuko.com,布布扣 //获取当前路径和文件名
9bubuko.com,布布扣 string strFullPath = dlg.FileName;
10bubuko.com,布布扣 if (strFullPath == "") return;
11bubuko.com,布布扣 int Index = strFullPath.LastIndexOf("//");
12bubuko.com,布布扣 string fileName = strFullPath.Substring(Index + 1);
13bubuko.com,布布扣 string filePath = strFullPath.Substring(0, Index);
14bubuko.com,布布扣
15bubuko.com,布布扣 pWorkspaceFactory = new RasterWorkspaceFactoryClass();
16bubuko.com,布布扣 pRasterWorkspace = (IRasterWorkspace)pWorkspaceFactory.OpenFromFile(filePath, 0);
17bubuko.com,布布扣 IRasterDataset pRasterDataset = (IRasterDataset)pRasterWorkspace.OpenRasterDataset(fileName);
18bubuko.com,布布扣 IRasterLayer pRasterLayer = new RasterLayerClass();
19bubuko.com,布布扣 pRasterLayer.CreateFromDataset(pRasterDataset);
20bubuko.com,布布扣 axMapControl1.Map.AddLayer(pRasterLayer);
21bubuko.com,布布扣 axMapControl1.ActiveView.Refresh();


3.加载CAD数据

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

4.加载PersonGeodatabase数据

1bubuko.com,布布扣 IWorkspaceFactory pAccessWorkspaceFactory;
2bubuko.com,布布扣 IFeatureWorkspace pFeatureWorkspace;
3bubuko.com,布布扣 IFeatureLayer pFeatureLayer;
4bubuko.com,布布扣 IFeatureDataset pFeatureDataset;
5bubuko.com,布布扣
6bubuko.com,布布扣 //获取当前路径和文件名
7bubuko.com,布布扣 OpenFileDialog dlg = new OpenFileDialog();
8bubuko.com,布布扣 dlg.Filter = "Personal Geodatabase(*.mdb)|*.mdb|All Files(*.*)|*.*";
9bubuko.com,布布扣 dlg.Title = "Open PersonGeodatabase file";
10bubuko.com,布布扣 dlg.ShowDialog();
11bubuko.com,布布扣 string strFullPath = dlg.FileName;
12bubuko.com,布布扣 if (strFullPath == "") return;
13bubuko.com,布布扣
14bubuko.com,布布扣 //打开personGeodatabase,并添加图层
15bubuko.com,布布扣 pAccessWorkspaceFactory = new AccessWorkspaceFactoryClass();
16bubuko.com,布布扣 //打开工作空间并遍历数据集
17bubuko.com,布布扣 IWorkspace pWorkspace = pAccessWorkspaceFactory.OpenFromFile(strFullPath, 0);
18bubuko.com,布布扣 IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
19bubuko.com,布布扣 pEnumDataset.Reset();
20bubuko.com,布布扣 IDataset pDataset = pEnumDataset.Next();
21bubuko.com,布布扣 //如果数据集是IFeatureDataset,则遍历它下面的子类
22bubuko.com,布布扣 if (pDataset is IFeatureDataset)
23bubuko.com,布布扣 {
24bubuko.com,布布扣 pFeatureWorkspace = (IFeatureWorkspace)pAccessWorkspaceFactory.OpenFromFile(strFullPath, 0);
25bubuko.com,布布扣 pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(pDataset.Name);
26bubuko.com,布布扣 IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets;
27bubuko.com,布布扣 pEnumDataset1.Reset();
28bubuko.com,布布扣 IDataset pDataset1 = pEnumDataset1.Next();
29bubuko.com,布布扣 //如果子类是FeatureClass,则添加到axMapControl1中
30bubuko.com,布布扣 if (pDataset1 is IFeatureClass)
31bubuko.com,布布扣 {
32bubuko.com,布布扣 pFeatureLayer = new FeatureLayerClass();
33bubuko.com,布布扣 pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
34bubuko.com,布布扣 pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
35bubuko.com,布布扣 axMapControl1.Map.AddLayer(pFeatureLayer);
36bubuko.com,布布扣 axMapControl1.ActiveView.Refresh();
37bubuko.com,布布扣 }
38bubuko.com,布布扣 else
39bubuko.com,布布扣 {
40bubuko.com,布布扣 MessageBox.Show("No FeatureLayer!");
41bubuko.com,布布扣 }
42bubuko.com,布布扣 }
43bubuko.com,布布扣 else
44bubuko.com,布布扣 {
45bubuko.com,布布扣 pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
46bubuko.com,布布扣 pFeatureLayer = new FeatureLayerClass();
47bubuko.com,布布扣 pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass("图层名");
48bubuko.com,布布扣 pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
49bubuko.com,布布扣 axMapControl1.Map.AddLayer(pFeatureLayer);
50bubuko.com,布布扣 axMapControl1.ActiveView.Refresh();
51bubuko.com,布布扣 }

5.加载SDE数据库数据

1bubuko.com,布布扣 public ESRI.ArcGIS.Geodatabase.IWorkspace arcSDEWorkspaceOpen(String server, String instance, String user, String password, String database, String version)
2bubuko.com,布布扣 {
3bubuko.com,布布扣 try
4bubuko.com,布布扣 {
5bubuko.com,布布扣 // Create and populate the property set
6bubuko.com,布布扣 ESRI.ArcGIS.esriSystem.IPropertySet propertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
7bubuko.com,布布扣 propertySet.SetProperty("SERVER", server);
8bubuko.com,布布扣 propertySet.SetProperty("INSTANCE", instance);
9bubuko.com,布布扣 propertySet.SetProperty("DATABASE", database);
10bubuko.com,布布扣 propertySet.SetProperty("USER", user);
11bubuko.com,布布扣 propertySet.SetProperty("PASSWORD", password);
12bubuko.com,布布扣 propertySet.SetProperty("VERSION", version);
13bubuko.com,布布扣
14bubuko.com,布布扣 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory;
15bubuko.com,布布扣 workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)new SdeWorkspaceFactoryClass();
16bubuko.com,布布扣 return workspaceFactory.Open(propertySet, 0);
17bubuko.com,布布扣 }
18bubuko.com,布布扣 catch (Exception e)
19bubuko.com,布布扣 {
20bubuko.com,布布扣 throw new Exception(String.Format("arcSDEWorkspaceOpen: {0}", e.Message), e);
21bubuko.com,布布扣 }
22bubuko.com,布布扣 }
23bubuko.com,布布扣
24bubuko.com,布布扣 IWorkspace pWorkspace = arcSDEWorkspaceOpen("计算机名", "5151", "数据库用户", "密码", "", "sde.DEFAULT");
25bubuko.com,布布扣
26bubuko.com,布布扣 IFeatureWorkspace pFeattureWS = (IFeatureWorkspace)pWorkspace;
27bubuko.com,布布扣 IFeatureClass pFeatureClass = pFeattureWS.OpenFeatureClass("图层名");
28bubuko.com,布布扣 IFeatureLayer pFeatureLayer = new FeatureLayerClass();
29bubuko.com,布布扣 pFeatureLayer.FeatureClass = pFeatureClass;
30bubuko.com,布布扣
31bubuko.com,布布扣 axMapControl1.AddLayer(pFeatureLayer);
32bubuko.com,布布扣 axMapControl1.Refresh();  

ArcGIS Engine中加载数据,布布扣,bubuko.com

ArcGIS Engine中加载数据

标签:blog   http   os   文件   数据   io   for   art   

原文地址:http://www.cnblogs.com/qiuzhongyang/p/3874122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!