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 类型转换之面转点相关操作的一些过程记录,我们下篇博文再见!

相关推荐
聊聊MES那点事2 小时前
面向.NET开发者:Prosys OPC UA .NET SDK 全面解析
.net·数据采集·prosys opc
喵叔哟2 小时前
3. 从0到上线:.NET 8 + ML.NET LTR 智能类目匹配实战--从业务到方案:消费类目智能匹配的整体设计
.net
百锦再2 小时前
从 .NET 到 Java 的转型指南:详细学习路线与实践建议
android·java·前端·数据库·学习·.net·数据库架构
ajassi20003 小时前
开源 C# 快速开发(十二)进程监控
开发语言·开源·c#
大飞pkz5 小时前
【设计模式】代理模式
开发语言·设计模式·c#·代理模式
sali-tec6 小时前
C# 基于halcon的视觉工作流-章40-OCR训练识别
开发语言·图像处理·算法·计算机视觉·c#·ocr
该用户已不存在7 小时前
.NET语言大舞台,有才你就来
c#·.net
mudtools8 小时前
不一样的.NET烟火,基于Roslyn的开源代码生成器
开源·.net