(注意 在NET中,会遇到以“_2”结尾的属性,这些属性是可写的。)
//定义一个几何字段,类型为点类型
ISpatialReference pSpatialReference = axMapControl1.ActiveView.FocusMap.SpatialReference; IGeometryDefEdit pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.SpatialReference_2 = pSpatialReference;
//定义一个字段集合对象 IFields pFields = new FieldsClass();
IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; //定义单个的字段
IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = \
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; pFieldsEdit.AddField(pField); pFieldEdit.GeometryDef_2 = pGeoDef; //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = \
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField);
//定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = \
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField);
//定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = \
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField);
//定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = \
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField);
IWorkspaceFactory pFtWsFct = new AccessWorkspaceFactory();
IFeatureWorkspace pWs = pFtWsFct.OpenFromFile(@\, 0) as IFeatureWorkspace;
IFeatureClass pFtClass = pWs.CreateFeatureClass(\, pFields, null, null, esriFeatureType.esriFTSimple, \, null)——————//////?????????????
如何改变字段的别名?
public void ChangeFieldAliasName(ITable pTable, string pOriFieldName, string pDesFieldName) {
IClassSchemaEdit pClassSchemaEdit = (IClassSchemaEdit)pTable; //给对象加上锁
ISchemaLock pSchemaLock = (ISchemaLock)pTable;
pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); if (pTable.FindField(pOriFieldName) != -1) {
pClassSchemaEdit.AlterFieldAliasName(pOriFieldName, pDesFieldName); pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
使用IFeatureSelection接口高亮显示
在介绍IMap接口那一节,我们用IMap的IMap.SelectFeature方法实现了对查询的要素高亮显示,现在我们用IFeatureSelection接口实现查询高亮显示
IMap pMap = axMapControl1.Map;
IFeatureLayer pFeaturelayer = GetLayer(pMap, \ IFeatureSelection pFeatureSelection = pFeaturelayer as IFeatureSelection; IQueryFilter pQuery = new QueryFilterClass(); pQuery.WhereClause = \
pFeatureSelection.SelectFeatures(pQuery,esriSelectionResultEnum.esriSelectionResultNew,false); axMapControl1.ActiveView.Refresh();
其中GetLayer函数是我们写的一个根据图层的名称获取图层的方法,代码如下
private ILayer GetLayer(IMap pMap, string LayerName) {
IEnumLayer pEnunLayer;
pEnunLayer = pMap.get_Layers(null, false); pEnunLayer.Reset(); ILayer pRetureLayer;
pRetureLayer = pEnunLayer.Next(); while (pRetureLayer != null) {
if (pRetureLayer.Name == LayerName) { break; }
pRetureLayer = pEnunLayer.Next(); }
return pRetureLayer; }
提问:以下三种方式的区别在哪里?
axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); axMapControl1.ActiveView.Refresh(); axMapControl1.Refresh();
创建符合要求的表:
public ITable CreateTable(string _TablePath, string _TableName) {
IWorkspaceFactory pWks = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pFwk = pWks.OpenFromFile(_TablePath, 0) as IFeatureWorkspace; //用于记录面中的ID;
IField pFieldID = new FieldClass();
IFieldEdit pFieldIID = pFieldID as IFieldEdit;
pFieldIID.Type_2 = esriFieldType.esriFieldTypeInteger; pFieldIID.Name_2 = \面ID\ //用于记录个数的;
IField pFieldCount = new FieldClass();
IFieldEdit pFieldICount = pFieldCount as IFieldEdit; pFieldICount.Type_2 = esriFieldType.esriFieldTypeInteger; pFieldICount.Name_2 = \个数\ //用于添加表中的必要字段
ESRI.ArcGIS.Geodatabase.IObjectClassDescription objectClassDescription = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass(); IFields pTableFields = objectClassDescription.RequiredFields; IFieldsEdit pTableFieldsEdit = pTableFields as IFieldsEdit; pTableFieldsEdit.AddField(pFieldID); pTableFieldsEdit.AddField(pFieldCount);
ITable pTable = pFwk.CreateTable(_TableName, pTableFields, null, null, \ return pTable; }
统计需要的数据:
///
/// 第一个参数为面数据,第二个参数为点数据,第三个为输出的表 ///
///
public void StatisticPointCount(IFeatureClass _pPolygonFClass, IFeatureClass _pPointFClass, ITable _pTable) {
IFeatureCursor pPolyCursor = _pPolygonFClass.Search(null, false); IFeature pPolyFeature = pPolyCursor.NextFeature();
while (pPolyFeature != null) {
IGeometry pPolGeo = pPolyFeature.Shape; int Count = 0;
ISpatialFilter spatialFilter = new SpatialFilterClass(); spatialFilter.Geometry = pPolGeo;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains; IFeatureCursor pPointCur = _pPointFClass.Search(spatialFilter, false); if (pPointCur != null) {
IFeature pPointFeature = pPointCur.NextFeature(); while (pPointFeature != null) {
pPointFeature= pPointCur.NextFeature(); Count++; } }
if (Count != 0) {
IRow pRow = _pTable.CreateRow();
pRow.set_Value(1, pPolyFeature.get_Value(0)); pRow.set_Value(2, Count); pRow.Store(); }
pPolyFeature = pPolyCursor.NextFeature(); } }
效果如下: