How to open a network dataset


Opening a network dataset

Do the following steps to open a network dataset:
  1. Get the IWorkspace. See the following code example:
[Java]
// For example, path = @"C:\myData\myfGDB.gdb".
public static IWorkspace fileGdbWorkspaceFromPath(String path)throws
    AutomationException, IOException{
    IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
    return workspaceFactory.openFromFile(path, 0);
}

// For example, path = @"C:\myData\mySDC".
public static IWorkspace sdcWorkspaceFromPath(String path)throws AutomationException,
    IOException{
    IWorkspaceFactory workspaceFactory = new SDCWorkspaceFactory();
    return workspaceFactory.openFromFile(path, 0);
}
  1. Using the workspace from Step 1, apply one of the following options to get the IDatasetContainer:
    1. For FileSystemWorkspaces, such as Smart Data Compression (SDC) and shapefile, get the workspace extension manager first, then use the extension manager to get the workspace extension. The workspace extension can be cast as a dataset container. This is different from local and remote databases because file workspaces do not have feature datasets.
    2. For LocalDatabaseWorkspaces, such as personal geodatabases and file geodatabase, and for RemoteDatabaseWorkspaces, such as SDE, get the feature dataset container and use it to get the feature dataset extension. The feature dataset extension can be cast as a dataset container.
  2. Open the network dataset using the dataset container.
  3. Optionally, get the data element from the network dataset.
The preceding steps are shown in the following code sample:
[Java]
public static INetworkDataset openNetworkDataset(IWorkspace networkDatasetWorkspace,
    String networkDatasetName, String featureDatasetName)throws AutomationException,
    IOException{
    // Shapefile and SDC network datasets will not have a feature dataset, 
    // so pass in the featureDatasetName as an empty string for those types.
    if (networkDatasetWorkspace == null || networkDatasetName == "")
        return null;
    // Get the IDatasetContainer based on the workspace type.
    IDatasetContainer3 datasetContainer3 = null;
    switch (networkDatasetWorkspace.getType()){
        // Example: FileSystemWorkspaces include SDC and shapefile.
        case esriWorkspaceType.esriFileSystemWorkspace:
            IWorkspaceExtensionManager workspaceExtensionManager = 
                (IWorkspaceExtensionManager)networkDatasetWorkspace;
            UID networkID = new UID();
            networkID.setValue("esriGeoDatabase.NetworkDatasetWorkspaceExtension");
            IWorkspaceExtension workspaceExtension =
                workspaceExtensionManager.findExtension(networkID);
            datasetContainer3 = new IDatasetContainer3Proxy(workspaceExtension);
            break;
            // Example: LocalDatabaseWorkspaces can be personal geodatabases and file geodatabases.
            // An example RemoteDatabaseWorkspace is an ArcSDE geodatabase.
            // Both LocalDatabaseWorkspace and RemoteDatabaseWorkspace network datasets are 
            // opened the same way.
        case esriWorkspaceType.esriLocalDatabaseWorkspace:
        case esriWorkspaceType.esriRemoteDatabaseWorkspace:
            if (featureDatasetName == null)
                return null;
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)
                networkDatasetWorkspace;
            IFeatureDataset featureDataset = featureWorkspace.openFeatureDataset
                (featureDatasetName);
            IFeatureDatasetExtensionContainer featureDatasetExtensionContainer = new
                IFeatureDatasetExtensionContainerProxy(featureDataset);
            IFeatureDatasetExtension featureDatasetExtension =
                featureDatasetExtensionContainer.findExtension
                (esriDatasetType.esriDTNetworkDataset);
            datasetContainer3 = new IDatasetContainer3Proxy(featureDatasetExtension);
            break;
    }
    if (datasetContainer3 == null)
        return null;
    // Use the container to open the network dataset.
    IDataset dataset = datasetContainer3.getDatasetByName
        (esriDatasetType.esriDTNetworkDataset, networkDatasetName);
    INetworkDataset networkDataset = new INetworkDatasetProxy(dataset);
    // Some Network Analyst methods, such as INASolver.Bind, require an IDENetworkDataset.
    // You can access the DataElement from the network dataset via the IDatasetComponent interface.
    IDatasetComponent datasetComponent = new IDatasetComponentProxy(networkDataset);
    IDENetworkDataset deNetworkDataset = (IDENetworkDataset)
        datasetComponent.getDataElement();
    return networkDataset;
}






Development licensingDeployment licensing
Engine Developer KitEngine: Network Analyst
ArcGIS for Desktop Basic: Network AnalystArcGIS for Desktop Basic: Network Analyst
ArcGIS for Desktop Standard: Network AnalystArcGIS for Desktop Standard: Network Analyst
ArcGIS for Desktop Advanced: Network AnalystArcGIS for Desktop Advanced: Network Analyst