Creating Data Driven Pages Based on Selection

887
8
Jump to solution
06-21-2012 07:15 AM
MarcCusumano
Occasional Contributor
I am working on creating an automated process that exports data driven pages to JPEG images based on a set of joins and spatial queries inside of a map document. All of the features and tables I am working with are in an SDE environment and cannot be modified. I am going through the process manually first before I begin trying to automate everything in Python.

I am stuck at the point where I need to create data driven pages based on a current selection. I have setup a join from a feature to a table keeping only matching features, and then performed a spatial query on those matching features with another feature to arrive at a final selection of 771 records out of 989. The next step is to build data-driven pages based only on these selected features, and not on the entire feature. I have gone through the documentation and have not come up with anything. Again, ideally I would not be creating a new feature, but it is looking like I may have to in a temporary workspace before setting up the map book.

Thanks.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
My motto has always been, "why do it in the UI, when you can do it with arcpy".  Everything you can do with DDP, you can do with arcpy.mapping plus a whole lot more.

Couple of things:

1) Curious why mxd = arcpy.mapping.MapDocument(mxd = "CURRENT") works.  The parameter name is mxd_path (but that doesn't work).  I personally use .MapDocument("CURRENT")

2) Your Layer variable doesn't look right.  Try, Layer = arcpy.mapping.ListLayers(mxd, "ImplausibleReads", df)[0]


Jeff

View solution in original post

0 Kudos
8 Replies
JeffBarrette
Esri Regular Contributor
There is an option when exporting DDP to use only the selected set.  So I would enable DDP on all features of the index layer, then create my selection, then export.

In the UI, go to File --> ExportMap --> Select PDF --> Pages Tab - check Selection (optionally you can show selection)

In arcpy, the parameters are:
exportToPDF (out_pdf, {page_range_type}, {page_range_string}, {multiple_files}, {resolution}, {image_quality}, {colorspace}, {compress_vectors}, {image_compression}, {picture_symbol}, {convert_markers}, {embed_fonts}, {layers_attributes}, {georef_info}, {jpeg_compression_quality}, {show_selection_symbology})

Set     page_range_type = "SELECTION",   optionally, set    show_selection_symbology=True

Jeff
0 Kudos
JeffBarrette
Esri Regular Contributor
I don't think my original response addresses the main concern (you want to go to JPEG, not PDF). Here is another shot at it.

Again, build your DDP using the complete set of features and then build your selection.  Next, use the .selectedPages property on the DDP object to process only the pages of interest.

Look at code examples 1 and 3 in the arcpy.mapping DDP object help topic.  In example 1, change PNG to JPEG and use the selected pages optoin from example 3.

I hope this helps,
Jeff
0 Kudos
MarcCusumano
Occasional Contributor
Thank you Jeff, that will make the process a lot cleaner. I am trying to automate this, so that when data is dumped into a feature class the script will create the data driven pages automatically and export to JPEG. Is there a way to build data driven pages without doing it manually through ArcMap?
0 Kudos
JeffBarrette
Esri Regular Contributor
We are not able to build DDP outside of ArcMap.

Jeff
0 Kudos
MarcCusumano
Occasional Contributor
That is what I figured. I will need to come up with a way to do this without using DDP. Here is what I have so far (this is solely to emulate the DDP functionality inside ArcMap for now; once it is working I will modify it to work from a standalone Python script).

"ImplausibleReads" is the layer I am trying to export as DDP, which is built on the "GAS_SELECTION" feature class.



import arcpy
from arcpy import env
env.workspace = "H:\SP1G0\Gas Leaks\GasLeaks.gdb"

mxd = arcpy.mapping.MapDocument(mxd = "CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
Layer = (mxd, "ImplausibleReads", df)[0]


# Create a search cursor

rows = arcpy.SearchCursor("Gas_Selection")

currentState = " "

  # Iterate, select and zoom to each row in the cursor and export to JPEG
 
for row in rows:
       [INDENT]if currentState != row.OBJECTID:[/INDENT]
            [INDENT][INDENT]currentState = row.OBJECTID[/INDENT][/INDENT]
            [INDENT][INDENT]arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION", ' "OBJECTID" = currentState ' )[/INDENT][/INDENT]
            [INDENT][INDENT]df.zoomToSelectedFeatures()[/INDENT][/INDENT]
            [INDENT][INDENT]arcpy.RefreshActiveView()[/INDENT][/INDENT]
            [INDENT][INDENT]arcpy.mapping.ExportToJPEG(mxd, r"H:\SP1G0\Gas Leaks\JPEG\Connection_Obj__" + str(currentState) + ".jpeg", df_export_width=1500, df_export_height=1200, resolution=300, jpeg_quality=70)[/INDENT][/INDENT]
del mxd






This results in an error "<type 'exceptions.RuntimeError'>: Object: Error in executing tool
Failed to execute (Script)."
0 Kudos
JeffBarrette
Esri Regular Contributor
My motto has always been, "why do it in the UI, when you can do it with arcpy".  Everything you can do with DDP, you can do with arcpy.mapping plus a whole lot more.

Couple of things:

1) Curious why mxd = arcpy.mapping.MapDocument(mxd = "CURRENT") works.  The parameter name is mxd_path (but that doesn't work).  I personally use .MapDocument("CURRENT")

2) Your Layer variable doesn't look right.  Try, Layer = arcpy.mapping.ListLayers(mxd, "ImplausibleReads", df)[0]


Jeff
0 Kudos
MarcCusumano
Occasional Contributor
OK I made the changes you suggested and also changed my data sources to a local drive whereas before it was a remote connection. I also changed the SearchCursor method to specify the OBJECTID field, and also modified the "SelectLayerByAttribute" query to simply search for OBJECTID = 1 just to see if I could get it working. Here is the code I am now running:

========================================================================================================

import arcpy
from arcpy import env
env.workspace = "E:\GasLeaksProject\2012\GasLeaks.gdb"

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
Layer = arcpy.mapping.ListLayers(mxd, "ImplausibleReads", df)[0]

# Create a search cursor, iterate through the Implausible Reads Feature, getting and zooming to respective extents and exporting layout to JPEG.
rows = arcpy.SearchCursor("E:/GasLeaksProject/2012/GasLeaks.gdb/Gas_Selection", "", "", "OBJECTID")

currentState = " "

# Iterate through the rows in the cursor
for row in rows:
[INDENT]if currentState != row.OBJECTID:[/INDENT]
[INDENT][INDENT] currentState = row.OBJECTID
arcpy.SelectLayerByAttribute_management (Layer, "NEW_SELECTION", ' "OBJECTID" = 1 ' )
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
arcpy.mapping.ExportToJPEG(mxd, r"E:\GasLeaksProject\JPEG\OBJECTID" + str(currentState) + ".jpeg", df_export_width=1500, df_export_height=1200, resolution=300, jpeg_quality=70)[/INDENT][/INDENT]
del mxd

========================================================================================================

This works and exports the extent of the feature with OBJECTID = 1, but then continues exporting the same feature only with a larger extent. When I try adding the "currentState" variable to the SelectLayerByAttribute line, it does not work, and returns

<type 'exceptions.SyntaxError'>: invalid syntax (JPEG.py, line 20)
Failed to execute (Script).

I'm oging to continue playing around with it to see if I can get it working.

Thanks for your help
0 Kudos
MarcCusumano
Occasional Contributor
Great news! I got the script working. Here is the final code:


====================================================================================================

import arcpy
from arcpy import env
env.workspace = "E:\GasLeaksProject\2012\GasLeaks.gdb"

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
Layer = arcpy.mapping.ListLayers(mxd, "ImplausibleReads", df)[0]
#arcpy.MakeFeatureLayer_management("Gas_Selection", "ImplausibleRead")

# Create a search cursor, iterate through the Implausible Reads Feature, getting and zooming to respective extents and exporting layout to JPEG.
rows = arcpy.SearchCursor("E:/GasLeaksProject/2012/GasLeaks.gdb/Gas_Selection", "", "", "OBJECTID")

#rows = arcpy.GetCount_management("Gas_Selection")
currentState = " "

# Iterate through the rows in the cursor
for row in rows:
      [INDENT] if currentState != row.OBJECTID:[/INDENT]
           [INDENT][INDENT] currentState = row.OBJECTID
            where_clause = "\"OBJECTID\"=" + str(currentState)
            arcpy.SelectLayerByAttribute_management (Layer, "NEW_SELECTION",  where_clause )
            df.zoomToSelectedFeatures()
            df.scale = 6914
            arcpy.RefreshActiveView()
            arcpy.SelectLayerByAttribute_management (Layer, "CLEAR_SELECTION",  "" )
            arcpy.mapping.ExportToJPEG(mxd, r"E:\GasLeaksProject\JPEG\OBJECTID" + str(currentState) + ".jpeg", df_export_width=1500, df_export_height=1200, resolution=300, jpeg_quality=70)[/INDENT][/INDENT]
del mxd


====================================================================================================

Thanks again!
0 Kudos