ArcEngine按自定义网格切割面

背景:

        使用IFeatureCursor.InsertFeature()时,由于图斑太大且节点多,导致一个图斑插入就造成内存溢出。在代码中调用gp工具Append_management时,会抛出:$ERROR 999998: Unexpected Error.分析图斑后,觉得是单图斑节点太多(上百万节点),可切割再进行导入。  

解决方式:

数据准备:一个gdb中存在面图层Source与Result结构完全一致,其中图层Source中有一个图斑。

 

 代码:

public static void Test()
{
    using (var comReleaser = new ESRI.ArcGIS.ADF.ComReleaser())
    {
        var pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
        var pWorkspace = pWorkspaceFactory.OpenFromFile(@"F:\GIS测试数据\Test.gdb", 0);
        comReleaser.ManageLifetime(pWorkspace);
        var pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
        var pSourceFeatureClass = pFeatureWorkspace.OpenFeatureClass("Source");
        var pTarFeatureClass = pFeatureWorkspace.OpenFeatureClass("Result");

        (pTarFeatureClass as ITable).DeleteSearchedRows(null);

        //获取目标数据集对应源数据集索引字典 key:targetField value:sourceField
        Dictionary<int, int> targetsource = new Dictionary<int, int>();
        for (int i = 0; i < pTarFeatureClass.Fields.FieldCount; i++)
        {
            IField tField = pTarFeatureClass.Fields.get_Field(i);
            if (!tField.Required) //目标图层的字段不是必须字段
            {
                int idx = pSourceFeatureClass.Fields.FindField(tField.Name);
                if (idx > -1) //源要素类中该字段存在
                    targetsource.Add(i, idx);
            }
        }

        IFeatureCursor tarFeatureCursor = pTarFeatureClass.Insert(true);
        comReleaser.ManageLifetime(tarFeatureCursor);
        IFeatureBuffer tBuffer = pTarFeatureClass.CreateFeatureBuffer();
        comReleaser.ManageLifetime(tBuffer);
        IFeatureCursor ocursor = pSourceFeatureClass.Search(null, true);
        comReleaser.ManageLifetime(ocursor);
        IFeature ofeature = null;
        while ((ofeature = ocursor.NextFeature()) != null)
        {
            foreach (KeyValuePair<int, int> keyValue in targetsource)
            {
                object value = ofeature.Value[keyValue.Value];
                //如果属性值是DBNull.Value类型的话,则不赋值,防止导出SHP是报错
                if (value != DBNull.Value)
                    tBuffer.Value[keyValue.Key] = value;
            }
            foreach (var item in SplitGeometryByGrid(ofeature.ShapeCopy))
            {
                tBuffer.Shape = item;
                tarFeatureCursor.InsertFeature(tBuffer);
            }
        }
        tarFeatureCursor.Flush();
    }
}

/// <summary>
/// 网格分割Geometry
/// </summary>
/// <param name="pGeometry">源几何图形</param>
/// <param name="xPart">X轴分段</param>
/// <param name="yPart">Y轴分段</param>
/// <returns>分割后的几何图形</returns>
public static List<IGeometry> SplitGeometryByGrid(IGeometry pGeometry, int xPart = 2, int yPart = 2)
{
    List<IGeometry> lisGeometries = new List<IGeometry>();

    IEnvelope envelope = pGeometry.Envelope;
    double xMax = envelope.XMax;
    double xMin = envelope.XMin;
    double yMax = envelope.YMax;
    double yMin = envelope.YMin;

    ITopologicalOperator pTopological = pGeometry as ITopologicalOperator;

    double partHeight = envelope.Height / yPart;
    double partWidth = envelope.Width / xPart;
    List<Tuple<double, double, double, double>> tuples = new List<Tuple<double, double, double, double>>();

    for (int x = 0; x < xPart; x++) //从下往上
    {
        for (int y = 0; y < yPart; y++) //从左往右
        {
            tuples.Add(new Tuple<double, double, double, double>(xMin + partWidth * x, yMin + partHeight * y, x == xPart - 1 ? xMax : xMin + partWidth * (x + 1), y == yPart - 1 ? yMax : yMin + partHeight * (y + 1)));
        }
    }

    foreach (var item in tuples)
    {
        IEnvelope envelope2 = new EnvelopeClass();
        envelope2.PutCoords(item.Item1, item.Item2, item.Item3, item.Item4);
        lisGeometries.Add(pTopological.Intersect(envelope2, esriGeometryDimension.esriGeometry2Dimension));
    }
    return lisGeometries;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值