3

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.

  • Did you try calling INetworkBuild.UpdateSchema after adding the source, but before calling BuildNetwork? – Kirk Kuykendall Sep 23 '16 at 20:59
  • "Every network dataset has a system junction source; this source is added to the network dataset once the UpdateSchema method is called with the supplied data element. The system junction source is created once the first source is added to the network." Perhaps this means a junction source must be created by calling UpdateSchema. edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriGeoDatabase/… – Kirk Kuykendall Sep 23 '16 at 21:04
1

Seems like the issue was elsewhere. Probably in the dataset definition or feature class. Please find below the complete working code.

IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
IWorkspace workspace = workspaceFactory.OpenFromFile("C:\\temp\\",0);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;

const string testShpName = "TEST3";
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(testShpName);
IGeoDataset geoDataset = (IGeoDataset) featureClass;

//Network dataset creation
// Create an empty data element for a buildable network dataset.
IDENetworkDataset2 deNetworkDataset = new DENetworkDatasetClass();
deNetworkDataset.Buildable = true;
deNetworkDataset.SupportsTurns = true;
deNetworkDataset.NetworkType = esriNetworkDatasetType.esriNDTShapefile;
deNetworkDataset.ElevationModel = esriNetworkElevationModel.esriNEMZCoordinates;

// Copy the shapefile's extent and spatial reference to the network dataset data element.
IDEGeoDataset deGeoDataset = (IDEGeoDataset) deNetworkDataset;
deGeoDataset.Extent = geoDataset.Extent;
deGeoDataset.SpatialReference = geoDataset.SpatialReference;

// Specify the name of the network dataset.
IDataElement dataElement = (IDataElement) deNetworkDataset;
dataElement.Name = "Lines_ND_"+Guid.NewGuid().ToString().Replace("-",string.Empty);

//Add network sources
// Create a new EdgeFeatureSource object and point it to the Streets shapefile.
IEdgeFeatureSource streetsEdgeFeatureSource = new EdgeFeatureSourceClass();
INetworkSource streetsNetworkSource = (INetworkSource) streetsEdgeFeatureSource;
streetsNetworkSource.Name = testShpName;//System.IO.Path.GetFileNameWithoutExtension(tmpLines); //"Streets";
streetsNetworkSource.ElementType = esriNetworkElementType.esriNETEdge;
streetsEdgeFeatureSource.ClassConnectivityGroup = 1;
streetsEdgeFeatureSource.ClassConnectivityPolicy=esriNetworkEdgeConnectivityPolicy.esriNECPEndVertex;
streetsEdgeFeatureSource.UsesSubtypes = false;

IArray sourceArray = new ArrayClass();
sourceArray.Add(streetsNetworkSource);
deNetworkDataset.Sources = sourceArray;

UID ndWorkspaceExtensionUID = new UIDClass();
ndWorkspaceExtensionUID.Value =
    "esriGeoDatabase.NetworkDatasetWorkspaceExtension";

// Get the workspace extension and create the network dataset based on the data element.
IWorkspaceExtensionManager workspaceExtensionManager =
    (IWorkspaceExtensionManager) workspace;
IWorkspaceExtension workspaceExtension =
    workspaceExtensionManager.FindExtension(ndWorkspaceExtensionUID);
IDatasetContainer2 datasetContainer = (IDatasetContainer2) workspaceExtension;
IDEDataset deDataset = (IDEDataset) deNetworkDataset;
INetworkDataset networkDataset = (INetworkDataset)
    datasetContainer.CreateDataset(deDataset);

// Once the network dataset is created, build it.
INetworkBuild networkBuild = (INetworkBuild) networkDataset;
networkBuild.BuildNetwork(geoDataset.Extent);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.