Looping through features

2360
6
03-29-2012 07:08 AM
JohannesRein
New Contributor II
Hello

I'm quiet new in python and i have the following question.

I want to export my map into jpg's with worldfile. Therfore i created a polygon-featureclass with a grid (400 objects).
As it is a little uninteresting to select and pan to every single polygon and export it, i tried to write a script doing that for me.
The problem is that i do not know how to loop through the features.

This is the code a made.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
gridLayer = arcpy.mapping.ListLayers(mxd, "Grid", df)[0]
arcpy.SelectLayerByAttribute_management(gridLayer, "NEW_SELECTION", "SORT_ID = 1")
df.extent = gridLayer.getSelectedExtent(False)
df.scale = 100000
arcpy.mapping.ExportToJPEG(mxd, r"C:\Daten\Images\Image1.jpg", df, df_export_width=1600, df_export_height=1200, world_file=TRUE)
del mxd


Thank you in advance,
Jo
Tags (2)
0 Kudos
6 Replies
DarrenWiens2
MVP Honored Contributor
Use a SearchCursor.
0 Kudos
AnthonyTimpson2
Occasional Contributor
hey there.. i made something similar a while back

basically it uses a grid to zoom to and export map blocks.

not sure if its helpful for you but maybe you can pull some parts out of it.



import arcpy
from arcpy import env
from arcpy import mapping
env.workspace = r"C:\Users\Desktop\Work\GridExportTesting"

mxd = mapping.MapDocument("Current")

fc = "Grid"
count = str(arcpy.GetCount_management(fc))

x = 1

while x < int(count) + 1:
    rows = arcpy.SearchCursor(fc, "ID = " + str(x))
    for row in rows:
        xmin, ymin, xmax, ymax  = row.shape.extent.XMin, row.shape.extent.YMin, row.shape.extent.XMax, row.shape.extent.YMax
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        newExtent = df.extent
        newExtent.XMin, newExtent.YMin, newExtent.XMax, newExtent.YMax = xmin, ymin, xmax, ymax
        df.extent = newExtent
        arcpy.RefreshActiveView()
        mapping.ExportToJPEG(mxd, r"C:\Users\Desktop\Work\GridExportTesting\Grid\JPEG_" + str(x) + ".jpg", df, df_export_width=1638, df_export_height=880, world_file=True)
        print('Exported image' )
        print(x, 'of', count)
    x += 1

print("Export Complete")
0 Kudos
JohannesRein
New Contributor II
Thanks for your answers, especially for the code sample.

I tried to change my code, unfortunaly it does not yet work.
The problem is the selection of the feature. In the posteted example the attribute seems to be a string. I tried with a string attribute and a number(long). None works.

import arcpy
from arcpy import env
from arcpy import mapping
env.workspace = r"C:\Daten\Test"
mxd = mapping.MapDocument("Current")
df = mapping.ListDataFrames(mxd, "Layers")[0]
fc = "Grid_100k"
count = str(arcpy.GetCount_management(fc))
x = 1
while x < int(count) + 1:
    rows = arcpy.SearchCursor(fc, "SORT_ID = " + str(x))
    for row in rows:
 df.extent = fc.getSelectedExtent(False)
 df.scale = 100000
 arcpy.mapping.ExportToJPEG(mxd, r"C:\Daten\Test\Image_" + Str(x) + ".jpg", df, df_export_width=1200, df_export_height=800, world_file=TRUE)
        print('Exported image')
        print(x, 'of', count)
    x = x + 1
print("Export Complete")


Update:

i found out that if i use a value instead of a variable, it works. But how can i use the value from the variable?

>>> arcpy.SelectLayerByAttribute_management("Grid_100k", "NEW_SELECTION", "\"SORT_ID\" = 23")
<Result 'Grid_100k'>
>>> arcpy.SelectLayerByAttribute_management("Grid_100k", "NEW_SELECTION", "\"SORT_ID\" = x")
Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute). 
0 Kudos
FabianBlau
Occasional Contributor II
arcpy.SelectLayerByAttribute_management("Grid_100k", "NEW_SELECTION", "\"SORT_ID\" = %i" % x)

or
arcpy.SelectLayerByAttribute_management("Grid_100k", "NEW_SELECTION", "\"SORT_ID\" = " + str(x))


you also can use a for-loop, for example:
for x in range(1, count+1):
0 Kudos
JohannesRein
New Contributor II
The looping and nameing of the files work, but all images have the same extend.
Why does the extend not update?

It seems that ArcMap does not like the script. It crashed several times.

>>> import arcpy
... from arcpy import env
... from arcpy import mapping
... env.workspace = r"C:\Daten\Test"
... mxd = mapping.MapDocument("Current")
... df = mapping.ListDataFrames(mxd, "Layers")[0]
... fc = "Grid_100k"
... count = str(arcpy.GetCount_management(fc))
... x = 1
... while x < int(count) + 1:
...     rows = arcpy.SearchCursor(fc, "MAP = '" + str(x) + "'")
...     print(x)
...     for row in rows:
...         df.zoomToSelectedFeatures()
...         df.scale = 100000
...         arcpy.RefreshActiveView()
...         arcpy.mapping.ExportToJPEG(mxd, r"C:\Daten\Test\100k_" + str(x) + ".jpg", df, df_export_width=5866, df_export_height=3106, world_file=True)
...         print('Bild exportiert')
...         print(x, 'von', count)
...         arcpy.SelectLayerByAttribute_management(fc, "CLEAR_SELECTION")
...     x += 1
... print("Export beendet")
0 Kudos
MathewCoyle
Frequent Contributor
You are using
df.zoomToSelectedFeatures()

But no where are you making a selection. And if you are starting with a selection in ArcMap it is getting cleared after the first iteration at this line.
arcpy.SelectLayerByAttribute_management(fc, "CLEAR_SELECTION")
0 Kudos