ArcGIS Engine - 界址点自动编号

		/// 
        /// 自动编号选择起点
        /// 
        public static Response AutoNumber(int PartCount)
        {
            partCount = PartCount;
            ILayer mLayer = GetLayerByName("范围线");
            ILayer pLayer = GetLayerByName("界址点");
            if (mLayer != null)
            {
                if (pLayer != null)
                {
                    string name = "";
                    if (PartCount != 0)
                    {
                        name = "下一部件";
                    }
                    if (MessageBox.Show("请拉框选择" + name + "开始编号的起点!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        operation = "选择编号起点";

                        //获取所有图层是否可选列表
                        GetAllLayer(listLayer, Selectable);
                        //设置只有界址点图层可选
                        IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                        pFeatureLayer.Selectable = true;
                    }
                    else
                    {
                        operation = "";
                        return new Response(Constant.Nothing, "");
                    }
                }
                else
                {
                    MessageBox.Show("请先加载界址点!", "提示", MessageBoxButtons.OK);
                }
            }
            else
            {
                MessageBox.Show("请先加载范围线!", "提示", MessageBoxButtons.OK);
            }
            
            return new Response(Constant.Success, "");
        }
        
		/// 
        /// 地图点击事件
        /// 
        /// 事件发送者
        /// 事件参数
        private void aecMap_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            string operation = PageManager.operation;
            if (operation == "选择编号起点")
            {
                IEnvelope pEnv = MapControl.TrackRectangle();
                MapControl.Map.SelectByShape(pEnv, null, true);
                MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);

                ISelection selection = MapControl.Map.FeatureSelection;
                PageManager.AutoNumberGetPoint(selection, PageManager.partCount);
            }
            
            MapControl.ActiveView.Refresh();
        }

	    /// 
        /// 自动编号获取选择要素点
        /// 
        public static Response AutoNumberGetPoint(ISelection selection, int PartCount)
        {
            IEnumFeatureSetup enumFeatureSetup = selection as IEnumFeatureSetup;
            enumFeatureSetup.AllFields = true;
            IEnumFeature enumFeature = enumFeatureSetup as IEnumFeature;
            IFeature feature = enumFeature.Next();
            if (feature != null)
            {
                if (MessageBox.Show("是否将当前选择点设为编号的起点!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    ProcessBar processBar = new ProcessBar();
                    processBar.Show();
                    processBar.Update();
                    int featureID = (int)feature.get_Value(0);
                    
                    //获取界址点
                    ILayer pLayer = GetLayerByName("界址点");
                    IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                    IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
                    int Count = pFeatureClass.FeatureCount(null);

                    //获取范围线
                    ILayer mLayer = GetLayerByName("范围线");
                    IFeatureLayer mFeatureLayer = mLayer as IFeatureLayer;
                    IFeatureClass mFeatureClass = mFeatureLayer.FeatureClass;
                    IFeature mFeature = mFeatureClass.GetFeature(0);
                    IGeometry mGeometry = mFeature.Shape as IGeometry;
                    IGeometryCollection mGeoColl = mGeometry as IGeometryCollection;

                    //检查更新
                    CheckUpdate(pFeatureClass, mFeatureClass, Count, PartCount);

                    //开始编号
                    SerialNumber(pFeatureClass, featureID, mGeoColl, Count, PartCount);
                    processBar.Close();
                   
                    //恢复可选设置
                    for (int i = 0; i < Selectable.Count;i++ )
                    {
                        string LayerName = listLayer[i].ToString();
                        ILayer pLayerName = GetLayerByName(LayerName);
                        IFeatureLayer pFeatureLayerName = pLayerName as IFeatureLayer;
                        pFeatureLayerName.Selectable = Selectable[i];
                    }
                }
               
            }

            return new Response(Constant.Success, "");
        }

        /// 
        /// 检查更新
        /// 
        public static void CheckUpdate(IFeatureClass pFeatureClass, IFeatureClass mFeatureClass, int Count, int PartCount)
        {
            IFeature mFeature = mFeatureClass.GetFeature(0);
            IGeometry mGeometry = mFeature.Shape as IGeometry;
            IGeometryCollection mGeoColl = mGeometry as IGeometryCollection;

            //判断是否有Rank字段
            int Field = pFeatureClass.FindField("Rank");
            if (Field == -1)
            {
                IFieldEdit pFieldEdit = new FieldClass();
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
                pFieldEdit.Name_2 = "Rank";
                IClass pClass = pFeatureClass as IClass;
                pClass.AddField((IField)pFieldEdit);
            }

            //判断是否有Distance字段
            int Field1 = pFeatureClass.FindField("Distance");
            if (Field1 == -1)
            {
                IFieldEdit pFieldEdit = new FieldClass();
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
                pFieldEdit.Name_2 = "Distance";
                IClass pClass = pFeatureClass as IClass;
                pClass.AddField((IField)pFieldEdit);
            }

            //判断范围线是否为多部件要素
            if (mGeoColl.GeometryCount >= 2)
            {
                //判断是否有Part字段
                int mField = pFeatureClass.FindField("Part");
                if (mField == -1)
                {
                    IFieldEdit pFieldEdit = new FieldClass();
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
                    pFieldEdit.Name_2 = "Part";
                    IClass pClass = pFeatureClass as IClass;
                    pClass.AddField((IField)pFieldEdit);
                }

                //判断字段是否赋值
                for (int l = 0; l < Count; l++)
                {
                    int PartValue = (int)pFeatureClass.GetFeature(l).get_Value(7);
                    if (PartValue == 0)
                    {
                        //字段赋值
                        for (int i = 0; i < mGeoColl.GeometryCount; i++)
                        {
                            IGeometry pGeometry = mGeoColl.get_Geometry(i);
                            IPointCollection mPointCollection = pGeometry as IPointCollection;
                            IPolygon pPolygon = new PolygonClass();
                            IGeometry Geometry = pPolygon as IGeometry;
                            IPointCollection pPointCollection = Geometry as IPointCollection;
                            pPointCollection.AddPointCollection(mPointCollection);

                            ISpatialFilter pSpatialFilter = new SpatialFilterClass();
                            pSpatialFilter.Geometry = pPolygon as IGeometry;
                            pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                            IFeatureCursor pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);
                            IFeature pFeature = pFeatureCursor.NextFeature();
                            while (pFeature != null)
                            {
                                pFeature.set_Value(7, i + 1);
                                pFeature.Store();
                                pFeature = pFeatureCursor.NextFeature();
                            }
                        }
                    }
                }
            }

            //检查是否有新添加的点
            for (int i = 0; i < Count; i++)
            {
                int RankValue = (int)pFeatureClass.GetFeature(i).get_Value(5);
                if (RankValue == 0)
                {
                    //界址点排序检查
                    PointRank(pFeatureClass, mFeatureClass);
                }
            }

            if (PartCount == 0)
            {
                //检查是否有删除的点
                int RankValueSum = 0;
                for (int i = 0; i < Count; i++)
                {
                    RankValueSum += (int)pFeatureClass.GetFeature(i).get_Value(5);
                }
                if (RankValueSum != (1 + Count) * Count / 2)
                {
                    //界址点排序检查
                    PointRank(pFeatureClass, mFeatureClass);
                }
            }
        }

        /// 
        /// 开始编号
        /// 
        public static void SerialNumber(IFeatureClass pFeatureClass, int featureID, IGeometryCollection mGeoColl, int Count, int PartCount)
        {
            //根据选定点进行编号
            IDataset dataset = pFeatureClass as IDataset;
            IWorkspace workspace = dataset.Workspace;
            IWorkspaceEdit pWorkspaceEdit = workspace as IWorkspaceEdit;
            pWorkspaceEdit.StartEditing(false);
            pWorkspaceEdit.StartEditOperation();
            
            int LastPartCount = PartCount;
            //差值
            int DifferenceVale = (int)pFeatureClass.GetFeature(featureID).get_Value(5) - 1 - PartCount;

            //如果为多部件要素
            if (mGeoColl.GeometryCount >= 2)
            {
                int part = (int)pFeatureClass.GetFeature(featureID).get_Value(7);
                for (int i = 0; i < Count; i++)
                {
                    int PartValue = (int)pFeatureClass.GetFeature(i).get_Value(7);
                    if (PartValue == part)
                    {
                        PartCount++;
                    }
                }
                //根据Part字段进行属性筛选
                IQueryFilter queryFilter = new QueryFilterClass();
                queryFilter.WhereClause = "Part =" + "" + part + "";
                IFeatureCursor pFeatureCursor = pFeatureClass.Search(queryFilter, false);
                IFeature Feature = pFeatureCursor.NextFeature();
                while (Feature != null)
                {
                    int RankValue = (int)Feature.get_Value(5);
                    if (RankValue > DifferenceVale + LastPartCount)
                    {
                        int Pointrank = RankValue - DifferenceVale;
                        Feature.set_Value(5, Pointrank);
                        Feature.set_Value(4, "J" + Pointrank);
                        Feature.Store();
                    }
                    else
                    {
                        int Pointrank = RankValue - DifferenceVale + PartCount - LastPartCount;
                        Feature.set_Value(5, Pointrank);
                        Feature.set_Value(4, "J" + Pointrank);
                        Feature.Store();
                    }
                    Feature = pFeatureCursor.NextFeature();
                }
            }
            else
            {
                for (int i = 0; i < Count; i++)
                {
                    int RankValue = (int)pFeatureClass.GetFeature(i).get_Value(5);
                    if (RankValue > DifferenceVale)
                    {
                        int Pointrank = RankValue - DifferenceVale;
                        pFeatureClass.GetFeature(i).set_Value(5, Pointrank);
                        pFeatureClass.GetFeature(i).set_Value(4, "J" + Pointrank);
                        pFeatureClass.GetFeature(i).Store();
                    }
                    else
                    {
                        int Pointrank = RankValue - DifferenceVale + Count;
                        pFeatureClass.GetFeature(i).set_Value(5, Pointrank);
                        pFeatureClass.GetFeature(i).set_Value(4, "J" + Pointrank);
                        pFeatureClass.GetFeature(i).Store();
                    }
                }
            }
            pWorkspaceEdit.StopEditOperation();
            pWorkspaceEdit.StopEditing(true);
            MapControl.Map.ClearSelection();
            MapControl.ActiveView.Refresh();

            if (mGeoColl.GeometryCount >= 2 && Count > PartCount)
            {
                AutoNumber(PartCount);
            }
            else
            {
                MessageBox.Show("编号完成!", "提示", MessageBoxButtons.OK);
                operation = "";
            }
        }

        /// 
        /// 界址点排序
        /// 
        private static IFeatureClass PointRank(IFeatureClass pFeatureClass, IFeatureClass mFeatureClass)
        {
            for (int k = 0; k < mFeatureClass.FeatureCount(null); k++ )
            {
                IFeature mFeature = mFeatureClass.GetFeature(k);
                IPointCollection mPointCollection = mFeature.Shape as IPointCollection;
                List<double> DistanceList = new List<double>();
                IPoint outPoint = new PointClass();
                IPolyline pPolyline = new PolylineClass();
                IGeometry pGeometry = pPolyline as IGeometry;
                IPointCollection pPointCollection = pGeometry as IPointCollection;
                pPointCollection.AddPointCollection(mPointCollection);

                //点到曲线起点的距离
                double distanceAlongCurve = 0;
                for (int i = 0; i < pFeatureClass.FeatureCount(null); i++)
                {
                    IFeature Feature = pFeatureClass.GetFeature(i);
                    IPoint pPoint = Feature.Shape as IPoint;
                    pPolyline.QueryPointAndDistance(esriSegmentExtension.esriNoExtension, pPoint, false, outPoint, ref distanceAlongCurve, 0, false);
                    double Distance = Math.Round(distanceAlongCurve, 0, MidpointRounding.AwayFromZero);
                    Feature.set_Value(6, Distance);
                    Feature.Store();
                    DistanceList.Add(Distance);
                }

                //列表排序
                DistanceList.Sort();
                for (int i = 0; i < DistanceList.Count; i++)
                {
                    double Distance = DistanceList[i];
                    for (int l = 0; l < pFeatureClass.FeatureCount(null); l++)
                    {
                        IFeature Feature = pFeatureClass.GetFeature(l);
                        if (Distance == (double)Feature.get_Value(6))
                        {
                            IPoint point = Feature.Shape as IPoint;
                            Feature.set_Value(5, i + 1);
                            Feature.set_Value(3, point.Y);
                            Feature.set_Value(2, point.X);
                            Feature.Store();
                            break;
                        }
                    }
                }
            }
            
            return pFeatureClass;
        }

你可能感兴趣的:(ArcGIS,Engine,java,开发语言)