Select to view content in your preferred language

Issues with sorting using arcpy.na.AddLocations

699
4
08-31-2021 12:24 PM
JeffHaupt
Emerging Contributor

Hey everyone! 

I'm new both to the community and to ArcGIS.  I have been tasked with creating a python script that will complete a route analysis on a regular basis and so far that has been successful.  One issue that I am having is that I am trying to sort of the field work_order_number, but I'm not quite sure how to do that.  Here is the code that I have used to be able to create the route layer and add the stops with additional fields, but I have tried several ways to sort using the sort_field in AddLocations and was even trying to use arcpy.da.UpdateCursors.  

 

routeLayer = arcpy.na.MakeRouteAnalysisLayer("https://www.arcgis.com/", f"Best Route {route_type_entry}", "Driving Time", "PRESERVE_FIRST", None, "LOCAL_TIME_AT_LOCATIONS", "ALONG_NETWORK", None, "DIRECTIONS", "LOCAL_TIME_AT_LOCATIONS")
    
    #Adding fields to the route layer that were from the original table
    #layer_object allows us to access the necessary parts of routeLayer to be able to add specific fields
    layer_object = routeLayer.getOutput(0)
    added_fields = [["Service_Address", "TEXT"],["Work_Order", "TEXT"],["Service_Date", "DATE"],["Route_Type", "TEXT"],["Description", "TEXT"],["Item_Size", "TEXT"],     ["Work_Order_Status", "TEXT"]]
    for i in range(len(added_fields)):
        arcpy.na.AddFieldToAnalysisLayer(layer_object, "Stops", added_fields[i][0], added_fields[i][1])
    
    ## This process is importing the XY points into the route layer to be analyzed 
    ## What is being passed in is the Network Analysis Layer, Stops table, the temporary view table, fields we want to include values for
    arcpy.na.AddLocations(routeLayer, "Stops", viewTable, "CurbApproach # 3;Work_Order work_order_number #; Service_Address request_address #; Service_Date scheduled_pickup_date #; Route_Type route_type #; Description description #; Item_Size item_size #; Work_Order_Status work_order_status #;", "5000 Meters", None, None, "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "EXCLUDE", None)

 

Any help that I can get on this would be appreciated!  Like I said, I'm brand new to the arcpy and ArcGIS world.

0 Kudos
4 Replies
DavidPike
MVP Frequent Contributor

You say sorting by parameter in the tool doesn't work, does it do anything?

arcpy.na.AddLocations(routeLayer, "Stops", viewTable, "CurbApproach # 3;Work_Order work_order_number #; Service_Address request_address #; Service_Date scheduled_pickup_date #; Route_Type route_type #; Description description #; Item_Size item_size #; Work_Order_Status work_order_status #;", "5000 Meters", "work_order_number", None, "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "EXCLUDE", None)

 

0 Kudos
JeffHaupt
Emerging Contributor

If I use "Name" to sort, I am told that it doesn't exist as a sort field

0 Kudos
DavidPike
MVP Frequent Contributor

I thought you said work_order_number? So 'name' is the field you want to sort by? 

If it's saying it's not a field, perhaps name is an alias, you can check this in the feature class properties.

0 Kudos
JeffHaupt
Emerging Contributor

I was trying "work_order_number" originally as this was a field I added to the analysis layer, but then I switched to "Name" as that seems to be a default field for Stops in the Route Analysis.  The attributes list the Field Name as "Name" and the alias as "Name".  Is it possible that the type of sort is limited to only certain types of properties?

Thanks again for your help on this!

0 Kudos