ArcObjects: Iterate Through Line, Grab points to create a point geometry

4590
1
03-31-2016 10:58 AM
JamariPowers
New Contributor III

I am using ArcMap 10.1, ArcObjects 10.1 and C#. I am having trouble capturing a line feature, iterating through the vertices of the line and creating a point geometry from its vertices. I am trying to accomplish this via button click.

In detail, I add in a shapfile to ArcMap. I start an Edit Session. Using the Line tool, I draw a line and finish my sketch. Once I either save the edits or finish editing completely, I want to be able to iterate over the points in the line I just drew and use it to create a point geometry (or create a record for each point in my attribute table under the Shape field)  for each set of points. I can pseudocode this, and know what I want to do but since I'm pretty new to ArcObjects, I'm having trouble starting and grabbing the line to iterate over the points. Any help on this would be greatly appreciated. Thanks in advance.

Of course I understand that this is about a 3-5 step process. Capture line and iterate through vertices, create the geometry for each record, and store in the table. I think just getting started with capturing the line and iterating through it is causing the most confusion.

0 Kudos
1 Reply
seria
by Esri Contributor
Esri Contributor

1. The easiest way to get a reference to features in ArcMap's ActiveView is to use the Add-in framework. Use this framework to create a button, and then use the button's on-click event as the starting point of your code.

2. If the feature class you grab is of type Polyline, you can cast the polyline to an IPointCollection, and then get the points in the collection and add them to an existing (possibly empty) Points feature class.

See the code below:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
namespace CreatePointFC_from_Polyline
{
    public class CreatePointFC_from_Polyline : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        public CreatePointFC_from_Polyline()
        {
        }
        protected override void OnClick()
        {
            IMap map = ArcMap.Document.ActiveView as IMap;
            ILayer lineLayer = map.Layer[0];
            IFeatureLayer lineFeatureLayer = lineLayer as IFeatureLayer;
            IFeatureClass lineFeatureClass = lineFeatureLayer.FeatureClass;
            ILayer pointLayer = map.Layer[1];
            IFeatureLayer pointFeatureLayer = pointLayer as IFeatureLayer;
            IFeatureClass pointFeatureClass = pointFeatureLayer.FeatureClass;
            // Get Feature from 1st layer in ArcMap's TOC - check Object ID
            IFeature lineFeature = lineFeatureClass.GetFeature(1);
            IPolyline polyline = lineFeature.Shape as IPolyline;
            IPointCollection pointCollection = polyline as IPointCollection;
            for (int i=0; i < pointCollection.PointCount; i++)
            {
                // Add point to a Points feature class
                IPoint point = pointCollection.get_Point(i);
                IFeature pointFeature = pointFeatureClass.CreateFeature();
                pointFeature.Shape = point;
                pointFeature.Store();
            }
            ArcMap.Document.ActiveView.Refresh();
            ArcMap.Application.CurrentTool = null;
        }
        protected override void OnUpdate()
        {
            Enabled = ArcMap.Application != null;
        }
    }
}

See the image below for clarity with respect to the various layers used in the code:

ScreenCapture.JPG

For more information regarding manipulation of geometries in ArcObjects, please visit the Online Web Help on ArcObjects here:

ArcObjects Help

http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/

Some useful topics related to ArcMap Add-Ins and the creation of Geometries:

Building Addins:

http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/#/Building_add_ins_for_ArcGIS_for_...

Creating Geometries:

http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/#/Creating_geometries/000100000m7v...

0 Kudos