2

I have a shapefile from which I want to retrieve the last row values for specific fields to use them as input in a tool. So far this is what I have which does not work.

length=int(arcpy.GetCount_management(shapefile).getOutput(0))
print length
cursor=arcpy.SearchCursor(shapefile)
for row in cursor:
        row= row[length-1]
        endx= row.getValue("LastX")
        endy= row.getValue("LastY")
        sourcePoint= arcpy.Point(endx, endy)
1

Dan has a good answer already posted but just to clarify why your code is failing: length is related to the number of rows, while row[length-1] is a specific column index (which probably doesn't exist since there's often more rows than columns. e.g. length = 53 (number of rows) would cause an error in general if there aren't 53 columns.

An in-place fix for your code (not ideal as it involves unnecessary calculations for each row):

length=int(arcpy.GetCount_management(shapefile).getOutput(0))
print length
cursor=arcpy.da.SearchCursor(shapefile)  # switch to data access module
for row in cursor:
    endx= row.getValue("LastX")
    endy= row.getValue("LastY")
sourcePoint= arcpy.Point(endx, endy) 

sourcePoint only uses the final endx and endy values returned by cursor, since this is now outside of the loop.

4

Another variation, using list comprehension and da.SearchCursor:

import arcpy
shapefile = r'C:\folder\shapefile.shp'
endx, endy = [i for i in arcpy.da.SearchCursor(shapefile,['LastX','LastY'])][-1] #-1 index for last record in list
lastpoint = arcpy.Point(endx, endy)

Im assuming LastX and LastY are fields. If you want the centroid's X and Y you can replace them with the SHAPE@X and SHAPE@Y tokens,

3

A variation on getting the last row could also be this approach, it gets a count on the row and then queries for that specifically:

objRes = arcpy.GetCount_management("myLayer")
n = int(objRes.getOutput(0))
sQuery = "OBJECTID >= " + str(n)
with arcpy.da.SearchCursor("myLayer",["X","Y"],sQuery) as cursor:
    for row in cursor:
        x = row[0]
        y = row[1]        
print x,y
  • 1
    Perhaps change to FID = n-1, because he is dealing with shapefile. – FelixIP Feb 14 at 22:21
2
cursor = arcpy.SearchCursor(shapefile, None, None, None, "FID A")
row = None
for row in cursor:
    pass
#row is now set as the last value returned by the iterator
row.getValue("the field you want the value of")

arcpy.SearchCursor returns an iterator, and to get the last item you need to iterate through all the items and keep a reference to the last one. Some more info here: https://stackoverflow.com/questions/2138873/cleanest-way-to-get-last-item-from-python-iterator

Be careful what you mean by "last row". I'm not sure the SearchCursor is guaranteed to return rows in any specific order. When I tested on a shapfile the last item returned had FID == 0. Use the sort_fields argument to define the "last row" to be whatever you like based on the field(s) of your choice. (In this answer "FID A", with "A" meaning ascending sort order on the FID field.) http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/searchcursor.htm

Also, consider using the cursor from the Data Access module which offers improved performance. http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/searchcursor-class.htm

  • 4
    I definitely recommend using da.SearchCursor instead, as SearchCursor is deprecated. – smiller Feb 14 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.