麻辣GIS微信平台

更多 GIS 干货

微信关注不错过

ArcGIS Engine 10 开发手册(4-22)应用示例:空间查询和创建Table

先来一个效果图:

创建符合要求的表

//输出结果为一个张表,这张表有3个字段,其中面ID为面要素数据的FID

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 ();
  }
}

效果如下:

上面这个例子只是用了空间过滤,没有用到属性过滤,我们将上面的代码稍微改动下,加上一句代码即可, 如下:

结果对照:

ArcGIS Engine 10 开发手册全集

ArcGIS Engine 10 开发手册全集: ArcGIS Engine 10 开发手册

相关阅读

麻辣GIS-Sailor

作者:

GIS爱好者,学GIS,更爱玩GIS。

声明

1.本文所分享的所有需要用户下载使用的内容(包括但不限于软件、数据、图片)来自于网络或者麻辣GIS粉丝自行分享,版权归该下载资源的合法拥有者所有,如有侵权请第一时间联系本站删除。

2.下载内容仅限个人学习使用,请切勿用作商用等其他用途,否则后果自负。

手机阅读
公众号关注
知识星球
手机阅读
麻辣GIS微信公众号关注
最新GIS干货
关注麻辣GIS知识星球
私享圈子

留言板(小编看到第一时间回复)