SuperMap iObjects .NET 11i 二次开发(十五)—— 类型转换之面转点

前言

上一篇博文简单介绍了有关类型转换之线面互转的相关操作,这个部分主要认识下类型转换之面转点的相关操作。


一、有关常用类说明

可跳转以下博文进行查看:

SuperMap iObjects .NET 11i 二次开发(十三)------ 类型转换之点线互转https://blog.csdn.net/Adoudoudou_/article/details/151954603?spm=1001.2014.3001.5502

SuperMap iObjects .NET 11i 二次开发(十四)------ 类型转换之线面互转https://blog.csdn.net/Adoudoudou_/article/details/151964135?spm=1001.2014.3001.5502

二、有关常用属性说明

|--------------------------|------------|
| GeoRegion.InnerPoint | 获取几何对象的内点。 |

三、界面设计

桌面端在进行类型转换中的面转点时,结果为面对象的内点,如果需要转换结果为面的节点,那么需要先将面转为线再将线转为点。在原有界面基础上,添加类型转换相关功能点,如下图所示:

四、功能实现

1、面转点

cs 复制代码
private void 面转点ToolStripMenuItem_Click(object sender, EventArgs e)
{
    try
    {
        WorkspaceTreeNodeBase treeNode = D_workspaceControl.WorkspaceTree.SelectedNode as WorkspaceTreeNodeBase;
        if (treeNode == null)
        {
            MessageBox.Show("请先选择一个数据集节点");
            return;
        }

        string polygonname = treeNode.Text;

        // 1. 获取选中的数据集并检查是否为面数据集
        Dataset selectedDataset = D_workspace.Datasources[0].Datasets[polygonname];

        // 类型检查
        if (selectedDataset.Type != DatasetType.Region)
        {
            MessageBox.Show($"请选择面数据集进行操作!\n当前选择的数据集 '{polygonname}' 的类型为: {selectedDataset.Type}");
            return;
        }

        // 验证是否为矢量数据集
        if (!(selectedDataset is DatasetVector))
        {
            MessageBox.Show("选中的数据集不是矢量数据集");
            return;
        }

        DatasetVector polygonDataset = (DatasetVector)selectedDataset;

        // 检查数据集是否为空
        if (polygonDataset.RecordCount == 0)
        {
            MessageBox.Show("面数据集中没有记录,无法进行转换");
            return;
        }

        // 设置点数据集的结构信息
        DatasetVectorInfo pointDatasetInfo = new DatasetVectorInfo();
        pointDatasetInfo.Name = polygonDataset.Name + "_Points";
        pointDatasetInfo.Type = DatasetType.Point;

        // 创建点数据集
        DatasetVector pointDataset = this.D_workspace.Datasources[0].Datasets.Create(pointDatasetInfo);
        if (pointDataset == null)
        {
            MessageBox.Show("点数据集创建失败");
            return;
        }

        // 2. 遍历面数据集中的每条记录并转换为点
        using (Recordset polygonRecordset = polygonDataset.GetRecordset(false, CursorType.Dynamic))
        using (Recordset pointRecordset = pointDataset.GetRecordset(false, CursorType.Dynamic))
        {
            int totalPoints = 0;

            polygonRecordset.MoveFirst();
            while (!polygonRecordset.IsEOF)
            {
                // 获取面几何对象
                GeoRegion geoRegion = polygonRecordset.GetGeometry() as GeoRegion;
                if (geoRegion != null)
                {
                    // 将面转换为线
                    GeoLine geoLine = geoRegion.ConvertToLine();
                    if (geoLine != null)
                    {
                        // 处理线的每个部分(支持多部分线)
                        for (int partIndex = 0; partIndex < geoLine.PartCount; partIndex++)
                        {
                            Point2Ds points = geoLine[partIndex];

                            // 为每个顶点创建点对象
                            foreach (Point2D point2D in points)
                            {
                                // 创建点几何对象
                                GeoPoint point = new GeoPoint(point2D);

                                // 添加新记录
                                pointRecordset.AddNew(point);
                                pointRecordset.Update();
                                totalPoints++;
                            }
                        }
                    }
                }

                polygonRecordset.MoveNext();
            }

            MessageBox.Show($"转换完成!共生成 {totalPoints} 个点要素到点数据集 '{pointDataset.Name}'");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show($"面转点过程中发生错误: {ex.Message}");
    }
}

2、面转内点(同桌面端)

cs 复制代码
private void 面转内点ToolStripMenuItem_Click(object sender, EventArgs e)
{
    try
    {
        WorkspaceTreeNodeBase treeNode = D_workspaceControl.WorkspaceTree.SelectedNode as WorkspaceTreeNodeBase;
        if (treeNode == null)
        {
            MessageBox.Show("请先选择一个数据集节点");
            return;
        }

        string polygonname = treeNode.Text;

        // 1. 获取选中的数据集并检查是否为面数据集
        Dataset selectedDataset = D_workspace.Datasources[0].Datasets[polygonname];

        // 类型检查
        if (selectedDataset.Type != DatasetType.Region)
        {
            MessageBox.Show($"请选择面数据集进行操作!\n当前选择的数据集 '{polygonname}' 的类型为: {selectedDataset.Type}");
            return;
        }

        if (!(selectedDataset is DatasetVector))
        {
            MessageBox.Show("选中的数据集不是矢量数据集");
            return;
        }

        DatasetVector polygonDataset = (DatasetVector)selectedDataset;

        // 检查数据集是否为空
        if (polygonDataset.RecordCount == 0)
        {
            MessageBox.Show("面数据集中没有记录,无法进行转换");
            return;
        }

        // 设置点数据集的结构信息
        DatasetVectorInfo pointDatasetInfo = new DatasetVectorInfo();
        pointDatasetInfo.Name = polygonDataset.Name + "_InnerPoints";
        pointDatasetInfo.Type = DatasetType.Point;

        // 创建点数据集
        DatasetVector pointDataset = this.D_workspace.Datasources[0].Datasets.Create(pointDatasetInfo);
        if (pointDataset == null)
        {
            MessageBox.Show("点数据集创建失败");
            return;
        }

        // 2. 遍历面数据集中的每条记录并获取内点
        using (Recordset polygonRecordset = polygonDataset.GetRecordset(false, CursorType.Dynamic))
        using (Recordset pointRecordset = pointDataset.GetRecordset(false, CursorType.Dynamic))
        {
            int processedCount = 0;
            int totalCount = polygonDataset.RecordCount;

            polygonRecordset.MoveFirst();
            while (!polygonRecordset.IsEOF)
            {
                // 获取面几何对象
                GeoRegion geoRegion = polygonRecordset.GetGeometry() as GeoRegion;
                if (geoRegion != null)
                {
                    // 获取面的内点(质心点)
                    Point2D innerPoint2D = geoRegion.InnerPoint;
                    if (innerPoint2D != null)
                    {
                        // 将Point2D转换为GeoPoint
                        GeoPoint geoPoint = new GeoPoint(innerPoint2D);

                        // 添加新的点记录
                        pointRecordset.AddNew(geoPoint);
                        pointRecordset.Update();
                        processedCount++;
                    }
                }

                polygonRecordset.MoveNext();
            }

            MessageBox.Show($"转换完成!共生成 {processedCount} 个内点到点数据集 '{pointDataset.Name}'");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show($"面转内点过程中发生错误: {ex.Message}");
    }
}

五、运行结果


总结

以上就是有关于 SuperMap iObjects 11i .NET 类型转换之面转点相关操作的一些过程记录,我们下篇博文再见!

相关推荐
星河队长1 小时前
VS创建C++动态库和C#访问过程
java·c++·c#
专注VB编程开发20年2 小时前
B.NET编写不阻塞UI线程的同步延时
ui·.net·vb.net·doevents
William_cl2 小时前
【C# MVC 前置】异步编程 async/await:从 “卡界面” 到 “秒响应” 的 Action 优化指南(附微软官方避坑清单)
microsoft·c#·mvc
yong99903 小时前
C#驱动斑马打印机实现包装自动打印
java·数据库·c#
Jose_lz3 小时前
C#开发学习杂笔(更新中)
开发语言·学习·c#
mingupup4 小时前
WPF/C#:使用Microsoft Agent Framework框架创建一个带有审批功能的终端Agent
c#·wpf
慧都小妮子5 小时前
基于.NET UA Client SDK构建跨平台OPC UA客户端应用
.net·opc ua·automation·跨平台应用·unified
追逐时光者6 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 58 期(2025年10.13-10.19)
后端·.net
YuanlongWang6 小时前
C# 设计模式——单例模式
单例模式·设计模式·c#
YuanlongWang6 小时前
C#基础——GC(垃圾回收)的工作流程与优化策略
java·jvm·c#