<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Looping through features in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283282#M21874</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for your answers, especially for the code sample.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried to change my code, unfortunaly it does not yet work. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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 &amp;lt; int(count) + 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(fc, "SORT_ID = " + str(x))
&amp;nbsp;&amp;nbsp;&amp;nbsp; 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)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Exported image')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(x, 'of', count)
&amp;nbsp;&amp;nbsp;&amp;nbsp; x = x + 1
print("Export Complete")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Update:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.SelectLayerByAttribute_management("Grid_100k", "NEW_SELECTION", "\"SORT_ID\" = 23")
&amp;lt;Result 'Grid_100k'&amp;gt;
&amp;gt;&amp;gt;&amp;gt; arcpy.SelectLayerByAttribute_management("Grid_100k", "NEW_SELECTION", "\"SORT_ID\" = x")
Runtime error &amp;lt;class 'arcgisscripting.ExecuteError'&amp;gt;: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute). &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 13:42:56 GMT</pubDate>
    <dc:creator>JohannesRein</dc:creator>
    <dc:date>2021-12-11T13:42:56Z</dc:date>
    <item>
      <title>Looping through features</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283279#M21871</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm quiet new in python and i have the following question.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to export my map into jpg's with worldfile. Therfore i created a polygon-featureclass with a grid (400 objects). &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The problem is that i do not know how to loop through the features.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is the code a made.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you in advance,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jo&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Mar 2012 14:08:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283279#M21871</guid>
      <dc:creator>JohannesRein</dc:creator>
      <dc:date>2012-03-29T14:08:20Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through features</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283280#M21872</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Use a &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000039000000"&gt;SearchCursor&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Mar 2012 14:44:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283280#M21872</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2012-03-29T14:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through features</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283281#M21873</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;hey there.. i made something similar a while back &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;basically it uses a grid to zoom to and export map blocks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;not sure if its helpful for you but maybe you can pull some parts out of it. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

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 &amp;lt; int(count) + 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(fc, "ID = " + str(x))
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmin, ymin, xmax, ymax&amp;nbsp; = row.shape.extent.XMin, row.shape.extent.YMin, row.shape.extent.XMax, row.shape.extent.YMax
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newExtent = df.extent
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newExtent.XMin, newExtent.YMin, newExtent.XMax, newExtent.YMax = xmin, ymin, xmax, ymax
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; df.extent = newExtent
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 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)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Exported image' )
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(x, 'of', count)
&amp;nbsp;&amp;nbsp;&amp;nbsp; x += 1

print("Export Complete")
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:42:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283281#M21873</guid>
      <dc:creator>AnthonyTimpson2</dc:creator>
      <dc:date>2021-12-11T13:42:54Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through features</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283282#M21874</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for your answers, especially for the code sample.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried to change my code, unfortunaly it does not yet work. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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 &amp;lt; int(count) + 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(fc, "SORT_ID = " + str(x))
&amp;nbsp;&amp;nbsp;&amp;nbsp; 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)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Exported image')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(x, 'of', count)
&amp;nbsp;&amp;nbsp;&amp;nbsp; x = x + 1
print("Export Complete")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Update:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.SelectLayerByAttribute_management("Grid_100k", "NEW_SELECTION", "\"SORT_ID\" = 23")
&amp;lt;Result 'Grid_100k'&amp;gt;
&amp;gt;&amp;gt;&amp;gt; arcpy.SelectLayerByAttribute_management("Grid_100k", "NEW_SELECTION", "\"SORT_ID\" = x")
Runtime error &amp;lt;class 'arcgisscripting.ExecuteError'&amp;gt;: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute). &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:42:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283282#M21874</guid>
      <dc:creator>JohannesRein</dc:creator>
      <dc:date>2021-12-11T13:42:56Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through features</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283283#M21875</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.SelectLayerByAttribute_management("Grid_100k", "NEW_SELECTION", "\"SORT_ID\" = %i" % x)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;or&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.SelectLayerByAttribute_management("Grid_100k", "NEW_SELECTION", "\"SORT_ID\" = " + str(x))&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;you also can use a for-loop, for example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;for x in range(1, count+1):&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Mar 2012 10:59:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283283#M21875</guid>
      <dc:creator>FabianBlau</dc:creator>
      <dc:date>2012-03-30T10:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through features</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283284#M21876</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The looping and nameing of the files work, but all images have the same extend.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Why does the extend not update?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It seems that ArcMap does not like the script. It crashed several times.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; 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 &amp;lt; int(count) + 1:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(fc, "MAP = '" + str(x) + "'")
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(x)
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; df.zoomToSelectedFeatures()
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; df.scale = 100000
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.ExportToJPEG(mxd, r"C:\Daten\Test\100k_" + str(x) + ".jpg", df, df_export_width=5866, df_export_height=3106, world_file=True)
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Bild exportiert')
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(x, 'von', count)
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(fc, "CLEAR_SELECTION")
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x += 1
... print("Export beendet")
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:42:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283284#M21876</guid>
      <dc:creator>JohannesRein</dc:creator>
      <dc:date>2021-12-11T13:42:59Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through features</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283285#M21877</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You are using &lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;df.zoomToSelectedFeatures()&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.SelectLayerByAttribute_management(fc, "CLEAR_SELECTION")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Apr 2012 12:49:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-features/m-p/283285#M21877</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-04-02T12:49:14Z</dc:date>
    </item>
  </channel>
</rss>

