Tuesday 23 May 2017

Splitting zAware polyline by points using ArcObjects with C#?


I'm trying to split a polyline by the boundaries of a polygon using IPolyCurve2.SplitAtPoints using C#. The polyline I'm trying to split is zAware and holds elevation values at a regular interval. After splitting the polyline I would obviously like to get the different parts and save them as new features in my feature class, then deleting the original one. The problem is that when I try to set feature.shape to the geometry in my geometry collection made up of all the polyline parts, I get the error message that the geometry has no Z values.


Does anybody know how to solve this issue? I've looked around in the forum but all I found was code that doesn't deal with polylines that hold Z values.


Here is the code I'm using, passing in the original polyline, the point collection that holds all the intersection points with the polygon and the original feature class I create new features in.


public void SplitPolylineFeature(IFeature pPolylineFeature, IPointCollection pSplitPointCollection, IFeatureClass pFeatureClass)
{
//split the feature, each split makes a new part

IEnumVertex pEnumVertex = pSplitPointCollection.EnumVertices;
IPolycurve2 pPolyCurve = pPolylineFeature.Shape as IPolycurve2;
IEnumSplitPoint pEnumSplitPoint = pPolyCurve.SplitAtPoints(pEnumVertex, true, true, -1);
object Missing = Type.Missing;

if(pEnumSplitPoint.SplitHappened)
{
//new geocoll for polycurve
IGeometryCollection pGeometryCollection = pPolyCurve as IGeometryCollection;


//loop through the parts of the split polyline
for(int intPartCount = 0; intPartCount < pGeometryCollection.GeometryCount; intPartCount++)
{
IGeometryCollection pLineGeoColl = new PolylineClass();
IGeometry pGeometry = pGeometryCollection.get_Geometry(intPartCount);
//IZAware zAware1 = (IZAware)pGeometry;
//zAware1.ZAware = true;

pLineGeoColl.AddGeometry(pGeometry, ref Missing, ref Missing);


IFeature pFeature = pFeatureClass.CreateFeature();

pFeature.Shape = pLineGeoColl as IGeometry; //code crashes here: Geometry has no Z values
pFeature.Store();

}
}
pPolylineFeature.Delete();
}

Answer




This code works for me:


public void SplitPolylineFeature(IFeature pPolylineFeature, IPointCollection pSplitPointCollection, IFeatureClass pFeatureClass)
{
//split the feature, each split makes a new part
IEnumVertex pEnumVertex = pSplitPointCollection.EnumVertices;
IPolycurve2 pPolyCurve = pPolylineFeature.Shape as IPolycurve2;
IEnumSplitPoint pEnumSplitPoint = pPolyCurve.SplitAtPoints(pEnumVertex, true, true, -1);
object Missing = Type.Missing;

if(pEnumSplitPoint.SplitHappened)

{
//new geocoll for polycurve
IGeometryCollection pGeometryCollection = pPolyCurve as IGeometryCollection;

//loop through the parts of the split polyline
for(int intPartCount = 0; intPartCount < pGeometryCollection.GeometryCount; intPartCount++)
{
IGeometryCollection pLineGeoColl = new PolylineClass();
IZAware zAware1 = (IZAware)pLineGeoColl;
zAware1.ZAware = true;


IGeometry pGeometry = pGeometryCollection.get_Geometry(intPartCount);

pLineGeoColl.AddGeometry(pGeometry, ref Missing, ref Missing);

IFeature pFeature = pFeatureClass.CreateFeature();

pFeature.Shape = pLineGeoColl as IGeometry;
pFeature.Store();
}

}
pPolylineFeature.Delete();
}

All I did was make pLineGeoColl ZAware.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...