Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am writing an addin, which needs to split a polyline into a number of segments taking the number of segments as an input from user. How can I accomplish this?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Use IGeometryBridge2.SplitAtDistances(). Also see the documentation on the equivalent IPolycurve2.SplitAtDistances() and the singular IPolycurve.SplitAtDistance() methods for more explanation.

The IGeometryBridge2 version must be used in .NET.

Edit: This code works for me:

private static IEnumerable<IPolyline> SplitPolylineIntoEqualSegments(IPolyline polyline, int numSegments)
{
    var geombridge = (IGeometryBridge2)new GeometryEnvironmentClass();
    var paths = (IGeometryCollection)polyline;
    for (int i = 0; i < paths.GeometryCount; i++)
    {
        var path = (IPath)paths.get_Geometry(i);
        var distances = new double[Math.Max(1, numSegments - 1)];
        for (int j = 0; j < distances.Length; j++)
        {
            distances[j] = (j + 1.0) / numSegments;
        }
        var polyline2 = PathToPolyline(path);
        geombridge.SplitAtDistances((IPolycurve2)polyline2, ref distances, true, true);
        var splitpaths = (IGeometryCollection)polyline2;
        for (int k = 0; k < splitpaths.GeometryCount; k++)
        {
            var splitpath = (IPath)splitpaths.get_Geometry(k);
            yield return PathToPolyline(splitpath);
        }
    }
}

private static IPolyline PathToPolyline(IPath path)
{
    var polyline = (IPolyline)new PolylineClass();
    var geomcoll = (IGeometryCollection)polyline;
    geomcoll.AddGeometry(path);
    geomcoll.GeometriesChanged();
    return polyline;
}
share|improve this answer
    
:: Thanks.. for the Code... But I am getting an Enumeration of polylines from this, How can I convert this into a Feature layer containing all these Polylines Thanks in advance –  Avinash Nirankari Feb 18 '13 at 6:44
    
@AvinashNirankari do you want to create a feature layer that will contain all these polylines? –  Emi Feb 18 '13 at 7:07
    
@Emi :: Yes... Actually I want to use InterpolateShape tool on all of these so I need one layer containing all these polylines.... –  Avinash Nirankari Feb 18 '13 at 9:39
    
Do you know LINQ? You can work with IEnumerable<T>'s in any way that you need. I purposely made the function generic so you (and other readers) could adapt it to your needs. –  blah238 Feb 18 '13 at 9:59
    
So what is your problem now? creating a polyline fature layer? –  Emi Feb 18 '13 at 13:02
show 2 more comments

This answer is not actually the answer of the actual question. It's the answer of your comment. Because of the code snippet, I wrote it as a answer:

void AddFeatures(List<IPolyline> polylines, IFeatureClass featureClass)
{
    if (featureClass == null)
        return;
    foreach (var polyline in polylines)
    {
        IFeature feature = featureClass.CreateFeature();
        feature.Shape = polyline;

        feature.Store();
    }
    _activeView.Refresh();
}
share|improve this answer
    
:: The code you have given is not giving any errors but it is not giving the correct results.... It is adding the only the first segment not the whole line.... –  Avinash Nirankari Feb 19 '13 at 12:56
    
Are you sure about all lines are polylines? because it works for me correctly –  Emi Feb 19 '13 at 14:15
    
check these things at debug mode: 1. count of polylines > 1; 2. x,y value of the from point and to point of 1st polyline and other polylines are different. –  Emi Feb 19 '13 at 14:23
    
:: Oh sorry..!! I was doing it the wrong way at first.... It worked... Thanks a Lot for your help..... –  Avinash Nirankari Feb 20 '13 at 5:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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