1

I am trying to create a Python Toolbox in ArcGIS to accomplish Network Analyst tasks. Globally the toolbox has to create and solve an ODCostMatrix Layer. The users has to enter Origins and Destinations (point feature class) that have to be loaded as "Origins" and "Destinations" of the ODCostMatrix Layer (same as the "Add Locations" tool). It looks like this:

Overview of the OD matrix toolbox

Points number 1 and 2 works, but the third one fails: The NA field mappings of Origins and Destinations is not shown. I am not sure on how to deal with NAClassFieldMap and GPNALayer datatypes in the Toolbox. Should I set up a parameter dependencies between the Origin Feature class and the NAClassFieldMap parameters ? I have added some comments in the toolbox script, especially in the end (# ORIGINS FIELDMAPPING in the script below). Any help or comments?

    import arcpy
    from arcpy import env

    class Toolbox(object):
        def __init__(self):
            """Define the toolbox (the name of the toolbox is the name of the
            .pyt file)."""
            self.label = "OD Matrix"
            self.alias = ""

            # List of tool classes associated with this toolbox
            self.tools = [Tool]

    class Tool(object):
        def __init__(self):
            """Define the tool (tool name is the name of the class)."""
            self.label = "OD Matrix"
            self.description = "OD Matrix with Origins and Destinations field mappings"
            self.canRunInBackground = False

        def getParameterInfo(self):

            #0 Network Dataset
            InNetworkDataset = arcpy.Parameter(
                displayName="Network Dataset",
                name="InNetworkDataset",
                datatype="DENetworkDataset",
                parameterType="Required",
                direction="Input")

            #1 Network Dataset Sources
            NetworkNDatasetSources = arcpy.Parameter(
                displayName="Network Dataset Sources",
                name="NetworkNDatasetSources",
                datatype="GPString",
                multiValue = True,
                parameterType="Derived",
                direction="Output")

            #2 Origins (Schools) (Point Feature Class)
            OriginFeatures = arcpy.Parameter(
                displayName="Origins : Schools",
                name="OriginFeatures",
                datatype="DEFeatureClass",
                parameterType="Required",
                direction="Input")

            #3 Origins NA Field Map (I fail in updating this parameter)
            NAOriginsFieldMap = arcpy.Parameter(
                displayName="Origins NA Field Map",
                name="OriginsNAFieldMap",
                datatype="NAClassFieldMap",
                parameterType="Required",
                direction="Input")

            #4 Destinations (home locations) (Point Feature Class)
            DestinationsFeatures = arcpy.Parameter(
                displayName="Destinations : home locations",
                name="DestinationsFeatures",
                datatype="DEFeatureClass",
                parameterType="Required",
                direction="Input")

            #5 Destinations NA Field Map
            NADestinationsFieldMap = arcpy.Parameter(
                displayName="Destinations NA Field Map",
                name="DestinationsNAFieldMap",
                datatype="NAClassFieldMap",
                parameterType="Required",
                direction="Input")

            #6 NA OD Cost Matrix Layer
            ODCostMatrixLayer = arcpy.Parameter(
                displayName="OD Layer",
                name="OD Layer",
                datatype="GPNALayer",
                parameterType="Derived",
                direction="Output")

            params = [InNetworkDataset, NetworkNDatasetSources, OriginFeatures, NAOriginsFieldMap, DestinationsFeatures, NADestinationsFieldMap, ODCostMatrixLayer]
            return params

        def isLicensed(self):
            """Set whether tool is licensed to execute."""
            return True

        def updateParameters(self, parameters):
            # If user enters a Network dataset
            if parameters[0].altered:
               NetworkDataset=parameters[0].value
               # List of network sources
               NDSourceList=[]
               descND=arcpy.Describe(NetworkDataset)
               for source in descND.sources:
                   NDSourceList.append(source.name)
               parameters[1].value=NDSourceList

               # Creating OD Cost Matrix Layer
               ODCostLayer=arcpy.MakeODCostMatrixLayer_na(NetworkDataset, "OD Cost Matrix", "Length", "", "", "", "ALLOW_UTURNS", "", "NO_HIERARCHY", "", "NO_LINES", "").getOutput(0)
               # Origins: Add Field to Analysis Layer
               arcpy.AddFieldToAnalysisLayer_na(ODCostLayer, "Origins", "OriginAddressID", "LONG", "", "", "", "", "NULLABLE")
               # Destinations: Add Field to Analysis Layer (2)
               arcpy.AddFieldToAnalysisLayer_na(ODCostLayer, "Destinations", "DestinationAddressID", "LONG", "", "", "", "", "NULLABLE")
               # Destinations: Add Field to Analysis Layer (3)
               arcpy.AddFieldToAnalysisLayer_na(ODCostLayer, "Destinations", "eleve_id", "LONG", "", "", "", "", "NULLABLE")
               # Destinations: Add Field to Analysis Layer (4)
               arcpy.AddFieldToAnalysisLayer_na(ODCostLayer, "Destinations", "datatype", "TEXT", "", "", "", "", "NULLABLE")

               # ORIGINS FIELDMAPPING 
               # If user enters the origins feature class (schools)
               if parameters[2].altered:
                  originFeatures=parameters[2].value
                  # Get fields of origins feature class
                  OriginsFeaturesFields=arcpy.ListFields(originFeatures)

                  # Create NAClassFieldMap to give a value to parameters[3]
                  # Origins layer Name from ODCostLayer "Origins"
                  subLayerNames=arcpy.na.GetNAClassNames(ODCostLayer)
                  originsLayerName = subLayerNames["Origins"]

                  # NAClassFieldMappings with "Origins" and OriginsFeaturesFields.
                  # This part is obviously not working...Have to do the same for NA "Destinations" sublayer
                  NAOriginsFieldMappings=arcpy.na.NAClassFieldMappings(ODCostLayer, originsLayerName, True, OriginsFeaturesFields)
                  # How can I pass the fieldMappings to fieldmaps to update parameter 3 ?
                  parameters[3].value = NAOriginsFieldMappings

                  # Get OD Cost Matrix Layer (Error Here, how to return the ODCostLayer to [6] (GPNALayer))
                  parameters[6].value = ODCostLayer
            return

        def updateMessages(self, parameters):
            """Modify the messages created by internal validation for each tool
            parameter.  This method is called after internal validation."""
            return

        def execute(self, parameters, messages):
            inNetworkDataset = parameters[0].valueAsText
            arcpy.AddMessage("***In network dataset : " + inNetworkDataset)
            inNetworkSources = parameters[1].valueAsText
            arcpy.AddMessage("***In network sources : " + inNetworkSources)
            OriginFeatures = parameters[2].valueAsText
            arcpy.AddMessage("*** Origins Features: " + OriginFeatures)
            NAOriginsFieldMap = parameters[3].valueAsText
            arcpy.AddMessage("*** NA Origins Field Mappings: " + NAOriginsFieldMap)
            DestinationsFeatures = parameters[4].valueAsText
            arcpy.AddMessage("*** Destinations Features: " + DestinationsFeatures)
            NADestinationsFieldMap = parameters[5].valueAsText
            arcpy.AddMessage("*** NA Destinations Field Mapppings: " + NADestinationsFieldMap)
            ODCostLayer = parameters[6].valueAsText
            arcpy.AddMessage("*** ODLayer: " + ODCostLayer)
            return

Your Answer

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

Browse other questions tagged or ask your own question.