I have a set of Data Driven Pages with counties and land cover already made. I am trying to create a tool that will take input of a county name and spawn a new set of DDP at a 4000M per grid square. I have been able to select, clip and create a smaller grid focusing on the area of interest but can not reset the index layer. Is this possible, and if not is there a work around?
Interesting workaround, I knew it would work. Thanks for your post, I will likely need this in the future. I figured 'replacing' completely the index datasource was 'breaking' the DDP functionality and wondered if I should have tested the part where you'd need to restructure the page numbering (or other fields) since those values may need to be dynamically maintained. I haven't confirmed, but I suspect as long as you aren't deleting the index fc or layer, you could go as far as clearing it of features and loading it with new ones.....sort of similar to 'reloading' an SDE fc to circumvent having to re-establish privileges, etc. - maybe that's a weak analogy....
Regardless, I see what you mean, thank you very much for the alternate working solution!
An interesting problem, have you tried something like deleting features in a 'dummy' or existing index layer for which DDP are already set up, then appending the new features corresponding to the new extent/size etc. in theory, since you haven't actually changed the internal relationship of the index feature layer in the map and the DDP software, once you 'refill' it with new legitimate features, the output will only need to be refreshed...the export to PDF should be dynamic anyway - how else do pages reflect new page content?
So set up your document DDP enabled mxd with the index and other necessary layers loaded and setup...then I'd run a simple test to see if you can delete an index feature, say, by page number, then append a grid feature (make one up) with that corresponding page number (and any related necessary values for that feature record) and see if you can export the new page by data driven arcpy methods. Fairly simple test I think- in fact, that's a great 'template' example I may experiment with tomorrow. As time permits of course...let me know what results you arrive at, I'd be interested how well this works.
Thank you very much for your response. Using part of what you said, I think I got it to work. I just used UpdateLayer to switch out the original Index layer for the grid created in the code. After manually refreshing the DDP it updated perfectly.
Unfortunately after working on the problem some more the fix no longer works. For whatever reason the layer updated as I wanted last night, but when I run the program now it automatically switches to a different layer. I am going to phrase this in another question and post it after I comb through the forums to see if it is already there. Very frustrating that it worked multiple times last night but not today.
I'd like to explore this problem if I get time tomorrow... did you notice the 'fix' stop working after you exited ArcMap or did something else? Also, the index layers you want to use are all from the same schema, i.e. same fields, etc., and nothing changing except the record values? If so, you can test the other idea of 'emptying' (deleting features) in the index and reloading it (appending features). Seems to me that way no internal ArcMap linked functionality would be broken....what worked before should simply be functioning the same way, just using new values.
I noticed the 'updateLayer' documentation stated it was equivalent to a kind of 'remove and replace':
"UpdateLayer, when not restricted to symbology, is essentially calling the RemoveLayer and AddLayer functions."
...which, I don't know, seems to me could 'break' something. If that sound vague, that's because it is....unknown to me until I test it out. Your method may have broken only parts evident later, just don't know. If I find out anything tomorrow I'll post back here.
EDIT: I just thought of something - there may be field value dependencies that exist... but for things like dynamic text, seems like you could simply adjust those if you need to in the layout template. Anyway, something to think about, and if you had to, you could even potentially keep the same records and update the field values (including the geometry) with an update cursor.
Unfortunately I altered major pieces of my overall code and would be unable to trace back to what I had last night that accomplished my goal.
A restatement of my problem would be as follows:
I have an MXD with Data Driven Pages enabled (sorted by county) that has multiple feature classes in it (Land cover, land ownership, road layers...) I am writing a tool that will take the input of a county name and create a new set of DDP based on a grid of Xnumber of meters (will be a constant in the code) showing only that county and export them to a PDF.
Last night I used UpdateLayer to replace the counties layer that the original DDP were sorted by with the grid layer I created within the script. When I refreshed using the DDP toolbar manually it switched the DDP index layer to the grid layer. (Perfect)
Today, after I altered my code, when I refreshed the DDP manually it switched the index to a different layer, one which will not work for organizing the DDP.
The actual UpdateLayer code was updated to reflect a separate mxd, even though it was a copy of the one previously used. I am wondering if it has to do with the order the layers are in the new mxd. If yesterdays version had the grid file in first position it could have automatically slid into the Index layer and worked no matter what I was replacing it with. If I get a chance I will code that layer to the top before I copy it, and then redo the layer update.
I moved the grid layer above the other (using MoveLayer) before running the UpdateLayer command and achieved the desired result. It is working again for the moment. When I refreshed the DDP from the DDP toolbar and checked the Index layer it had become the grid layer I wanted.
Edit: The dynamic text name updated from the County name to the Grid name. The page number also updated to reflect the proper number of pages in what will be the new mapbook.
...then, ListLayers returns a list object filtering for only those layers with the text in the name, "Rivers" (for the above example which is in the webhelp ref for updateLayer documentation). Incidentally, the use of updateLayer in the example is arguably a bad choice because this is just a layer object variable -- the  simply extracts the object from the list (hopefully the only object in the list). In other words, it has nothing to do with the order in the map.
Is the order of the layer in the map relevant to the setup of the DDP index layer? I don't think so. I think it is a refence by name only, right?
EDIT: Should probably include the 3 lines of code in the sample together:
The 1st line I've already discussed; the 2nd line sets a sourceLayer layer object from a lyr file. Finally the 3rd line uses the UpdateLayer method (or function) to 'switch' layers. So, you could use this technique with a lyr library defining county extents, etc., to change the index layer without cluttering your map of 'extra' layers.....theoretically, of course. Hope I am understanding the technique you are trying to emulate here. Good luck!
I have replicated the problem by commenting out the first four lines of code. With them in, the Grid layer becomes the Index layer, with them removed the LINN_OrAndWaOwnership layer becomes the Index layer. I used the move command because I will be reordering 10+ layers and wanted to streamline the process.
# Reset Data Driven Page Index Layer indexLayerOut = arcpy.mapping.ListLayers(mxd2, "Counties", df) indexLayerIn = arcpy.mapping.ListLayers(mxd2, "LINN_grid", df) arcpy.mapping.UpdateLayer(df, indexLayerOut, indexLayerIn, False) print "index updated" # Refresh DDP - Does not work, must be done manually arcpy.RefreshActiveView()
I agree that within the DDP toolbar relative placement has nothing to do with what is set as the index layer. I am only about 4 months into my programming so there is a ton I don't understand about the inner workings. I really appreciate your help.
Edit: To further confuse things (or shed light, who knows?) after the index layer has been updated I moved the OrWaOwnership layer above the grid layer later in the script. This resulted in the Ownership layer is once again becoming the Index layer.