<?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: for row in rows - set values to different variables in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392710#M31073</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You may be able to achieve what you want just using raster extents and not getting into cursors at all.&amp;nbsp; Does the following code work for you?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14132995501736851 jive_text_macro" jivemacro_uid="_14132995501736851"&gt;
&lt;P&gt;def rasterDim(raster, EPSG=None):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ras = arcpy.Raster(raster)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; extent = arcpy.Describe(ras).Extent&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if EPSG is not None:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sr = arcpy.SpatialReference(EPSG)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; extent = extent.projectAs(sr)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return extent.width, extent.height&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Pass the function above the text path to the raster and an optional EPSG code if you want to get the width and height based on a different projection than the raster's spatial reference.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 14 Oct 2014 15:13:57 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2014-10-14T15:13:57Z</dc:date>
    <item>
      <title>for row in rows - set values to different variables</title>
      <link>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392703#M31066</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;My script geterates two lines in a temp shapefile.&lt;/P&gt;&lt;P&gt;I'm then using SearchCursor and Describe to get the lengths of these lines, like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14129369393481079 jive_text_macro" jivemacro_uid="_14129369393481079"&gt;
&lt;P&gt;rows = arcpy.SearchCursor(tempSHP)&lt;/P&gt;
&lt;P&gt;len = 0&lt;/P&gt;
&lt;P&gt;shapeName = arcpy.Describe(tempSHP).shapeFieldName&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;for row in rows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; feat = row.getValue(shapeName)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; len = feat.length&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print len&lt;/P&gt;


&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I really want is to set the length of each line to a different variable - I tried various things like below but haven't found the right way to approach this little problem (the error for the code below said "row does not support indexing"):&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14129371499706221 jive_text_macro" jivemacro_uid="_14129371499706221"&gt;
&lt;P&gt;for row in rows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; featX = row[0].getValue(shapeName)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lenX = featX.length&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; featY = row[1].getValue(shapeName)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lenY = featY.length&lt;/P&gt;


&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Oct 2014 10:38:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392703#M31066</guid>
      <dc:creator>BenLeslie1</dc:creator>
      <dc:date>2014-10-10T10:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: for row in rows - set values to different variables</title>
      <link>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392704#M31067</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Indexing is supported in arcpy.da, not plain arcpy.&lt;/P&gt;&lt;P&gt;Try&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14129435551791983 jive_text_macro" jivemacro_uid="_14129435551791983"&gt;
&lt;P&gt;rows = arcpy.da.SearchCursor(tempSHP)&lt;/P&gt;
&lt;P&gt;# or even better:&lt;/P&gt;
&lt;P&gt;with arcpy.da.SearchCursor(tempSHP) as rows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # do something&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The second method will automatically clean up rows and row when you exit the with block, so you don't have to do so manually.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Oct 2014 12:22:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392704#M31067</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2014-10-10T12:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: for row in rows - set values to different variables</title>
      <link>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392705#M31068</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Use a dictionary to store the 'variables':&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;rows = arcpy.SearchCursor(shp)&lt;/P&gt;&lt;P&gt;shapeName= arcpy.Describe(tempSHP).shapeFieldName&lt;/P&gt;&lt;P&gt;count = 0&lt;/P&gt;&lt;P&gt;d={}&lt;/P&gt;&lt;P&gt;for row in rows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; count +=1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; feat = row.getValue(shapeName)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; d["string{0}".format(count)] = str(feat.area)&lt;/P&gt;&lt;P&gt;print d['string1']&lt;/P&gt;&lt;P&gt;print d['string2']&lt;/P&gt;&lt;P&gt;print d['string3']&lt;/P&gt;&lt;P&gt;#etc&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Oct 2014 13:12:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392705#M31068</guid>
      <dc:creator>Pieter_Geertvan_den_Beukel</dc:creator>
      <dc:date>2014-10-10T13:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: for row in rows - set values to different variables</title>
      <link>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392706#M31069</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The reason for the "row does not support indexing" error is tied to the data type of row.&amp;nbsp; The code you posted is returning an &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#/Polygon/018z00000061000000/"&gt;arcpy Polygon &lt;/A&gt; object, and that object doesn't support indexing.&amp;nbsp; Since I don't know whether you are working with points, lines, or polygons, I will assume polygons and use the centroids in the code snippet below:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14132305969548509" jivemacro_uid="_14132305969548509"&gt;
&lt;P&gt;for row in rows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lenX = row.getValue(shapeName).centroid.X&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lenY = row.getValue(shapeName).centroid.Y&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And I agree with &lt;A href="https://community.esri.com/migrated-users/9431"&gt;Greg Keith&lt;/A&gt;‌, the Data Access (arcpy.da) cursors are much more robust than the older/original arcpy cursors.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Oct 2014 19:59:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392706#M31069</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2014-10-13T19:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: for row in rows - set values to different variables</title>
      <link>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392707#M31070</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Read the original post a bit too quickly, I now see we are talking about lines.&amp;nbsp; I guess I don't completely understand the intent or goal of the original code.&amp;nbsp; Are featX and featY different features or the X and Y component of the same feature?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Oct 2014 20:20:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392707#M31070</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2014-10-13T20:20:20Z</dc:date>
    </item>
    <item>
      <title>Re: for row in rows - set values to different variables</title>
      <link>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392708#M31071</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm trying to measure the width and height of a raster in metres.&amp;nbsp; I'm working in WGS84 (degrees) so Raster Properties aren't much good - and I don't like the results when I re-project the raster.&amp;nbsp; So I have constructed two lines based on TOP, BOTTOM, RIGHT, LEFT raster properties, then I reproject those and get their lengths.&amp;nbsp; They are separate features and they will be deleted once I know their lengths.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Pieter GvdB's answer works for me.&amp;nbsp; I'm pretty new to python, I tried Greg Keith's answer but my knowledge isn't good enough to know how to fit it in my code.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Oct 2014 07:09:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392708#M31071</guid>
      <dc:creator>BenLeslie1</dc:creator>
      <dc:date>2014-10-14T07:09:53Z</dc:date>
    </item>
    <item>
      <title>Re: for row in rows - set values to different variables</title>
      <link>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392709#M31072</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Using arcpy.da, you can retrieve the fields by index. &lt;A href="http://geonet.esri.com/# prints value of fieldone in first record"&gt;Arcpy.da SearchCursors&lt;/A&gt; return tuples.&lt;/P&gt;&lt;P&gt;So, in the code below, you'd retrieve values by index; you don't use getValue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14132883622433744 jive_text_macro" jivemacro_uid="_14132883622433744" modifiedtitle="true"&gt;
&lt;P&gt;# create list of field names you want returned. This is faster if you don't want all fields.&lt;/P&gt;
&lt;P&gt;# optionally, you could skip this (and leave fldList out of SearchCursor call below)&lt;/P&gt;
&lt;P&gt;# if you did want all fields. Use an asterisk instead of fldList in that case.&lt;/P&gt;
&lt;P&gt;fldList = ['fieldone', 'fldFive']&lt;/P&gt;
&lt;P&gt;with arcpy.da.SearchCursor(tempSHP, fldList) as rows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row[0]&amp;nbsp; # prints value of fieldone in first record, then second, etc&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row[1]&amp;nbsp; # prints value of fieldfive in first record, then second, etc&lt;/P&gt;


&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also note in the help link above that various shape field properties can be accessed by tokens instead of field names, and that none of this works on raster fields.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Oct 2014 12:08:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392709#M31072</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2014-10-14T12:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: for row in rows - set values to different variables</title>
      <link>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392710#M31073</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You may be able to achieve what you want just using raster extents and not getting into cursors at all.&amp;nbsp; Does the following code work for you?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14132995501736851 jive_text_macro" jivemacro_uid="_14132995501736851"&gt;
&lt;P&gt;def rasterDim(raster, EPSG=None):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ras = arcpy.Raster(raster)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; extent = arcpy.Describe(ras).Extent&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if EPSG is not None:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sr = arcpy.SpatialReference(EPSG)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; extent = extent.projectAs(sr)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return extent.width, extent.height&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Pass the function above the text path to the raster and an optional EPSG code if you want to get the width and height based on a different projection than the raster's spatial reference.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Oct 2014 15:13:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-row-in-rows-set-values-to-different-variables/m-p/392710#M31073</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2014-10-14T15:13:57Z</dc:date>
    </item>
  </channel>
</rss>

