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 开发手册

如您有疑问,可在文末留言,或到QQ群提问。

本站QQ群:291616564 麻辣GIS

微信公众号:malagis,扫描右边二维码直接关注。

微信捐助麻辣GIS 支付宝捐助麻辣GIS

如果本文对您有所帮助,欢迎对我们团队进行打赏捐助,让我们在传播3S的路上可以走得更远。


作者:,GIS爱好者。
分享本文,请您带上本文链接

发表评论