C#+AE不同shp文件要素的复制
C#+AE做的一个DEMO,实现了在不同shp文件下要素的复制,其中shp文件要素属性格式相同。
开发环境:VS2010+ArcObject10.1
功能介绍:首先,窗体加载的时候,加载数据AsiaCities_new.shp和AsiaCities_old.shp。
点击按钮,执行复制的过程。即筛选AsiaCities_old.shp中POPULATION = 0的所有要素,然后复制到AsiaCities_new.shp中,复制之前先判断复制后 的文件中是否已存在复制的要素,如已存在则不复制,如不存在则复制。
代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using ESRI.ArcGIS.Carto; 10 using ESRI.ArcGIS.Geodatabase; 11 using ESRI.ArcGIS.Geometry; 12 using ESRI.ArcGIS.Display; 13 14 namespace Test_FieldsQuery 15 { 16 public partial class Form1 : Form 17 { 18 ILayer pLayer1; 19 IFeatureLayer pFeatureLayer1; 20 IFeatureClass pFeatureClass1; 21 22 ILayer pLayer; 23 IFeatureLayer pFeatureLayer; 24 IFeatureClass pFeatureClass; 25 26 public Form1() 27 { 28 InitializeComponent(); 29 } 30 31 private void Form1_Load(object sender, EventArgs e) 32 { 33 axMapControl1.AddShapeFile("数据\\", "AsiaCities_new.shp"); 34 axMapControl1.AddShapeFile("数据\\", "AsiaCities_old.shp"); 35 36 pLayer1 = axMapControl1.get_Layer(1); 37 pFeatureLayer1 = pLayer1 as IFeatureLayer; 38 pFeatureClass1 = pFeatureLayer1.FeatureClass; 39 40 pLayer = axMapControl1.get_Layer(0); 41 pFeatureLayer = pLayer as IFeatureLayer; 42 pFeatureClass = pFeatureLayer.FeatureClass; 43 } 44 45 private void button1_Click(object sender, EventArgs e) 46 { 47 IQueryFilter pQueryFilter1 = new QueryFilterClass(); 48 pQueryFilter1.WhereClause = "POPULATION = 0"; 49 50 IFeatureCursor pFeatureCursor1 = pFeatureClass1.Search(pQueryFilter1, false); 51 IFeature pFeature1 = pFeatureCursor1.NextFeature(); 52 53 54 55 IQueryFilter pQueryFilter = new QueryFilterClass(); 56 pQueryFilter.WhereClause = "POPULATION = 0"; 57 58 IFeatureCursor pFeatureCursor = pFeatureClass.Search(pQueryFilter, false); 59 IFeature pFeature = pFeatureCursor.NextFeature(); 60 61 int i=0,j=0; //判断全部或者部分要素复制 62 63 while (pFeature != null) 64 { 65 bool flag0 = false; 66 67 ITable pTable=pFeatureClass as ITable; 68 IRow pRow = pTable.GetRow(pFeature.OID); 69 70 while (pFeature1 != null) 71 { 72 ITable pTable1 = pFeatureClass1 as ITable; 73 IRow pRow1 = pTable1.GetRow(pFeature1.OID); 74 75 string a0 = pRow.get_Value(1).ToString(); 76 string b0 = pRow.get_Value(2).ToString(); 77 string c0 = pRow.get_Value(3).ToString(); 78 string d0 = pRow.get_Value(4).ToString(); 79 string e0 = pRow.get_Value(5).ToString(); 80 string f0 = pRow.get_Value(6).ToString(); 81 82 string a1 = pRow1.get_Value(1).ToString(); 83 string b1 = pRow1.get_Value(2).ToString(); 84 string c1 = pRow1.get_Value(3).ToString(); 85 string d1 = pRow1.get_Value(4).ToString(); 86 string e1 = pRow1.get_Value(5).ToString(); 87 string f1 = pRow1.get_Value(6).ToString(); 88 89 if (a0 == a1 && b0 == b1 && c0 == c1 && d0 == d1 && e0 == e1 && f0 == f1) 90 { 91 pFeature1 = pFeatureCursor1.NextFeature(); 92 flag0 = true; 93 i++; 94 break; 95 } 96 pFeature1 = pFeatureCursor1.NextFeature(); 97 } 98 if (flag0 == true) 99 { 100 pFeature = pFeatureCursor.NextFeature(); 101 continue; 102 } 103 if (AddFeatureToFeatureClass(pFeatureClass1, pFeature)) 104 { 105 j++; 106 } 107 pFeature = pFeatureCursor.NextFeature(); 108 } 109 if (i > 0 && j > 0) 110 MessageBox.Show("部分要素已复制,其中部分要素重复!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 111 if (i > 0 && j == 0) 112 MessageBox.Show("没有复制任何要素,复制的要素均已存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 113 if (i == 0 && j > 0) 114 MessageBox.Show("所有要素均已复制!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 115 } 116 117 private bool AddFeatureToFeatureClass(IFeatureClass pFeatureClass, IFeature pFeature) 118 { 119 IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true); 120 IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer(); 121 IFields pFields = pFeatureClass.Fields; 122 for (int i = 1; i <= pFields.FieldCount - 1; i++) 123 { 124 IField pField = pFields.get_Field(i); 125 if (pField.Type == esriFieldType.esriFieldTypeGeometry) 126 { 127 pFeatureBuffer.set_Value(i, pFeature.Shape); 128 } 129 else 130 { 131 switch (pField.Type) 132 { 133 case esriFieldType.esriFieldTypeInteger: 134 pFeatureBuffer.set_Value(i, Convert.ToInt32(pFeature.get_Value(i))); 135 break; 136 case esriFieldType.esriFieldTypeDouble: 137 pFeatureBuffer.set_Value(i, Convert.ToDouble(pFeature.get_Value(i))); 138 break; 139 case esriFieldType.esriFieldTypeString: 140 pFeatureBuffer.set_Value(i, Convert.ToString(pFeature.get_Value(i))); 141 break; 142 default: 143 break; 144 } 145 } 146 } 147 pFeatureCursor.InsertFeature(pFeatureBuffer); 148 return true; 149 } 150 } 151 }
DEMO下载地址:Test_FieldsQuery.rar