PYTHON GIS - How to control multiple dataframes with data driven page and save them as separate ArcGIS files

0

 


In this article you will learn how to use datadriven page to control several dataframes in an ArcMap file and generate several ArcGIS (MXD) files with the different views using Python. No programming required.


Surely you found very useful my article “PYTHON GIS - Export Data Driven Pages to individual ArcGIS / ArcMap (MXD) Files”, but in that article it was assumed that there is only one main dataframe that needs to be controlled by datadriven page, but now we will go further, and we will achieve what many times I was told it was impossible, “control” several dataframes with datadriven page...


Suppose you need to show on a map the route of a long linear project at a very detailed scale, you have two options:


How to display detailed views along a line?


Option 1: use Strip Map Index to generate a grid oriented with that alignment and activate datadriven page with that grid in a single dataframe. The result will be a MXD file with the path of the whole line.


Pros: -Faster, even if there are subsequent adjustments. Cons: -The number of map pages needed can reach exorbitant numbers, and impractical if maps need to be printed.


Option 2: use Strip Map Index to generate a grid oriented with that alignment, create two or three dataframes, and match each grid to the different dataframes, with everything and orientation manually, once you have adjusted the dataframes you have to save the file and do the same with the subsequent grids and repeat the procedure until you have covered all the alignment. The result will be several MXD files showing the path of the line in several dataframes.


Pros: - Optimal use of map page space, reducing considerably the number of files and/or graphic outputs to be printed. Cons: Very time consuming when generating the maps and corrections can be very cumbersome as each file has to be corrected individually. 


With the following procedure you will learn how to combine both options into one, eliminating the cons of both, you will be much more efficient since you will obtain the result of the second option without the time consumption that this way of doing it implies, all this using Python code (Do not worry, you do not need to know how to program to achieve it).


Although it is not necessary to know how to program to carry out this procedure, I have to recognize that it is one of the most complicated codes that I have created, follow very carefully the following steps:


How to use Data Driven Page for multiple Dataframes using Python?


Step 0: Create a map that has all the dataframes to be used (In this case let's assume two dataframes, VISTA_01 [a], and VISTA_02 [b]).  


Ilustración de un mapa con 2 dataframes o vistas



Step 1: Create a feature that represents the windows that you want to show in the different dataframes, the most practical tools to do this are Grid Index Feature or Strip Map Index, being this last one the ideal for projects where an alignment must be followed, and the one that I used for this example.


Muestra gráfica del resultado de la herramienta Strip Map Index



Step 2A: For the feature created in step 1, create a field called “MAP” of type Double according to the procedure shown below.  



Representación gráfica de la creación del campo MAP




Step 2B: Indicate in the “MAP” field (a) to which map the created windows belong. For example: if there are two dataframes and the number of views is four, Views one and two will be in MAP 1, therefore, for these views the MAP field will be filled in with the number 1 (b); On the other hand views three and four will be in MAP 2, therefore the MAP field for views three and four will be filled with the number 2 (c), so depending on the number of views generated and the number of dataframe contemplated in the MXD file this procedure will be repeated with all the views represented by the polygons created in the feature of step 1.



Llenado del campo MAP según el numero de archivos MXD a producir



Step 3: For the feature created in step 1, create a field called “CUAD” of type Double as seen in step 2A and indicate there to which dataframe the created windows belong (For example if there are two dataframes and the number of views is four, View one and view three will be in dataframe 1, therefore, for these views the CUAD field will be filled in with the number 1 [a]; On the other hand, views two and four will be in dataframe 2, so the CUAD field for views 2 and 4 will be filled with the number 2 [b], depending on the number of views generated and the number of dataframes contemplated in the MXD file).



Diligenciamiento del campo CUAD según el numero de dataframe presentes en el documento de salida



Step 4: Drag the feature created in step 1 to each of the dataframes (it must be on all layers on all dataframes [a]), and apply a Definition Query for that feature on each dataframe according to each unique value of the CUAD field corresponding to the dataframe (For example, if there are two dataframes, it means the CUAD field can only have values 1 and 2, so apply the Definition Query CUAD = 1 to the layer loaded to dataframe 1 at the first position [b]; then apply the Definition Query CUAD = 2 and on the layer loaded to dataframe 2 at the first position [c]).


NOTE: For a more detailed explanation of how to create a Definition Query, see my article “PYTHON GIS - How to modify and/or create a Definition Query to a layer within several ArcGIS (ArcMap / MXD) files”, section “How to modify and/or create a SQL expression to an ArcGIS / ArcMap layer”.


Aplicación de la definition query según el campo CUAD en el dataframe respectivo




Step 5: Define the working scale for each of the dataframes in the “Standard” toolbar, each dataframe must be activated for the scale setting to take effect (In this case the working scale is 1:5000).



Ajuste de escala en la barra de herramientas Standard


Step 6: Configure the datadriven page in the first dataframe (a), specifying the Sort field with the MAP field (b), the Name field (it is not relevant which field is used), the Angle Field (only if Strip Map Index [c] was used), and in the “Extent” tab (d) check the option “Center And Mantain Current Scale” (e).


Ilustración de como definir el Data Driven Pages



Step 7: Copy the following code into a note blog.





import arcpy                                                       #01
import arcpy.mapping as map                                        #02
mxd = arcpy.mapping.MapDocument("CURRENT")                         #03
dfa = arcpy.mapping.ListDataFrames(mxd, "VISTA_01")[0]             #04
lyra = arcpy.mapping.ListLayers(mxd, "", dfa)[0]                   #05
rowsa = arcpy.SearchCursor(lyra)                                   #06
for row in rowsa:                                                  #07
  AA = row.getValue("MAP")                                         #08
  mxd.dataDrivenPages.currentPageID = int(row.getValue("MAP"))-1   #09
  newDFexta = row.getValue("Shape").extent                         #10
  dfa.extent = newDFexta                                           #11
  dfa.scale = 5000                                                 #12
  dfa.rotation = row.getValue("Angle")                             #13
# Copy form here to insert new dataframe                           #14
  df = arcpy.mapping.ListDataFrames(mxd, "VISTA_02")[0]            #15
  lyr = arcpy.mapping.ListLayers(mxd, "", df)[0]                   #16
  rows = arcpy.SearchCursor(lyr.dataSource, '"MAP" = ' + str(AA))  #17
  for row in rows:                                                 #18
    newDFext = row.getValue("Shape").extent                        #19
    df.extent = newDFext                                           #20
    df.scale = 5000                                                #21
    df.rotation = row.getValue("Angle")                            #22
# Copy form here to insert new dataframe                           #23
    mxd.saveACopy(r"C:\EJEMPLO\EJEMPLO\ARCHIVO_" + str(int(AA)))   #24
del mxd                                                            #25

                                                                     



Step 8: Modify in line #04 of the code the word “VISTA_01” and place the name you want to use for the first dataframe.


Step 9: Modify in line #15 of the code the word “VISTA_02” and place the name you want to use for the second dataframe.


Step 10: Modify in line #12 of the code the number 5000 and place the denominator of the scale to be used in the map for the first dataframe (Same as the one established in step 5).


Step 11: Modify in line #21 of the code the number 5000 and place the denominator of the scale to be used in the map for the second dataframe (Same as the one established in step 5).


Step 12: Modify in line #24 of the code the path “C:\EJEMPLO\EJEMPLO” to the path where the MXD files are to be generated and saved; in the same line modify the word “ARCHIVO_” to the prefix that all MXD files will have.


Step 13: Open the Python window in the Standard toolbar (This must be done in the MXD file created in step 0).


Muestra la ubicación de la ventana Python dentro de la barra de herramientas Standard (estándar)



Step 14: Copy the modified code into the Python window and press the Enter key twice.


After a few seconds you will see how the MXD files start to appear in the specified path (a), and when you open them you will notice how each one has the corresponding view (b).


Resultado del código ejecutado


Verificación del resultado optenido




Notes:


  • Currently, the code works with two dataframes, if you need to use a third dataframe you must copy the code from line #14 to line #23, go to line #23 press Enter and paste the code, then make the necessary adjustments shown in steps 9 and 11.  
  • Using this code requires a lot of imagination to prepare the data and visualize the final product long before it is ready, but if you manage to master it I assure you that you will be more productive than 20 people together, you do not need more than a single ArcMap file to produce dozens of maps optimally, no matter the length of a line, you always have to concentrate on polishing a single file.


I hope you enjoyed this article, soon I will upload a video explaining the procedure that you can consult in this blog. Save my blog among your favorite links, I will be uploading many more tricks of this style, remember that you can send me your concerns in the contact page or leave your comment, I will be attentive to respond.


You may like these posts

No comments