0

I did a python script to do a query from the UF field from my attribute table. The code is very simple and works well. The output show me a map with the selected values.

Now I neeed to sum the values from another field (lets call this field "NEW") only from the selected features from UF field.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")  

for lyr in arcpy.mapping.ListLayers(mxd):  
    if lyr.name == "Brasil_Municipios":  
        lyr.definitionQuery = "UF = 'GO'"   
arcpy.RefreshActiveView()
arcpy.RefreshTOC()

I can get the SUM with Summary Tool, but how to print in the screen the result?

import arcpy  

mxd = arcpy.mapping.MapDocument("CURRENT")  
fc = 'G:\AgroBD.gdb\Brasil_Municipios'

for lyr in arcpy.mapping.ListLayers(mxd):  
    if lyr.name == "Brasil_Municipios":  
        lyr.definitionQuery = "UF = 'GO'"   
        arcpy.Statistics_analysis("Brasil_Municipios", 
 "G:\AgroBD.gdb\Brasil_Municipios2", [["media", "SUM"]], "")
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
3

To summarize a field I would always use the Summary Statistics tool which I would expect to honour any current selection if used against a feature layer or table view.

I would use a SearchCursor to read the row that has the result you want first. Then, depending on whether your active view is the Layout View or the Data View, I would either update the text in a text element or update the value in a labeled point feature that you guarantee to be in the currently viewed extent.

Your Answer

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

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