arcengine 按面积(批量)分割

第一、填写面积,选择分割方式(横向、纵向、鼠标指向)在这里插入图片描述

二、开始分割,只需对分割的地块,绘制一条线段
在这里插入图片描述
三、完成分割
在这里插入图片描述
axMapControl1_OnMouseDown事件下

            #region 面积分割
            if (areaDi)
            {
                if (axMapControl1.Map.LayerCount == 0) return;
                if (axMapControl1.Map.LayerCount > 1)
                {
                    MessageBox.Show("请只添加一个图层!", "提示");
                    return;
                }
                // TODO:  Add Cut.OnMouseDown implementation
                if (e.button == 1)//左键按下
                {
                    IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
                    //划线
                    IGeometry pline = (IGeometry)axMapControl1.TrackLine();
                    //拿到划线范围内的所有feature
                    IGeometry pGeometry = pline as IGeometry;
                    IMap pMap = axMapControl1.Map;
                    pMap.SelectByShape(pGeometry, null, false);//< br >
                    //转成IRealtionOperator接口,以便在遍历时判断几何位置关系。
                    IRelationalOperator rela = (pline as IPolyline) as IRelationalOperator;
                    //拿到切割目标集合
                    IEnumFeature pEnumFeature = (IEnumFeature)pMap.FeatureSelection;
                    IEnumFeatureSetup pEnumFeatureSetup = pEnumFeature as IEnumFeatureSetup;
                    pEnumFeatureSetup.AllFields = true;
                    IFeature pFeature = pEnumFeature.Next();
                    IFeature fsFeature = null;
                    //依次遍历
                    while (pFeature != null)
                    {
                        if (rela.Crosses(pFeature.Shape) || fsFeature != null)//判断是否Cross  (rela.Crosses(pFeature.Shape))
                        {
                            // FrmPartitionarea frmPartitionarea = new FrmPartitionarea((pFeature.Shape as IArea).Area);
                            // frmPartitionarea.Owner = this;
                            //frmPartitionarea.Show();
                            //if (frmPartitionarea.ShowDialog() == DialogResult.OK)
                            //{
                            if (Math.Round((pFeature.Shape as IArea).Area, 2) < Math.Round(divisionArea, 2))
                            {
                                MessageBox.Show("分割面积大约图斑面积!", "错误");
                                //return;//当分割面积大约面要素面积时,返回
                                pFeature = pEnumFeature.Next();
                                continue;
                            }
                            try
                            {
                                //把面转成ITopologicalOperator,使用Cut函数
                                ITopologicalOperator topo = (pFeature.Shape as IPolygon) as ITopologicalOperator;
                                //新建两个面,作为切割函数的参数,切割后的两个对象保存于这两个面之中
                                IGeometry pLeftGeometry = new PolygonClass();
                                IGeometry pRightGeometry = new PolygonClass();
                                double yValue = pFeature.Shape.Envelope.YMax - pFeature.Shape.Envelope.YMin;
                                double xValue = pFeature.Shape.Envelope.XMax - pFeature.Shape.Envelope.XMin;
                                bool plusChk = false;
                                bool reduceChk = false;
                                bool fgChk = false;
                                double left = 0;
                                double right = 0;
                                double newValue = 0;
                                double newValue2 = 0;
                                List<double> oldleft = new List<double>();
                                bool chkFX = false;
                                bool chkcw = false;
                                int n = 0;
                                for (int j = 0; j < 100; j++)
                                {
                                    IPoint point1 = new PointClass();
                                    IPoint point2 = new PointClass();
                                    ILine newline = new LineClass();
                                    IPolyline fgx = null;
                                    //double count = pFeature.Shape.Envelope.YMax - pFeature.Shape.Envelope.YMin;
                                    //pline.
                                    if (portrait1)//纵
                                    {
                                        if (j == 0)
                                        {
                                            point1.X = pFeature.Shape.Envelope.XMin + xValue / 2;
                                            point1.Y = pFeature.Shape.Envelope.YMin;
                                            point2.X = pFeature.Shape.Envelope.XMin + xValue / 2;
                                            point2.Y = pFeature.Shape.Envelope.YMax;
                                            newValue = pFeature.Shape.Envelope.XMin + xValue / 2;
                                        }
                                        else if (plusChk)
                                        {
                                            point1.X = newValue + (xValue / Math.Pow(2, j + 1));
                                            point1.Y = pFeature.Shape.Envelope.YMin;
                                            point2.X = newValue + (xValue / Math.Pow(2, j + 1));
                                            point2.Y = pFeature.Shape.Envelope.YMax;
                                            newValue = newValue + (xValue / Math.Pow(2, j + 1));
                                        }
                                        else if (reduceChk)
                                        {
                                            point1.X = newValue - (xValue / Math.Pow(2, j + 1));
                                            point1.Y = pFeature.Shape.Envelope.YMin;
                                            point2.X = newValue - (xValue / Math.Pow(2, j + 1));
                                            point2.Y = pFeature.Shape.Envelope.YMax;
                                            newValue = newValue - (xValue / Math.Pow(2, j + 1));
                                        }
                                    }
                                    else if (transverse1)//横
                                    {
                                        if (j == 0)
                                        {
                                            point1.Y = pFeature.Shape.Envelope.YMin + yValue / 2;
                                            point1.X = pFeature.Shape.Envelope.XMin;
                                            point2.Y = pFeature.Shape.Envelope.YMin + yValue / 2;
                                            point2.X = pFeature.Shape.Envelope.XMax;
                                            newValue = pFeature.Shape.Envelope.YMin + yValue / 2;
                                        }
                                        else if (plusChk)
                                        {
                                            point1.Y = newValue + (yValue / Math.Pow(2, j + 1));
                                            point1.X = pFeature.Shape.Envelope.XMin;
                                            point2.Y = newValue + (yValue / Math.Pow(2, j + 1));
                                            point2.X = pFeature.Shape.Envelope.XMax;
                                            newValue = newValue + (yValue / Math.Pow(2, j + 1));
                                        }
                                        else if (reduceChk)
                                        {
                                            point1.Y = newValue - (yValue / Math.Pow(2, j + 1));
                                            point1.X = pFeature.Shape.Envelope.XMin;
                                            point2.Y = newValue - (yValue / Math.Pow(2, j + 1));
                                            point2.X = pFeature.Shape.Envelope.XMax;
                                            newValue = newValue - (yValue / Math.Pow(2, j + 1));
                                        }
                                    }
                                    else if (Mousepointing1)//鼠标指向 有问题
                                    {
                                        IPolyline lin2 = pline as IPolyline;
                                        IPoint point3 = new PointClass();
                                        IPoint point4 = new PointClass();
                                        if (j == 0)
                                        {
                                            point3.X = (pFeature.Shape.Envelope.XMin + pFeature.Shape.Envelope.XMax) / 2;
                                            point3.Y = (pFeature.Shape.Envelope.YMin + pFeature.Shape.Envelope.YMax) / 2;
                                            point4.X = lin2.ToPoint.X - (lin2.FromPoint.X - point3.X);
                                            point4.Y = lin2.ToPoint.Y - (lin2.FromPoint.Y - point3.Y);
                                            newValue = point3.Y;
                                            newValue2 = point4.Y;
                                        }
                                        else if (plusChk)
                                        {
                                            point3.X = (pFeature.Shape.Envelope.XMin + pFeature.Shape.Envelope.XMax) / 2;
                                            point3.Y = newValue + (yValue / Math.Pow(2, j + 1));
                                            point4.X = lin2.ToPoint.X - (lin2.FromPoint.X - point3.X);
                                            point4.Y = newValue2 + (yValue / Math.Pow(2, j + 1));
                                            newValue = point3.Y;
                                            newValue2 = point4.Y;
                                        }
                                        else if (reduceChk)
                                        {
                                            point3.X = (pFeature.Shape.Envelope.XMin + pFeature.Shape.Envelope.XMax) / 2;
                                            point3.Y = newValue - (yValue / Math.Pow(2, j + 1));
                                            point4.X = lin2.ToPoint.X - (lin2.FromPoint.X - point3.X);
                                            point4.Y = newValue2 - (yValue / Math.Pow(2, j + 1));
                                            newValue = point3.Y;
                                            newValue2 = point4.Y;
                                        }
                                        ILine newline2 = new LineClass();
                                        newline2.PutCoords(point3, point4);
                                        ISegmentCollection pSegCol2 = new PolylineClass();
                                        pSegCol2.AddSegment(newline2 as ISegment);
                                        fgx = DataManager.getExtendLine(pSegCol2 as IPolyline, 3, 1000000);
                                    }
                                    if (fgx == null)
                                    {
                                        newline.PutCoords(point1, point2);
                                        ISegmentCollection pSegCol = new PolylineClass();
                                        pSegCol.AddSegment(newline as ISegment);
                                        fgx = pSegCol as IPolyline;
                                    }
                                    try
                                    {
                                        try
                                        {
                                            topo.Cut((fgx as IPolyline), out pLeftGeometry, out pRightGeometry);
                                            //chkcw = false;
                                        }
                                        catch
                                        {
                                            //chkcw = true;
                                            //newValue = newValue - (yValue / Math.Pow(2, j + 1));
                                            //newValue2 = newValue2 - (yValue / Math.Pow(2, j + 1));
                                        }
                                        left = Math.Round((pLeftGeometry as IArea).Area, 2);
                                        right = Math.Round((pRightGeometry as IArea).Area, 2);
                                        //oldleft.Add(left);
                                        //if(Math.Abs(left- divisionArea))
                                        if (Math.Round((left + right), 0) == Math.Round(divisionArea, 0))
                                        {
                                            fgChk = false;
                                            break;
                                        }
                                        if (Mousepointing1)
                                        {
                                            //if (oldleft.Count > 2)
                                            //{
                                            //    if (Math.Abs(oldleft[oldleft.Count - 1] - divisionArea) > Math.Abs(oldleft[oldleft.Count - 2] - divisionArea))
                                            //    {
                                            //        if (chkFX)
                                            //            chkFX = false;
                                            //        else
                                            //            chkFX = true;
                                            //    }
                                            //    if (oldleft[oldleft.Count - 1] == oldleft[oldleft.Count - 2])
                                            //    {
                                            //        if (chkFX)
                                            //            chkFX = false;
                                            //        else
                                            //            chkFX = true;
                                            //        j = 2;
                                            //    }
                                            //}
                                            //if (left / divisionArea > 2 && (!chkcw))
                                            //{
                                            //    j = 2;
                                            //}
                                            //if (left == 0 || right == 0)
                                            //{
                                            //    if (left == Math.Round(divisionArea, 2) || right == Math.Round(divisionArea, 2))
                                            //    {
                                            //        fgChk = false;
                                            //        break;
                                            //    }
                                            //    else
                                            //    { chkFX = true; }
                                            //}
                                        }
                                        if (transverse1)
                                        {
                                            if (upperChk)//分割上面
                                            {
                                                if (left == Math.Round(divisionArea, 2))
                                                {
                                                    fgChk = true;
                                                    break;
                                                }
                                                else if (left > Math.Round(divisionArea, 2))
                                                {
                                                    reduceChk = false;
                                                    plusChk = true;
                                                }
                                                else if (left < Math.Round(divisionArea, 2))
                                                {
                                                    plusChk = false;
                                                    reduceChk = true;
                                                }
                                            }
                                            else if (lowerChk) //分割下面
                                            {
                                                if (right == Math.Round(divisionArea, 2))
                                                {
                                                    fgChk = true;
                                                    break;
                                                }
                                                else if (right > Math.Round(divisionArea, 2))
                                                {
                                                    plusChk = false;
                                                    reduceChk = true;
                                                }
                                                else if (right < Math.Round(divisionArea, 2))
                                                {
                                                    reduceChk = false;
                                                    plusChk = true;
                                                }
                                            }
                                        }
                                        else if (portrait1)
                                        {
                                            if (rightChk)//分割右面
                                            {
                                                if (right == Math.Round(divisionArea, 2))
                                                {
                                                    fgChk = true;
                                                    break;
                                                }
                                                else if (right > Math.Round(divisionArea, 2))
                                                {
                                                    reduceChk = false;
                                                    plusChk = true;
                                                }
                                                else if (right < Math.Round(divisionArea, 2))
                                                {
                                                    plusChk = false;
                                                    reduceChk = true;
                                                }
                                            }
                                            else if (leftChk) //分割左面
                                            {
                                                if (left == Math.Round(divisionArea, 2))
                                                {
                                                    fgChk = true;
                                                    break;
                                                }
                                                else if (left > Math.Round(divisionArea, 2))
                                                {
                                                    plusChk = false;
                                                    reduceChk = true;
                                                }
                                                else if (left < Math.Round(divisionArea, 2))
                                                {
                                                    reduceChk = false;
                                                    plusChk = true;
                                                }
                                            }
                                        }
                                        else if (Mousepointing1)
                                        {
                                            if (left == Math.Round(divisionArea, 2))
                                            {
                                                fgChk = true;
                                                break;
                                            }
                                            else if (left < Math.Round(divisionArea, 2))
                                            {
                                                plusChk = false;
                                                reduceChk = true;
                                            }
                                            else if (left > Math.Round(divisionArea, 2))
                                            {
                                                reduceChk = false;
                                                plusChk = true;
                                            }

                                        }
                                    }
                                    catch { }
                                    if (j == 99)
                                    {
                                        MessageBox.Show("图形面积远大于分割面积,无法切割成功!");
                                    }
                                }
                                //topo.Cut((pline as IPolyline), out pLeftGeometry, out pRightGeometry);
                                //left = (pLeftGeometry as IArea).Area;
                                //double right = (pRightGeometry as IArea).Area;
                                //MessageBox.Show(left.ToString()+","+right.ToString(),MessageBoxButtons.OKCancel);
                                //DialogResult result=null;
                                // if (pFeature.HasOID)
                                //DialogResult result = MessageBox.Show("图层ID:" + pFeature.get_Value(0).ToString() + ",确定要分割!!!!", "切割面积分别为:" + Math.Round(left, 3).ToString() + "," + Math.Round(right, 3).ToString(), MessageBoxButtons.OKCancel);
                                if (fgChk)
                                {
                                    //获取字段名
                                    List<string> pAllFieldsList = new List<string>();
                                    pAllFieldsList = KYKJ.AttributeOperate.AttTableOperate.get_FieldsString(pFeatureLayer.FeatureClass);
                                    //由FeatureClass新建Feature
                                    IFeature pFeatureLeft = pFeatureLayer.FeatureClass.CreateFeature();
                                    IFeature pFeatureRight = pFeatureLayer.FeatureClass.CreateFeature();
                                    //赋值
                                    pFeatureLeft.Shape = pLeftGeometry;
                                    pFeatureRight.Shape = pRightGeometry;
                                    for (int i = 0; i < pAllFieldsList.Count; i++)
                                    {
                                        if (pAllFieldsList[i].ToUpper().Contains("ID") || pAllFieldsList[i].ToUpper().Contains("SHP") || pAllFieldsList[i].ToUpper().Contains("SHAPE_")) continue;
                                        int fieldindex = pFeatureLeft.Fields.FindField(pAllFieldsList[i]);
                                        string sd = pFeature.Value[fieldindex].ToString();
                                        pFeatureLeft.set_Value(fieldindex, pFeature.Value[fieldindex]);
                                        pFeatureRight.set_Value(fieldindex, pFeature.Value[fieldindex]);
                                    }
                                    //保存
                                    pFeatureLeft.Store();
                                    pFeatureRight.Store();
                                    if (transverse1 && tValue > 1)
                                    {
                                        if (upperChk)
                                            fsFeature = pFeatureRight;
                                        else
                                            fsFeature = pFeatureLeft;
                                    }
                                    else if (portrait1 && pValue > 1)
                                    {
                                        if (rightChk)
                                            fsFeature = pFeatureLeft;
                                        else
                                            fsFeature = pFeatureRight;
                                    }
                                    else
                                        fsFeature = null;
                                    //删除
                                    pFeature.Delete();
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("分割失败,可能该图形为多面体(包含z)!");
                                //string ss = ex.ToString();
                            }
                            // }
                        }
                        if (tValue > 1)
                        {
                            pFeature = fsFeature;
                            tValue--;
                        }
                        else if (pValue > 1)
                        {
                            pFeature = fsFeature;
                            pValue--;
                        }
                        if (fsFeature == null)
                            pFeature = pEnumFeature.Next();
                    }
                    //清除选择
                    IFeatureSelection pFeatureSelectionClear = axMapControl1.get_Layer(0) as IFeatureSelection;
                    pFeatureSelectionClear.Clear();
                    axMapControl1.Refresh();
                }
            }
            #endregion

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值