Solved! Go to Solution.
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
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
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
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.
figures it would be a simple syntax error
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
For the 'filename', make sure the name does not contain any spaces, special characters, or begin with a number.
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"
FID | Shape | Unit1 | Hectares | Shape_Leng | Shape_Area |
0 | Polygon | CT001GM | 0 | 0.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
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
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.