1

I have multiple fields in my attribute table so I want to use one general layer and apply to many fields (same range). The problem is everytime I apply the layer or it`s get the old field and apply to my map or apply to new field but do not get the settings.

I can get the map with the correct range but always do not came with the legend format (in percentage (%)).

import arcpy
import csv
import pythonaddins

TabelaP1 = arcpy.GetParameterAsText(0)
Ano = arcpy.GetParameterAsText(1)
IndexL = int(arcpy.GetParameterAsText(2))
SiglaProduto = arcpy.GetParameterAsText(3)
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
lyr = arcpy.mapping.ListLayers(mxd, "", df)[IndexL] 
Layer1 = arcpy.mapping.Layer(r'F:\Agro\Layers\PercAPH2.lyr')
arcpy.mapping.UpdateLayer(df, lyr, Layer1, True)

arcpy.AddMessage('Calculando Porcentagem de Area Plantada')

if '2002' < Ano < '2016':

    arcpy.AddMessage('Iniciando Calculo para o Ano {0}'.format(Ano))

    NovoCampo1 = 'PercAPH_'+SiglaProduto+'_'+Ano
    Campo1 = 'APH_'+SiglaProduto+'_'+Ano
    arcpy.AddMessage('Campo selecionado: {0}'.format(Campo1))
    arcpy.AddField_management(TabelaP1, NovoCampo1, 'Float')
    arcpy.CalculateField_management(TabelaP1, NovoCampo1, '(!{0}!/!AreaTotalHec!)*100'.format(Campo1), "PYTHON_9.3")
    lyr.symbologyType == "GRADUATED_COLORS"

    lyr.symbology.valueField = NovoCampo1
    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()

if Ano < '2003' or Ano > '2015':

    arcpy.AddMessage('O Ano {0} informado nao possui Serie Historica disponivel'.format(Ano))

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

[Wrong Format[1]

The original layer give me the label in % with round(2) for the numbers. I tried many ways, including make by ArcCatolog for the GDB but also do not work. There is any possible way to solve this?

3
  • 1
    Have you considered creating subtypes? This way you could have a symbol column with subtype rules that you could set up symbology. desktop.arcgis.com/en/arcmap/10.3/manage-data/subtypes/…
    – jbalk
    Aug 22, 2017 at 4:47
  • jbalk, subtype is available for integer fields, in my case I have float fields. Do you have nay other ideia that I can solve this?
    – vcruvinelr
    Aug 22, 2017 at 11:31
  • You could add a new field for the subtype and base the integers on the percentage like 0-10% = 1, 10-20% = 2, etc.
    – jbalk
    Aug 24, 2017 at 4:26

1 Answer 1

1

In the code samples for the Update Layer function, esri uses a layer from a one mxd to update a layer in another mxd.
Try creating a template mxd that contains the source layer that you want to use to symbolize the other layers. Reference the layer in that mxd instead of the .lyr file.
It may not work the same way with a layer file, and esri has not provided an example of using Update Layer with a .lyr file, although they do mention that it is possible.

Create a variable for your template mxd, create another variable to reference the layer in that mxd. Then use that layer as your source layer instead of the layer file you have saved to disk.

Something like this at the beginning of you code:

mxd2 = arcpy.mapping.MapDocument(r'C:\Temp\Template.mxd')
df2 = arcpy.mapping.ListDataFrames(mxd2)[0]
Layer1 = arcpy.mapping.ListLayers(mxd2, "PercAPH2", df)[0])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.