核心代码:
1 #region JoinWork_IntersectMax 2 private void CreateNewFields_IntersectMax(IFeatureClass destFeatureClass,IFeatureClass sourceFeatureClass) 3 { 4 List<IField> fieldList = new List<IField>(); 5 foreach (var item in this.chkField.CheckedItems) 6 { 7 IField field=sourceFeatureClass.Fields.get_Field( sourceFeatureClass.Fields.FindField(item.ToString())); 8 fieldList.Add(field); 9 } 10 string[] fieldArray = Utils.FieldUtils.GetFieldsNameArray(destFeatureClass); 11 foreach (IField field in fieldList) 12 { 13 if (!fieldArray.Contains(field.Name)) 14 { 15 Utils.FieldUtils.CreateField(destFeatureClass, field.Type, field.Name, "Source_"+ field.Name, field.Length, field.Precision, field.Scale); 16 } 17 } 18 } 19 private void GetFieldIndex(IFeatureClass destFeatureClass, IFeatureClass sourceFeatureClass, out List<int> destCLSFieldIDList, out List<int> sourceCLSFieldIDList) 20 { 21 List<int> destIdList = new List<int>(); 22 List<int> sourceIdList = new List<int>(); 23 foreach (var item in this.chkField.CheckedItems) 24 { 25 destIdList.Add( destFeatureClass.Fields.FindField(item.ToString())); 26 sourceIdList.Add( sourceFeatureClass.Fields.FindField(item.ToString())); 27 } 28 destCLSFieldIDList = destIdList; 29 sourceCLSFieldIDList = sourceIdList; 30 } 31 32 private void JoinWork_IntersectMax(IFeatureClass featureClassD, IFeatureClass featureClassS) 33 { 34 IFeatureCursor featureCursorD = null; 35 IFeatureCursor featureCursorD_S = null; 36 try 37 { 38 int index = 1; 39 int count = featureClassD.FeatureCount(null); 40 Utils.UserArcMapProgressbar userArcMapProgressbar = new UserArcMapProgressbar(m_hookHelper); 41 userArcMapProgressbar.SetProgressbar(1,count, 1); 42 43 //create Fields 44 CreateNewFields_IntersectMax(featureClassD, featureClassS); 45 //get link field id list 46 List<int> destCLSFieldIdList; 47 List<int> sourceCLSFieldIdList; 48 GetFieldIndex(featureClassD, featureClassS, out destCLSFieldIdList, out sourceCLSFieldIdList); 49 50 Utils.UserWorkspace.FeatureWorkspace featureWorkspace = new Utils.UserWorkspace.FeatureWorkspace(featureClassD); 51 featureWorkspace.StartEdit(false); 52 53 featureCursorD = featureClassD.Update(null, true); 54 IFeature featureD = featureCursorD.NextFeature(); 55 while (featureD != null) 56 { 57 userArcMapProgressbar.Position = index; 58 userArcMapProgressbar.Message = $"空间连接_相交,取重叠面积最大者_已处理:{index}/{count}"; 59 userArcMapProgressbar.Step(); 60 ISpatialFilter spatialFilterD_S = Utils.FilterUtils.SpatialFilter(featureD.Shape, esriSpatialRelEnum.esriSpatialRelIntersects); 61 featureCursorD_S = featureClassS.Search(spatialFilterD_S, true); 62 IFeature featureS = featureCursorD_S.NextFeature(); 63 Dictionary<int, double> feature_Area = new Dictionary<int, double>(); 64 while (featureS != null) 65 { 66 ITopologicalOperator topologicalOperatorD_S = featureD.Shape as ITopologicalOperator; 67 IGeometry geometryD_S = topologicalOperatorD_S.Intersect(featureS.Shape, esriGeometryDimension.esriGeometry2Dimension); 68 if (!feature_Area.ContainsKey(featureS.OID)) 69 feature_Area.Add(featureS.OID, (geometryD_S as IArea).Area); 70 featureS = featureCursorD_S.NextFeature(); 71 } 72 if (feature_Area.Count > 0) 73 { 74 IFeature featureGoal; 75 var matches = feature_Area.Where(item => item.Value == feature_Area.Values.Max()); 76 featureGoal = featureClassS.GetFeature(matches.First().Key); 77 for (int i = 0; i < sourceCLSFieldIdList.Count; i++) 78 { 79 featureD.set_Value(destCLSFieldIdList[i], featureGoal.get_Value(sourceCLSFieldIdList[i])); 80 } 81 featureCursorD.UpdateFeature(featureD); 82 } 83 featureD = featureCursorD.NextFeature(); 84 index++; 85 } 86 userArcMapProgressbar.Hide(); 87 featureWorkspace.StopEdit(true); 88 } 89 catch (Exception exp) 90 { 91 ErrorForm err = new ErrorForm(exp.Message + "\r\n" + exp.StackTrace); 92 err.Show(); 93 } 94 finally 95 { 96 System.Runtime.InteropServices.Marshal.FinalReleaseComObject(featureCursorD); 97 System.Runtime.InteropServices.Marshal.FinalReleaseComObject(featureCursorD_S); 98 } 99 } 100 #endregion
原文地址:https://www.cnblogs.com/yzhyingcool/p/11013145.html
时间: 2024-11-06 07:18:45