I'm a total noob/beginner in ArcObjects. I'm coding in C#. I want to extract 2 points from a given featureClass, create a straight line between those 2 points, create a new feature class and add those points to the new featureClass. The following is code (I have taken a lot of code from internet), but I am facing problems.

        IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
        IWorkspace workspace = workspaceFactory.OpenFromFile(@"myGDB1.gdb", 0);

        IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
        IFeatureClass fc_tenActors = featureWorkspace.OpenFeatureClass("ten_points");

        ISpatialReference lineFeatureSpatialReference = GetSpatialReference(fc_tenActors);
        IFeatureClass fc_line1 = IFeatureWorkspace__CreateFeatureClass(featureWorkspace, "Line1", lineFeatureSpatialReference);
        fc_line1 = featureWorkspace.OpenFeatureClass("Line1");
        IFeatureCursor featureCursorInsert = fc_line1.Insert(true);
        IFeatureBuffer featureBufferInsert = fc_line1.CreateFeatureBuffer();

        IFeature pFeature1 = fc_tenActors.GetFeature(3);
        IGeometry pFirstPointShape = pFeature1.Shape;
        IPoint pFirstPoint = pFirstPointShape as IPoint;

        IFeature pFeature2 = fc_tenActors.GetFeature(4);
        IGeometry pSecondPointShape = pFeature2.Shape;
        IPoint pSecondPoint = pSecondPointShape as IPoint;

  //************* PROBLEM HERE************************************
        IFeature pLine = CreateSegment(pFirstPoint, pSecondPoint);  
 //************* PROBLEM HERE**************************************
  //  This is problem. pLine is empty after this statement.

        featureBufferInsert.Shape = pLine.Shape;  

        AddFields(featureBufferInsert, pLine);


public static IFeature CreateSegment(IPoint pFrom, IPoint pTo)
        Line line = new LineClass();
        return line as IFeature;

private static void AddFields(IFeatureBuffer featureBuffer, IFeature feature)
        // Copy the attributes of the orig feature the new feature
        IRowBuffer rowBuffer = (IRowBuffer)featureBuffer;
        IFields fieldsNew = rowBuffer.Fields;

        IFields fields = feature.Fields;
        for (int i = 0; i <= fields.FieldCount - 1; i++)
            IField field = fields.get_Field(i);
            if ((field.Type != esriFieldType.esriFieldTypeGeometry) &&
                (field.Type != esriFieldType.esriFieldTypeOID))
                int intFieldIndex = fieldsNew.FindField(field.Name);
                if (intFieldIndex != -1)
                    featureBuffer.set_Value(intFieldIndex, feature.get_Value(i));

4 Answers 4


The code is in VB, but translating to C# should be trivial.

First, create your featureclass:

Public Function CreateStandaloneFeatureClass(ByVal workspace As IWorkspace, ByVal featureClassName As String, ByVal fieldsCollection As IFields, ByVal shapeFieldName As String) As IFeatureClass
    Dim featureWorkspace As IFeatureWorkspace = CType(workspace, IFeatureWorkspace)
    Dim fcDesc As IFeatureClassDescription = New FeatureClassDescriptionClass()
    Dim ocDesc As IObjectClassDescription = CType(fcDesc, IObjectClassDescription)

    ' Use IFieldChecker to create a validated fields collection.
    Dim fieldChecker As IFieldChecker = New FieldCheckerClass()
    Dim enumFieldError As IEnumFieldError = Nothing
    Dim validatedFields As IFields = Nothing
    fieldChecker.ValidateWorkspace = workspace
    fieldChecker.Validate(fieldsCollection, enumFieldError, validatedFields)

    ' The enumFieldError enumerator can be inspected at this point to determine
    ' which fields were modified during validation.
    Dim featureClass As IFeatureClass = featureWorkspace.CreateFeatureClass(featureClassName, validatedFields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID, esriFeatureType.esriFTSimple, shapeFieldName, "")
    Return featureClass
End Function

Now query for the two IPoint features that you want to make a line from. You can either create them here using IPoint.PutCoords() or get them from elsewhere. Create your line and store it in the featureclass.

Dim FirstPoint As IPoint = 'get your point. Can store a previous point or use PutCoords() method to create from a lat long.
Dim SecondPoint As IPoint = 

Dim polyline As IPolyline = new Polyline
 polyline.FromPoint = FirstPoint
 polyline.ToPoint = SecondPoint
 Dim feature As IFeature = featureClass.CreateFeature
 feature.Shape = polyline

I'm not a C# developer I use VB but I think the problem is in your CreateSegment function.

You appear to be returning an IFeature object when you are creating an ILine object.

I'm going to guess the code should be:

public static ILine CreateSegment(IPoint pFrom, IPoint pTo)
    ILine line = new LineClass()
    return line as ILine;

and your call to this function should be:

ILine pLine = CreateSegment(pFirstPoint, pSecondPoint);

You are missing quite some basic code in order to store your line feature into your database. First, it should be a polyline instead of line, which adds supports for multiple segments. Second you should start editing before creating a feature.

I rewrote the code from scratch but in VB.NET, you should be able to convert it easily or use it directly.

Note: you don't have to start the editing from ArcMap to do that, this code starts, stops and save edits.

Public Sub CreateLineFeatureBetweenTwoPoints(ByVal pWorkspace As ESRI.ArcGIS.Geodatabase.IWorkspace, ByVal pLineFeatureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, ByVal pFromPoint As ESRI.ArcGIS.Geodatabase.IFeature, ByVal pToPoint As ESRI.ArcGIS.Geodatabase.IFeature)

    Dim pLineGeometry As ESRI.ArcGIS.Geometry.IPolyline
    pLineGeometry = New ESRI.ArcGIS.Geometry.Polyline

    Dim pPointCol As ESRI.ArcGIS.Geometry.IPointCollection

    pPointCol = pLineGeometry

    'retrive the spatial reference
    pLineGeometry.SpatialReference = pFromPoint.Shape.SpatialReference

    Dim pLineFeature As ESRI.ArcGIS.Geodatabase.IFeature

    'start editing that is an improtant step
    Dim pWorkspaceEdit As ESRI.ArcGIS.Geodatabase.IWorkspaceEdit
    pWorkspaceEdit = pWorkspace

    pLineFeature = pLineFeatureClass.CreateFeature
    pLineFeature.Shape = pLineGeometry


End Sub

And here is how you call the function.

Private Sub MainSub()

    Dim pWorkspace As ESRI.ArcGIS.Geodatabase.IWorkspace

    Dim workspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory

    workspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory

    'open the path to your GDB
    pWorkspace = workspaceFactory.OpenFromFile("c:\test.gdb", 0)

    Dim pFWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace

    pFWorkspace = pWorkspace
    Dim pPointClass As ESRI.ArcGIS.Geodatabase.IFeatureClass
    pPointClass = pFWorkspace.OpenFeatureClass("PointFC") 'this is your fc class

    Dim pLineClass As ESRI.ArcGIS.Geodatabase.IFeatureClass

    pLineClass = pFWorkspace.OpenFeatureClass("LineFC") 'this is your fc class

    Dim pPoint1 As ESRI.ArcGIS.Geodatabase.IFeature
    Dim pPoint2 As ESRI.ArcGIS.Geodatabase.IFeature

    pPoint1 = pPointClass.GetFeature(1)
    pPoint2 = pPointClass.GetFeature(2)

    CreateLineFeatureBetweenTwoPoints(pWorkspace, pLineClass, pPoint1, pPoint2)

End Sub

The result will look like this


If you want to save feature (people already told how create feature) in .shp, you can use official documentation:

http://www.edndoc.esri.com/arcobjects/9.2/NET/91E3E8A0-D355-43D2-A201-0A8CEB719334.htm - how convert feature to new shapefile.

But if you do not like write many lines of code, you can:

  • At the beginning, create this shapefile (by hands - in ArcMap, in program you only open this file)
  • And then (in code) add new feature in this file. For example, if you want to save points in your file. Here featureClass is shapefile .shp, feature is a new object.

    ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.Point();
    point.X = lon; point.Y = lat;
    IFeature feature = featureClass.CreateFeature();
    feature.Shape = point;

Update If you want to create feature, you can

  • create polyline

    IPolyline polyline= new ESRI.ArcGIS.Geometry.Polyline() as IPolyline;
    polyline.FromPoint = fromPoint;
    polyline.ToPoint = toPoint;
    • create feature

      IFeature feature = featureClass.CreateFeature();
      feature.Shape = polyline;

What's the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.