MikeEber

arrgghh why is my code not working???!!!

Discussion created by MikeEber on Oct 22, 2013
I have tried a million things to get this to work.  However when I set the results that are returned I get an error thrown.  (displayed further down in the message list)

My function:

        internal static void GetDistanceBetweenPoints(IFeatureClass stationClass, List<IFeature> targets,
            IFeatureClass outputClass, IGPMessages messages)
        {
            messages.AddMessage("Creating the cost array from hydrography");
            IFeatureCursor outputCursor = outputClass.Insert(true);
            try
            {
                // First get the origin point for computing distances
                IFeature originFeature = stationClass.Search(null, false).NextFeature();
                Point origin = originFeature.Shape as Point;
                IRasterWorkspaceEx rasterWorkspace = GPDatabase.GetRasterWorkspace();
                IRasterCatalog hydrographyCatalog = rasterWorkspace.OpenRasterCatalog("Hydrography");
                messages.AddMessage("Hydrography opened.");
                IFeatureClass hydrographyFeatureClass = (IFeatureClass) hydrographyCatalog;
                IFeatureCursor stationCursor = stationClass.Search(null, false);
                IFeature stationFeature = stationCursor.NextFeature();
                messages.AddMessage("Station feature defined.");
                int rasterObjectId = GPUtilities.CheckCoincidence(stationFeature.ShapeCopy, hydrographyCatalog, messages);
                if (rasterObjectId == -1)
                {
                    messages.AddMessage("Station is not coincident with the waterways and will be relocated.");
                    if (!RelocateToWaterway(ref originFeature, hydrographyFeatureClass, messages))
                    {
                        messages.AddMessage("Station could not be relocated to water within 4 nautical miles.");
                        return;
                    }
                    else
                    {
                        origin = originFeature.Shape as Point;
                    }
                }
                messages.AddMessage("Station is coincident with the hydrography map.");
                IRasterCatalogItem hydrographyCatalogItem =
                    (IRasterCatalogItem) hydrographyFeatureClass.GetFeature(rasterObjectId);
                messages.AddMessage(string.Format("Hydrography CatalogItem created : {0}",
                    hydrographyCatalogItem.RasterDataset.CompleteName));
                IGeoDataset hydrographyRasterDataset = (IGeoDataset) hydrographyCatalogItem.RasterDataset;
                IGeoDataset hydrographyBackTrack = (IGeoDataset) hydrographyCatalogItem.RasterDataset;
                messages.AddMessage("Cost array construction completed.");
                IDistanceOp2 distanceComputer = new RasterDistanceOpClass();

                messages.AddMessage(string.Format("Beginning process of {0} targets", targets.Count));
                
                for (int index = 0; index < targets.Count; index++)
                {

                    int targetObjectId = GPUtilities.CheckCoincidence(targets[index].ShapeCopy, hydrographyCatalog,
                        messages);
                    if (targetObjectId == -1)
                    {
                        messages.AddMessage("Target is not coincident with the waterways and will be relocated.");
                        IFeature targetPoint = targets[index];
                        if (!RelocateToWaterway(ref targetPoint, hydrographyFeatureClass, messages))
                        {
                            CreatePlaceholder(outputCursor, outputClass, messages);
                            return;
                        }
                        targets[index] = targetPoint;
                    }
                    messages.AddMessage("Target is coincident with the hydrography map.");
                    IPointCollection points = new MultipointClass();
                    points.AddPoint(origin);
                    points.AddPoint(targets[index].Shape as Point);
                    messages.AddMessage(string.Format("Computing distance from origin to target {0}", index + 1));
                    IGeometryCollection result = distanceComputer.CostPathAsPolyline(points,
                        hydrographyRasterDataset, hydrographyBackTrack);
                    if (result == null)
                    {
                        messages.AddMessage("No result was returned for this target.");
                        CreatePlaceholder(outputCursor, outputClass, messages);
                        continue;
                    }

                    messages.AddMessage("Saving geometry as new feature.");
                    IFeatureBuffer buffer = outputClass.CreateFeatureBuffer();
                    IFeature feature = (IFeature) buffer;
                    feature.Shape = (IGeometry) result;
                    outputCursor.InsertFeature(buffer);
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(outputCursor);
            }
            catch (Exception ex)
            {
                messages.AddMessage(string.Format("Process failed: {0}", ex.Message));
                System.Runtime.InteropServices.Marshal.ReleaseComObject(outputCursor);
                throw;
            }
        }


Eight minutes later...the results:

Process failed: This spatial reference object cannot be defined from the available information.


I have the points coming in from ArcMAP and I assume that the interface defines the points with the same coordinate system as the map itself.  And I'm creating the hydrography map from the same map.  And I'm passing one point for station and one point for target.  And every attempt to get a fracking polyline returned has failed.  This is frustrating.

So the exception is thrown right after I write the message : "Saving geometry as new feature."

Outcomes