ArcObjects Library Reference  (GeoDatabase)    

IFeatureClass Example

[Visual Basic 6.0]

The following example is a routine that accepts an input IFeatureClassName, and output IFeatureClassName and an IQueryFilter. It uses IFeatureClass::CreateFeature and IFeature::Store to mimic an edit session and append the features from the input feature class that satisfy the input query filter to the output feature class. Because it is done within an edit session, this script can be used to append features to feature classes in a geometric network or topology.

 Public Sub t_InsertRecords(pFeatclsNm As IFeatureClassName, _
                           pInputFCName As IFeatureClassName, _
                           pQFilt As IQueryFilter)
  ' +++ Note: this function assumes that the schema between the input feature class and
  ' the destination feature class are the same. If they are not, then this
  ' function will always report errors.
  
  ' get the feature classes from the name
  Dim pFName As IName
  Dim pInputName As IName
  
  Set pFName = pFeatclsNm
  Set pInputName = pInputFCName
  
  Dim pFeatcls As IFeatureClass
  Dim pInputFCls As IFeatureClass
  Set pFeatcls = pFName.Open
  Set pInputFCls = pInputName.Open
 
  ' get the workspace and start editing
  Dim pDataset As IDataset
  Set pDataset = pFeatcls
  
  Dim pWorkspace As IWorkspace
  Set pWorkspace = pDataset.Workspace
  
  Dim pWorkspaceEdit As IWorkspaceEdit
  Set pWorkspaceEdit = pWorkspace
  
  pWorkspaceEdit.StartEditing True
  pWorkspaceEdit.StartEditOperation
       
  ' open a cursor on the input feature class with the given query filter
  Dim pFeatCursor As IFeatureCursor
  Set pFeatCursor = pInputFCls.Search(pQFilt, False)
  
  ' loop through the input features in the cursor, and insert
  ' them into the destination feature class. This is slow since we must use
  ' IFeature::Store to mimic an edit session.
  Dim pFeat As IFeature
  Dim pRow As IRow
  Dim pFlds As IFields
  Dim lSFld As Long
  Dim i As Long
  
  Set pRow = pFeatCursor.NextFeature
  Do Until pRow Is Nothing
    Set pFeat = pFeatcls.CreateFeature
    Set pFlds = pFeat.Fields
    For i = 0 To pFlds.FieldCount - 1
        lSFld = pRow.Fields.FindField(pFlds.Field(i).Name)
        pFeat.Value(i) = pRow.Value(lSFld)
    Next i
    pFeat.Store
    ' get next row
    Set pRow = pFeatCursor.NextFeature
  Loop
  
  pWorkspaceEdit.StopEditOperation
  pWorkspaceEdit.StopEditing True
End Sub

[C#]

    //IFeatureClass Example

 

    public void IFeatureClass_Example(IFeatureClass featureClass)
    {
        //IFeatureClass::FeatureCount
        IQueryFilter queryFilter = new QueryFilterClass();
        queryFilter.WhereClause = "subtype = 'COM'";
        Console.WriteLine("The feature count for features with a COM subtype is {0}", featureClass.FeatureCount(queryFilter));

 

        //IFeatureClass::AreaField
        IField areaField = featureClass.AreaField;
        if (areaField != null)
        {
            Console.WriteLine("The feature class's area field is named {0} and found at an index of {1}", areaField.Name, featureClass.Fields.FindField(areaField.Name));
        }

 

        //IFeatureClass FeatureDataset Example
        IFeatureDataset featureDataset = featureClass.FeatureDataset;
        //if the feature class is stand alone null will be returned by the feature dataset property.
        if (featureDataset != null)
        {
            Console.WriteLine("The feature class's feature dataset is named {0}", featureDataset.BrowseName);
        }

 

//IFeatureClass FeatureClassID

Console.WriteLine("The feature class's FeatureClassID is {0}", featureClass.FeatureClassID);

 

        //IFeatureClass FeatureType Example
        if (featureClass.FeatureType == esriFeatureType.esriFTComplexEdge | featureClass.FeatureType == esriFeatureType.esriFTComplexJunction |
             featureClass.FeatureType == esriFeatureType.esriFTSimpleEdge | featureClass.FeatureType == esriFeatureType.esriFTSimpleJunction)
        {
            //this is a network feature class. Put your code here
            //in this example, we simply print out a statement
            Console.WriteLine("This feature class is in a geomtric network");
        }
        else
        {
            //not a network feature type
            Console.WriteLine("This feature class is not in a geometric network");
        }

 

        //IFeatureClass LengthField Example
        IField lengthField = featureClass.LengthField;
        if (lengthField != null)
        {
            Console.WriteLine("The feature class's lenght field is named {0} and found at an index of {1}", lengthField.Name, featureClass.Fields.FindField(lengthField.Name));
        }

 

        //IFeatureClass ShapeFieldName Example
        IField shapeField = featureClass.Fields.get_Field(featureClass.Fields.FindField(featureClass.ShapeFieldName));
        if (shapeField != null)
        {
            Console.WriteLine("The feature class's shape field is named {0} and found at an index of {1}", featureClass.ShapeFieldName, featureClass.Fields.FindField(lengthField.Name));
        }

 

        //IFeatureClass ShapeType Example

        if (featureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
        {
            //ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon
            Console.WriteLine("This is a polygon feaure class");
        }
        else
        {
            Console.WriteLine("This feature class is not a polygon feaure class");
            //it maybe one of the following  [see esriGeometryType Constants (esriGeometry)]
            //ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint
            //ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultipoint
            //ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline
            //ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultipatch
        }

 

        //IFeatureClass GetFeature Example
        //Get feature with OID 11, because it is known to exist
        //This method is typically used to get a feature by know OID
        //If you wish to loop through a series of features, use a Cursor.

        // get the index of the field we are intersected in
        int fieldIndex = featureClass.FindField("Material");
        IFeature feature = featureClass.GetFeature(11);
        Console.WriteLine("The feature with an OID of 11 has a value for the field Material of {0}", feature.get_Value(fieldIndex));
    }


[Visual Basic .NET, C++]
No example is available for Visual Basic .NET or C++. To view a Visual Basic 6.0 or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.