『公告』 预祝您龙年大吉,万事如意, 过节期间, 大家如需数据服务,请拨打400 或直接添加客服微信,再祝大家龙年,心想事成。
关注我们 新浪 腾讯

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

ArcGIS Engine 10 开发手册(4-22)应用示例:空间查询和创建Table
空间查询和创建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 ();

  }

}

       效果如下:

 

 

 

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

 

 

 

       结果对照:

 

      京ICP备08100627号-22 京公网安备 11010802030428号