Select to view content in your preferred language

Zoom to slected feaure and print loop

1735
6
07-13-2017 09:33 AM
by Anonymous User
Not applicable

I need help please! I have only been coding for about 3 days. I am trying to write a script in arcpy that zooms to each selected feature in a feature class  and prints them as a PDF one by one.

So the script will first zoom to the first selected feature and print as a PDF, then zoom to the next one and print as a PDF and zoom to the third one and print as a PDF, and so on until it has gone down the list. Any help will be appreciated

Tags (2)
0 Kudos
6 Replies
XanderBakker
Esri Esteemed Contributor

What do you have at this moment? Is this something that you want to offer as a tool or Add-In to end users? Are you working with ArcMap or ArcGIS Pro? Did you have a look at Create a map book with Python—ArcGIS Pro | ArcGIS Desktop or Building map books with ArcGIS—Help | ArcGIS Desktop ?

RebeccaStrauch__GISP
MVP Emeritus
JoshuaBixby
MVP Esteemed Contributor

Is this a one-off task?  Does it have to be scripted or would something like Data Driven Pages work for you?

A rudimentary script that iterates through selected features in a layer and exports a JPG is below:

import os

out_folder = # Folder to dump exported JPEG or other file
lyr_with_sel = # Name of layer with selected features
scale = None # Set scale of export, None fits extent to selected feature

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

with arcpy.da.SearchCursor(lyr_with_sel, ["OID@", "SHAPE@"]) as cur:
    for oid, shape in cur:
        df.extent = shape.extent
        if scale:
            df.scale = scale
        arcpy.RefreshActiveView()
        arcpy.mapping.ExportToJPEG(mxd,
                                   os.path.join(out_folder, "out_{}.jpg".format(oid)),
                                   df)

If you have only been coding for 3 days, this may be pushing it a bit.  The basic script above utilizes several different aspects of ArcPy, I encourage you to understand how each one works.

RebeccaStrauch__GISP
MVP Emeritus

and keep in mind that is the features your are zooming to are Point features, you will need to zoom out some since points have no geographic extent/area.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

That is why I put the scale variable into the script.  If None, it will size to the object; if set, it will center on the feature and zoom to the specified scale.

0 Kudos
by Anonymous User
Not applicable

Thank you guys. I was able to achieve what I wanted by simply using data driven pages, and specifying certain pages to print.

0 Kudos