I have a problem adding the source geometry for the Network Dataset in ArcObjects. I wish my ND to be very simple, built upon a single ShapeFile which contains lines. Each line has a DriveTime defined in minutes.
var linesPath = @"C:\temp\DA\0\c5b581681b604a9ea4dcd1667f9d2b35.shp";
IDENetworkDataset2 deNetworkDataset = new DENetworkDatasetClass();
deNetworkDataset.Buildable = true;
deNetworkDataset.SupportsTurns = true;
var featureClass = ShapefileHelper.OpenShapeFile(tmpLines);
IGeoDataset geoDataset = (IGeoDataset) featureClass;
IDEGeoDataset deGeoDataset = (IDEGeoDataset) deNetworkDataset;
deGeoDataset.Extent = geoDataset.Extent;
deGeoDataset.SpatialReference = geoDataset.SpatialReference;
IDataElement dataElement = (IDataElement) deNetworkDataset;
dataElement.Name = "Lines_ND";
IEdgeFeatureSource streetsEdgeFeatureSource = new EdgeFeatureSourceClass();
INetworkSource streetsNetworkSource = (INetworkSource) streetsEdgeFeatureSource;
streetsNetworkSource.Name = System.IO.Path.GetFileNameWithoutExtension(tmpLines);
streetsNetworkSource.ElementType = esriNetworkElementType.esriNETEdge;
streetsEdgeFeatureSource.ClassConnectivityPolicy=esriNetworkEdgeConnectivityPolicy.esriNECPEndVertex;
streetsEdgeFeatureSource.UsesSubtypes = false;
IArray sourceArray = new ArrayClass();
sourceArray.Add(streetsNetworkSource);
deNetworkDataset.Sources = sourceArray;
I omit the code for the attributes, since no matter whether they are defined my code finishes with error.
UID ndWorkspaceExtensionUID = new UIDClass();
ndWorkspaceExtensionUID.Value = "esriGeoDatabase.NetworkDatasetWorkspaceExtension";
IWorkspaceFactory workspaceFactory2 = new ShapefileWorkspaceFactory();
IWorkspaceName worName = workspaceFactory2.Create("c:\\temp\\", Guid.NewGuid().ToString(), null, 0);
IName name = (IName)worName;
IWorkspace workspace = name.Open() as IWorkspace;
IWorkspaceExtensionManager workspaceExtensionManager = (IWorkspaceExtensionManager) workspace;
IWorkspaceExtension workspaceExtension = workspaceExtensionManager.FindExtension(ndWorkspaceExtensionUID);
IDatasetContainer2 datasetContainer2 = (IDatasetContainer2) workspaceExtension;
IDEDataset deDataset = (IDEDataset) deNetworkDataset;
INetworkDataset networkDataset = (INetworkDataset)datasetContainer2.CreateDataset(deDataset);
INetworkBuild networkBuild = (INetworkBuild) networkDataset;
networkBuild.BuildNetwork(geoDataset.Extent);
The error is "HRESULT: 0x80042262". I found that is says "The source is not valid for the network". The input ShapeFile should be correct, because I can succesfully create and build a network using ArcMap. Thus I believe, that the EdgeFeatureSource has a wrong definition.
To be fair, I do not understand why I need to define the EdgeFeatureSource. The ShapeFile with lines is already defined and should be the (only!) source for the network. The code seems quite cryptic in Network Datasets though.
Just for reference, I attach the Shapefile and ND created in ArcMap - I wish to achieve the same result in ArcObjects.
INetworkBuild.UpdateSchema
after adding the source, but before callingBuildNetwork
?