2

And how can I improve my performance? I wrote a simple python script to export a few hundred maps:

Open a MXD, add a layer, apply a definition query, apply a symbology, export to PNG.

The definition query is in the loop. Everything else is always the same. And there are always 153 features selected in the FC. In the first three minutes between 10 and 20 pngs are produced. After that only 2. How can I prevent this and improve the performence? There are hundreds of pngs waiting.

import arcpy,os

field1 = "Field1"
mxd = r"X:\GIS\test.mxd"
mxd2 = arcpy.mapping.MapDocument(mxd)
df = arcpy.mapping.ListDataFrames(mxd2)[0]
shp = r"X:\GIS\TestGDB\test"
dbf = r"X:\GIS\testdata.dbf"
elementlist = ["value1","value2",...] #here are a few hundret elements in the list
seasonlist = ["spring","summer","autumn","winter"]
for element in elementlist:
    for index,season in enumerate(seasonlist):#[0:1]:
        where_clause = '"%s" = %s' % (str(field1),str("'"+element+"'"))

        layer = shp.split(os.sep)[-1]
        layername = layer
        arcpy.MakeFeatureLayer_management(shp,layername)
        layer = arcpy.mapping.Layer(layer)
        layer.name = layername + "_" + season

        layer2 = arcpy.mapping.TableView(dbf)
        layername2 = layer2.name + "_" + season

        layer2.definitionQuery = where_clause

        arcpy.mapping.AddTableView(df,layer2)
        arcpy.AddJoin_management(layer,in_field,layer2,join_field,"KEEP_ALL")

        expression = "!" + symbologyfield + "!"
        arcpy.CalculateField_management(layer,valuefield,expression,"PYTHON")

        arcpy.RemoveJoin_management(layer)
        #arcpy.UpdateLayer(df, update_layer, source_layer, {symbology_only})

        arcpy.ApplySymbologyFromLayer_management(layer,layerfile)
        arcpy.mapping.AddLayer(df,layer,"TOP")

        layer.visible = True

        for element2 in arcpy.mapping.ListLayoutElements(mxd2,"TEXT_ELEMENT"):
            if element2.name == textfield1:
                element2.text = str(element)
            if element2.name == textfield2:
                element2.text = str(season)


        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()

        arcpy.mapping.ExportToPNG(mxd2, output + os.sep + str(element) + "_" + str(season) + r".png",resolution=printresulution)
3

There should be no need to include:

    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()

unless you are running this from within ArcMap and want to see your maps displayed in that application before exporting them.

At the moment you are only presenting a copy/paste of a loop from your code rather than a code snippet that starts with import arcpy and works up to where you are stuck.

Writing code snippets to get quicker answers? provides advice on what a code snippet for an ArcPy question should look like.

  • 2
    Complimenting @PolyGeo's answer you are not showing the whole code, as I read it you are for ever adding layer to the map so not surprising it is getting slower as you will are having hundreds of layers added to it... You need to edit your question and show us the full code. – Hornbydd Dec 14 '17 at 11:24
  • I applied the "rules" to code snippets, Even though, I think the code didn't improve that way. @Hornbydd I am adding layers to an mxd which I don't save. There is no code comming behind the code I show. The code before is not of interest for my problem. – Alex Dec 14 '17 at 12:19
  • 2
    @Alex Adding the extra code is very useful I can now see where the other objects/variables are coming from. It looks likes it will loop only 8 times, 4 seasons by 2 values. So how are you creating 10 and 20 pngs in the first 3 minutes when your code will only ever generate 8 PNG'S? You are not explaining the full story, suggest you amend your question to explain what is actually going on, i.e how are you running this code? – Hornbydd Dec 14 '17 at 14:41
  • @Hornbydd ok I am glad it helps. The valuelist is a lot longer, but I think it is not helpfull to add a few hundret values. Though, I made it obvious now that the list is much longer. Sorry, I didn't mean do be missleading here. – Alex Dec 14 '17 at 15:09
  • 2
    @Alex the comment about adding layers is important. You are adding layers every loop which will have an effect on performance. You say you're not saving the MXD, but you're still adding to the same MXD without removing any. – Midavalo Dec 14 '17 at 15:12

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.