为了完成拓扑分析模块,需要在系统中新增创建文件地理数据库、要素数据集并加载要素数据集中的数据一系列功能。
很简单,和ArcGIS中新建文件地理数据库、导入数据、加载数据的功能一样:
(1)创建文件地理数据库:选择某一位置,在选定的位置创建文件地理数据库;
(2)创建要素数据集:选择已经建立的文件地理数据库,在该数据库中新建要素数据集(IFeatureWorkspace接口中的CreateFeatureDataset方法)并选择shp文件将其导入到建立好的要素数据集中(FeatureClassToFeatureClass类和Geoprocessor实现)
(3)遍历要素数据集,将要素数据集中的要素类和拓扑数据加载到地图中。
主要有三个界面,分别是主界面、创建要素数据集界面和加载要素数据集界面:
(1)主界面:设计得比较简单,满足基本的显示功能,以及三个功能按钮。
(2)新建要素数据集窗体:选择数据集的创建路径,即创建要素数据集的文件地理数据库。选择数据集的坐标系,定义数据集名称,选择要导入要素数据集的shp要素将其名称加到列表框中。
(3)加载数据集窗体:选择文件地理数据库,遍历数据库中的要素数据集,将其名称显示在列表框中,勾选需加载的数据集将其加载到地图中。
(1)新建文件地理数据库,通过IWorkspaceFactory接口的Create方法实现,主要代码:
//按下新建文件GDB按钮
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "文件地理数据库(*.gdb)|*.gdb";
saveFileDialog.Title = "新建文件地理数据库";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (System.IO.File.Exists(saveFileDialog.FileName))
{
MessageBox.Show("文件已存在!");
return;
}
try
{
int index = saveFileDialog.FileName.LastIndexOf("\\");
string GDBName = saveFileDialog.FileName.Substring(index + 1);
string GDBPath = saveFileDialog.FileName.Substring(0, index);
IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactory();
pWorkspaceFactory.Create(GDBPath, GDBName, null, 0);
MessageBox.Show("创建成功!", "提示");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
(2)新建要素数据集,三个参数分别为:创建要素数据集的工作空间即选择的文件地理数据库、要素数据集的名称、要素数据集的坐标系
//在文件数据库中新建要素数据集
public IFeatureDataset CreateDatasetInGDB(IWorkspace pWorkspace, string name, ISpatialReference pSpatialReference)
{
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureDataset pFeatureDataset = pFeatureWorkspace.CreateFeatureDataset(name, pSpatialReference);
return pFeatureDataset;
}
(3)要素数据集中导入shp要素类,参数分别是待导入的要素类,输出的路径名称即文件数据库的地址,导入后要素类的名称。
//将要素类导入要素数据集方法
public static bool ImportFeatureIntoDS(IFeatureClass pInFeatureClass, string outPath, string name)
{
object sev = null;
Geoprocessor GP = new Geoprocessor();
try
{
FeatureClassToFeatureClass featureClassToFeatureClass = new FeatureClassToFeatureClass();
featureClassToFeatureClass.in_features = pInFeatureClass;
featureClassToFeatureClass.out_path = outPath;
featureClassToFeatureClass.out_name = name;
GP.OverwriteOutput = false;
IGeoProcessorResult pResult = new GeoProcessorResult();
GP.Execute(featureClassToFeatureClass, null);
return true;
}
catch (Exception ex)
{
MessageBox.Show(GP.GetMessages(ref sev));
return false;
}
}
(4)加载要素数据集中的要素类和拓扑,主要是分别遍历要素数据集中的要素类和拓扑。
//加载数据集
private void button3_Click(object sender, EventArgs e)
{
IFeatureWorkspace pFeaWS = pWorkspace as IFeatureWorkspace;
List FeatureDSList2 = new List();
for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
foreach (IFeatureDataset eachDS in FeatureDSList)
{
if (eachDS.Name == checkedListBox1.CheckedItems[i].ToString())
{
FeatureDSList2.Add(eachDS);
}
}
}
foreach (IFeatureDataset FeaDS2 in FeatureDSList2)
{
IEnumDataset pEnumDS = FeaDS2.Subsets;
IDataset pFeaDS2;
while ((pFeaDS2 = pEnumDS.Next()) != null)
{
if (pFeaDS2 is IFeatureClass)
{
IFeatureLayer pFeaLayer = new FeatureLayer();
pFeaLayer.FeatureClass = pFeaWS.OpenFeatureClass(pFeaDS2.Name);
pFeaLayer.Name = pFeaDS2.Name;
pMap.AddLayer(pFeaLayer as ILayer);
}
}
//加载拓扑结果
ITopologyLayer pTopolayer;
ITopology pTopology;
ILayer pLayer;
ITopologyContainer pTopoContainer = (ITopologyContainer)FeaDS2;
for (int i = 0; i < pTopoContainer.TopologyCount; i++)
{
pTopolayer = new TopologyLayerClass();
pTopology = pTopoContainer.Topology[i];
pTopolayer.Topology = pTopology;
pLayer = pTopolayer as ILayer;
pLayer.Name = FeaDS2.Name + "_拓扑" + i + 1;
pMap.AddLayer(pLayer);
}
}
}
(1)github:https://github.com/ranhongwu/190807CreateFileGDB 。
(2)CSDN:https://download.csdn.net/download/weixin_41607453/11506201 。