Iterative Zoom to and export to JPG

4551
13
Jump to solution
11-14-2011 07:40 AM
AnthonyTimpson2
Occasional Contributor
Hello,


I have been trying to string together a simple script to read through a Grid.shp, Zoom to each feature, Export that image to a jpg with a world file with a filename based on the Grid index of the iterated row. 

i tried to make this with Modelbuilder but it lacks the functionality it seems


I am absolutely green when it comes to python and I would greatly appreciate a nudge in the right direction.

thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Anthony,

Here is an example on how to do this.  What the script does is iterate through a shapefile called 'Grid' and adjusts the MXDs extent to each feature in the GRID shapefile, and then exports the map to a JPG based on that extent

import arcpy
from arcpy import env
from arcpy import mapping
env.workspace = r"C:\temp\python"

mxd = mapping.MapDocument(r"C:\DATA\Philadelphia.mxd")

fc = "GRID.shp"
count = str(arcpy.GetCount_management(fc))

x = 1

while x < int(count) + 1:
    rows = arcpy.SearchCursor(fc, "OID = " + str(x))
    for row in rows:
        xmin = row.shape.extent.XMin
        ymin = row.shape.extent.YMin
        xmax = row.shape.extent.XMax
        ymax = row.shape.extent.YMax
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        newExtent = df.extent
        newExtent.XMin, newExtent.YMin = xmin, ymin
        newExtent.XMax, newExtent.YMax = xmax, ymax
        df.extent = newExtent
        mapping.ExportToJPEG(mxd, r"C:\temp\python\JPEG_" + str(x) + ".jpg")
        print "successfully printed JPG"
    x += 1

View solution in original post

13 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Anthony,

Here is an example on how to do this.  What the script does is iterate through a shapefile called 'Grid' and adjusts the MXDs extent to each feature in the GRID shapefile, and then exports the map to a JPG based on that extent

import arcpy
from arcpy import env
from arcpy import mapping
env.workspace = r"C:\temp\python"

mxd = mapping.MapDocument(r"C:\DATA\Philadelphia.mxd")

fc = "GRID.shp"
count = str(arcpy.GetCount_management(fc))

x = 1

while x < int(count) + 1:
    rows = arcpy.SearchCursor(fc, "OID = " + str(x))
    for row in rows:
        xmin = row.shape.extent.XMin
        ymin = row.shape.extent.YMin
        xmax = row.shape.extent.XMax
        ymax = row.shape.extent.YMax
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        newExtent = df.extent
        newExtent.XMin, newExtent.YMin = xmin, ymin
        newExtent.XMax, newExtent.YMax = xmax, ymax
        df.extent = newExtent
        mapping.ExportToJPEG(mxd, r"C:\temp\python\JPEG_" + str(x) + ".jpg")
        print "successfully printed JPG"
    x += 1
nathanbush
New Contributor

hi Jake,

I'm also pretty new to Python, but iterating through many polygons within my shapefile and exporting to JPEGs is exactly what I need to do. I've copied your script, but I keep coming up with the attribute error 'Extent' object has no attribute 'Xmin'. My shapefile only has 4 fields (FID, Shape*, Unit, Hectares), am I missing something here?

Any help is good help.

Thanks

Nate

0 Kudos
DarrenWiens2
MVP Honored Contributor

In Python, case matters. XMin (a defined property of Extent) is different from Xmin (something unknown). You can see the exact list of valid Extent properties here.

nathanbush
New Contributor

figures it would be a simple syntax error

0 Kudos
nathanbush
New Contributor

Hi Jake,

Thanks for the script, its almost doing exactly what I want it to do. Thanks Darren for your correction.

I've got the script to run and iterate through each row and produce a JPEG. However, the JPEGs are all blank - just a white space JPEG of 23 KB. I was thinking that the iteration is not allowing Arc to redraw the map and taking a snapshot of a blank dataframe before the orthophoto and polygons had time to draw. I added a "time.sleep" of 20 seconds (didn't work), so I increased to 60 sec (didn't work). I've also tried exporting to PDFs, but same issue.

Any ideas???

Nate

Here's the Script:

import time

import arcpy

from arcpy import env 

from arcpy import mapping 

mxd = arcpy.mapping.MapDocument(r"N:\Data\My_map.mxd") 

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

fc = r"N:\Data\IWMMunits.shp"   

count = str(arcpy.GetCount_management(fc)) 

x = 0 

while x < int(count) + 1: 

    rows = arcpy.SearchCursor(fc, "FID = " + str(x))  

    for row in rows: 

        xmin = row.shape.extent.XMin 

        ymin = row.shape.extent.YMin 

        xmax = row.shape.extent.XMax 

        ymax = row.shape.extent.YMax  

        filename = row.getValue("Unit_1")

        df = arcpy.mapping.ListDataFrames(mxd)[0]  @

        newExtent = df.extent 

        newExtent.XMin, newExtent.YMin = xmin, ymin 

        newExtent.XMax, newExtent.YMax = xmax, ymax 

        df.extent = newExtent 

        time.sleep(60)

        mapping.ExportToJPEG(mxd,r"N:\Data\Test_JPEGS\{0}.jpg".format(filename)) 

        print "successfully printed JPG" 

    x += 1

0 Kudos
JakeSkinner
Esri Esteemed Contributor

For the 'filename', make sure the name does not contain any spaces, special characters, or begin with a number.

0 Kudos
nathanbush
New Contributor

Jake,

Thanks again for the timely response. I'm still getting blank JPEGs.

I've taken a subset of my data and removed any special characters in the rows within the specified fieldname (Unit1)

The attribute table is simply "FID", "Shape","Unit1", "Hectares", "Shape_Leng", "Shape_Area"

"Unit1" is the field name I want to iterate through and also will be used to name the JPEGs, ex: "CT001GM.jpg" 

FIDShapeUnit1HectaresShape_LengShape_Area
0PolygonCT001GM00.084042-0.000311

import time

import arcpy

from arcpy import env 

from arcpy import mapping 

env.workspace = r"N:\NWRS\DNR\Coastal_Network"

mxd = arcpy.mapping.MapDocument(r"N:\NWRS\DNR\Coastal_Network\IWMM.mxd") 

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

fc = r"N:\NWRS\DNR\Coastal_Network\Test2.shp"   

count = str(arcpy.GetCount_management(fc)) 

x = 0 

while x < int(count) + 1: 

    rows = arcpy.SearchCursor(fc, "FID = " + str(x))  

    for row in rows: 

        xmin = row.shape.extent.XMin 

        ymin = row.shape.extent.YMin 

        xmax = row.shape.extent.XMax 

        ymax = row.shape.extent.YMax  

        filename = row.getValue("Unit1")      # this should be the attribute field name correct?

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

        newExtent = df.extent 

        newExtent.XMin, newExtent.YMin = xmin, ymin 

        newExtent.XMax, newExtent.YMax = xmax, ymax 

        df.extent = newExtent 

        time.sleep(60)

        mapping.ExportToJPEG(mxd,r"N:\NWRS\DNR\Coastal_Network\Test_JPEGS\{0}.jpg".format(filename)) 

        print "successfully printed JPG" 

    x += 1

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Attached is some data you can test with. Extract the zip file to the following location:

C:\temp\python\userdata

Then you can run the code below.  It should successfully print out 4 JPGs to the same directory.

import arcpy
from arcpy import env 
from arcpy import mapping 

env.workspace = r"C:\temp\python\userdata"

mxd = arcpy.mapping.MapDocument(r"C:\temp\python\userdata\IWMM.mxd") 
df = arcpy.mapping.ListDataFrames(mxd)[0] 

fc = "GRID.shp"   
count = str(arcpy.GetCount_management(fc)) 

x = 0 
while x < int(count) + 1: 
    rows = arcpy.SearchCursor(fc, "FID = " + str(x))  
    for row in rows: 
        xmin = row.shape.extent.XMin 
        ymin = row.shape.extent.YMin 
        xmax = row.shape.extent.XMax 
        ymax = row.shape.extent.YMax  
        filename = row.getValue("Unit1")
        df = arcpy.mapping.ListDataFrames(mxd)[0] 
        newExtent = df.extent 
        newExtent.XMin, newExtent.YMin = xmin, ymin 
        newExtent.XMax, newExtent.YMax = xmax, ymax 
        df.extent = newExtent 
        mapping.ExportToJPEG(mxd,r"C:\temp\python\userdata\{0}.jpg".format(filename)) 
        print "successfully printed JPG" 
    x += 1

del mxd, row, rows
0 Kudos
nathanbush
New Contributor

Thanks Jake, it worked on my data too!

Is there a way to get the polygons to show up too, since its parcel boundaries - need that displayed as well.

again, thanks for tolerating my neediness.  

0 Kudos