1.加载Shapefile数据
IWorkspaceFactory pWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer; //获取当前路径和文件名
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Shape(*.shp)|*.shp|All Files(*.*)|*.*";
dlg.Title = "Open Shapefile data";
dlg.ShowDialog();
string strFullPath = dlg.FileName;
if (strFullPath == "") return;
int Index = strFullPath.LastIndexOf("\\");
string filePath = strFullPath.Substring(, Index);
string fileName = strFullPath.Substring(Index + ); //打开工作空间并添加shp文件
pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(filePath, );
pFeatureLayer = new FeatureLayerClass(); pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh();
2.加载栅格数据
IWorkspaceFactory pWorkspaceFactory;
IRasterWorkspace pRasterWorkspace; OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Layer File(*.lyr)|*.jpg;*.bmp;*.tiff";
dlg.Title = "Open Raster Data File";
dlg.ShowDialog();
//获取当前路径和文件名
string strFullPath = dlg.FileName;
if (strFullPath == "") return;
int Index = strFullPath.LastIndexOf("\\");
string fileName = strFullPath.Substring(Index + );
string filePath = strFullPath.Substring(, Index); pWorkspaceFactory = new RasterWorkspaceFactoryClass();
pRasterWorkspace = (IRasterWorkspace)pWorkspaceFactory.OpenFromFile(filePath, );
IRasterDataset pRasterDataset = (IRasterDataset)pRasterWorkspace.OpenRasterDataset(fileName);
IRasterLayer pRasterLayer = new RasterLayerClass();
pRasterLayer.CreateFromDataset(pRasterDataset);
axMapControl1.Map.AddLayer(pRasterLayer);
axMapControl1.ActiveView.Refresh();
3.加载CAD数据
IWorkspaceFactory pWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
IFeatureDataset pFeatureDataset;
//获取当前路径和文件名
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "CAD(*.dwg)|*.dwg|All Files(*.*)|*.*";
dlg.Title = "Open CAD Data file";
dlg.ShowDialog();
string strFullPath = dlg.FileName;
if (strFullPath == "") return;
int Index = strFullPath.LastIndexOf("\\");
string filePath = strFullPath.Substring(, Index);
string fileName = strFullPath.Substring(Index + );
//打开CAD数据集
pWorkspaceFactory = new CadWorkspaceFactoryClass();
pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(filePath, );
//打开一个要素集
pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName);
//IFeaturClassContainer可以管理IFeatureDataset中的每个要素类
IFeatureClassContainer pFeatClassContainer = (IFeatureClassContainer)pFeatureDataset;
//对CAD文件中的要素进行遍历处理
for (int i = ; i < pFeatClassContainer.ClassCount - ; 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();
}
}
4.加载PersonGeodatabase数据
IWorkspaceFactory pAccessWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
IFeatureDataset pFeatureDataset; //获取当前路径和文件名
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Personal Geodatabase(*.mdb)|*.mdb|All Files(*.*)|*.*";
dlg.Title = "Open PersonGeodatabase file";
dlg.ShowDialog();
string strFullPath = dlg.FileName;
if (strFullPath == "") return; //打开personGeodatabase,并添加图层
pAccessWorkspaceFactory = new AccessWorkspaceFactoryClass();
//打开工作空间并遍历数据集
IWorkspace pWorkspace = pAccessWorkspaceFactory.OpenFromFile(strFullPath, );
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, );
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();
}
else
{
MessageBox.Show("No FeatureLayer!");
}
}
else
{
pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass("图层名");
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh();
}
5.加载SDE数据库数据
public ESRI.ArcGIS.Geodatabase.IWorkspace arcSDEWorkspaceOpen(String server, String instance, String user, String password, String database, String version)
{
try
{
// Create and populate the property set
ESRI.ArcGIS.esriSystem.IPropertySet propertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
propertySet.SetProperty("SERVER", server);
propertySet.SetProperty("INSTANCE", instance);
propertySet.SetProperty("DATABASE", database);
propertySet.SetProperty("USER", user);
propertySet.SetProperty("PASSWORD", password);
propertySet.SetProperty("VERSION", version); ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory;
workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)new SdeWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, );
}
catch (Exception e)
{
throw new Exception(String.Format("arcSDEWorkspaceOpen: {0}", e.Message), e);
}
} IWorkspace pWorkspace = arcSDEWorkspaceOpen("计算机名", "", "数据库用户", "密码", "", "sde.DEFAULT"); IFeatureWorkspace pFeattureWS = (IFeatureWorkspace)pWorkspace;
IFeatureClass pFeatureClass = pFeattureWS.OpenFeatureClass("图层名");
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureClass; axMapControl1.AddLayer(pFeatureLayer);
axMapControl1.Refresh();
6、文件地理数据库加载
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
IFeatureDataset pFeatureDataset;
string filePath = @"D:\My Documents\ArcGIS\新建文件地理数据库.gdb";
string filename = "haha";
//打开personGeodatabase,并添加图层
IWorkspaceFactory pAccessWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
//打开工作空间并遍历数据集
IWorkspace pWorkspace = pAccessWorkspaceFactory.OpenFromFile(filePath, );
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(filePath, );
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(filename);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
IRgbColor pRgbColor = new RgbColorClass();
pRgbColor.Red = ;
pRgbColor.Green = ;
pRgbColor.Blue = ;
pRgbColor.Transparency = ;
ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = pRgbColor;
pRgbColor.Transparency = ;
pLineSymbol.Color = pRgbColor;
pLineSymbol.Width = ;
pFillSymbol.Outline = pLineSymbol;
ISimpleRenderer pSimpleRender = new SimpleRendererClass();
pSimpleRender.Symbol = pFillSymbol as ISymbol;
IGeoFeatureLayer pGeoFeatureLayer = pFeatureLayer as IGeoFeatureLayer;
pGeoFeatureLayer.Renderer = pSimpleRender as IFeatureRenderer;
pFeatureLayer = pGeoFeatureLayer as IFeatureLayer;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh();
}
}