步骤
1,创建字段 IFields
1 /// <summary> 2 /// 创建:"面"-"点数"的字段. 3 /// </summary> 4 /// <returns></returns> 5 public static ESRI.ArcGIS.Geodatabase.IFields CreateFields() { 6 ESRI.ArcGIS.Geodatabase.IField fieldId = new ESRI.ArcGIS.Geodatabase.FieldClass(); 7 // todo(IFieldEdit2). 8 // 面ID. 9 ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldIdEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldId; 10 fieldIdEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger; 11 fieldIdEdit.Name_2 = " 面ID " ; 12 13 // 点数. 14 ESRI.ArcGIS.Geodatabase.IField fieldCount = new ESRI.ArcGIS.Geodatabase.FieldClass(); 15 ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldCountEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldCount; 16 fieldCountEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger; 17 fieldCountEdit.Name_2 = " 个数 " ; 18 19 ESRI.ArcGIS.Geodatabase.IObjectClassDescription objClsDes = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass(); 20 ESRI.ArcGIS.Geodatabase.IFields fields = objClsDes.RequiredFields; 21 ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; 22 fieldsEdit.AddField(fieldId); 23 fieldsEdit.AddField(fieldCount); 24 return fields; 25 }
2,根据Fields穿件 ITable
1 /// <summary> 2 /// Creates a table with some default fields. 3 /// </summary> 4 /// <param name="workspace"> An IWorkspace2 interface </param> 5 /// <param name="tableName"> 表名称,如: "owners" </param> 6 /// <param name="fields"> An IFields interface or Nothing. </param> 7 /// <returns></returns> 8 public static ESRI.ArcGIS.Geodatabase.ITable CreateTable(ESRI.ArcGIS.Geodatabase.IWorkspace2 workspace, System.String tableName, ESRI.ArcGIS.Geodatabase.IFields fields) { 9 // create the behavior clasid for the featureclass 10 ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass(); 11 // valid feature workspace not passed in as an argument to the method 12 if (workspace == null ) return null ; 13 14 ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; 15 16 // table with that name already exists return that table 17 if (workspace.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTTable, tableName)) 18 return featureWorkspace.OpenTable(tableName); 19 20 uid.Value = " esriGeoDatabase.Object " ; 21 22 ESRI.ArcGIS.Geodatabase.IObjectClassDescription objClsDes = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass(); 23 24 // if a fields collection is not passed in then supply our own 25 if (fields == null ) { 26 // create the fields using the required fields method 27 fields = objClsDes.RequiredFields; 28 ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast 29 30 ESRI.ArcGIS.Geodatabase.IField field = new ESRI.ArcGIS.Geodatabase.FieldClass(); 31 32 // create a user defined text field 33 ESRI.ArcGIS.Geodatabase.IFieldEdit fieldEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit)field; 34 35 // setup field properties 36 fieldEdit.Name_2 = " SampleField " ; 37 fieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString; 38 fieldEdit.IsNullable_2 = true ; 39 fieldEdit.AliasName_2 = " Sample Field Column " ; 40 fieldEdit.DefaultValue_2 = " test " ; 41 fieldEdit.Editable_2 = true ; 42 fieldEdit.Length_2 = 100 ; 43 44 // add field to field collection 45 fieldsEdit.AddField(field); 46 fields = (ESRI.ArcGIS.Geodatabase.IFields)fieldsEdit; 47 } 48 49 // Use IFieldChecker to create a validated fields collection. 50 ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass(); 51 ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null ; 52 ESRI.ArcGIS.Geodatabase.IFields validatedFields = null ; 53 fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)workspace; 54 fieldChecker.Validate(fields, out enumFieldError, out validatedFields); 55 56 // The enumFieldError enumerator can be inspected at this point to determine 57 // which fields were modified during validation. 58 59 // create and return the table 60 return featureWorkspace.CreateTable(tableName, validatedFields, uid, null , "" ); 61 }
3,根据创建ITable的结构,统计"面"要素中"点"要素的个数
1 /// <summary> 2 /// 查询"面"要素中的"点个数". 3 /// </summary> 4 /// <param name="polygonFeatureClass"> "面"要素类. </param> 5 /// <param name="pointFeatureClass"> "点"要素类. </param> 6 /// <param name="t"> ITable表. </param> 7 /// <returns></returns> 8 public static ESRI.ArcGIS.Geodatabase.ITable PointInPolygonCount(ESRI.ArcGIS.Geodatabase.IFeatureClass polygonFeatureClass, ESRI.ArcGIS.Geodatabase.IFeatureClass pointFeatureClass, ESRI.ArcGIS.Geodatabase.ITable t) { 9 if (!(polygonFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)) 10 throw new InvalidCastException( " Statistics_Assist::PointInPolygonCount:polygonFeatureClass " ); 11 if (!(pointFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)) 12 throw new InvalidCastException( " Statistics_Assist::PointInPolygonCount:pointFeatureClass " ); 13 14 ESRI.ArcGIS.Geodatabase.ISpatialFilter polySpatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass(); 15 polySpatialFilter.WhereClause = " name like '%辽宁%' " ; // 指定名称的"面"要素. 16 17 ESRI.ArcGIS.Geodatabase.IFeatureCursor fCurPoly = polygonFeatureClass.Search(polySpatialFilter, false ); 18 ESRI.ArcGIS.Geodatabase.IFeature fPoly = null ; 19 while ((fPoly = fCurPoly.NextFeature()) != null ) { 20 ESRI.ArcGIS.Geometry.IGeometry polyGeo = fPoly.Shape; 21 ESRI.ArcGIS.Geodatabase.ISpatialFilter pntSpatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass(); 22 pntSpatialFilter.Geometry = polyGeo; 23 // 包含在"面"要素中的"点". 24 pntSpatialFilter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelContains; 25 ESRI.ArcGIS.Geodatabase.IFeatureCursor fCurPnt = pointFeatureClass.Search(pntSpatialFilter, false ); 26 ESRI.ArcGIS.Geodatabase.IFeature fPnt = null ; 27 int count = 0 ; /// /"点"要素的个数. 28 while ((fPnt = fCurPnt.NextFeature()) != null ) 29 ++ count; 30 31 if (count != 0 ) { 32 ESRI.ArcGIS.Geodatabase.IRow r = t.CreateRow(); 33 r.set_Value( 1 , fPoly.get_Value( 0 )); // 注意的是,0下标是不可更改的(OID). 34 r.set_Value( 2 , count); 35 r.Store(); 36 } 37 } 38 39 return t; 40 }
4,在住函数中调用
1 try { 2 // 面. 3 ESRI.ArcGIS.Carto.IFeatureLayer PolyFLyr = (ESRI.ArcGIS.Carto.IFeatureLayer)Engine.App_Code.Layer_Assist.GetLayerByName(mapCtrl_main.Map, " 省市 " ); 4 5 // 点. 6 ESRI.ArcGIS.Carto.IFeatureLayer PntFLyr = (ESRI.ArcGIS.Carto.IFeatureLayer)Engine.App_Code.Layer_Assist.GetLayerByName(mapCtrl_main.Map, " 地市级以上居民地 " ); 7 8 // 表. 9 string tPath = @" G:\doc\gis\1.400\data\feature " ; 10 11 // 1,根据"表路径"和"表名"创建. 12 // ESRI.ArcGIS.Geodatabase.ITable tOri = Engine.App_Code.AttributeTable_Assist.CreateTable(tPath, "tOri"); 13 // 2,根据"表路径"和"表名",以及"字段"创建. 14 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 wsf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); 15 ESRI.ArcGIS.Geodatabase.IWorkspace2 ws = (ESRI.ArcGIS.Geodatabase.IWorkspace2)wsf.OpenFromFile(tPath, 0 ); 16 ESRI.ArcGIS.Geodatabase.IFields fields = Engine.App_Code.AttributeTable_Assist.CreateFields(); 17 ESRI.ArcGIS.Geodatabase.ITable t = Engine.App_Code.AttributeTable_Assist.CreateTable(ws, " fields_table_LiaoNing " , fields); 18 // 查询面中的点. 19 ESRI.ArcGIS.Geodatabase.ITable tRes = Engine.App_Code.Statistics_Assist.PointInPolygonCount(PolyFLyr.FeatureClass, PntFLyr.FeatureClass, t); 20 }
可以通过"表路径"和"表名"创建ITable
1 /// <summary> 2 /// 创建表. 3 /// </summary> 4 /// <param name="tablePath"> 即将创建表的路径,如:"G:\doc\gis\1.400\data\feature" </param> 5 /// <param name="tableName"> 表名,如:"owner" </param> 6 /// <returns></returns> 7 public static ESRI.ArcGIS.Geodatabase.ITable CreateTable( string tablePath, string tableName) { 8 // 需要注意的是必须为"IWorkspaceFactory2",不能为"IWorkspaceFactory". 9 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 wsf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); 10 ESRI.ArcGIS.Geodatabase.IFeatureWorkspace fws = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)wsf.OpenFromFile(tablePath, 0 ); 11 12 ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass(); 13 14 ESRI.ArcGIS.Geodatabase.IField fieldId = new ESRI.ArcGIS.Geodatabase.FieldClass(); 15 // todo(IFieldEdit2). 16 // 面ID. 17 ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldIdEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldId; 18 fieldIdEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger; 19 fieldIdEdit.Name_2 = " 面ID " ; 20 21 // 点数. 22 ESRI.ArcGIS.Geodatabase.IField fieldCount = new ESRI.ArcGIS.Geodatabase.FieldClass(); 23 ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldCountEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldCount; 24 fieldCountEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger; 25 fieldCountEdit.Name_2 = " 个数 " ; 26 27 ESRI.ArcGIS.Geodatabase.IObjectClassDescription objClsDes = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass(); 28 ESRI.ArcGIS.Geodatabase.IFields fields = objClsDes.RequiredFields; 29 ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; 30 fieldsEdit.AddField(fieldId); 31 fieldsEdit.AddField(fieldCount); 32 33 return fws.CreateTable(tableName, fields, uid, null , "" ); 34 }