统计"面"要素中"点"要素的个数.

时间:2021-09-04 22:40:36

步骤

  1,创建字段 IFields

 /// <summary>
/// 创建:"面"-"点数"的字段.
/// </summary>
/// <returns></returns>
public static ESRI.ArcGIS.Geodatabase.IFields CreateFields() {
ESRI.ArcGIS.Geodatabase.IField fieldId = new ESRI.ArcGIS.Geodatabase.FieldClass();
//todo(IFieldEdit2).
//面ID.
ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldIdEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldId;
fieldIdEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger;
fieldIdEdit.Name_2 = "面ID"; //点数.
ESRI.ArcGIS.Geodatabase.IField fieldCount = new ESRI.ArcGIS.Geodatabase.FieldClass();
ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldCountEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldCount;
fieldCountEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger;
fieldCountEdit.Name_2 = "个数"; ESRI.ArcGIS.Geodatabase.IObjectClassDescription objClsDes = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass();
ESRI.ArcGIS.Geodatabase.IFields fields = objClsDes.RequiredFields;
ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields;
fieldsEdit.AddField(fieldId);
fieldsEdit.AddField(fieldCount);
return fields;
}

  2,根据Fields穿件 ITable

 /// <summary>
/// Creates a table with some default fields.
/// </summary>
/// <param name="workspace">An IWorkspace2 interface</param>
/// <param name="tableName">表名称,如: "owners"</param>
/// <param name="fields">An IFields interface or Nothing.</param>
/// <returns></returns>
public static ESRI.ArcGIS.Geodatabase.ITable CreateTable(ESRI.ArcGIS.Geodatabase.IWorkspace2 workspace, System.String tableName, ESRI.ArcGIS.Geodatabase.IFields fields) {
// create the behavior clasid for the featureclass
ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass();
// valid feature workspace not passed in as an argument to the method
if (workspace == null) return null; ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // table with that name already exists return that table
if (workspace.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTTable, tableName))
return featureWorkspace.OpenTable(tableName); uid.Value = "esriGeoDatabase.Object"; ESRI.ArcGIS.Geodatabase.IObjectClassDescription objClsDes = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass(); // if a fields collection is not passed in then supply our own
if (fields == null) {
// create the fields using the required fields method
fields = objClsDes.RequiredFields;
ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast ESRI.ArcGIS.Geodatabase.IField field = new ESRI.ArcGIS.Geodatabase.FieldClass(); // create a user defined text field
ESRI.ArcGIS.Geodatabase.IFieldEdit fieldEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit)field; // setup field properties
fieldEdit.Name_2 = "SampleField";
fieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString;
fieldEdit.IsNullable_2 = true;
fieldEdit.AliasName_2 = "Sample Field Column";
fieldEdit.DefaultValue_2 = "test";
fieldEdit.Editable_2 = true;
fieldEdit.Length_2 = ; // add field to field collection
fieldsEdit.AddField(field);
fields = (ESRI.ArcGIS.Geodatabase.IFields)fieldsEdit;
} // Use IFieldChecker to create a validated fields collection.
ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();
ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null;
ESRI.ArcGIS.Geodatabase.IFields validatedFields = null;
fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)workspace;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields); // The enumFieldError enumerator can be inspected at this point to determine
// which fields were modified during validation. // create and return the table
return featureWorkspace.CreateTable(tableName, validatedFields, uid, null, "");
}

  3,根据创建ITable的结构,统计"面"要素中"点"要素的个数

 /// <summary>
/// 查询"面"要素中的"点个数".
/// </summary>
/// <param name="polygonFeatureClass">"面"要素类.</param>
/// <param name="pointFeatureClass">"点"要素类.</param>
/// <param name="t">ITable表.</param>
/// <returns></returns>
public static ESRI.ArcGIS.Geodatabase.ITable PointInPolygonCount(ESRI.ArcGIS.Geodatabase.IFeatureClass polygonFeatureClass, ESRI.ArcGIS.Geodatabase.IFeatureClass pointFeatureClass, ESRI.ArcGIS.Geodatabase.ITable t) {
if (!(polygonFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon))
throw new InvalidCastException("Statistics_Assist::PointInPolygonCount:polygonFeatureClass");
if (!(pointFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint))
throw new InvalidCastException("Statistics_Assist::PointInPolygonCount:pointFeatureClass"); ESRI.ArcGIS.Geodatabase.ISpatialFilter polySpatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();
polySpatialFilter.WhereClause = "name like '%辽宁%'"; //指定名称的"面"要素. ESRI.ArcGIS.Geodatabase.IFeatureCursor fCurPoly = polygonFeatureClass.Search(polySpatialFilter, false);
ESRI.ArcGIS.Geodatabase.IFeature fPoly = null;
while ((fPoly = fCurPoly.NextFeature()) != null) {
ESRI.ArcGIS.Geometry.IGeometry polyGeo = fPoly.Shape;
ESRI.ArcGIS.Geodatabase.ISpatialFilter pntSpatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();
pntSpatialFilter.Geometry = polyGeo;
//包含在"面"要素中的"点".
pntSpatialFilter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelContains;
ESRI.ArcGIS.Geodatabase.IFeatureCursor fCurPnt = pointFeatureClass.Search(pntSpatialFilter, false);
ESRI.ArcGIS.Geodatabase.IFeature fPnt = null;
int count = ; ////"点"要素的个数.
while ((fPnt = fCurPnt.NextFeature()) != null)
++count; if (count != ) {
ESRI.ArcGIS.Geodatabase.IRow r = t.CreateRow();
r.set_Value(, fPoly.get_Value()); //注意的是,0下标是不可更改的(OID).
r.set_Value(, count);
r.Store();
}
} return t;
}

  4,在住函数中调用

 try {
//面.
ESRI.ArcGIS.Carto.IFeatureLayer PolyFLyr = (ESRI.ArcGIS.Carto.IFeatureLayer)Engine.App_Code.Layer_Assist.GetLayerByName(mapCtrl_main.Map, "省市"); //点.
ESRI.ArcGIS.Carto.IFeatureLayer PntFLyr = (ESRI.ArcGIS.Carto.IFeatureLayer)Engine.App_Code.Layer_Assist.GetLayerByName(mapCtrl_main.Map, "地市级以上居民地"); //表.
string tPath = @"G:\doc\gis\1.400\data\feature"; //1,根据"表路径"和"表名"创建.
//ESRI.ArcGIS.Geodatabase.ITable tOri = Engine.App_Code.AttributeTable_Assist.CreateTable(tPath, "tOri");
//2,根据"表路径"和"表名",以及"字段"创建.
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 wsf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
ESRI.ArcGIS.Geodatabase.IWorkspace2 ws = (ESRI.ArcGIS.Geodatabase.IWorkspace2)wsf.OpenFromFile(tPath, );
ESRI.ArcGIS.Geodatabase.IFields fields = Engine.App_Code.AttributeTable_Assist.CreateFields();
ESRI.ArcGIS.Geodatabase.ITable t = Engine.App_Code.AttributeTable_Assist.CreateTable(ws, "fields_table_LiaoNing", fields);
//查询面中的点.
ESRI.ArcGIS.Geodatabase.ITable tRes = Engine.App_Code.Statistics_Assist.PointInPolygonCount(PolyFLyr.FeatureClass, PntFLyr.FeatureClass, t);
}

可以通过"表路径"和"表名"创建ITable

 /// <summary>
/// 创建表.
/// </summary>
/// <param name="tablePath">即将创建表的路径,如:"G:\doc\gis\1.400\data\feature"</param>
/// <param name="tableName">表名,如:"owner"</param>
/// <returns></returns>
public static ESRI.ArcGIS.Geodatabase.ITable CreateTable(string tablePath, string tableName) {
//需要注意的是必须为"IWorkspaceFactory2",不能为"IWorkspaceFactory".
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 wsf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace fws = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)wsf.OpenFromFile(tablePath, ); ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass(); ESRI.ArcGIS.Geodatabase.IField fieldId = new ESRI.ArcGIS.Geodatabase.FieldClass();
//todo(IFieldEdit2).
//面ID.
ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldIdEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldId;
fieldIdEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger;
fieldIdEdit.Name_2 = "面ID"; //点数.
ESRI.ArcGIS.Geodatabase.IField fieldCount = new ESRI.ArcGIS.Geodatabase.FieldClass();
ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldCountEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldCount;
fieldCountEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger;
fieldCountEdit.Name_2 = "个数"; ESRI.ArcGIS.Geodatabase.IObjectClassDescription objClsDes = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass();
ESRI.ArcGIS.Geodatabase.IFields fields = objClsDes.RequiredFields;
ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields;
fieldsEdit.AddField(fieldId);
fieldsEdit.AddField(fieldCount); return fws.CreateTable(tableName, fields, uid, null, "");
}