mzcoyle

Strange bugs or functionality with mapping module

Discussion created by mzcoyle on May 7, 2013
Latest reply on Jul 8, 2013 by myn@bjtu
I have two problems with the same script that worked in 10.0 but now is throwing some serious curveballs at me using 10.1.

Problem #1
I have a script that worked fine in 10.0 giving a list of some unique IDs to zoom to in an mxd, change the definition query, fill out some text elements from that ID and export to pdf. Using 10.1 it seems I have to now remake the mxd, dataframe, and layer objects for each iteration. Having these objects static outside the item loop and just referencing them each time (my original setup) I get a weird situation where the first item is exported twice using the same extents but different text elements/definition query. Then after that every map is one item behind in the list in extents but with the right text elements and definition query. Has anyone else seen this before? Am I just missing something or is this a bug?

Here's an example of how I got it working putting the mapping objects inside the loop.
        for item in item_list:
            print("Selecting {0} from {1} for export...".format(item, item_field))
            # Map Doc
            mxd = arcpy.mapping.MapDocument(map_path)
            # Dataframe
            df = arcpy.mapping.ListDataFrames(mxd, df_name)[0]
            # Layer
            lyr = arcpy.mapping.ListLayers(mxd, layer_name, df)[0]

            arcpy.SelectLayerByAttribute_management(
                lyr, "NEW_SELECTION", "{0} = {1}".format(arcpy.AddFieldDelimiters(lyr.dataSource, item_field), item))

            print("Zooming to extent")
            df.extent = lyr.getSelectedExtent(False)
            if df.scale < 30000:
                df.scale = int(math.ceil(df.scale / 5000.0)) * 5000
            elif 30000 <= df.scale < 100000:
                df.scale = int(math.ceil(df.scale / 10000.0)) * 10000
            else:
                pass


Problem #2
My second but not sure if it is related problem. When I set the dataframe scale to a round number using my rounding code I do not get the expected results. Eg 4442 when it should be 5000, 8883 when it should be 10000, 17766 when it should be 20000 etc. It is always offset by these amounts so it is not just skipping over and keeping the lyr.getSelectedExtent(False) value. I even try having a static df.scale = 10000 for each feature and still get the error where they are all 8883. I have tried using the code manually in arcmap using this mxd and it gives the expected result of rounded scale values, the only different being initializing the mxd object with the 'current' keyword instead of the document path.

Edit: Oddity continues... I added a legend object instead of the graphics/text I had representing the legend before and problem #2 went away...

Outcomes