ArcObjects - How to conver m-points to a polyline?

1410
3
12-26-2013 05:59 AM
ShaningYu
Frequent Contributor
I am working on an ArcObjects / SOE project.  I need to convert multiple points into a line feature.  Using the approach at http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/index.html#/How_to_create_a_polyli..., I got a PolylineClass object: poinColl (its PointCount=213).  In the following step, I tried to cast the PolylineClass object as a Polyline like that
             IPointCollection4 pointColl = CreatePolylineByPoints(pointsCollection, point)  // returns  ESRI.ArcGIS.Geometry.PointClass
             IPolyline polyLine = (IPolyline)pointColl;
The polyLine has no value (e.g. XMax=XMin=...=0.0, MMax=MMin=NaN).
Then I turned to an alternative approach that works for IPointCollection rather than IPointCollection4
            IFeatureClass fc = CreateEmptyPolylineFC(fws, "MyPolyline", sr, routeIdFieldName);
            IFeature polylineFeature = fc.CreateFeature();
            // Assign polyline geometry and M-value
            IPointCollection4 pointColl = (IPointCollection4)polylineFeature;     //(IPointCollection)polylineFeature;
I got InvalidVastException - Unable to cast COM object of type 'System._ComObject' to interface  type 'ESRI.ArcGIS.Geometry.IPointCollection4'...
Another alternative I did was to cast IPointCollection4 object as IPointCollection, but also got InvalidCastException.
What's wrong in my code above?  Thanks for help.
0 Kudos
3 Replies
ShaningYu
Frequent Contributor
The problem in my 1st thread was solved. I revised the method in http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000000v0000000 as below in order to get the return of a PolylineClass object.
void ESRI.ArcGIS.Geometry.IPointCollection4 CreatePolylineByPoints(List<ESRI.ArcGIS.Geometry.IPoint> pointsCollection) {
//Build a polyline from a sequence of vertices (points).
//Add arrays of points to a geometry using the IGeometryBridge2 interface on the GeometryEnvironment singleton object.
ESRI.ArcGIS.Geometry.IGeometryBridge2 pGeoBrg = new ESRI.ArcGIS.Geometry.GeometryEnvironment()as IGeometryBridge2;

//pPointColl is the new polyline.
ESRI.ArcGIS.Geometry.IPointCollection4 pPointColl = new ESRI.ArcGIS.Geometry.PolylineClass();

//TODO:
//Set pPointColl.SpatialReference = 'Define the spatial reference of the new polyline.

for (int i = 0; i < pointsCollection.Count; i++) { // 200+ points
pPointColl.AddPoint(pointsCollection);
}

ESRI.ArcGIS.esriSystem.WKSPoint[] aWKSPointBuffer = new ESRI.ArcGIS.esriSystem.WKSPoint[pPointColl.PointCount ];
// 200+ points in aWKSPointBuffer, but each one's X=0.0 and Y=0.0

pGeoBrg.SetWKSPoints(pPointColl, ref aWKSPointBuffer);
return pPointColl;
}

See the red-marked note. All of the X/Y values are 0.0. I could not figure out why. Please post your comment if you have the knowledge on it. Thanks.
0 Kudos
RichardFairhurst
MVP Honored Contributor
This post was moved to from the General forum to the ArcObjects forum, since it is not a general ArcGIS question.
0 Kudos
ShaningYu
Frequent Contributor
I have solved this problem by using a simple approach.  The general procedure is below:
1) Create a List<ESRI.ArcGIS.Geometry.IPoint> to collect all of points.
2) Create an object of  ISegmentCollection path = new ESRI.ArcGIS.Geometry.PathClass();
3) Create a segment by entering 2 points (looping the List<ESRI.ArcGIS.Geometry.IPoint>)
4) Add the segment into the path.
            object obj = Type.Missing;
            for (int i = 1; i < pointsCollection.Count; i++)     {
                ILine pLine = CreateLine(pointsCollection[i - 1], pointsCollection);
                path.AddSegment((ISegment)pLine, ref obj, ref obj);
            }
        private ILine CreateLine(IPoint ptA, IPoint ptB)    {
            ILine pLine = new Line();
            pLine.PutCoords(ptA, ptB);
            return pLine;
        }
5) Create a PolylineClass and complete the change
            ESRI.ArcGIS.Geometry.IPolyline pSegPoly = new ESRI.ArcGIS.Geometry.PolylineClass();
            ESRI.ArcGIS.Geometry.IGeometryCollection pGeoColl = pSegPoly as IGeometryCollection;
            pGeoColl.AddGeometry((IGeometry)path, ref obj, ref obj);
            pGeoColl.GeometriesChanged();
0 Kudos