SolverInsertCursor

Zusammenfassung

A write cursor for a solver class, specific to an input type, that can be used to add rows directly to the input.

Diskussion

Instances of this class are not created directly. Instead, they are created by calling the insertCursor method on one of the solver classes.

Methodenübersicht

MethodeErläuterung
insertRow (row)

Insert a row into a network analysis solver input using a cursor.

Methoden

insertRow (row)
ParameterErläuterungDatentyp
row

The row to insert, represented as either a list or a tuple. The length of the list and the ordering and datatypes of the list items must match the list of field names specified in the field_names parameter of the insertCursor method used to instantiate the cursor.

List

Codebeispiel

SolverInsertCursor example

Use the insertCursor method and a SolverInsertCursor object to set inputs for an analysis.

# An example showing how to use the insertCursor() method to set inputs for an analysis
import arcpy
arcpy.CheckOutExtension("network")

nds = "C:/data/NorthAmerica.gdb/Routing/Routing_ND"
nd_layer_name = "Routing_ND"
output_routes = "C:/data/io.gdb/Routes"

# Create a network dataset layer and get the desired travel mode for analysis
arcpy.nax.MakeNetworkDatasetLayer(nds, nd_layer_name)
nd_travel_modes = arcpy.nax.GetTravelModes(nd_layer_name)
travel_mode = nd_travel_modes["Driving Time"]

# Instantiate a Route solver object
route = arcpy.nax.Route(nd_layer_name)
# Set properties
route.timeUnits = arcpy.nax.TimeUnits.Minutes
route.travelMode = travel_mode
route.routeShapeType = arcpy.nax.RouteShapeType.TrueShapeWithMeasures
# Set input stops using insertCursor()
fields = ["Name", "RouteName", "SHAPE@XY"]
with route.insertCursor(arcpy.nax.RouteInputDataType.Stops, fields) as cur:
    cur.insertRow(["Stop 1", "Route 1", (-117.10191118199998, 32.634351493000054)])
    cur.insertRow(["Stop 2", "Route 1", (-116.97970607599996, 32.56210221400005)])
    cur.insertRow(["Stop 1", "Route 2", (-116.97141447099995, 32.654230331000065)])
    cur.insertRow(["Stop 2", "Route 2", (-117.00762504, 32.70097640100005)])
# Solve the analysis
result = route.solve()
SolverInsertCursor example 2

Use the insertCursor method and a SolverInsertCursor object to set inputs for an analysis. Use a URL for the network data source and PointGeometry objects as inputs with the SHAPE@ token.

# An example showing how to use the insertCursor() method to set inputs for an analysis
# Use ArcGIS Online as the network data source and PointGeometry objects as inputs
import arcpy

sr_wgs84 = arcpy.SpatialReference(4326)

# Instantiate a Route solver object using ArcGIS Online as the network data source
route = arcpy.nax.Route("https://www.arcgis.com/")

# Input data with latitude and longitude values specified in WGS84 coordinates
input_data = [
    ["Stop 1", "Route 1", -117.10191118199998, 32.634351493000054],
    ["Stop 2", "Route 1", -116.97970607599996, 32.56210221400005],
    ["Stop 1", "Route 2", -116.97141447099995, 32.654230331000065],
    ["Stop 2", "Route 2", -117.00762504, 32.70097640100005]
]

# Set input stops using insertCursor()
fields = ["Name", "RouteName", "SHAPE@"]
with route.insertCursor(arcpy.nax.RouteInputDataType.Stops, fields) as cur:
    for input_pt in input_data:
        # Construct a PointGeometry object for the point using the correct spatial reference
        pt_geom = arcpy.PointGeometry(arcpy.Point(input_pt[2], input_pt[3]), sr_wgs84)
        # Insert the data using its shape
        cur.insertRow([input_pt[0], input_pt[1], pt_geom])

# Solve the analysis
result = route.solve()
print(result.solveSucceeded)