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_polyline/0001000000v0000000/, 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.

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.

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[i]);

}

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.