ArcEngine10.2 how to create a geodatabase network dataset

ESRI 官方例子 how to create a geodatabase network dataset

public static void CreateGeodatabaseNetworkDataset()
{
    // ------------------------------------------------------------------------
    // Creating a new network dataset data element
    // ------------------------------------------------------------------------
    // Create a new empty data element for a buildable network dataset.
    IDENetworkDataset deNetworkDataset = new DENetworkDatasetClass();
    deNetworkDataset.Buildable = true;
    // Open the feature dataset and cast to the IGeoDataset interface.
    IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
    IWorkspace workspace = workspaceFactory.OpenFromFile(@"C:\Users\Desktop\CreateNetworkDataset\Paris.gdb", 0);
    IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
    IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("RoadNetwork");
    IGeoDataset geoDataset = (IGeoDataset)featureDataset;
    // Copy the feature dataset'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 = "ParisNet";
    // ------------------------------------------------------------------------
    // Adding the network sources
    // ------------------------------------------------------------------------
    // ---- Specifying the connectivity settings for the edge source ----
    // Create a new EdgeFeatureSource object and point it to the Streets feature class.
    INetworkSource edgeNetworkSource = new EdgeFeatureSourceClass();
    edgeNetworkSource.Name = "Streets";
    edgeNetworkSource.ElementType = esriNetworkElementType.esriNETEdge;
    // Set the edge feature source's connectivity settings.
    IEdgeFeatureSource edgeFeatureSource = (IEdgeFeatureSource)edgeNetworkSource;
    edgeFeatureSource.UsesSubtypes = true;
    edgeFeatureSource.set_SubtypeConnectivityGroup(1, 1);
    edgeFeatureSource.set_SubtypeConnectivityPolicy(1,
        esriNetworkEdgeConnectivityPolicy.esriNECPEndVertex);
    edgeFeatureSource.set_SubtypeConnectivityGroup(2, 1);
    edgeFeatureSource.set_SubtypeConnectivityPolicy(2,
        esriNetworkEdgeConnectivityPolicy.esriNECPEndVertex);
    edgeFeatureSource.set_SubtypeConnectivityGroup(3, 1);
    edgeFeatureSource.set_SubtypeConnectivityPolicy(3,
        esriNetworkEdgeConnectivityPolicy.esriNECPAnyVertex);
    // ---- Specifying the directions settings for the edge source ----
    // Create a StreetNameFields object and populate its settings.
    IStreetNameFields streetNameFields = new StreetNameFieldsClass();
    streetNameFields.Priority = 1; // Indicates the primary street name.
    streetNameFields.StreetNameFieldName = "FULL_NAME";
    // Add the StreetNameFields object to a new NetworkSourceDirections object,
    // then add it to the EdgeFeatureSource created earlier.
    INetworkSourceDirections nsDirections = new NetworkSourceDirectionsClass();
    IArray nsdArray = new ArrayClass();
    nsdArray.Add(streetNameFields);
    nsDirections.StreetNameFields = nsdArray;
    edgeNetworkSource.NetworkSourceDirections = nsDirections;
    // ---- Specifying the turn source ----
    deNetworkDataset.SupportsTurns = true;
    // Create a new TurnFeatureSource object and point it to the ParisTurns feature class.
    INetworkSource turnNetworkSource = new TurnFeatureSourceClass();
    turnNetworkSource.Name = "ParisTurns";
    turnNetworkSource.ElementType = esriNetworkElementType.esriNETTurn;
    IArray sourceArray = new ArrayClass();
    sourceArray.Add(edgeNetworkSource);
    sourceArray.Add(turnNetworkSource);
    deNetworkDataset.Sources = sourceArray;
    // ------------------------------------------------------------------------
    // Adding the network attributes
    // ------------------------------------------------------------------------
    IArray attributeArray = new ArrayClass();
    // ----- Oneway network attribute -----
    // Create a new EvaluatedNetworkAttribute object and populate its settings.
    IEvaluatedNetworkAttribute oneWayAttribute = new EvaluatedNetworkAttributeClass();
    INetworkAttribute2 oneWayNetworkAttribute2 = (INetworkAttribute2)oneWayAttribute;
    oneWayNetworkAttribute2.Name = "Oneway";
    oneWayNetworkAttribute2.UsageType =
        esriNetworkAttributeUsageType.esriNAUTRestriction;
    oneWayNetworkAttribute2.DataType = esriNetworkAttributeDataType.esriNADTBoolean;
    oneWayNetworkAttribute2.Units = esriNetworkAttributeUnits.esriNAUUnknown;
    oneWayNetworkAttribute2.UseByDefault = true;
    // Create evaluator objects and set them on the EvaluatedNetworkAttribute object.
    INetworkFieldEvaluator oneWayToFromNetworkFieldEvaluator = new
        NetworkFieldEvaluatorClass();
    INetworkEvaluator oneWayToFromNetworkEvaluator = (INetworkEvaluator)
        oneWayToFromNetworkFieldEvaluator;
    oneWayToFromNetworkFieldEvaluator.SetExpression("restricted",
        "restricted = False\n\rSelect Case UCase([Oneway])\n\r Case \"N\", \"TF\", \"T\": restricted = True\n\rEnd Select");
    oneWayAttribute.set_Evaluator(edgeNetworkSource,
        esriNetworkEdgeDirection.esriNEDAlongDigitized, oneWayToFromNetworkEvaluator);
    INetworkFieldEvaluator oneWayFromToNetworkFieldEvaluator = new
        NetworkFieldEvaluatorClass();
    INetworkEvaluator oneWayFromToNetworkEvaluator = (INetworkEvaluator)
        oneWayFromToNetworkFieldEvaluator;
    oneWayFromToNetworkFieldEvaluator.SetExpression("restricted",
        "restricted = False\n\rSelect Case UCase([Oneway])\n\r Case \"N\", \"FT\", \"F\": restricted = True\n\rEnd Select");
    oneWayAttribute.set_Evaluator(edgeNetworkSource,
        esriNetworkEdgeDirection.esriNEDAgainstDigitized, oneWayFromToNetworkEvaluator);
    INetworkConstantEvaluator oneWayNetworkConstantEvaluator = new
        NetworkConstantEvaluatorClass();
    INetworkEvaluator oneWayConstantNetworkEvaluator = (INetworkEvaluator)
        oneWayNetworkConstantEvaluator;
    oneWayNetworkConstantEvaluator.ConstantValue = false; // False = traversable
    oneWayAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETEdge,
        oneWayConstantNetworkEvaluator);
    oneWayAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETJunction,
        oneWayConstantNetworkEvaluator);
    oneWayAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETTurn,
        oneWayConstantNetworkEvaluator);
    // Add the attribute to the array.
    attributeArray.Add(oneWayAttribute);
    // ----- Minutes network attribute -----
    // Create a new EvaluatedNetworkAttribute object and populate its settings.
    IEvaluatedNetworkAttribute minutesAttribute = new EvaluatedNetworkAttributeClass();
    INetworkAttribute2 minutesNetworkAttribute2 = (INetworkAttribute2)minutesAttribute;
    minutesNetworkAttribute2.Name = "Minutes";
    minutesNetworkAttribute2.UsageType = esriNetworkAttributeUsageType.esriNAUTCost;
    minutesNetworkAttribute2.DataType = esriNetworkAttributeDataType.esriNADTDouble;
    minutesNetworkAttribute2.Units = esriNetworkAttributeUnits.esriNAUMinutes;
    minutesNetworkAttribute2.UseByDefault = true;
    // Create evaluator objects and set them on the EvaluatedNetworkAttribute object.
    INetworkFieldEvaluator ftMinutesNetworkFieldEvaluator = new
        NetworkFieldEvaluatorClass();
    INetworkEvaluator ftMinutesNetworkEvaluator = (INetworkEvaluator)
        ftMinutesNetworkFieldEvaluator;
    ftMinutesNetworkFieldEvaluator.SetExpression("[FT_Minutes]", "");
    minutesAttribute.set_Evaluator(edgeNetworkSource,
        esriNetworkEdgeDirection.esriNEDAlongDigitized, ftMinutesNetworkEvaluator);
    INetworkFieldEvaluator tfMinutesNetworkFieldEvaluator = new
        NetworkFieldEvaluatorClass();
    INetworkEvaluator tfMinutesNetworkEvaluator = (INetworkEvaluator)
        tfMinutesNetworkFieldEvaluator;
    tfMinutesNetworkFieldEvaluator.SetExpression("[TF_Minutes]", "");
    minutesAttribute.set_Evaluator(edgeNetworkSource,
        esriNetworkEdgeDirection.esriNEDAgainstDigitized, tfMinutesNetworkEvaluator);
    INetworkConstantEvaluator minutesNetworkConstantEvaluator = new
        NetworkConstantEvaluatorClass();
    INetworkEvaluator minutesConstantNetworkEvaluator = (INetworkEvaluator)
        minutesNetworkConstantEvaluator;
    minutesNetworkConstantEvaluator.ConstantValue = 0;
    minutesAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETEdge,
        minutesConstantNetworkEvaluator);
    minutesAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETJunction,
        minutesConstantNetworkEvaluator);
    minutesAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETTurn,
        minutesConstantNetworkEvaluator);
    // Add the attribute to the array.
    attributeArray.Add(minutesAttribute);
    // ----- Meters network attribute -----
    // Create a new EvaluatedNetworkAttribute object and populate its settings.
    IEvaluatedNetworkAttribute metersAttribute = new EvaluatedNetworkAttributeClass();
    INetworkAttribute2 metersNetworkAttribute2 = (INetworkAttribute2)metersAttribute;
    metersNetworkAttribute2.Name = "Meters";
    metersNetworkAttribute2.UsageType = esriNetworkAttributeUsageType.esriNAUTCost;
    metersNetworkAttribute2.DataType = esriNetworkAttributeDataType.esriNADTDouble;
    metersNetworkAttribute2.Units = esriNetworkAttributeUnits.esriNAUMeters;
    metersNetworkAttribute2.UseByDefault = false;
    // Create evaluator objects and set them on the EvaluatedNetworkAttribute object.
    INetworkFieldEvaluator metersNetworkFieldEvaluator = new NetworkFieldEvaluatorClass()
        ;
    INetworkEvaluator metersNetworkEvaluator = (INetworkEvaluator)
        metersNetworkFieldEvaluator;
    metersNetworkFieldEvaluator.SetExpression("[Meters]", "");
    metersAttribute.set_Evaluator(edgeNetworkSource,
        esriNetworkEdgeDirection.esriNEDAlongDigitized, metersNetworkEvaluator);
    metersAttribute.set_Evaluator(edgeNetworkSource,
        esriNetworkEdgeDirection.esriNEDAgainstDigitized, metersNetworkEvaluator);
    INetworkConstantEvaluator metersNetworkConstantEvaluator = new
        NetworkConstantEvaluatorClass();
    INetworkEvaluator metersConstantNetworkEvaluator = (INetworkEvaluator)
    metersNetworkConstantEvaluator;
    metersNetworkConstantEvaluator.ConstantValue = 0;
    metersAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETEdge,
                                         metersConstantNetworkEvaluator);
    metersAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETJunction,
                                         metersConstantNetworkEvaluator);
    metersAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETTurn,
                                         metersConstantNetworkEvaluator);
    // Add the attribute to the array.
    attributeArray.Add(metersAttribute);
    // ----- RoadClass network attribute -----
    // Create a new EvaluatedNetworkAttribute object and populate its settings.
    IEvaluatedNetworkAttribute roadClassAttribute = new EvaluatedNetworkAttributeClass();
    INetworkAttribute2 roadClassNetworkAttribute2 = (INetworkAttribute2)
        roadClassAttribute;
    roadClassNetworkAttribute2.Name = "RoadClass";
    roadClassNetworkAttribute2.UsageType =
        esriNetworkAttributeUsageType.esriNAUTDescriptor;
    roadClassNetworkAttribute2.DataType = esriNetworkAttributeDataType.esriNADTInteger;
    roadClassNetworkAttribute2.Units = esriNetworkAttributeUnits.esriNAUUnknown;
    roadClassNetworkAttribute2.UseByDefault = false;
    // Create evaluator objects and set them on the EvaluatedNetworkAttribute object.
    INetworkFieldEvaluator roadClassNetworkFieldEvaluator = new
        NetworkFieldEvaluatorClass();
    INetworkEvaluator roadClassNetworkEvaluator = (INetworkEvaluator)
        roadClassNetworkFieldEvaluator;
    roadClassNetworkFieldEvaluator.SetExpression("rc",
        "rc = 1 'Local road\n\rSelect Case UCase([CFCC])\n\r" +
        " Case \"A10\", \"A11\", \"A12\", \"A13\", \"A14\", \"A15\", \"A16\", \"A17\", \"A18\", \"A19\"\n\r" + " rc = 2 'Highway\n\r" + " Case \"A60\", \"A63\"\n\r" + " rc = 3 'Ramp\n\r" + " Case \"A65\", \"A66\", \"A68\", \"A69\"\n\r" + " rc = 4 'Ferry\n\r" + " Case \"A62\"\n\r" + " rc = 5 'Roundabout\n\r" + "End Select");
    roadClassAttribute.set_Evaluator(edgeNetworkSource,
        esriNetworkEdgeDirection.esriNEDAlongDigitized, roadClassNetworkEvaluator);
    roadClassAttribute.set_Evaluator(edgeNetworkSource,
        esriNetworkEdgeDirection.esriNEDAgainstDigitized, roadClassNetworkEvaluator);
    INetworkConstantEvaluator roadClassNetworkConstantEvaluator = new
        NetworkConstantEvaluatorClass();
    INetworkEvaluator roadClassConstantNetworkEvaluator = (INetworkEvaluator)
        roadClassNetworkConstantEvaluator;
    roadClassNetworkConstantEvaluator.ConstantValue = 0;
    roadClassAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETEdge,
        roadClassConstantNetworkEvaluator);
    roadClassAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETJunction,
        roadClassConstantNetworkEvaluator);
    roadClassAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETTurn,
        roadClassConstantNetworkEvaluator);
    // Add the attribute to the array.
    attributeArray.Add(roadClassAttribute);
    // ----- TurnRestriction network attribute -----
    // Create a new EvaluatedNetworkAttribute object and populate its settings.
    IEvaluatedNetworkAttribute turnAttribute = new EvaluatedNetworkAttributeClass();
    INetworkAttribute2 turnNetworkAttribute2 = (INetworkAttribute2)turnAttribute;
    turnNetworkAttribute2.Name = "TurnRestriction";
    turnNetworkAttribute2.UsageType = esriNetworkAttributeUsageType.esriNAUTDescriptor;
    turnNetworkAttribute2.DataType = esriNetworkAttributeDataType.esriNADTInteger;
    turnNetworkAttribute2.Units = esriNetworkAttributeUnits.esriNAUUnknown;
    turnNetworkAttribute2.UseByDefault = false;
    INetworkConstantEvaluator turnTrueNetworkConstantEvaluator = new
        NetworkConstantEvaluatorClass();
    INetworkEvaluator turnTrueNetworkEvaluator = (INetworkEvaluator)
        turnTrueNetworkConstantEvaluator;
    turnTrueNetworkConstantEvaluator.ConstantValue = true;
    turnAttribute.set_Evaluator(turnNetworkSource, esriNetworkEdgeDirection.esriNEDNone,
        turnTrueNetworkEvaluator);
    INetworkConstantEvaluator turnFalseNetworkConstantEvaluator = new
        NetworkConstantEvaluatorClass();
    INetworkEvaluator turnFalseNetworkEvaluator = (INetworkEvaluator)
        turnFalseNetworkConstantEvaluator;
    turnFalseNetworkConstantEvaluator.ConstantValue = false;
    turnAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETEdge,
        turnFalseNetworkEvaluator);
    turnAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETJunction,
        turnFalseNetworkEvaluator);
    turnAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETTurn,
        turnFalseNetworkEvaluator);
    // Add the attribute to the array.
    attributeArray.Add(turnAttribute);
    deNetworkDataset.Attributes = attributeArray;
    // ------------------------------------------------------------------------
    // Specifying directions settings
    // ------------------------------------------------------------------------
    // Create a NetworkDirections object and populate its settings.
    INetworkDirections networkDirections = new NetworkDirectionsClass();
    networkDirections.DefaultOutputLengthUnits = esriNetworkAttributeUnits.esriNAUMeters;
    networkDirections.LengthAttributeName = "Meters";
    networkDirections.TimeAttributeName = "Minutes";
    networkDirections.RoadClassAttributeName = "RoadClass";
    // Add the NetworkDirections object to the network dataset data element.
    deNetworkDataset.Directions = networkDirections;
    // ------------------------------------------------------------------------
    // Creating and building the network dataset
    // ------------------------------------------------------------------------
    // Get the feature dataset extension and create the network dataset based on the data element.
    IFeatureDatasetExtensionContainer fdxContainer = (IFeatureDatasetExtensionContainer)
        featureDataset;
    IFeatureDatasetExtension fdExtension = fdxContainer.FindExtension
        (esriDatasetType.esriDTNetworkDataset);
    IDatasetContainer2 datasetContainer2 = (IDatasetContainer2)fdExtension;
    IDEDataset deDataset = (IDEDataset)deNetworkDataset;
    INetworkDataset networkDataset = (INetworkDataset)datasetContainer2.CreateDataset
        (deDataset);
    // Once the network dataset is created, build it.
    INetworkBuild networkBuild = (INetworkBuild)networkDataset;
    networkBuild.BuildNetwork(geoDataset.Extent);
}
posted @ 2020-04-08 16:37  lqqgis  阅读(160)  评论(0编辑  收藏  举报