Set number formatting in Python in ARCGIS Pro

3753
13
Jump to solution
12-09-2020 07:12 PM
LeandraGordon
Occasional Contributor II

Hi All,

Is there any way set the default numeric formatting s in Arcpy for pro?

I am using ArcPro 2.4.0

I have a number of layers in a map that I am preparing for sharing into Portal and we wish to format the number of decimal places and enable/disable the thousands separators

I am using .LRYX files to style the features and this is set in the layer file e.g.:
{
"type" : "CIMFieldDescription",
"alias" : "SVCurr",
"fieldName" : "AllData_SVCurr",
"numberFormat" : {
"type" : "CIMNumericFormat",
"alignmentOption" : "esriAlignRight",
"alignmentWidth" : 0,
"roundingOption" : "esriRoundNumberOfDecimals",
"roundingValue" : 0,
"useSeparator" : true
},

 but it does not seem to be reading the layer file for this setting ( I am running Apply Symbology from Layer)

https://pro.arcgis.com/en/pro-app/tool-reference/data-management/alter-field-properties.htm does not seem to have these options

Thanks in advance.

13 Replies
LeandraGordon
Occasional Contributor II

I think you are right. I can't get my script to work in 2.8.1...

0 Kudos
LeandraGordon
Occasional Contributor II

I think you are right Jared - 'UseSeparator' seems to have disappeared from my .LYRX files in 2.8.

0 Kudos
LeandraGordon
Occasional Contributor II

delete this..

0 Kudos
LeandraGordon
Occasional Contributor II

Hi Jared - they look like they are missing from the layer file but they are still in the CIM. I created a script in a Python Toolbox to check the LayerCIM as it seems that LYRX files might be abbreviated:

class ShowCIMforLayer(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "ShowCIMforLayer"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        
        # parameter 0 - this is just a text value, not the layer
        param0 = arcpy.Parameter(
            displayName="Layer Name",
            name ="DATA_LYR_NAME",
            datatype="GPString",
            parameterType="Required",
            direction="Input")
        param0.filter.list = ['SPATIALDATA_LYR', 'HEADERSPATIAL_LYR', 'LGA_LYR','HD_LV_M_LYR'] 
        param0.value = "SPATIALDATA_LYR" #DEFAULT
            
        params = [param0]
        #params = None
        return params

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

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        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):
    
        DATA_LYR_NAME=parameters[0].valueAsText
        #FEATURE_CLASS = parameters[1].valueAsText
    
        currentProject  = arcpy.mp.ArcGISProject("CURRENT")
        currentMap = currentProject.listMaps()[0] #I think this assumes there is only one map
        theActiveMap = currentProject.activeMap
        theFilePath = arcpy.mp.ArcGISProject("CURRENT").filePath
        
        #This works for the active map only
        TheMap = currentProject.activeMap
        
            
        if TheMap.name == "Map":
        
            # Layer ----------------------
            messages.addMessage("Map: " + TheMap.name)
            #This will trap errors where layers are missing
            layers = TheMap.listLayers()
            
            for layer in layers:
                messages.addMessage(layer.name)
                if layer.name == DATA_LYR_NAME:
                    lyrCIM = layer.getDefinition('V2')
                    
                    #showPopups#MapTips don't seem to show up in Portal
                    messages.addMessage( layer.name + " showPopups: " + str(lyrCIM.showPopups))
                    messages.addMessage( layer.name + " showMapTips: " + str(lyrCIM.showMapTips))
                    
                    #If the layer has a featureTable, this will exclude the vicmap basemaps#.fieldDescriptions
                    if hasattr(lyrCIM, 'featureTable') :
                        #https://community.esri.com/t5/python-questions/cim-lyr-featuretable-fielddescriptions-returns-an/td-p/1046116
                        messages.addMessage ("number of fieldDescriptions: " + str(len(lyrCIM.featureTable.fieldDescriptions))) #https://community.esri.com/t5/python-questions/cim-lyr-featuretable-fielddescriptions-returns-an/td-p/1046116
                        for fd in lyrCIM.featureTable.fieldDescriptions:
                            messages.addMessage ("-----------fieldName: "+ fd.fieldName)
                            messages.addMessage ("alias: "+ fd.alias)
                            messages.addMessage ("visible: " +  str(fd.visible))
                            
                            if hasattr(fd, 'numberFormat'):
                                messages.addMessage(fd.fieldName + "------------numberFormat:")
                                fdNumFmt = fd.numberFormat
                                if hasattr(fdNumFmt, 'roundingOption'):
                                    messages.addMessage( " roundingOption: " + str(fdNumFmt.roundingOption))
                                    messages.addMessage( " roundingValue: " + str(fdNumFmt.roundingValue))
                                if hasattr(fdNumFmt, 'useSeparator'):
                                    messages.addMessage( " useSeparator: " + str(fdNumFmt.useSeparator))
                            
                            

                        #messages.addMessage ("--- ---lyrCIM.featureTable.fieldDescriptions: " + str(lyrCIM.featureTable.fieldDescriptions)) 
                    
                        #Have a look at whats available
                    #if hasattr(lyrCIM.featureTable.fieldDescriptions, 'fields'):
                        #messages.addMessage ("--- ---lyrCIM.featureTable.fieldDescriptions.fields[0]: " + str(lyrCIM.featureTable.fieldDescriptions.fields[0])) 

        messages.addMessage("Finish") 
        
        return