1

I am using a python script, which opens an mxd, loads a feature class, exports the map to a png. That script produces a few hundreds of maps. After my update to 10.4 (maybe that is a coincidence) the north arrow changes the position. On some maps it is in the right position, sometimes a bit right or left from the correct position and sometimes not on the map at all. Is there any explanation for that? How can I prevent that from happening? My script is not touching the north arrow.

Ok, I could break down the code to 47 lines. It is still to much, but I don't know what else I can leave out. I have one mxd and one feature class with two fields. One field speciefies the species and one the Date. The script prints maps of all species at the first date than maps of all species at the second date a.s.o.

The scipt prints the first 48 maps (all 24 species at the first and second date) correct. After that the north arrow starts to move in a loop. It is never at the same position. After 22 more maps the north arrow has left the map completly. However, with another dataset, the values are 45 and 5 and the north arrow moves into another direction.

import arcpy,datetime,os
arcpy.env.overwriteoutput = True
mxd = r"C:/GIS/Project.mxd"
fc = r"X:/GIS/data.gdb/grid"
field = "Species"
datefield = "Season"
output = r"X/GIS/TEST"
printresulution = 150
def multiMap(mxd,fc,field,datefield,output,printresulution):
    mxd2 = mxd
    mxd = arcpy.mapping.MapDocument(mxd2)
    data_frame = arcpy.mapping.ListDataFrames(mxd)[0]
    layer = arcpy.mapping.Layer(fc)
    layername = layer.name
    specieslist = []
    datelist = []
    cursor = arcpy.SearchCursor(fc)
    for row in cursor:
        if row.getValue(field) not in specieslist:
            specieslist.append(row.getValue(field))
        if row.getValue(datefield) not in datelist:
            datelist.append(row.getValue(datefield))
    counter = 0
    for tag in range(0,len(datelist)):
        counter += 1
        for specie in range(0,len(specieslist)):
            where_clause =  "%s = '%s' AND %s = '%s'" % (field,specieslist[specie],datefield,datelist[tag])
            layer.definitionQuery = where_clause
            for lyr in arcpy.mapping.ListLayers(mxd, "Raster_grouplayer",arcpy.mapping.ListDataFrames(mxd)[0]):
                arcpy.mapping.AddLayerToGroup(arcpy.mapping.ListDataFrames(mxd)[0],lyr,layer,"TOP")
                mxd.save()
                layer.visible = True
                arcpy.RefreshTOC()
                arcpy.mapping.ExportToPNG(mxd, output + os.sep + specieslist[specie].replace(".","").replace(r"/","_").replace(",","_") + "_" + str(counter) + "_" + datelist[tag].replace(".","").replace("/","").replace(" ","_") + r".png",resolution=printresulution)
            mxd.save()
            del data_frame,mxd
            mxd = arcpy.mapping.MapDocument(mxd2)
            data_frame = arcpy.mapping.ListDataFrames(mxd)[0]
            layerlist = arcpy.mapping.ListLayers(mxd,"",data_frame)
            for element in layerlist:
                if element.name == layername:
                    arcpy.mapping.RemoveLayer(data_frame,element)
            mxd.save()
    mxd = arcpy.mapping.MapDocument(mxd2)
    mxd.save()
multiMap(mxd,fc,field,datefield,output,printresulution)

One more edit: I don't really to save the mxd and maybe there is the mistake. However, I would like to keep the possibility to change the map and save these changes. That is why I kept it in the snippet.

  • 1
    Questions about code should include a snippet. While your script doesn't touch the north arrow, you are producing your exported maps from the script. You could use arcpy to find the North Arrow element on your layout and place it for each map it outputs – Midavalo Jun 30 '16 at 8:33
  • 1
    @Midavolo & PolyGeo Are you serrious? Should I post 150 lines of code here, which are not including what is actually happening. I thought rather I ask if somebody had the same problem and has a solution for that. I am pretty sure it is not about the code, but about the north arrow or the new version of ArcGIS – Alex Jun 30 '16 at 8:51
  • 2
    it's hard to know what's happening without seeing more of what you're doing - that may be code, screenshots, settings, etc. It's hard to diagnose from just a description. Please see the second half of my comment above also. – Midavalo Jun 30 '16 at 8:54
  • 1
    Please do not post 150 lines of code. To help our volunteers help you we ask that you provide just a code snippet that works up to the point where the symptoms that you describe appear. – PolyGeo Jun 30 '16 at 9:07
  • @PolyGeo Do I need anything else to do to reopen the Question? – Alex Jun 30 '16 at 12:02
2

Rebuild your MXD from scratch in 10.4, rather than using an old one from an earlier version.

The reason I suggest this is that your script seems to work fine as per your description - I have created a mock grid with MXD etc. and output 150 png files, and the north arrow doesn't budge. Tested in ArcGIS Desktop 10.4.

I tried this first using a script I wrote based on yours (I rewrote to get my head around what your script was doing), and then a straight copy/paste of yours (modifying only the path to MXD and GDB).

I don't think you need all the mxd.save() and the del data_frame, mxd that you have through your script, however these don't play a part in the issues you are having.

A new MXD may remove any old remnants of previous version issues - I have seen issues carried over in MXDs on update of ArcGIS Desktop and solution many times was to rebuild the MXD from scratch.

To save time for testing you could try rebuilding just the Layout and copying all the layers from your original MXD to see if that works, otherwise rebuild the whole thing.

The way I have typically rebuilt an MXD is as follows:

  1. Save all your required Layers as Layer Files (right-click on Layer in ToC, Save As Layer File...)
  2. Start new session of ArcMap and add layers in from Geodatabase (NOT from your saved Layer Files)
  3. Apply symbology, importing from your saved layer files - reason I do this is just to be sure nothing odd is saved in your layer files. This way you're just pulling through symbology.
  4. Set up your layout and Save MXD.

Retry your script to see if the issue still occurs.

  • @Midavallo Thanks for your errfort. As my MXD is fully loaded with many different things it is not as quick to completly rebuilt it from scratch. However, I build a short version of my MXD and guess you are right with the issues you mentioned. – Alex Jul 4 '16 at 10:56

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.