ArcGIS AddIn开发之:固定距离+节点生成点

用途:用以确定线状地物的控制点,设立界桩等控规、水域确权划界的业务需求。


此处只列出核心代码:

private int generatePntsByFeature(IFeature pFeature,IFeatureLayer pPointLyr,int pntid)
        {
            IFeatureCursor pPointCursor = pPointLyr.FeatureClass.Insert(true);
            IWorkspaceEdit pwsEdit = (IWorkspaceEdit)((IDataset)pPointLyr.FeatureClass).Workspace;
            pwsEdit.StartEditing(false);
            pwsEdit.StartEditOperation();
            string leixing = "", lnxuhao = "", suozaixiangzhen = "", leftorright = "", rivername = "", riverduanname = "", rvzimu = "", lxzimu = "";
            if (chkJiaShan.Checked)  //此处为某项目的特殊需求,用以实现对该项目相关属性的写入
            {
                leixing = cmbLineType.Items[cmbLineType.SelectedIndex].ToString();
                lnxuhao = pFeature.get_Value(pFeature.Fields.FindField("线段编号")).ToString();
                suozaixiangzhen = pFeature.get_Value(pFeature.Fields.FindField("所在乡镇")).ToString();
                
                leftorright = pFeature.get_Value(pFeature.Fields.FindField(cmbLeftOrRight.Items[cmbLeftOrRight.SelectedIndex].ToString())).ToString();
                rivername = pFeature.get_Value(pFeature.Fields.FindField(cmbRiverName.Items[cmbRiverName.SelectedIndex].ToString())).ToString();
                riverduanname = pFeature.get_Value(pFeature.Fields.FindField(cmbRiverDuanName.Items[cmbRiverDuanName.SelectedIndex].ToString())).ToString();
                NCPinYinHelpercs ncp = new NCPinYinHelpercs();
                rvzimu = rivername;
                lxzimu = leixing;
                //lxzimu = ncp.GetShortPinyin(leixing);
                if (leixing != "中心线")
                {
                    //lxzimu = ncp.GetShortPinyin(leftorright.Substring(0, 1)) + "-" + lxzimu;
                    lxzimu = leftorright.Substring(0, 1) + "-" + lxzimu;
                }
            }

            string pntArray = "";

            if (rdVertex.Checked)  <span style="font-family:Microsoft YaHei;">//根据线的节点生成点</span>
            {
                IPointCollection pntCol = (IPointCollection)pFeature.Shape;
                for (int i = 0; i < pntCol.PointCount; i++)
                {
                    if (i == 0 && !chkOverLayPoint.Checked)
                    {
                        //检查首点是否重复
                        ISpatialFilter psf = new SpatialFilterClass();
                        ITopologicalOperator ptptmp =pntCol.get_Point(0) as ITopologicalOperator;
                        IGeometry pgm = ptptmp.Buffer(0.2);
                        psf.Geometry = pgm;
                        psf.SpatialRel = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects;
                        IFeatureCursor ptmpCursor = pPointLyr.FeatureClass.Search(psf, false);
                        IFeature ptmpFeature = ptmpCursor.NextFeature();
                        if (ptmpFeature != null)
                        {
                            pntArray = "," + ptmpFeature.get_Value(ptmpFeature.Fields.FindField("信息点编号")).ToString();
                            continue;
                        }
                    }

                    IFeatureBuffer pntFeatureBuffer = pPointLyr.FeatureClass.CreateFeatureBuffer();
                    pntFeatureBuffer.Shape = pntCol.get_Point(i);

                    if (chkJiaShan.Checked)  //项目需求,对属性的写入
                    {
                        string nid = rvzimu + "-" + lxzimu + "-D" + pntid.ToString();
                        pntArray = pntArray + "," + nid;
                        pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("信息点编号"), nid);
                        pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("源序号"), leixing + "_" + lnxuhao);
                        pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("所在乡镇"), suozaixiangzhen);
                        pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("河道名称"), rivername);
                        pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("河段名称"), riverduanname);
                        pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("线段类型"), leixing);
                    }
                    else
                    {
                        //编号写入一个默认的字段中
                        pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("PointId"),pntid);
                    }

                    pPointCursor.InsertFeature(pntFeatureBuffer);
                    pntid++;
                }
                pPointCursor.Flush();
            }
            else if (rdEqualDis.Checked)  //按照一定间距生成节点
            {
                IPolyline pln = pFeature.Shape as IPolyline;
                IPoint pnt = null;
                for (double i = SplitDis; i < pln.Length; i = i + SplitDis)
                {
                    if (i == 0)
                    {
                        if (!chkOverLayPoint.Checked)
                        {
                            //检查首点是否重复时
                            //检查首点是否重复
                            ISpatialFilter psf = new SpatialFilterClass();
                            ITopologicalOperator ptptmp =pln.FromPoint as ITopologicalOperator;
                            IGeometry pgm = ptptmp.Buffer(0.2);
                            psf.Geometry = pgm;
                            psf.SpatialRel = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects;
                            IFeatureCursor ptmpCursor = pPointLyr.FeatureClass.Search(psf, false);
                            IFeature ptmpFeature = ptmpCursor.NextFeature();
                            if (ptmpFeature != null)
                            {
                                pntArray = "," + ptmpFeature.get_Value(ptmpFeature.Fields.FindField("信息点编号")).ToString();
                                continue;
                            }
                            else
                            {
                                //不包含,取首点
                                pnt = pln.FromPoint;
                            }
                        }
                        else
                        {
                            //不检查时,直接取首点
                            pnt = pln.FromPoint;
                        }
                    }
                    else if(i>0 && (pln.Length - i < 0.2 * SplitDis))
                    {
                        //此代码的含义为最后一个点时,如果点很接近终点,则直接使用终点,不进行操作
                        break;
                    }
                    else
                    {
                        pnt = getpntAtDis(pln, i);
                    }
                    
                    string nid;
                    IFeatureBuffer pNewFeature = pPointLyr.FeatureClass.CreateFeatureBuffer();

                    nid = rvzimu + "-" + lxzimu + "-D" + pntid.ToString();

                    pNewFeature.Shape = pnt;
                    pNewFeature.set_Value(pNewFeature.Fields.FindField("信息点编号"), nid);
                    pNewFeature.set_Value(pNewFeature.Fields.FindField("源序号"), leixing + "-" + lnxuhao);
                    pNewFeature.set_Value(pNewFeature.Fields.FindField("所在乡镇"), suozaixiangzhen);
                    pNewFeature.set_Value(pNewFeature.Fields.FindField("河道名称"), rivername);
                    pNewFeature.set_Value(pNewFeature.Fields.FindField("河段名称"), riverduanname);
                    //pNewFeature.set_Value(pNewFeature.Fields.FindField("线段类型"), leixing);

                    pPointCursor.InsertFeature(pNewFeature);
                    pntArray = pntArray + "," + nid;
                    pntid++;
                }

                //处理终点
                pnt = pln.ToPoint;
                string nid2;
                IFeatureBuffer pNewFeature2 = pPointLyr.FeatureClass.CreateFeatureBuffer();

                nid2 = rvzimu + "-" + lxzimu + "-D" + pntid.ToString();

                pNewFeature2.Shape = pnt;
                pNewFeature2.set_Value(pNewFeature2.Fields.FindField("信息点编号"), nid2);
                pNewFeature2.set_Value(pNewFeature2.Fields.FindField("源序号"), leixing + "-" + lnxuhao);
                pNewFeature2.set_Value(pNewFeature2.Fields.FindField("所在乡镇"), suozaixiangzhen);
                pNewFeature2.set_Value(pNewFeature2.Fields.FindField("河道名称"), rivername);
                pNewFeature2.set_Value(pNewFeature2.Fields.FindField("河段名称"), riverduanname);
                //pNewFeature.set_Value(pNewFeature.Fields.FindField("线段类型"), leixing);

                pPointCursor.InsertFeature(pNewFeature2);
                pntArray = pntArray + "," + nid2;
                pntid++;

                pPointCursor.Flush();
            }
            else if (rdMix.Checked)  //节点+固定距离生成节点模式
            {
                IPointCollection pntCol = (IPointCollection)pFeature.Shape;
                IPolyline pln = pFeature.Shape as IPolyline;
                //先生成节点序列,两两判断节点间距离,如果过长,则生成splitdis的等分点
                IPoint CurrentPoint, PrePoint;
                for (int i = 0; i < pntCol.PointCount; i++)
                {
                    if (i == 0 && !chkOverLayPoint.Checked)
                    {
                        //检查首点是否重复
                        ISpatialFilter psf = new SpatialFilterClass();
                        ITopologicalOperator ptptmp = pntCol.get_Point(0) as ITopologicalOperator;
                        IGeometry pgm = ptptmp.Buffer(0.2);
                        psf.Geometry = pgm;
                        psf.SpatialRel = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects;
                        IFeatureCursor ptmpCursor = pPointLyr.FeatureClass.Search(psf, false);
                        IFeature ptmpFeature = ptmpCursor.NextFeature();
                        if (ptmpFeature != null)
                        {
                            pntArray = "," + ptmpFeature.get_Value(ptmpFeature.Fields.FindField("信息点编号")).ToString();
                            continue;
                        }
                    }

                    IFeatureBuffer pntFeatureBuffer;
                    string nid;
                    //是否需要增加中点
                    if (i > 0)
                    {
                        CurrentPoint = pntCol.get_Point(i);
                        PrePoint = pntCol.get_Point(i - 1);
                        IPointCollection tmpPolyline = new PolylineClass();
                        tmpPolyline.AddPoint(PrePoint);
                        tmpPolyline.AddPoint(CurrentPoint);

                        double lenght = Math.Sqrt((CurrentPoint.X - PrePoint.X) * (CurrentPoint.X - PrePoint.X) + (CurrentPoint.Y - PrePoint.Y) * (CurrentPoint.Y - PrePoint.Y));
                        //此时需要增加中点
                        for (int zi = 0; zi < Math.Round(Math.Round(lenght / SplitDis) - 1);zi++ )
                        {
                            IPoint pnt = getpntAtDis((IPolyline)tmpPolyline, (zi+1)*SplitDis);
                            nid = rvzimu + "-" + lxzimu + "-D" + pntid.ToString();
                            pntArray = pntArray + "," + nid;

                            pntFeatureBuffer = pPointLyr.FeatureClass.CreateFeatureBuffer();
                            pntFeatureBuffer.Shape = pnt;
                            pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("信息点编号"), nid);
                            pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("源序号"), leixing + "_" + lnxuhao);
                            pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("所在乡镇"), suozaixiangzhen);
                            pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("河道名称"), rivername);
                            pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("河段名称"), riverduanname);
                            //pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("线段类型"), leixing);
                            pPointCursor.InsertFeature(pntFeatureBuffer);
                            pntid++;
                        }
                    }

                    nid = rvzimu + "-" + lxzimu + "-D" + pntid.ToString();
                    pntArray = pntArray + "," + nid;

                    pntFeatureBuffer = pPointLyr.FeatureClass.CreateFeatureBuffer();
                    pntFeatureBuffer.Shape = pntCol.get_Point(i);
                    pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("信息点编号"), nid);
                    pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("源序号"), leixing + "_" + lnxuhao);
                    pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("所在乡镇"), suozaixiangzhen);
                    pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("河道名称"), rivername);
                    pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("河段名称"), riverduanname);
                    //pntFeatureBuffer.set_Value(pntFeatureBuffer.Fields.FindField("线段类型"), leixing);
                    pPointCursor.InsertFeature(pntFeatureBuffer);
                    pntid++;
                }
                pPointCursor.Flush();
            }
            else
            {

            }
            string ssstmp = pntArray.Remove(0,1);
            pFeature.set_Value(pFeature.Fields.FindField("点序列"),ssstmp); //去掉第一个,
            pwsEdit.StopEditOperation();
            pwsEdit.StopEditing(true);
            return pntid;
        }

        private IPoint getpntAtDis(IPolyline pln, double dis)
        {
            IConstructPoint pCPoint = new PointClass();
            pCPoint.ConstructAlong(pln, esriSegmentExtension.esriNoExtension, dis, false);
            return (IPoint)pCPoint;
        }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值