Select to view content in your preferred language

How to set a response object with my polylines?

616
2
10-21-2013 05:43 AM
MichaelEber
Deactivated User
I  have a geo service that seems to finally be working. I have the service setup with a list of Polylines as the returned object. This is what I'm trying to implement but it doesn't work:

 IPointCollection points = new MultipointClass();
 points.AddPoint(station);
 points.AddPoint(targets[index]);
 messages.AddMessage(string.Format("Computing distance from origin to target {0}", index));
 Polyline result = distanceComputer.CostPathAsPolyline(points,
     hydrographyRasterDataset, 
     hydrographyRasterDataset) as Polyline;
 IGeometry feature = result as IGeometry;
 messages.AddMessage("Getting new row for output.");
 IFeatureBuffer buffer = outputClass.CreateFeatureBuffer();
 IFeature polyline = (IFeature) buffer;
 messages.AddMessage("adding shape");
 polyline.Shape = feature;
 messages.AddMessage("adding object");
 polyline.set_Value(polyline.Fields.FindField("SHAPE"), result);
 messages.AddMessage("adding length");
 polyline.set_Value(polyline.Fields.FindField("SHAPE_Length"), ((IPolyline)result).Length);
 messages.AddMessage("Commiting to output");
 outputCursor.InsertFeature(buffer);

When I try setting the value for "SHAPE" I get an exception and the message is:

This spatial reference object cannot be defined from the available information
So how exactly to I set the polyline in the return object (basically adding rows of Polylines to the OutputFeatureSet) so that it does not blow up?
0 Kudos
2 Replies
NeilClemmons
Honored Contributor
You're setting the Shape field value here:

polyline.Shape = feature;

This is not the correct way to set the Shape field (the above line was correct) and you don't need to do it twice:

polyline.set_Value(polyline.Fields.FindField("SHAPE"), result);

You can't set the Shape.Length field value.  It is read-only and is managed by the geodatabase.  It will update when you update the Shape field.

polyline.set_Value(polyline.Fields.FindField("SHAPE_Length"), ((IPolyline)result).Length);
0 Kudos
MichaelEber
Deactivated User
I added more debugging to the code and found that when I get the IGeometryCollection back from CostPathAsPolyline then cast it to Polyline, the resulting object is null!!!???  How do I go from an IGeometryCollection to a Polyline correctly?
0 Kudos